-
Notifications
You must be signed in to change notification settings - Fork 716
/
Ticks.tsx
64 lines (60 loc) · 1.65 KB
/
Ticks.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React from 'react';
import cx from 'classnames';
import { Line } from '@visx/shape';
import { Group } from '@visx/group';
import { Text } from '@visx/text';
import Orientation from '../constants/orientation';
import { TicksRendererProps, AxisScale } from '../types';
export default function Ticks<Scale extends AxisScale>({
hideTicks,
horizontal,
orientation,
tickClassName,
tickComponent,
tickLabelProps: allTickLabelProps,
tickStroke = '#222',
tickTransform,
ticks,
strokeWidth,
tickLineProps,
}: TicksRendererProps<Scale>) {
return ticks.map(({ value, index, from, to, formattedValue }) => {
const tickLabelProps = allTickLabelProps[index] ?? {};
const tickLabelFontSize = Math.max(
10,
(typeof tickLabelProps.fontSize === 'number' && tickLabelProps.fontSize) || 0,
);
const tickYCoord =
to.y + (horizontal && orientation !== Orientation.top ? tickLabelFontSize : 0);
return (
<Group
key={`visx-tick-${value}-${index}`}
className={cx('visx-axis-tick', tickClassName)}
transform={tickTransform}
>
{!hideTicks && (
<Line
from={from}
to={to}
stroke={tickStroke}
strokeWidth={strokeWidth}
strokeLinecap="square"
{...tickLineProps}
/>
)}
{tickComponent ? (
tickComponent({
...tickLabelProps,
x: to.x,
y: tickYCoord,
formattedValue,
})
) : (
<Text x={to.x} y={tickYCoord} {...tickLabelProps}>
{formattedValue}
</Text>
)}
</Group>
);
});
}