-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
hooks.js
210 lines (191 loc) · 6.7 KB
/
hooks.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* This file is part of the nivo project.
*
* Copyright 2016-present, Raphaël Benitte.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { useMemo } from 'react'
import { area, line } from 'd3-shape'
import { curveFromProp, useTheme, useValueFormatter } from '@nivo/core'
import { useOrdinalColorScale, useInheritedColor } from '@nivo/colors'
import { computeXYScalesForSeries } from '@nivo/scales'
import { LineDefaultProps } from './props'
export const useLineGenerator = ({ curve }) => {
return useMemo(
() =>
line()
.defined(d => d.x !== null && d.y !== null)
.x(d => d.x)
.y(d => d.y)
.curve(curveFromProp(curve)),
[curve]
)
}
export const useAreaGenerator = ({ curve, yScale, areaBaselineValue }) => {
return useMemo(() => {
return area()
.defined(d => d.x !== null && d.y !== null)
.x(d => d.x)
.y1(d => d.y)
.curve(curveFromProp(curve))
.y0(yScale(areaBaselineValue))
}, [curve, yScale, areaBaselineValue])
}
const usePoints = ({ series, getPointColor, getPointBorderColor, formatX, formatY }) => {
return useMemo(() => {
return series.reduce((acc, serie) => {
return [
...acc,
...serie.data
.filter(datum => datum.position.x !== null && datum.position.y !== null)
.map((datum, i) => {
const point = {
id: `${serie.id}.${i}`,
index: acc.length + i,
serieId: serie.id,
serieColor: serie.color,
x: datum.position.x,
y: datum.position.y,
}
point.color = getPointColor(serie)
point.borderColor = getPointBorderColor(point)
point.data = {
...datum.data,
xFormatted: formatX(datum.data.x),
yFormatted: formatY(datum.data.y),
}
return point
}),
]
}, [])
}, [series, getPointColor, getPointBorderColor, formatX, formatY])
}
export const useSlices = ({ enableSlices, points, width, height }) => {
return useMemo(() => {
if (enableSlices === false) return []
if (enableSlices === 'x') {
const map = new Map()
points.forEach(point => {
if (point.data.x === null || point.data.y === null) return
if (!map.has(point.x)) map.set(point.x, [point])
else map.get(point.x).push(point)
})
return Array.from(map.entries())
.sort((a, b) => a[0] - b[0])
.map(([x, slicePoints], i, slices) => {
const prevSlice = slices[i - 1]
const nextSlice = slices[i + 1]
let x0
if (!prevSlice) x0 = x
else x0 = x - (x - prevSlice[0]) / 2
let sliceWidth
if (!nextSlice) sliceWidth = width - x0
else sliceWidth = x - x0 + (nextSlice[0] - x) / 2
return {
id: x,
x0,
x,
y0: 0,
y: 0,
width: sliceWidth,
height,
points: slicePoints.reverse(),
}
})
} else if (enableSlices === 'y') {
const map = new Map()
points.forEach(point => {
if (point.data.x === null || point.data.y === null) return
if (!map.has(point.y)) map.set(point.y, [point])
else map.get(point.y).push(point)
})
return Array.from(map.entries())
.sort((a, b) => a[0] - b[0])
.map(([y, slicePoints], i, slices) => {
const prevSlice = slices[i - 1]
const nextSlice = slices[i + 1]
let y0
if (!prevSlice) y0 = y
else y0 = y - (y - prevSlice[0]) / 2
let sliceHeight
if (!nextSlice) sliceHeight = height - y0
else sliceHeight = y - y0 + (nextSlice[0] - y) / 2
return {
id: y,
x0: 0,
x: 0,
y0,
y,
width,
height: sliceHeight,
points: slicePoints.reverse(),
}
})
}
}, [enableSlices, points])
}
export const useLine = ({
data,
xScale: xScaleSpec = LineDefaultProps.xScale,
xFormat,
yScale: yScaleSpec = LineDefaultProps.yScale,
yFormat,
width,
height,
colors = LineDefaultProps.colors,
curve = LineDefaultProps.curve,
areaBaselineValue = LineDefaultProps.areaBaselineValue,
pointColor = LineDefaultProps.pointColor,
pointBorderColor = LineDefaultProps.pointBorderColor,
enableSlices = LineDefaultProps.enableSlicesTooltip,
}) => {
const formatX = useValueFormatter(xFormat)
const formatY = useValueFormatter(yFormat)
const getColor = useOrdinalColorScale(colors, 'id')
const theme = useTheme()
const getPointColor = useInheritedColor(pointColor, theme)
const getPointBorderColor = useInheritedColor(pointBorderColor, theme)
const { xScale, yScale, series: rawSeries } = useMemo(
() => computeXYScalesForSeries(data, xScaleSpec, yScaleSpec, width, height),
[data, xScaleSpec, yScaleSpec, width, height]
)
const series = useMemo(
() =>
rawSeries.map(serie => ({
...serie,
color: getColor(serie),
})),
[rawSeries, getColor]
)
const points = usePoints({
series,
getPointColor,
getPointBorderColor,
formatX,
formatY,
})
const slices = useSlices({
enableSlices,
points,
width,
height,
})
const lineGenerator = useLineGenerator({ curve })
const areaGenerator = useAreaGenerator({
curve,
yScale,
areaBaselineValue,
})
return {
lineGenerator,
areaGenerator,
getColor,
series,
xScale,
yScale,
slices,
points,
}
}