-
-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathtypes.ts
502 lines (454 loc) · 11.6 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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
import { ScaleBand, ScaleLinear, ScaleTime } from 'd3-scale'
import { CurveFactory, stackOffsetNone } from 'd3-shape'
import React, { CSSProperties, RefObject } from 'react'
import * as TSTB from 'ts-toolbelt'
import { TooltipRendererProps } from './components/TooltipRenderer'
export type ChartOptions<TDatum> = {
data: UserSerie<TDatum>[]
primaryAxis: AxisOptions<TDatum>
secondaryAxes: AxisOptions<TDatum>[]
padding?:
| number
| {
left?: number
right?: number
top?: number
bottom?: number
}
getSeriesStyle?: (
series: Series<TDatum>,
status: SeriesFocusStatus
) => SeriesStyles
getDatumStyle?: (
datum: Datum<TDatum>,
status: DatumFocusStatus
) => DatumStyles
getSeriesOrder?: (series: Series<TDatum>[]) => Series<TDatum>[]
interactionMode?: InteractionMode
showVoronoi?: boolean
showDebugAxes?: boolean
memoizeSeries?: boolean
defaultColors?: string[]
initialWidth?: number
initialHeight?: number
brush?: {
style?: CSSProperties
onSelect?: (selection: {
pointer: Pointer
start: unknown
end: unknown
}) => void
}
onFocusDatum?: (datum: Datum<TDatum> | null) => void
onClickDatum?: (
datum: Datum<TDatum> | null,
event: React.MouseEvent<SVGSVGElement, MouseEvent>
) => void
dark?: boolean
renderSVG?: () => React.ReactNode
primaryCursor?: boolean | CursorOptions
secondaryCursor?: boolean | CursorOptions
tooltip?: boolean | TooltipOptions<TDatum>
useIntersectionObserver?: boolean
intersectionObserverRootMargin?:
| `${number}px`
| `${number}px ${number}px`
| `${number}px ${number}px ${number}px ${number}px`
}
export type RequiredChartOptions<TDatum> = TSTB.Object.Required<
ChartOptions<TDatum>,
| 'getSeriesOrder'
| 'interactionMode'
| 'tooltipMode'
| 'showVoronoi'
| 'defaultColors'
| 'initialWidth'
| 'initialHeight'
| 'useIntersectionObserver'
| 'intersectionObserverThreshold'
| 'primaryCursor'
| 'secondaryCursor'
| 'padding'
>
export type ResolvedChartOptions<TDatum> = Omit<
RequiredChartOptions<TDatum>,
'tooltip'
> & {
tooltip: ResolvedTooltipOptions<TDatum>
}
export type ChartContextValue<TDatum> = {
getOptions: () => ResolvedChartOptions<TDatum>
gridDimensions: GridDimensions
primaryAxis: Axis<TDatum>
secondaryAxes: Axis<TDatum>[]
series: Series<TDatum>[]
orderedSeries: Series<TDatum>[]
datumsByInteractionGroup: Map<any, Datum<TDatum>[]>
datumsByTooltipGroup: Map<any, Datum<TDatum>[]>
width: number
height: number
svgRef: RefObject<SVGSVGElement>
getSeriesStatusStyle: (
series: Series<TDatum>,
focusedDatum: Datum<TDatum> | null
) => SeriesStyles
getDatumStatusStyle: (
datum: Datum<TDatum>,
focusedDatum: Datum<TDatum> | null
) => DatumStyles
axisDimensionsState: [
AxisDimensions,
React.Dispatch<React.SetStateAction<AxisDimensions>>
]
focusedDatumState: [
Datum<TDatum> | null,
React.Dispatch<React.SetStateAction<Datum<TDatum> | null>>
]
isInteractingState: [boolean, React.Dispatch<React.SetStateAction<boolean>>]
}
export type TooltipOptions<TDatum> = {
align?: AlignMode
alignPriority?: AlignPosition[]
padding?: number
arrowPadding?: number
groupingMode?: TooltipGroupingMode
show?: boolean
// anchor?: AnchorMode
// arrowPosition?: AlignPosition
render?: (props: TooltipRendererProps<TDatum>) => React.ReactNode
// formatSecondary?: (d: unknown) => string | number
// formatTertiary?: (d: unknown) => string | number
invert?: boolean,
showDatumInTooltip?: (datum: Datum<TDatum>) => boolean
}
export type ResolvedTooltipOptions<TDatum> = TSTB.Object.Required<
TooltipOptions<TDatum>,
'align' | 'alignPriority' | 'padding' | 'arrowPadding' | 'anchor' | 'render'
>
export type SeriesStyles = CSSProperties & {
area?: CSSProperties
line?: CSSProperties
circle?: CSSProperties
rectangle?: CSSProperties
}
export type DatumStyles = CSSProperties & {
area?: CSSProperties
line?: CSSProperties
circle?: CSSProperties
rectangle?: CSSProperties
}
export type Position = 'top' | 'right' | 'bottom' | 'left'
export type InteractionMode = 'closest' | 'primary'
export type TooltipGroupingMode = 'single' | 'primary' | 'secondary' | 'series'
export type AlignMode =
| 'auto'
| 'right'
| 'topRight'
| 'bottomRight'
| 'left'
| 'topLeft'
| 'bottomLeft'
| 'top'
| 'bottom'
| 'center'
export type AlignPosition =
| 'right'
| 'topRight'
| 'bottomRight'
| 'left'
| 'topLeft'
| 'bottomLeft'
| 'top'
| 'bottom'
export type AxisType = 'ordinal' | 'time' | 'localTime' | 'linear' | 'log'
export type AnchorMode =
| 'pointer'
| 'closest'
| 'center'
| 'top'
| 'bottom'
| 'left'
| 'right'
| 'gridTop'
| 'gridBottom'
| 'gridLeft'
| 'gridRight'
| 'gridCenter'
export type Side = 'left' | 'right' | 'top' | 'bottom'
type PointerBase = {
x: number
y: number
svgHovered: boolean
}
export type PointerUnpressed = PointerBase & {
dragging: false
}
export type PointerPressed = PointerBase & {
dragging: true
startX: number
startY: number
}
export type Pointer = PointerUnpressed | PointerPressed
export type ChartOffset = {
left: number
top: number
width: number
height: number
}
export type AxisDimension = {
paddingLeft: number
paddingRight: number
paddingTop: number
paddingBottom: number
width: number
height: number
}
export type AxisDimensions = {
left: Record<string, AxisDimension>
right: Record<string, AxisDimension>
top: Record<string, AxisDimension>
bottom: Record<string, AxisDimension>
}
export type AxisOptionsBase = {
primaryAxisId?: string
elementType?: 'line' | 'area' | 'bar' | 'bubble'
showDatumElements?: boolean | 'onFocus'
curve?: CurveFactory
invert?: boolean
position?: Position
minTickPaddingForRotation?: number
tickLabelRotationDeg?: number
tickCount?: number
shouldNice?: boolean
innerBandPadding?: number
outerBandPadding?: number
innerSeriesBandPadding?: number
outerSeriesBandPadding?: number
minBandSize?: number
maxBandSize?: number
minDomainLength?: number
showGrid?: boolean
show?: boolean
stacked?: boolean
stackOffset?: typeof stackOffsetNone
id?: string | number
styles?: CSSProperties & {
line?: CSSProperties
tick?: CSSProperties
}
}
export type AxisTimeOptions<TDatum> = AxisOptionsBase & {
scaleType?: 'time' | 'localTime'
getValue: (datum: TDatum) => ChartValue<Date>
min?: Date
max?: Date
hardMin?: Date
hardMax?: Date
padBandRange?: boolean
formatters?: {
scale?: (
value: Date,
formatters: AxisTimeOptions<TDatum>['formatters']
) => string
tooltip?: (
value: Date,
formatters: AxisTimeOptions<TDatum>['formatters']
) => React.ReactNode
cursor?: (
value: Date,
formatters: AxisTimeOptions<TDatum>['formatters']
) => React.ReactNode
}
}
export type AxisLinearOptions<TDatum> = AxisOptionsBase & {
scaleType?: 'linear' | 'log'
getValue: (datum: TDatum) => ChartValue<number>
min?: number
max?: number
hardMin?: number
hardMax?: number
// base?: number
formatters?: {
scale?: (
value: number,
formatters: AxisLinearOptions<TDatum>['formatters']
) => string
tooltip?: (
value: number,
formatters: AxisLinearOptions<TDatum>['formatters']
) => React.ReactNode
cursor?: (
value: number,
formatters: AxisLinearOptions<TDatum>['formatters']
) => React.ReactNode
}
}
export type AxisBandOptions<TDatum> = AxisOptionsBase & {
scaleType?: 'band'
getValue: (datum: TDatum) => ChartValue<any>
originalSinBandSize?: number
formatters?: {
scale?: (
value: any,
formatters: AxisBandOptions<TDatum>['formatters']
) => string
tooltip?: (
value: React.ReactNode,
formatters: AxisBandOptions<TDatum>['formatters']
) => string
cursor?: (
value: React.ReactNode,
formatters: AxisBandOptions<TDatum>['formatters']
) => string
}
}
export type AxisOptions<TDatum> =
| AxisTimeOptions<TDatum>
| AxisLinearOptions<TDatum>
| AxisBandOptions<TDatum>
export type AxisOptionsWithScaleType<TDatum> = TSTB.Object.Required<
AxisOptions<TDatum>,
'scaleType'
>
export type BuildAxisOptions<TDatum> = TSTB.Object.Required<
AxisOptions<TDatum>,
'position' | 'scaleType'
>
export type ResolvedAxisOptions<TAxisOptions> = TSTB.Object.Required<
TAxisOptions & {},
| 'scaleType'
| 'position'
| 'minTickPaddingForRotation'
| 'tickLabelRotationDeg'
| 'innerBandPadding'
| 'outerBandPadding'
| 'innerSeriesBandPadding'
| 'outerSeriesBandPadding'
| 'show'
| 'stacked'
>
export type ChartValue<T> = T | null | undefined
export type AxisBase = {
position: Position
isVertical: boolean
range: [number, number]
}
export type AxisTime<TDatum> = Omit<
AxisBase & ResolvedAxisOptions<AxisTimeOptions<TDatum>>,
'format'
> & {
isPrimary?: boolean
isInvalid: boolean
axisFamily: 'time'
scale: ScaleTime<number, number, never>
outerScale: ScaleTime<number, number, never>
primaryBandScale?: ScaleBand<number>
seriesBandScale?: ScaleBand<number>
formatters: {
default: (value: Date) => string
scale: (value: Date) => string
tooltip: (value: Date) => React.ReactNode
cursor: (value: Date) => React.ReactNode
}
}
export type AxisLinear<TDatum> = Omit<
AxisBase & ResolvedAxisOptions<AxisLinearOptions<TDatum>>,
'format'
> & {
isPrimary?: boolean
isInvalid: boolean
axisFamily: 'linear'
scale: ScaleLinear<number, number, never>
outerScale: ScaleLinear<number, number, never>
primaryBandScale?: ScaleBand<number>
seriesBandScale?: ScaleBand<number>
formatters: {
default: (value: ChartValue<any>) => string
scale: (value: number) => string
tooltip: (value: number) => React.ReactNode
cursor: (value: number) => React.ReactNode
}
}
export type AxisBand<TDatum> = Omit<
AxisBase & ResolvedAxisOptions<AxisBandOptions<TDatum>>,
'format'
> & {
isPrimary?: boolean
isInvalid: boolean
axisFamily: 'band'
scale: ScaleBand<any>
outerScale: ScaleBand<any>
primaryBandScale: ScaleBand<number>
seriesBandScale: ScaleBand<number>
formatters: {
default: (value: any) => string
scale: (value: any) => string
tooltip: (value: React.ReactNode) => string
cursor: (value: React.ReactNode) => string
}
}
export type Axis<TDatum> =
| AxisTime<TDatum>
| AxisLinear<TDatum>
| AxisBand<TDatum>
export type UserSerie<TDatum> = {
data: TDatum[]
id?: string
label?: string
color?: string
primaryAxisId?: string
secondaryAxisId?: string
}
//
export type Series<TDatum> = {
originalSeries: UserSerie<TDatum>
index: number
indexPerAxis: number
id: string
label: string
secondaryAxisId?: string
datums: Datum<TDatum>[]
style?: CSSProperties
}
export type Datum<TDatum> = {
originalSeries: UserSerie<TDatum>
seriesIndex: number
seriesId: string
seriesLabel: string
index: number
originalDatum: TDatum
secondaryAxisId?: string
seriesIndexPerAxis: number
primaryValue?: any
secondaryValue?: any
stackData?: StackDatum<TDatum>
interactiveGroup?: Datum<TDatum>[]
tooltipGroup?: Datum<TDatum>[]
style?: CSSProperties
element?: Element | null
}
export type StackDatum<TDatum> = {
0: number
1: number
data: Datum<TDatum>
length: number
}
//
export type Measurement = Side | 'width' | 'height'
export type GridDimensions = {
left: number
top: number
right: number
bottom: number
width: number
height: number
}
export type CursorOptions = {
value?: unknown
show?: boolean
showLine?: boolean
showLabel?: boolean
onChange?: (value: any) => void
}
export type SeriesFocusStatus = 'none' | 'focused'
export type DatumFocusStatus = 'none' | 'focused' | 'groupFocused'