/
props.ts
571 lines (561 loc) · 17.1 KB
/
props.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
import { svgDefaultProps } from '@nivo/bar'
import {
themeProperty,
motionProperties,
defsProperties,
getLegendsProps,
groupProperties,
} from '../../../lib/componentProperties'
import {
chartDimensions,
ordinalColors,
chartGrid,
axes,
isInteractive,
commonAccessibilityProps,
} from '../../../lib/chart-properties'
import { ChartProperty, Flavor } from '../../../types'
const allFlavors: Flavor[] = ['svg', 'canvas', 'api']
const props: ChartProperty[] = [
{
key: 'data',
group: 'Base',
help: 'Chart data.',
type: 'object[]',
required: true,
flavors: allFlavors,
},
{
key: 'indexBy',
group: 'Base',
help: 'Key to use to index the data.',
description: `
Key to use to index the data,
this key must exist in each data item.
You can also provide a function which will
receive the data item and must return the desired index.
`,
type: 'string | (datum: RawDatum): string | number',
flavors: allFlavors,
required: false,
defaultValue: svgDefaultProps.indexBy,
},
{
key: 'keys',
group: 'Base',
help: 'Keys to use to determine each serie.',
type: 'string[]',
flavors: allFlavors,
required: false,
defaultValue: svgDefaultProps.keys,
},
{
key: 'groupMode',
group: 'Base',
help: `How to group bars.`,
type: `'grouped' | 'stacked'`,
flavors: allFlavors,
required: false,
defaultValue: svgDefaultProps.groupMode,
control: {
type: 'radio',
choices: [
{ label: 'stacked', value: 'stacked' },
{ label: 'grouped', value: 'grouped' },
],
},
},
{
key: 'layout',
group: 'Base',
help: `How to display bars.`,
type: `'horizontal' | 'vertical'`,
flavors: allFlavors,
required: false,
defaultValue: svgDefaultProps.layout,
control: {
type: 'radio',
choices: [
{ label: 'horizontal', value: 'horizontal' },
{ label: 'vertical', value: 'vertical' },
],
},
},
{
key: 'valueScale',
group: 'Base',
type: 'object',
help: `value scale configuration.`,
defaultValue: svgDefaultProps.valueScale,
flavors: allFlavors,
required: false,
control: {
type: 'object',
props: [
{
key: 'type',
help: `Scale type.`,
type: 'string',
required: true,
flavors: allFlavors,
control: {
type: 'choices',
disabled: true,
choices: ['linear', 'symlog'].map(v => ({
label: v,
value: v,
})),
},
},
],
},
},
{
key: 'indexScale',
group: 'Base',
type: 'object',
help: `index scale configuration.`,
defaultValue: svgDefaultProps.indexScale,
flavors: allFlavors,
required: false,
control: {
type: 'object',
props: [
{
key: 'type',
help: `Scale type.`,
type: 'string',
required: true,
flavors: ['svg', 'canvas', 'api'],
control: {
type: 'choices',
disabled: true,
choices: ['band'].map(v => ({
label: v,
value: v,
})),
},
},
{
key: 'round',
required: true,
flavors: ['svg', 'canvas', 'api'],
help: 'Toggle index scale (for bar width) rounding.',
type: 'boolean',
control: { type: 'switch' },
},
],
},
},
{
key: 'reverse',
group: 'Base',
help: 'Reverse bars, starts on top instead of bottom for vertical layout and right instead of left for horizontal one.',
type: 'boolean',
required: false,
flavors: allFlavors,
defaultValue: svgDefaultProps.reverse,
control: { type: 'switch' },
},
{
key: 'minValue',
group: 'Base',
help: 'Minimum value.',
description: `
Minimum value, if 'auto',
will use min value from the provided data.
`,
required: false,
flavors: allFlavors,
defaultValue: svgDefaultProps.minValue,
type: `number | 'auto'`,
control: {
type: 'switchableRange',
disabledValue: 'auto',
defaultValue: -1000,
min: -1000,
max: 0,
},
},
{
key: 'maxValue',
group: 'Base',
help: 'Maximum value.',
description: `
Maximum value, if 'auto',
will use max value from the provided data.
`,
required: false,
flavors: allFlavors,
defaultValue: svgDefaultProps.maxValue,
type: `number | 'auto'`,
control: {
type: 'switchableRange',
disabledValue: 'auto',
defaultValue: 1000,
min: 0,
max: 1000,
},
},
{
key: 'valueFormat',
group: 'Base',
help: 'Optional formatter for values.',
description: `
The formatted value can then be used for labels & tooltips.
Under the hood, nivo uses [d3-format](https://github.com/d3/d3-format),
please have a look at it for available formats, you can also pass a function
which will receive the raw value and should return the formatted one.
`,
required: false,
flavors: allFlavors,
type: 'string | (value: number) => string | number',
control: { type: 'valueFormat' },
},
{
key: 'padding',
help: 'Padding between each bar (ratio).',
type: 'number',
required: false,
flavors: allFlavors,
defaultValue: svgDefaultProps.padding,
group: 'Base',
control: {
type: 'range',
min: 0,
max: 0.9,
step: 0.05,
},
},
{
key: 'innerPadding',
help: 'Padding between grouped/stacked bars.',
type: 'number',
required: false,
flavors: allFlavors,
defaultValue: svgDefaultProps.innerPadding,
group: 'Base',
control: {
type: 'range',
unit: 'px',
min: 0,
max: 10,
},
},
...chartDimensions(allFlavors),
themeProperty(allFlavors),
ordinalColors({
flavors: allFlavors,
defaultValue: svgDefaultProps.colors,
}),
{
key: 'colorBy',
type: `'id' | 'indexValue'`,
help: 'Property used to determine node color.',
description: `
Property to use to determine node color.
`,
flavors: allFlavors,
required: false,
defaultValue: svgDefaultProps.colorBy,
group: 'Style',
control: {
type: 'choices',
choices: [
{
label: 'id',
value: 'id',
},
{
label: 'indexValue',
value: 'indexValue',
},
],
},
},
{
key: 'borderRadius',
help: 'Rectangle border radius.',
type: 'number',
flavors: ['svg', 'canvas', 'api'],
required: false,
defaultValue: svgDefaultProps.borderRadius,
group: 'Style',
control: {
type: 'range',
unit: 'px',
min: 0,
max: 36,
},
},
{
key: 'borderWidth',
help: 'Width of bar border.',
type: 'number',
flavors: ['svg', 'canvas', 'api'],
required: false,
defaultValue: svgDefaultProps.borderWidth,
group: 'Style',
control: { type: 'lineWidth' },
},
{
key: 'borderColor',
help: 'Method to compute border color.',
description: `
how to compute border color,
[see dedicated documentation](self:/guides/colors).
`,
type: 'string | object | Function',
flavors: ['svg', 'canvas', 'api'],
required: false,
defaultValue: svgDefaultProps.borderColor,
group: 'Style',
control: { type: 'inheritedColor' },
},
...defsProperties('Style', ['svg']),
{
key: 'layers',
flavors: ['svg', 'canvas'],
help: 'Defines the order of layers.',
description: `
Defines the order of layers, available layers are:
\`grid\`, \`axes\`, \`bars\`, \`markers\`, \`legends\`,
\`annotations\`. The \`markers\` layer is not available
in the canvas flavor.
You can also use this to insert extra layers to the chart,
this extra layer must be a function which will receive
the chart computed data and must return a valid SVG
element.
`,
type: 'Array<string | Function>',
required: false,
defaultValue: svgDefaultProps.layers,
group: 'Customization',
},
{
key: 'enableLabel',
help: 'Enable/disable labels.',
type: 'boolean',
flavors: ['svg', 'canvas', 'api'],
required: false,
defaultValue: svgDefaultProps.enableLabel,
group: 'Labels',
control: { type: 'switch' },
},
{
key: 'label',
group: 'Labels',
help: 'Define how bar labels are computed.',
description: `
Define how bar labels are computed.
By default it will use the bar's value.
It accepts a string which will be used to access
a specific bar data property, such as
\`'value'\` or \`'id'\`.
You can also use a funtion if you want to
add more logic, this function will receive
the current bar's data and must return
the computed label which, depending on the context,
should return a string or an svg element (Bar) or
a string (BarCanvas). For example let's say you want
to use a label with both the id and the value,
you can achieve this with:
\`\`\`
label={d => \`\${d.id}: \${d.value}\`}
\`\`\`
`,
type: 'string | Function',
flavors: ['svg', 'canvas', 'api'],
required: false,
defaultValue: svgDefaultProps.label,
},
{
key: 'labelSkipWidth',
help: 'Skip label if bar width is lower than provided value, ignored if 0.',
type: 'number',
flavors: ['svg', 'canvas', 'api'],
required: false,
defaultValue: svgDefaultProps.labelSkipWidth,
group: 'Labels',
control: {
type: 'range',
unit: 'px',
min: 0,
max: 36,
},
},
{
key: 'labelSkipHeight',
help: 'Skip label if bar height is lower than provided value, ignored if 0.',
type: 'number',
flavors: ['svg', 'canvas', 'api'],
required: false,
defaultValue: svgDefaultProps.labelSkipHeight,
group: 'Labels',
control: {
type: 'range',
unit: 'px',
min: 0,
max: 36,
},
},
{
key: 'labelTextColor',
help: 'Defines how to compute label text color.',
type: 'string | object | Function',
flavors: ['svg', 'canvas', 'api'],
required: false,
defaultValue: svgDefaultProps.labelTextColor,
control: { type: 'inheritedColor' },
group: 'Labels',
},
...chartGrid({
flavors: allFlavors,
xDefault: svgDefaultProps.enableGridX,
yDefault: svgDefaultProps.enableGridY,
values: true,
}),
...axes({ flavors: allFlavors }),
isInteractive({
flavors: ['svg', 'canvas'],
defaultValue: svgDefaultProps.isInteractive,
}),
{
key: 'tooltip',
flavors: ['svg', 'canvas'],
group: 'Interactivity',
type: 'Function',
required: false,
help: 'Tooltip custom component',
description: `
A function allowing complete tooltip customisation,
it must return a valid HTML element and will receive
the following data:
\`\`\`
{
bar: {
id: string | number,
value: number,
formattedValue: string,
index: number,
indexValue: string | number,
// datum associated to the current index (raw data)
data: object
},
color: string,
label: string
}
\`\`\`
You can also customize the style of the tooltip
using the \`theme.tooltip\` object.
`,
},
{
key: 'custom tooltip example',
flavors: ['svg', 'canvas'],
group: 'Interactivity',
help: 'Showcase custom tooltip component.',
type: 'boolean',
required: false,
control: { type: 'switch' },
},
{
key: 'onClick',
flavors: ['svg', 'canvas'],
group: 'Interactivity',
type: 'Function',
required: false,
help: 'onClick handler',
description: `
onClick handler, will receive node data as first argument
& event as second one. The node data has the following shape:
\`\`\`
{
id: string | number,
value: number,
formattedValue: string,
index: number,
indexValue: string | number,
color: string,
// datum associated to the current index (raw data)
data: object
}
\`\`\`
`,
},
{
key: 'legends',
flavors: ['svg', 'canvas'],
type: 'object[]',
help: `Optional chart's legends.`,
group: 'Legends',
required: false,
control: {
type: 'array',
props: getLegendsProps(['svg']),
shouldCreate: true,
addLabel: 'add legend',
shouldRemove: true,
getItemTitle: (index: number, legend: any) =>
`legend[${index}]: ${legend.anchor}, ${legend.direction}`,
svgDefaultProps: {
dataFrom: 'keys',
anchor: 'top-left',
direction: 'column',
justify: false,
translateX: 0,
translateY: 0,
itemWidth: 100,
itemHeight: 20,
itemsSpacing: 0,
symbolSize: 20,
itemDirection: 'left-to-right',
onClick: (data: any) => {
console.log(JSON.stringify(data, null, ' '))
},
},
},
},
...motionProperties(['svg'], svgDefaultProps, 'react-spring'),
{
key: 'isFocusable',
flavors: ['svg'],
required: false,
group: 'Accessibility',
help: 'Make the root SVG element and each bar item focusable, for keyboard navigation.',
description: `
If enabled, focusing will also reveal the tooltip if \`isInteractive\` is \`true\`,
when a bar gains focus and hide it on blur.
Also note that if this option is enabled, focusing a bar will reposition the tooltip
at a fixed location.
`,
type: 'boolean',
control: { type: 'switch' },
},
...commonAccessibilityProps(['svg']),
{
key: 'barAriaLabel',
flavors: ['svg'],
required: false,
group: 'Accessibility',
help: '[aria-label](https://www.w3.org/TR/wai-aria/#aria-label) for bar items.',
type: '(data) => string',
},
{
key: 'barAriaLabelledBy',
flavors: ['svg'],
required: false,
group: 'Accessibility',
help: '[aria-labelledby](https://www.w3.org/TR/wai-aria/#aria-labelledby) for bar items.',
type: '(data) => string',
},
{
key: 'barAriaDescribedBy',
flavors: ['svg'],
required: false,
group: 'Accessibility',
help: '[aria-describedby](https://www.w3.org/TR/wai-aria/#aria-describedby) for bar items.',
type: '(data) => string',
},
]
export const groups = groupProperties(props)