diff --git a/packages/vx-axis/package.json b/packages/vx-axis/package.json index 50bfe051e..02de21660 100644 --- a/packages/vx-axis/package.json +++ b/packages/vx-axis/package.json @@ -33,6 +33,7 @@ "@vx/group": "0.0.153", "@vx/point": "0.0.153", "@vx/shape": "0.0.158", + "@vx/text": "0.0.153", "classnames": "^2.2.5", "prop-types": "^15.6.0" }, diff --git a/packages/vx-axis/src/axis/Axis.js b/packages/vx-axis/src/axis/Axis.js index 61db8779b..cf6a8630c 100644 --- a/packages/vx-axis/src/axis/Axis.js +++ b/packages/vx-axis/src/axis/Axis.js @@ -4,6 +4,7 @@ import cx from 'classnames'; import { Line } from '@vx/shape'; import { Point } from '@vx/point'; import { Group } from '@vx/group'; +import { Text } from '@vx/text'; import center from '../utils/center'; import identity from '../utils/identity'; import getLabelTransform from '../utils/labelTransform'; @@ -39,6 +40,7 @@ const propTypes = { tickStroke: PropTypes.string, tickTransform: PropTypes.string, tickValues: PropTypes.array, + tickComponent: PropTypes.func, top: PropTypes.number, children: PropTypes.func, }; @@ -79,6 +81,7 @@ export default function Axis({ tickStroke = 'black', tickTransform, tickValues, + tickComponent, top = 0, }) { let values = scale.ticks ? scale.ticks(numTicks) : scale.domain(); @@ -187,18 +190,25 @@ export default function Axis({ to={tickToPoint} stroke={tickStroke} /> + )} + {tickComponent ? tickComponent({ + x: tickToPoint.x, + y: tickToPoint.y + (horizontal && !isTop ? tickLabelFontSize : 0), + formattedValue: format(val, index), + ...tickLabelPropsObj + }) : ( + + {format(val, index)} + )} - - {format(val, index)} - ); })} @@ -215,7 +225,7 @@ export default function Axis({ )} {label && ( - {label} - + )} ); diff --git a/packages/vx-axis/src/axis/AxisBottom.js b/packages/vx-axis/src/axis/AxisBottom.js index 5cab1b066..dd5093ce3 100644 --- a/packages/vx-axis/src/axis/AxisBottom.js +++ b/packages/vx-axis/src/axis/AxisBottom.js @@ -28,6 +28,7 @@ const propTypes = { tickStroke: PropTypes.string, tickTransform: PropTypes.string, tickValues: PropTypes.array, + tickComponent: PropTypes.func, top: PropTypes.number, children: PropTypes.func, }; @@ -63,6 +64,7 @@ export default function AxisBottom({ tickStroke, tickTransform, tickValues, + tickComponent, top, }) { return ( @@ -91,6 +93,7 @@ export default function AxisBottom({ tickStroke={tickStroke} tickTransform={tickTransform} tickValues={tickValues} + tickComponent={tickComponent} top={top} children={children} /> diff --git a/packages/vx-axis/src/axis/AxisLeft.js b/packages/vx-axis/src/axis/AxisLeft.js index b10c060e3..c5a36a331 100644 --- a/packages/vx-axis/src/axis/AxisLeft.js +++ b/packages/vx-axis/src/axis/AxisLeft.js @@ -28,6 +28,7 @@ const propTypes = { tickStroke: PropTypes.string, tickTransform: PropTypes.string, tickValues: PropTypes.array, + tickComponent: PropTypes.func, top: PropTypes.number, children: PropTypes.func, }; @@ -64,6 +65,7 @@ export default function AxisLeft({ tickStroke, tickTransform, tickValues, + tickComponent, top, }) { return ( @@ -92,6 +94,7 @@ export default function AxisLeft({ tickStroke={tickStroke} tickTransform={tickTransform} tickValues={tickValues} + tickComponent={tickComponent} top={top} children={children} /> diff --git a/packages/vx-axis/src/axis/AxisRight.js b/packages/vx-axis/src/axis/AxisRight.js index c86b9d5cd..66f1eba93 100644 --- a/packages/vx-axis/src/axis/AxisRight.js +++ b/packages/vx-axis/src/axis/AxisRight.js @@ -28,6 +28,7 @@ const propTypes = { tickStroke: PropTypes.string, tickTransform: PropTypes.string, tickValues: PropTypes.array, + tickComponent: PropTypes.func, top: PropTypes.number, children: PropTypes.func, }; @@ -64,6 +65,7 @@ export default function AxisRight({ tickStroke, tickTransform, tickValues, + tickComponent, top, }) { return ( @@ -92,6 +94,7 @@ export default function AxisRight({ tickStroke={tickStroke} tickTransform={tickTransform} tickValues={tickValues} + tickComponent={tickComponent} top={top} children={children} /> diff --git a/packages/vx-axis/src/axis/AxisTop.js b/packages/vx-axis/src/axis/AxisTop.js index 6dd6c6f1c..7779f0554 100644 --- a/packages/vx-axis/src/axis/AxisTop.js +++ b/packages/vx-axis/src/axis/AxisTop.js @@ -28,6 +28,7 @@ const propTypes = { tickStroke: PropTypes.string, tickTransform: PropTypes.string, tickValues: PropTypes.array, + tickComponent: PropTypes.func, top: PropTypes.number, children: PropTypes.func, }; @@ -63,6 +64,7 @@ export default function AxisTop({ tickStroke, tickTransform, tickValues, + tickComponent, top, }) { return ( @@ -91,6 +93,7 @@ export default function AxisTop({ tickStroke={tickStroke} tickTransform={tickTransform} tickValues={tickValues} + tickComponent={tickComponent} top={top} children={children} /> diff --git a/packages/vx-axis/test/Axis.test.js b/packages/vx-axis/test/Axis.test.js index 1923b7d8b..efa74b9fe 100644 --- a/packages/vx-axis/test/Axis.test.js +++ b/packages/vx-axis/test/Axis.test.js @@ -3,6 +3,7 @@ import { Axis } from '../src'; import { shallow } from 'enzyme'; import { scaleLinear, scaleBand } from '../../vx-scale'; import { Line } from '@vx/shape'; +import { Text } from '@vx/text' const axisProps = { orientation: 'left', @@ -79,7 +80,7 @@ describe('', () => { const ticks = wrapper.find('.vx-axis-tick'); ticks.forEach(tick => { - expect(tick.find('text').props()).toEqual( + expect(tick.find(Text).props()).toEqual( expect.objectContaining(tickProps), ); }); @@ -109,7 +110,7 @@ describe('', () => { ); const label = wrapper.find('.vx-axis-label'); - expect(label.find('text').props()).toEqual( + expect(label.find(Text).props()).toEqual( expect.objectContaining(labelProps), ); }); @@ -218,8 +219,8 @@ describe('', () => { wrapper .children() .find('.vx-axis-tick') - .find('text') - .text(), + .find(Text) + .prop('children') ).toBe('test!!!'); }); @@ -235,9 +236,9 @@ describe('', () => { wrapper .children() .find('.vx-axis-tick') - .find('text') - .text(), - ).toBe('0'); + .find(Text) + .prop('children') + ).toBe(0); }); test('it should use center if scale is band', () => { diff --git a/packages/vx-axis/test/AxisBottom.test.js b/packages/vx-axis/test/AxisBottom.test.js index 1481dbafe..13a03a4cd 100644 --- a/packages/vx-axis/test/AxisBottom.test.js +++ b/packages/vx-axis/test/AxisBottom.test.js @@ -69,6 +69,6 @@ describe('', () => { const label = 'test'; const wrapper = shallow().dive(); const text = wrapper.find('.vx-axis-label'); - expect(text.text()).toEqual(label); + expect(text.prop('children')).toEqual(label); }); }); diff --git a/packages/vx-axis/test/AxisLeft.test.js b/packages/vx-axis/test/AxisLeft.test.js index 2476193ae..3b4040b2c 100644 --- a/packages/vx-axis/test/AxisLeft.test.js +++ b/packages/vx-axis/test/AxisLeft.test.js @@ -69,6 +69,6 @@ describe('', () => { const label = 'test'; const wrapper = shallow().dive(); const text = wrapper.find('.vx-axis-label'); - expect(text.text()).toEqual(label); + expect(text.prop('children')).toEqual(label); }); }); diff --git a/packages/vx-axis/test/AxisRight.test.js b/packages/vx-axis/test/AxisRight.test.js index a96981c62..3ffb79909 100644 --- a/packages/vx-axis/test/AxisRight.test.js +++ b/packages/vx-axis/test/AxisRight.test.js @@ -69,6 +69,6 @@ describe('', () => { const label = 'test'; const wrapper = shallow().dive(); const text = wrapper.find('.vx-axis-label'); - expect(text.text()).toEqual(label); + expect(text.prop('children')).toEqual(label); }); }); diff --git a/packages/vx-axis/test/AxisTop.test.js b/packages/vx-axis/test/AxisTop.test.js index e8bbe7996..11c7ef0b5 100644 --- a/packages/vx-axis/test/AxisTop.test.js +++ b/packages/vx-axis/test/AxisTop.test.js @@ -69,6 +69,6 @@ describe('', () => { const label = 'test'; const wrapper = shallow().dive(); const text = wrapper.find('.vx-axis-label'); - expect(text.text()).toEqual(label); + expect(text.prop('children')).toEqual(label); }); }) diff --git a/packages/vx-demo/components/tiles/axis.js b/packages/vx-demo/components/tiles/axis.js index 7854b4168..eee0fe185 100644 --- a/packages/vx-demo/components/tiles/axis.js +++ b/packages/vx-demo/components/tiles/axis.js @@ -123,6 +123,9 @@ export default ({ width, height, margin }) => { dx: '-0.25em', dy: '0.25em', })} + tickComponent={({ formattedValue, ...tickProps}) => ( + {formattedValue} + )} /> { dx: '-0.25em', dy: '0.25em', })} + tickComponent={({ formattedValue, ...tickProps}) => ( + {formattedValue} + )} /> <AxisTop/> The x offset to the tick's text. +tickComponent + +function +Function returning a React component to render as tick. + + hideAxisLine false bool @@ -376,6 +382,12 @@

<AxisBottom/>

The x offset to the tick's text. +tickComponent + +function +Function returning a React component to render as tick. + + hideAxisLine false bool @@ -545,6 +557,12 @@

<AxisLeft/>

The x offset to the tick's text. +tickComponent + +function +Function returning a React component to render as tick. + + hideAxisLine false bool @@ -714,6 +732,12 @@

<AxisRight/>

The x offset to the tick's text. +tickComponent + +function +Function returning a React component to render as tick. + + hideAxisLine false bool @@ -889,6 +913,12 @@

<Axis />

The x offset to the tick's text. +tickComponent + +function +Function returning a React component to render as tick. + + hideAxisLine false bool @@ -925,4 +955,4 @@

<Axis />

- \ No newline at end of file +