forked from plouc/nivo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.ts
366 lines (338 loc) · 10.6 KB
/
hooks.ts
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import { useCallback, useMemo, useState } from 'react'
import { pie as d3Pie } from 'd3-shape'
import { useArcGenerator, computeArcBoundingBox } from '@nivo/arcs'
import {
degreesToRadians,
radiansToDegrees,
useValueFormatter,
usePropertyAccessor,
} from '@nivo/core'
import { OrdinalColorScaleConfig, useOrdinalColorScale } from '@nivo/colors'
import { defaultProps } from './props'
import {
MayHaveLabel,
CompletePieSvgProps,
ComputedDatum,
DatumId,
PieArc,
PieCustomLayerProps,
} from './types'
/**
* Format data so that we get a consistent data structure.
* It will also add the `formattedValue` and `color` property.
*/
export const useNormalizedData = <RawDatum extends MayHaveLabel>({
data,
id = defaultProps.id,
value = defaultProps.value,
valueFormat,
colors = defaultProps.colors as OrdinalColorScaleConfig<
Omit<ComputedDatum<RawDatum>, 'arc' | 'color' | 'fill'>
>,
}: Pick<CompletePieSvgProps<RawDatum>, 'id' | 'value' | 'valueFormat' | 'colors'> & {
data: readonly RawDatum[]
}): Omit<ComputedDatum<RawDatum>, 'arc' | 'fill'>[] => {
const getId = usePropertyAccessor<RawDatum, DatumId>(id)
const getValue = usePropertyAccessor<RawDatum, number>(value)
const formatValue = useValueFormatter<number>(valueFormat)
const getColor = useOrdinalColorScale<Omit<ComputedDatum<RawDatum>, 'arc' | 'color' | 'fill'>>(
colors,
'id'
)
return useMemo(
() =>
data.map(datum => {
const datumId = getId(datum)
const datumValue = getValue(datum)
const normalizedDatum: Omit<ComputedDatum<RawDatum>, 'arc' | 'color' | 'fill'> = {
id: datumId,
label: datum.label ?? datumId,
hidden: false,
value: datumValue,
formattedValue: formatValue(datumValue),
data: datum,
}
return {
...normalizedDatum,
color: getColor(normalizedDatum),
}
}),
[data, getId, getValue, formatValue, getColor]
)
}
/**
* Compute arcs, which don't depend yet on radius.
*/
export const usePieArcs = <RawDatum>({
data,
startAngle,
endAngle,
innerRadius,
outerRadius,
padAngle,
sortByValue,
activeId,
activeInnerRadiusOffset,
activeOuterRadiusOffset,
hiddenIds,
}: {
data: Omit<ComputedDatum<RawDatum>, 'arc' | 'fill'>[]
// in degrees
startAngle: number
// in degrees
endAngle: number
// in pixels
innerRadius: number
// in pixels
outerRadius: number
padAngle: number
sortByValue: boolean
activeId: null | DatumId
activeInnerRadiusOffset: number
activeOuterRadiusOffset: number
hiddenIds: DatumId[]
}): {
dataWithArc: Omit<ComputedDatum<RawDatum>, 'fill'>[]
legendData: Omit<ComputedDatum<RawDatum>, 'arc' | 'fill'>[]
} => {
const pie = useMemo(() => {
const innerPie = d3Pie<Omit<ComputedDatum<RawDatum>, 'arc' | 'fill'>>()
.value(d => d.value)
.startAngle(degreesToRadians(startAngle))
.endAngle(degreesToRadians(endAngle))
.padAngle(degreesToRadians(padAngle))
if (!sortByValue) {
innerPie.sortValues(null)
}
return innerPie
}, [startAngle, endAngle, padAngle, sortByValue])
return useMemo(() => {
const hiddenData = data.filter(item => !hiddenIds.includes(item.id))
const dataWithArc = pie(hiddenData).map(
(
arc: Omit<
PieArc,
'angle' | 'angleDeg' | 'innerRadius' | 'outerRadius' | 'thickness'
> & {
data: Omit<ComputedDatum<RawDatum>, 'arc' | 'fill'>
}
) => {
const angle = Math.abs(arc.endAngle - arc.startAngle)
return {
...arc.data,
arc: {
index: arc.index,
startAngle: arc.startAngle,
endAngle: arc.endAngle,
innerRadius:
activeId === arc.data.id
? innerRadius - activeInnerRadiusOffset
: innerRadius,
outerRadius:
activeId === arc.data.id
? outerRadius + activeOuterRadiusOffset
: outerRadius,
thickness: outerRadius - innerRadius,
padAngle: arc.padAngle,
angle,
angleDeg: radiansToDegrees(angle),
},
}
}
)
const legendData = data.map(item => ({ ...item, hidden: hiddenIds.includes(item.id) }))
return { dataWithArc, legendData }
}, [
pie,
data,
hiddenIds,
activeId,
innerRadius,
activeInnerRadiusOffset,
outerRadius,
activeOuterRadiusOffset,
])
}
/**
* Compute pie layout using explicit radius/innerRadius,
* expressed in pixels.
*/
export const usePie = <RawDatum>({
data,
radius,
innerRadius,
startAngle = defaultProps.startAngle,
endAngle = defaultProps.endAngle,
padAngle = defaultProps.padAngle,
sortByValue = defaultProps.sortByValue,
cornerRadius = defaultProps.cornerRadius,
activeInnerRadiusOffset = defaultProps.activeInnerRadiusOffset,
activeOuterRadiusOffset = defaultProps.activeOuterRadiusOffset,
}: Pick<
Partial<CompletePieSvgProps<RawDatum>>,
| 'startAngle'
| 'endAngle'
| 'padAngle'
| 'sortByValue'
| 'cornerRadius'
| 'activeInnerRadiusOffset'
| 'activeOuterRadiusOffset'
> & {
data: Omit<ComputedDatum<RawDatum>, 'arc'>[]
radius: number
innerRadius: number
}) => {
const [activeId, setActiveId] = useState<DatumId | null>(null)
const [hiddenIds, setHiddenIds] = useState<DatumId[]>([])
const pieArcs = usePieArcs({
data,
startAngle,
endAngle,
innerRadius,
outerRadius: radius,
padAngle,
sortByValue,
activeId,
activeInnerRadiusOffset,
activeOuterRadiusOffset,
hiddenIds,
})
const toggleSerie = useCallback((id: DatumId) => {
setHiddenIds(state =>
state.indexOf(id) > -1 ? state.filter(item => item !== id) : [...state, id]
)
}, [])
const arcGenerator = useArcGenerator({ cornerRadius, padAngle: degreesToRadians(padAngle) })
return { ...pieArcs, arcGenerator, setActiveId, toggleSerie }
}
/**
* Compute pie layout using a box to find radius/innerRadius,
* expressed in ratio (0~1), can optionally use the `fit`
* attribute to find the most space efficient layout.
*
* It also returns `centerX`/`centerY` as those can be altered
* if `fit` is `true`.
*/
export const usePieFromBox = <RawDatum>({
data,
width,
height,
innerRadius: innerRadiusRatio = defaultProps.innerRadius,
startAngle = defaultProps.startAngle,
endAngle = defaultProps.endAngle,
padAngle = defaultProps.padAngle,
sortByValue = defaultProps.sortByValue,
cornerRadius = defaultProps.cornerRadius,
fit = defaultProps.fit,
activeInnerRadiusOffset = defaultProps.activeInnerRadiusOffset,
activeOuterRadiusOffset = defaultProps.activeOuterRadiusOffset,
}: Pick<
CompletePieSvgProps<RawDatum>,
| 'width'
| 'height'
| 'innerRadius'
| 'startAngle'
| 'endAngle'
| 'padAngle'
| 'sortByValue'
| 'cornerRadius'
| 'fit'
| 'activeInnerRadiusOffset'
| 'activeOuterRadiusOffset'
> & {
data: Omit<ComputedDatum<RawDatum>, 'arc'>[]
}) => {
const [activeId, setActiveId] = useState<string | number | null>(null)
const [hiddenIds, setHiddenIds] = useState<DatumId[]>([])
const computedProps = useMemo(() => {
let radius = Math.min(width, height) / 2
let innerRadius = radius * Math.min(innerRadiusRatio, 1)
let centerX = width / 2
let centerY = height / 2
let boundingBox
if (fit) {
const { points, ...box } = computeArcBoundingBox(
centerX,
centerY,
radius,
startAngle - 90,
endAngle - 90
)
const ratio = Math.min(width / box.width, height / box.height)
const adjustedBox: {
width: number
height: number
x?: number
y?: number
} = {
width: box.width * ratio,
height: box.height * ratio,
}
adjustedBox.x = (width - adjustedBox.width) / 2
adjustedBox.y = (height - adjustedBox.height) / 2
centerX = ((centerX - box.x) / box.width) * box.width * ratio + adjustedBox.x
centerY = ((centerY - box.y) / box.height) * box.height * ratio + adjustedBox.y
boundingBox = { box, ratio, points }
radius = radius * ratio
innerRadius = innerRadius * ratio
}
return {
centerX,
centerY,
radius,
innerRadius,
debug: boundingBox,
}
}, [width, height, innerRadiusRatio, startAngle, endAngle, fit, cornerRadius])
const pieArcs = usePieArcs({
data,
startAngle,
endAngle,
innerRadius: computedProps.innerRadius,
outerRadius: computedProps.radius,
padAngle,
sortByValue,
activeId,
activeInnerRadiusOffset,
activeOuterRadiusOffset,
hiddenIds,
})
const toggleSerie = useCallback((id: DatumId) => {
setHiddenIds(state =>
state.indexOf(id) > -1 ? state.filter(item => item !== id) : [...state, id]
)
}, [])
const arcGenerator = useArcGenerator({
cornerRadius,
padAngle: degreesToRadians(padAngle),
})
return {
arcGenerator,
setActiveId,
toggleSerie,
...pieArcs,
...computedProps,
}
}
/**
* Memoize the context to pass to custom layers.
*/
export const usePieLayerContext = <RawDatum>({
dataWithArc,
arcGenerator,
centerX,
centerY,
radius,
innerRadius,
}: PieCustomLayerProps<RawDatum>): PieCustomLayerProps<RawDatum> =>
useMemo(
() => ({
dataWithArc,
arcGenerator,
centerX,
centerY,
radius,
innerRadius,
}),
[dataWithArc, arcGenerator, centerX, centerY, radius, innerRadius]
)