-
Notifications
You must be signed in to change notification settings - Fork 688
/
AxisRenderer.tsx
105 lines (100 loc) 路 2.54 KB
/
AxisRenderer.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import React from 'react';
import cx from 'classnames';
import { Line } from '@visx/shape';
import { Text } from '@visx/text';
import { TextProps } from '@visx/text/lib/Text';
import getLabelTransform from '../utils/getLabelTransform';
import Orientation from '../constants/orientation';
import { AxisRendererProps, AxisScale } from '../types';
import Ticks from './Ticks';
const defaultTextProps: Partial<TextProps> = {
textAnchor: 'middle',
fontFamily: 'Arial',
fontSize: 10,
fill: '#222',
};
export default function AxisRenderer<Scale extends AxisScale>({
axisFromPoint,
axisLineClassName,
axisToPoint,
hideAxisLine,
hideTicks,
horizontal,
label = '',
labelClassName,
labelOffset = 14,
labelProps = defaultTextProps,
orientation = Orientation.bottom,
scale,
stroke = '#222',
strokeDasharray,
strokeWidth = 1,
tickClassName,
tickComponent,
tickLineProps,
tickLabelProps,
tickLength = 8,
tickStroke = '#222',
tickTransform,
ticks,
ticksComponent = Ticks,
}: AxisRendererProps<Scale>) {
const tickLabelPropsDefault = {
...defaultTextProps,
...(typeof tickLabelProps === 'object' ? tickLabelProps : null),
};
// compute the max tick label size to compute label offset
const allTickLabelProps = ticks.map(({ value, index }) =>
typeof tickLabelProps === 'function'
? tickLabelProps(value, index, ticks)
: tickLabelPropsDefault,
);
const maxTickLabelFontSize = Math.max(
10,
...allTickLabelProps.map((props) => (typeof props.fontSize === 'number' ? props.fontSize : 0)),
);
return (
<>
{ticksComponent({
hideTicks,
horizontal,
orientation,
scale,
tickClassName,
tickComponent,
tickLabelProps: allTickLabelProps,
tickStroke,
tickTransform,
ticks,
strokeWidth,
tickLineProps,
})}
{!hideAxisLine && (
<Line
className={cx('visx-axis-line', axisLineClassName)}
from={axisFromPoint}
to={axisToPoint}
stroke={stroke}
strokeWidth={strokeWidth}
strokeDasharray={strokeDasharray}
/>
)}
{label && (
<Text
className={cx('visx-axis-label', labelClassName)}
{...getLabelTransform({
labelOffset,
labelProps,
orientation,
range: scale.range(),
tickLabelFontSize: maxTickLabelFontSize,
tickLength,
})}
{...labelProps}
>
{label}
</Text>
)}
</>
);
}