Skip to content

Commit

Permalink
Merge pull request #260 from sto3psl/tick-component
Browse files Browse the repository at this point in the history
add tickComponent prop and use @vx/text for axis ticks
  • Loading branch information
hshoff authored Mar 29, 2018
2 parents 9d7ffaa + 86369e7 commit 48c34f9
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 25 deletions.
1 change: 1 addition & 0 deletions packages/vx-axis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
36 changes: 23 additions & 13 deletions packages/vx-axis/src/axis/Axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -39,6 +40,7 @@ const propTypes = {
tickStroke: PropTypes.string,
tickTransform: PropTypes.string,
tickValues: PropTypes.array,
tickComponent: PropTypes.func,
top: PropTypes.number,
children: PropTypes.func,
};
Expand Down Expand Up @@ -79,6 +81,7 @@ export default function Axis({
tickStroke = 'black',
tickTransform,
tickValues,
tickComponent,
top = 0,
}) {
let values = scale.ticks ? scale.ticks(numTicks) : scale.domain();
Expand Down Expand Up @@ -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
}) : (
<Text
x={tickToPoint.x}
y={
tickToPoint.y +
(horizontal && !isTop ? tickLabelFontSize : 0)

}
{...tickLabelPropsObj}
>
{format(val, index)}
</Text>
)}
<text
x={tickToPoint.x}
y={
tickToPoint.y +
(horizontal && !isTop ? tickLabelFontSize : 0)

}
{...tickLabelPropsObj}
>
{format(val, index)}
</text>
</Group>
);
})}
Expand All @@ -215,7 +225,7 @@ export default function Axis({
)}

{label && (
<text
<Text
className={cx('vx-axis-label', labelClassName)}
{...getLabelTransform({
labelOffset,
Expand All @@ -228,7 +238,7 @@ export default function Axis({
{...labelProps}
>
{label}
</text>
</Text>
)}
</Group>
);
Expand Down
3 changes: 3 additions & 0 deletions packages/vx-axis/src/axis/AxisBottom.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const propTypes = {
tickStroke: PropTypes.string,
tickTransform: PropTypes.string,
tickValues: PropTypes.array,
tickComponent: PropTypes.func,
top: PropTypes.number,
children: PropTypes.func,
};
Expand Down Expand Up @@ -63,6 +64,7 @@ export default function AxisBottom({
tickStroke,
tickTransform,
tickValues,
tickComponent,
top,
}) {
return (
Expand Down Expand Up @@ -91,6 +93,7 @@ export default function AxisBottom({
tickStroke={tickStroke}
tickTransform={tickTransform}
tickValues={tickValues}
tickComponent={tickComponent}
top={top}
children={children}
/>
Expand Down
3 changes: 3 additions & 0 deletions packages/vx-axis/src/axis/AxisLeft.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const propTypes = {
tickStroke: PropTypes.string,
tickTransform: PropTypes.string,
tickValues: PropTypes.array,
tickComponent: PropTypes.func,
top: PropTypes.number,
children: PropTypes.func,
};
Expand Down Expand Up @@ -64,6 +65,7 @@ export default function AxisLeft({
tickStroke,
tickTransform,
tickValues,
tickComponent,
top,
}) {
return (
Expand Down Expand Up @@ -92,6 +94,7 @@ export default function AxisLeft({
tickStroke={tickStroke}
tickTransform={tickTransform}
tickValues={tickValues}
tickComponent={tickComponent}
top={top}
children={children}
/>
Expand Down
3 changes: 3 additions & 0 deletions packages/vx-axis/src/axis/AxisRight.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const propTypes = {
tickStroke: PropTypes.string,
tickTransform: PropTypes.string,
tickValues: PropTypes.array,
tickComponent: PropTypes.func,
top: PropTypes.number,
children: PropTypes.func,
};
Expand Down Expand Up @@ -64,6 +65,7 @@ export default function AxisRight({
tickStroke,
tickTransform,
tickValues,
tickComponent,
top,
}) {
return (
Expand Down Expand Up @@ -92,6 +94,7 @@ export default function AxisRight({
tickStroke={tickStroke}
tickTransform={tickTransform}
tickValues={tickValues}
tickComponent={tickComponent}
top={top}
children={children}
/>
Expand Down
3 changes: 3 additions & 0 deletions packages/vx-axis/src/axis/AxisTop.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const propTypes = {
tickStroke: PropTypes.string,
tickTransform: PropTypes.string,
tickValues: PropTypes.array,
tickComponent: PropTypes.func,
top: PropTypes.number,
children: PropTypes.func,
};
Expand Down Expand Up @@ -63,6 +64,7 @@ export default function AxisTop({
tickStroke,
tickTransform,
tickValues,
tickComponent,
top,
}) {
return (
Expand Down Expand Up @@ -91,6 +93,7 @@ export default function AxisTop({
tickStroke={tickStroke}
tickTransform={tickTransform}
tickValues={tickValues}
tickComponent={tickComponent}
top={top}
children={children}
/>
Expand Down
15 changes: 8 additions & 7 deletions packages/vx-axis/test/Axis.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -79,7 +80,7 @@ describe('<Axis />', () => {

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),
);
});
Expand Down Expand Up @@ -109,7 +110,7 @@ describe('<Axis />', () => {
);

const label = wrapper.find('.vx-axis-label');
expect(label.find('text').props()).toEqual(
expect(label.find(Text).props()).toEqual(
expect.objectContaining(labelProps),
);
});
Expand Down Expand Up @@ -218,8 +219,8 @@ describe('<Axis />', () => {
wrapper
.children()
.find('.vx-axis-tick')
.find('text')
.text(),
.find(Text)
.prop('children')
).toBe('test!!!');
});

Expand All @@ -235,9 +236,9 @@ describe('<Axis />', () => {
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', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/vx-axis/test/AxisBottom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ describe('<AxisBottom />', () => {
const label = 'test';
const wrapper = shallow(<AxisBottom {...axisProps} label={label} />).dive();
const text = wrapper.find('.vx-axis-label');
expect(text.text()).toEqual(label);
expect(text.prop('children')).toEqual(label);
});
});
2 changes: 1 addition & 1 deletion packages/vx-axis/test/AxisLeft.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ describe('<AxisLeft />', () => {
const label = 'test';
const wrapper = shallow(<AxisLeft {...axisProps} label={label} />).dive();
const text = wrapper.find('.vx-axis-label');
expect(text.text()).toEqual(label);
expect(text.prop('children')).toEqual(label);
});
});
2 changes: 1 addition & 1 deletion packages/vx-axis/test/AxisRight.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ describe('<AxisRight />', () => {
const label = 'test';
const wrapper = shallow(<AxisRight {...axisProps} label={label} />).dive();
const text = wrapper.find('.vx-axis-label');
expect(text.text()).toEqual(label);
expect(text.prop('children')).toEqual(label);
});
});
2 changes: 1 addition & 1 deletion packages/vx-axis/test/AxisTop.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ describe('<AxisTop />', () => {
const label = 'test';
const wrapper = shallow(<AxisTop {...axisProps} label={label} />).dive();
const text = wrapper.find('.vx-axis-label');
expect(text.text()).toEqual(label);
expect(text.prop('children')).toEqual(label);
});
})
3 changes: 3 additions & 0 deletions packages/vx-demo/components/tiles/axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ export default ({ width, height, margin }) => {
dx: '-0.25em',
dy: '0.25em',
})}
tickComponent={({ formattedValue, ...tickProps}) => (
<text {...tickProps}>{formattedValue}</text>
)}
/>
<AxisBottom
top={height - margin.bottom}
Expand Down
3 changes: 3 additions & 0 deletions packages/vx-demo/pages/axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ export default ({ width, height, margin }) => {
dx: '-0.25em',
dy: '0.25em',
})}
tickComponent={({ formattedValue, ...tickProps}) => (
<text {...tickProps}>{formattedValue}</text>
)}
/>
<AxisBottom
top={height - margin.bottom}
Expand Down
32 changes: 31 additions & 1 deletion packages/vx-demo/static/docs/vx-axis.html
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ <h2 id="-axistop-"><code>&lt;AxisTop/&gt;</code></h2>
<td style="text-align:left">The x offset to the tick&#39;s text.</td>
</tr>
<tr>
<td style="text-align:left">tickComponent</td>
<td style="text-align:left"></td>
<td style="text-align:left">function</td>
<td style="text-align:left">Function returning a React component to render as tick.</td>
</tr>
<tr>
<td style="text-align:left">hideAxisLine</td>
<td style="text-align:left">false</td>
<td style="text-align:left">bool</td>
Expand Down Expand Up @@ -376,6 +382,12 @@ <h2 id="-axisbottom-"><code>&lt;AxisBottom/&gt;</code></h2>
<td style="text-align:left">The x offset to the tick&#39;s text.</td>
</tr>
<tr>
<td style="text-align:left">tickComponent</td>
<td style="text-align:left"></td>
<td style="text-align:left">function</td>
<td style="text-align:left">Function returning a React component to render as tick.</td>
</tr>
<tr>
<td style="text-align:left">hideAxisLine</td>
<td style="text-align:left">false</td>
<td style="text-align:left">bool</td>
Expand Down Expand Up @@ -545,6 +557,12 @@ <h2 id="-axisleft-"><code>&lt;AxisLeft/&gt;</code></h2>
<td style="text-align:left">The x offset to the tick&#39;s text.</td>
</tr>
<tr>
<td style="text-align:left">tickComponent</td>
<td style="text-align:left"></td>
<td style="text-align:left">function</td>
<td style="text-align:left">Function returning a React component to render as tick.</td>
</tr>
<tr>
<td style="text-align:left">hideAxisLine</td>
<td style="text-align:left">false</td>
<td style="text-align:left">bool</td>
Expand Down Expand Up @@ -714,6 +732,12 @@ <h2 id="-axisright-"><code>&lt;AxisRight/&gt;</code></h2>
<td style="text-align:left">The x offset to the tick&#39;s text.</td>
</tr>
<tr>
<td style="text-align:left">tickComponent</td>
<td style="text-align:left"></td>
<td style="text-align:left">function</td>
<td style="text-align:left">Function returning a React component to render as tick.</td>
</tr>
<tr>
<td style="text-align:left">hideAxisLine</td>
<td style="text-align:left">false</td>
<td style="text-align:left">bool</td>
Expand Down Expand Up @@ -889,6 +913,12 @@ <h2 id="-axis-"><code>&lt;Axis /&gt;</code></h2>
<td style="text-align:left">The x offset to the tick&#39;s text.</td>
</tr>
<tr>
<td style="text-align:left">tickComponent</td>
<td style="text-align:left"></td>
<td style="text-align:left">function</td>
<td style="text-align:left">Function returning a React component to render as tick.</td>
</tr>
<tr>
<td style="text-align:left">hideAxisLine</td>
<td style="text-align:left">false</td>
<td style="text-align:left">bool</td>
Expand Down Expand Up @@ -925,4 +955,4 @@ <h2 id="-axis-"><code>&lt;Axis /&gt;</code></h2>
</div>
</div>
<script async defer src="https://buttons.github.io/buttons.js"></script>
</html>
</html>

0 comments on commit 48c34f9

Please sign in to comment.