/
hooks.ts
146 lines (134 loc) · 4.28 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
import { useMemo } from 'react'
import { scaleLinear } from 'd3-scale'
import {
// @ts-ignore
bindDefs,
useCurveInterpolation,
usePropertyAccessor,
useValueFormatter,
} from '@nivo/core'
import { useOrdinalColorScale } from '@nivo/colors'
import { svgDefaultProps } from './props'
import {
RadarColorMapping,
RadarCommonProps,
RadarDataProps,
RadarCustomLayerProps,
RadarSvgProps,
BoundLegendProps,
} from './types'
export const useRadar = <D extends Record<string, unknown>>({
data,
keys,
indexBy,
maxValue,
valueFormat,
curve,
width,
height,
colors = svgDefaultProps.colors,
legends,
defs,
fill,
}: {
data: RadarDataProps<D>['data']
keys: RadarDataProps<D>['keys']
indexBy: RadarDataProps<D>['indexBy']
maxValue: RadarCommonProps<D>['maxValue']
valueFormat?: RadarCommonProps<D>['valueFormat']
curve: RadarCommonProps<D>['curve']
width: number
height: number
colors: RadarCommonProps<D>['colors']
legends: RadarCommonProps<D>['legends']
defs: RadarSvgProps<D>['defs']
fill: RadarSvgProps<D>['fill']
}) => {
const getIndex = usePropertyAccessor<D, string>(indexBy)
const indices = useMemo(() => data.map(getIndex), [data, getIndex])
const formatValue = useValueFormatter<number, string>(valueFormat)
const getColor = useOrdinalColorScale<{ key: string; index: number }>(colors, 'key')
const colorByKey: RadarColorMapping = useMemo(
() =>
keys.reduce<RadarColorMapping>((mapping, key, index) => {
mapping[key] = getColor({ key, index })
return mapping
}, {}),
[keys, getColor]
)
const { boundDefs, fillByKey } = useMemo(() => {
// expand keys into structure expected by bindDefs
const keyData = keys.map(k => ({ key: k, color: colorByKey[k], data, fill: null }))
const boundDefs = bindDefs(defs, keyData, fill)
const fillByKey = keyData.reduce<Record<string, string | null>>((mapping, keyDatum) => {
const { key: keyName, fill } = keyDatum
mapping[keyName] = fill
return mapping
}, {})
return { boundDefs, fillByKey }
}, [keys, data, defs, fill, colorByKey])
const { radius, radiusScale, centerX, centerY, angleStep } = useMemo(() => {
const allValues: number[] = data.reduce(
(acc: number[], d) => [...acc, ...keys.map(key => d[key] as number)],
[] as number[]
)
const computedMaxValue = maxValue !== 'auto' ? maxValue : Math.max(...allValues)
const radius = Math.min(width, height) / 2
const radiusScale = scaleLinear<number, number>()
.range([0, radius])
.domain([0, computedMaxValue])
return {
radius,
radiusScale,
centerX: width / 2,
centerY: height / 2,
angleStep: (Math.PI * 2) / data.length,
}
}, [keys, data, maxValue, width, height])
const curveFactory = useCurveInterpolation(curve)
const customLayerProps: RadarCustomLayerProps<D> = useMemo(
() => ({
data,
keys,
indices,
colorByKey,
centerX,
centerY,
radiusScale,
angleStep,
}),
[data, keys, indices, colorByKey, centerX, centerY, radiusScale, angleStep]
)
const legendData = useMemo(
() => keys.map(key => ({ id: key, label: key, color: colorByKey[key] })),
[keys, colorByKey]
)
const boundLegends: BoundLegendProps[] = useMemo(
() =>
legends.map(({ data: customData, ...legend }) => {
const boundData = customData?.map(cd => {
const findData = legendData.find(ld => ld.id === cd.id) || {}
return { ...findData, ...cd }
})
return { ...legend, data: boundData || legendData }
}),
[legends, legendData]
)
return {
getIndex,
indices,
formatValue,
colorByKey,
fillByKey,
boundDefs,
radius,
radiusScale,
centerX,
centerY,
angleStep,
curveFactory,
legendData,
boundLegends,
customLayerProps,
}
}