forked from plouc/nivo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.ts
317 lines (263 loc) · 8.63 KB
/
types.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
import * as React from 'react'
import { AnnotationMatcher } from '@nivo/annotations'
import { AxisProps, CanvasAxisProps, GridValues } from '@nivo/axes'
import {
Box,
CartesianMarkerProps,
Dimensions,
Margin,
MotionProps,
PropertyAccessor,
SvgDefsAndFill,
Theme,
ValueFormat,
} from '@nivo/core'
import { InheritedColorConfig, OrdinalColorScaleConfig } from '@nivo/colors'
import { LegendProps } from '@nivo/legends'
import { AnyScale, ScaleSpec, ScaleBandSpec } from '@nivo/scales'
import { SpringValues } from '@react-spring/web'
export interface BarDatum {
[key: string]: string | number
}
export interface DataProps<RawDatum extends BarDatum> {
data: readonly RawDatum[]
}
export type BarDatumWithColor = BarDatum & {
color: string
}
export type ComputedDatum<RawDatum> = {
id: string | number
value: number | null
formattedValue: string
hidden: boolean
index: number
indexValue: string | number
data: Exclude<RawDatum, null | undefined | false | '' | 0>
fill?: string
}
export type ComputedBarDatumWithValue<RawDatum> = ComputedBarDatum<RawDatum> & {
data: ComputedDatum<RawDatum> & {
value: number
}
}
export type ComputedBarDatum<RawDatum> = {
key: string
index: number
data: ComputedDatum<RawDatum>
x: number
y: number
absX: number
absY: number
width: number
height: number
color: string
label: string
}
export type BarsWithHidden<RawDatum> = Array<
Partial<{
key: string
x: number
y: number
width: number
height: number
color: string
}> & {
data: Partial<ComputedDatum<RawDatum>> & {
id: string | number
hidden: boolean
}
}
>
export type LegendLabelDatum<RawDatum> = Partial<ComputedDatum<RawDatum>> & {
id: string | number
hidden: boolean
}
export type LegendData = {
id: string | number
label: string | number
hidden: boolean
color: string
}
export interface BarLegendProps extends LegendProps {
dataFrom: 'indexes' | 'keys'
}
export type LabelFormatter = (label: string | number) => string | number
export type ValueFormatter = (value: number) => string | number
export type BarLayerId = 'grid' | 'axes' | 'bars' | 'markers' | 'legends' | 'annotations'
export type BarCanvasLayerId = Exclude<BarLayerId, 'markers'>
interface BarCustomLayerBaseProps<RawDatum>
extends Pick<
BarCommonProps<RawDatum>,
| 'borderRadius'
| 'borderWidth'
| 'enableLabel'
| 'isInteractive'
| 'labelSkipHeight'
| 'labelSkipWidth'
| 'tooltip'
>,
Dimensions {
bars: readonly ComputedBarDatum<RawDatum>[]
legendData: [BarLegendProps, readonly LegendData[]][]
margin: Margin
innerWidth: number
innerHeight: number
isFocusable: boolean
getTooltipLabel: (datum: ComputedDatum<RawDatum>) => string | number
xScale: AnyScale
yScale: AnyScale
}
export interface BarCustomLayerProps<RawDatum>
extends BarCustomLayerBaseProps<RawDatum>,
BarHandlers<RawDatum, SVGRectElement> {}
export interface BarCanvasCustomLayerProps<RawDatum>
extends BarCustomLayerBaseProps<RawDatum>,
BarHandlers<RawDatum, HTMLCanvasElement> {}
export type BarCanvasCustomLayer<RawDatum> = (
context: CanvasRenderingContext2D,
props: BarCanvasCustomLayerProps<RawDatum>
) => void
export type BarCustomLayer<RawDatum> = React.FC<BarCustomLayerProps<RawDatum>>
export type BarCanvasLayer<RawDatum> = BarCanvasLayerId | BarCanvasCustomLayer<RawDatum>
export type BarLayer<RawDatum> = BarLayerId | BarCustomLayer<RawDatum>
export interface BarItemProps<RawDatum extends BarDatum>
extends Pick<
BarCommonProps<RawDatum>,
'borderRadius' | 'borderWidth' | 'isInteractive' | 'tooltip'
>,
BarHandlers<RawDatum, SVGRectElement> {
bar: ComputedBarDatum<RawDatum> & {
data: {
value: number
}
}
style: SpringValues<{
borderColor: string
color: string
height: number
labelColor: string
labelOpacity: number
labelX: number
labelY: number
opacity: number
transform: string
width: number
}>
label: string
shouldRenderLabel: boolean
isFocusable: boolean
ariaLabel?: BarSvgProps<RawDatum>['barAriaLabel']
ariaLabelledBy?: BarSvgProps<RawDatum>['barAriaLabelledBy']
ariaDescribedBy?: BarSvgProps<RawDatum>['barAriaDescribedBy']
}
export type RenderBarProps<RawDatum extends BarDatum> = Omit<
BarItemProps<RawDatum>,
| 'isInteractive'
| 'style'
| 'tooltip'
| 'isFocusable'
| 'ariaLabel'
| 'ariaLabelledBy'
| 'ariaDescribedBy'
> & {
borderColor: string
labelColor: string
}
export interface BarTooltipProps<RawDatum> extends ComputedDatum<RawDatum> {
color: string
label: string
value: number
}
export type BarHandlers<RawDatum, Element> = {
onClick?: (
datum: ComputedDatum<RawDatum> & { color: string },
event: React.MouseEvent<Element>
) => void
onMouseEnter?: (datum: ComputedDatum<RawDatum>, event: React.MouseEvent<Element>) => void
onMouseLeave?: (datum: ComputedDatum<RawDatum>, event: React.MouseEvent<Element>) => void
}
export type BarCommonProps<RawDatum> = {
indexBy: PropertyAccessor<RawDatum, string>
keys: readonly string[]
maxValue: 'auto' | number
minValue: 'auto' | number
margin?: Box
innerPadding: number
padding: number
valueScale: ScaleSpec
indexScale: ScaleBandSpec
enableGridX: boolean
gridXValues?: GridValues<string | number>
enableGridY: boolean
gridYValues?: GridValues<string | number>
borderColor: InheritedColorConfig<ComputedBarDatumWithValue<RawDatum>>
borderRadius: number
borderWidth: number
enableLabel: boolean
label: PropertyAccessor<ComputedDatum<RawDatum>, string>
labelFormat: string | LabelFormatter
labelSkipWidth: number
labelSkipHeight: number
labelTextColor: InheritedColorConfig<ComputedBarDatumWithValue<RawDatum>>
isInteractive: boolean
tooltip: React.FC<BarTooltipProps<RawDatum>>
valueFormat?: ValueFormat<number>
legendLabel?: PropertyAccessor<LegendLabelDatum<RawDatum>, string>
tooltipLabel: PropertyAccessor<ComputedDatum<RawDatum>, string>
groupMode: 'grouped' | 'stacked'
layout: 'horizontal' | 'vertical'
reverse: boolean
colorBy: 'id' | 'indexValue'
colors: OrdinalColorScaleConfig<ComputedDatum<RawDatum>>
theme: Theme
annotations: readonly AnnotationMatcher<ComputedBarDatum<RawDatum>>[]
legends: readonly BarLegendProps[]
renderWrapper?: boolean
initialHiddenIds: readonly (string | number)[]
enableTotals?: boolean
totalsOffset?: number
}
export type BarSvgProps<RawDatum extends BarDatum> = Partial<BarCommonProps<RawDatum>> &
DataProps<RawDatum> &
BarHandlers<RawDatum, SVGRectElement> &
SvgDefsAndFill<ComputedBarDatum<RawDatum>> &
Dimensions &
MotionProps &
Partial<{
axisBottom: AxisProps | null
axisLeft: AxisProps | null
axisRight: AxisProps | null
axisTop: AxisProps | null
barComponent: React.FC<BarItemProps<RawDatum>>
markers: readonly CartesianMarkerProps[]
layers: readonly BarLayer<RawDatum>[]
role: string
ariaLabel?: React.AriaAttributes['aria-label']
ariaLabelledBy?: React.AriaAttributes['aria-labelledby']
ariaDescribedBy?: React.AriaAttributes['aria-describedby']
isFocusable?: boolean
barAriaLabel?: (data: ComputedDatum<RawDatum>) => React.AriaAttributes['aria-label']
barAriaLabelledBy?: (
data: ComputedDatum<RawDatum>
) => React.AriaAttributes['aria-labelledby']
barAriaDescribedBy?: (
data: ComputedDatum<RawDatum>
) => React.AriaAttributes['aria-describedby']
}>
export type BarCanvasProps<RawDatum extends BarDatum> = Partial<BarCommonProps<RawDatum>> &
DataProps<RawDatum> &
BarHandlers<RawDatum, HTMLCanvasElement> &
Dimensions &
Partial<{
axisBottom: CanvasAxisProps<any> | null
axisLeft: CanvasAxisProps<any> | null
axisRight: CanvasAxisProps<any> | null
axisTop: CanvasAxisProps<any> | null
renderBar: (context: CanvasRenderingContext2D, props: RenderBarProps<RawDatum>) => void
layers: BarCanvasLayer<RawDatum>[]
pixelRatio: number
}>
export type BarAnnotationsProps<RawDatum> = {
annotations: readonly AnnotationMatcher<ComputedBarDatum<RawDatum>>[]
bars: readonly ComputedBarDatum<RawDatum>[]
}