/
LinesLabels.js
72 lines (65 loc) · 2.11 KB
/
LinesLabels.js
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
import { memo } from 'react'
import PropTypes from 'prop-types'
import { useSprings, animated } from '@react-spring/web'
import { useTheme, useMotionConfig } from '@nivo/core'
import { inheritedColorPropType } from '@nivo/colors'
import { useSeriesLabels } from './hooks'
const LinesLabels = ({ series, getLabel, position, padding, color }) => {
const theme = useTheme()
const { animate, config: springConfig } = useMotionConfig()
const labels = useSeriesLabels({
series,
getLabel,
position,
padding,
color,
})
const springs = useSprings(
labels.length,
labels.map(label => ({
x: label.x,
y: label.y,
opacity: label.opacity,
config: springConfig,
immediate: !animate,
}))
)
return springs.map((animatedProps, index) => {
const label = labels[index]
return (
<animated.text
key={label.id}
x={animatedProps.x}
y={animatedProps.y}
textAnchor={label.textAnchor}
dominantBaseline="central"
opacity={animatedProps.opacity}
style={{
...theme.labels.text,
fill: label.color,
}}
>
{label.label}
</animated.text>
)
})
}
LinesLabels.propTypes = {
series: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.string.isRequired,
data: PropTypes.arrayOf(
PropTypes.shape({
x: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired,
y: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
})
).isRequired,
})
).isRequired,
getLabel: PropTypes.oneOfType([PropTypes.oneOf([false]), PropTypes.string, PropTypes.func])
.isRequired,
position: PropTypes.oneOf(['start', 'end']).isRequired,
padding: PropTypes.number.isRequired,
color: inheritedColorPropType.isRequired,
}
export default memo(LinesLabels)