1
1
import { useState , useCallback , useMemo } from 'react'
2
2
import omit from 'lodash/omit'
3
3
import { area , curveMonotoneX } from 'd3-shape'
4
- import { storiesOf } from '@storybook/react'
4
+ import { Meta } from '@storybook/react'
5
5
import {
6
6
ScatterPlot ,
7
7
ResponsiveScatterPlot ,
@@ -11,6 +11,11 @@ import {
11
11
// @ts -ignore
12
12
} from '../src'
13
13
14
+ export default {
15
+ component : ScatterPlot ,
16
+ title : 'ScatterPlot' ,
17
+ } as Meta
18
+
14
19
type SampleDatum = {
15
20
id : number
16
21
x : number
@@ -163,17 +168,15 @@ const commonProps = {
163
168
] ,
164
169
}
165
170
166
- const stories = storiesOf ( 'ScatterPlot' , module )
167
-
168
- stories . add ( 'default' , ( ) => < ScatterPlot < SampleDatum > { ...commonProps } data = { [ sampleData [ 1 ] ] } /> )
171
+ export const Default = ( ) => < ScatterPlot < SampleDatum > { ...commonProps } data = { [ sampleData [ 1 ] ] } />
169
172
170
- stories . add ( 'multiple series' , ( ) => < ScatterPlot < SampleDatum > { ...commonProps } /> )
173
+ export const mutlipleSeries = ( ) => < ScatterPlot < SampleDatum > { ...commonProps } />
171
174
172
- stories . add ( 'alternative colors' , ( ) => (
175
+ export const alternativeColors = ( ) => (
173
176
< ScatterPlot < SampleDatum > { ...commonProps } colors = { { scheme : 'category10' } } />
174
- ) )
177
+ )
175
178
176
- stories . add ( 'using time scales' , ( ) => (
179
+ export const usingTimeScales = ( ) => (
177
180
< ScatterPlot < { x : string ; y : number } >
178
181
{ ...commonProps }
179
182
data = { [
@@ -215,9 +218,9 @@ stories.add('using time scales', () => (
215
218
tickValues : 'every 2 days' ,
216
219
} }
217
220
/>
218
- ) )
221
+ )
219
222
220
- stories . add ( 'using logarithmic scales' , ( ) => (
223
+ export const usingLogarithmicScales = ( ) => (
221
224
< ScatterPlot < { x : number ; y : number } >
222
225
{ ...commonProps }
223
226
data = { [
@@ -250,9 +253,9 @@ stories.add('using logarithmic scales', () => (
250
253
tickValues : [ 2 , 4 , 8 , 16 , 32 , 64 ] ,
251
254
} }
252
255
/>
253
- ) )
256
+ )
254
257
255
- stories . add ( 'using symmetric logarithmic scales' , ( ) => (
258
+ export const usingSymmetricLogarithmicScales = ( ) => (
256
259
< ScatterPlot < { x : number ; y : number } >
257
260
{ ...commonProps }
258
261
data = { [
@@ -280,18 +283,18 @@ stories.add('using symmetric logarithmic scales', () => (
280
283
tickValues : [ 0 , 1 , 2 , 3 , 4 , 5 ] ,
281
284
} }
282
285
/>
283
- ) )
286
+ )
284
287
285
- stories . add ( 'node size' , ( ) => < ScatterPlot < SampleDatum > { ...commonProps } nodeSize = { 24 } /> )
288
+ export const nodeSize = ( ) => < ScatterPlot < SampleDatum > { ...commonProps } nodeSize = { 24 } />
286
289
287
- stories . add ( 'varying node size' , ( ) => (
290
+ export const varyingNodeSize = ( ) => (
288
291
< ScatterPlot < SampleDatum >
289
292
{ ...commonProps }
290
- nodeSize = { { key : 'z' , values : [ 0 , 4 ] , sizes : [ 9 , 32 ] } }
293
+ nodeSize = { { key : 'data. z' , values : [ 0 , 4 ] , sizes : [ 9 , 32 ] } }
291
294
/>
292
- ) )
295
+ )
293
296
294
- stories . add ( 'custom tooltip' , ( ) => (
297
+ export const customTooltip = ( ) => (
295
298
< ScatterPlot < SampleDatum >
296
299
{ ...commonProps }
297
300
tooltip = { ( { node } ) => (
@@ -312,7 +315,7 @@ stories.add('custom tooltip', () => (
312
315
</ div >
313
316
) }
314
317
/>
315
- ) )
318
+ )
316
319
317
320
const SyncCharts = ( ) => {
318
321
const [ nodeId , setNodeId ] = useState ( null )
@@ -370,50 +373,52 @@ const SyncCharts = () => {
370
373
)
371
374
}
372
375
373
- stories . add ( 'synchronizing charts' , ( ) => < SyncCharts /> , {
374
- info : {
375
- text : `
376
- You can synchronize several charts using mouse handlers.
377
- This example wraps 2 scatterplots in a parent component and
378
- store current symbol id in a state which is then used to
379
- determine symbol size, using \`onMouseMove\`, \`onMouseLeave\`
380
- and a custom function for \`nodeSize\`.
381
-
382
- Note that \`useMesh\` \`debugMesh\` are enabled on this example
383
- hence the extra red lines displayed on the chart.
384
-
385
- The parent component hooks should look like this:
386
-
387
- \`\`\`
388
- const [nodeId, setNodeId] = useState(null)
389
- const handleMouseMove = useCallback((node) => setNodeId(node.id), [setNodeId])
390
- const handleMouseLeave = useCallback(() => setNodeId(null), [setNodeId])
391
- const getNodeSize = useMemo(
392
- () => node => {
393
- if (nodeId !== null && nodeId === node.id) return 46
394
- return 8
395
- },
396
- [nodeId]
397
- )
398
- \`\`\`
399
-
400
- and the two scatterplots share those properties:
401
-
402
- \`\`\`
403
- <ResponsiveScatterPlot
404
- {/* other required props */}
405
- nodeSize={getNodeSize}
406
- onMouseMove={handleMouseMove}
407
- onMouseLeave={handleMouseLeave}
408
- />
409
- \`\`\`
410
-
411
- This approach can also be used to synchronize another chart type.
412
- ` ,
376
+ export const synchronizingCharts = ( ) => < SyncCharts />
377
+ synchronizingCharts . story = {
378
+ parameters : {
379
+ info : {
380
+ text : `
381
+ You can synchronize several charts using mouse handlers.
382
+ This example wraps 2 scatterplots in a parent component and
383
+ store current symbol id in a state which is then used to
384
+ determine symbol size, using \`onMouseMove\`, \`onMouseLeave\`
385
+ and a custom function for \`nodeSize\`.
386
+
387
+ Note that \`useMesh\` \`debugMesh\` are enabled on this example
388
+ hence the extra red lines displayed on the chart.
389
+
390
+ The parent component hooks should look like this:
391
+
392
+ \`\`\`
393
+ const [nodeId, setNodeId] = useState(null)
394
+ const handleMouseMove = useCallback((node) => setNodeId(node.id), [setNodeId])
395
+ const handleMouseLeave = useCallback(() => setNodeId(null), [setNodeId])
396
+ const getNodeSize = useMemo(
397
+ () => node => {
398
+ if (nodeId !== null && nodeId === node.id) return 46
399
+ return 8
400
+ },
401
+ [nodeId]
402
+ )
403
+ \`\`\`
404
+
405
+ and the two scatterplots share those properties:
406
+
407
+ \`\`\`
408
+ <ResponsiveScatterPlot
409
+ nodeSize={getNodeSize}
410
+ onMouseMove={handleMouseMove}
411
+ onMouseLeave={handleMouseLeave}
412
+ />
413
+ \`\`\`
414
+
415
+ This approach can also be used to synchronize another chart type.
416
+ ` ,
417
+ } ,
413
418
} ,
414
- } )
419
+ }
415
420
416
- stories . add ( 'using mouse enter/leave' , ( ) => (
421
+ export const usingMouseEnterLeave = ( ) => (
417
422
< ScatterPlot
418
423
{ ...commonProps }
419
424
onMouseEnter = { ( data , e ) => {
@@ -423,7 +428,7 @@ stories.add('using mouse enter/leave', () => (
423
428
console . log ( { is : 'mouseleave' , data, event : e } ) // eslint-disable-line
424
429
} }
425
430
/>
426
- ) )
431
+ )
427
432
428
433
const CustomNode = ( {
429
434
node,
@@ -486,7 +491,7 @@ const CustomNode = ({
486
491
)
487
492
}
488
493
489
- stories . add ( 'custom node' , ( ) => (
494
+ export const customNode = ( ) => (
490
495
< ScatterPlot < { x : number ; y : number } >
491
496
{ ...commonProps }
492
497
colors = { { scheme : 'set2' } }
@@ -531,7 +536,7 @@ stories.add('custom node', () => (
531
536
] }
532
537
nodeComponent = { CustomNode }
533
538
/>
534
- ) )
539
+ )
535
540
536
541
const AreaLayer = ( {
537
542
nodes,
@@ -549,67 +554,47 @@ const AreaLayer = ({
549
554
return < path d = { areaGenerator ( nodes ) as string } fill = "rgba(232, 193, 160, .65)" />
550
555
}
551
556
552
- stories . add (
553
- 'adding extra layers' ,
554
- ( ) => (
555
- < ScatterPlot < { x : number ; y : number ; low : number ; high : number } >
556
- { ...commonProps }
557
- data = { [
558
- {
559
- id : 'things' ,
560
- data : [
561
- { x : 0 , y : 3.3 , low : 2.3 , high : 4.2 } ,
562
- { x : 1 , y : 3.5 , low : 2.7 , high : 4.1 } ,
563
- { x : 2 , y : 3.8 , low : 3.1 , high : 4.6 } ,
564
- { x : 3 , y : 4.1 , low : 2.9 , high : 4.5 } ,
565
- { x : 4 , y : 4.4 , low : 3.2 , high : 5.1 } ,
566
- { x : 5 , y : 4.7 , low : 3.7 , high : 5.4 } ,
567
- { x : 6 , y : 4.9 , low : 3.2 , high : 5.8 } ,
568
- { x : 7 , y : 5.2 , low : 4.2 , high : 6.1 } ,
569
- { x : 8 , y : 5.4 , low : 3.8 , high : 6.7 } ,
570
- { x : 9 , y : 5.6 , low : 3.5 , high : 7.1 } ,
571
- { x : 10 , y : 5.8 , low : 3.2 , high : 6.8 } ,
572
- { x : 11 , y : 6.0 , low : 4 , high : 7.2 } ,
573
- { x : 12 , y : 6.2 , low : 4.2 , high : 9.1 } ,
574
- { x : 13 , y : 6.4 , low : 3.9 , high : 9 } ,
575
- ] ,
576
- } ,
577
- ] }
578
- yScale = { {
579
- type : 'linear' ,
580
- max : 10 ,
581
- } }
582
- legends = { [ ] }
583
- layers = { [
584
- 'grid' ,
585
- 'axes' ,
586
- AreaLayer ,
587
- 'nodes' ,
588
- 'markers' ,
589
- 'mesh' ,
590
- 'legends' ,
591
- 'annotations' ,
592
- ] }
593
- annotations = { [
594
- {
595
- type : 'circle' ,
596
- match : { index : 10 } ,
597
- noteX : 50 ,
598
- noteY : 50 ,
599
- offset : 3 ,
600
- noteTextOffset : - 3 ,
601
- noteWidth : 10 ,
602
- note : 'an annotation' ,
603
- } ,
604
- ] }
605
- />
606
- ) ,
607
- {
608
- info : {
609
- text : `
610
- You can use the layers property to add extra layers
611
- to the scatterplot chart.
612
- ` ,
613
- } ,
614
- }
557
+ export const addingExtraLayers = ( ) => (
558
+ < ScatterPlot < { x : number ; y : number ; low : number ; high : number } >
559
+ { ...commonProps }
560
+ data = { [
561
+ {
562
+ id : 'things' ,
563
+ data : [
564
+ { x : 0 , y : 3.3 , low : 2.3 , high : 4.2 } ,
565
+ { x : 1 , y : 3.5 , low : 2.7 , high : 4.1 } ,
566
+ { x : 2 , y : 3.8 , low : 3.1 , high : 4.6 } ,
567
+ { x : 3 , y : 4.1 , low : 2.9 , high : 4.5 } ,
568
+ { x : 4 , y : 4.4 , low : 3.2 , high : 5.1 } ,
569
+ { x : 5 , y : 4.7 , low : 3.7 , high : 5.4 } ,
570
+ { x : 6 , y : 4.9 , low : 3.2 , high : 5.8 } ,
571
+ { x : 7 , y : 5.2 , low : 4.2 , high : 6.1 } ,
572
+ { x : 8 , y : 5.4 , low : 3.8 , high : 6.7 } ,
573
+ { x : 9 , y : 5.6 , low : 3.5 , high : 7.1 } ,
574
+ { x : 10 , y : 5.8 , low : 3.2 , high : 6.8 } ,
575
+ { x : 11 , y : 6.0 , low : 4 , high : 7.2 } ,
576
+ { x : 12 , y : 6.2 , low : 4.2 , high : 9.1 } ,
577
+ { x : 13 , y : 6.4 , low : 3.9 , high : 9 } ,
578
+ ] ,
579
+ } ,
580
+ ] }
581
+ yScale = { {
582
+ type : 'linear' ,
583
+ max : 10 ,
584
+ } }
585
+ legends = { [ ] }
586
+ layers = { [ 'grid' , 'axes' , AreaLayer , 'nodes' , 'markers' , 'mesh' , 'legends' , 'annotations' ] }
587
+ annotations = { [
588
+ {
589
+ type : 'circle' ,
590
+ match : { index : 10 } ,
591
+ noteX : 50 ,
592
+ noteY : 50 ,
593
+ offset : 3 ,
594
+ noteTextOffset : - 3 ,
595
+ noteWidth : 10 ,
596
+ note : 'an annotation' ,
597
+ } ,
598
+ ] }
599
+ />
615
600
)
0 commit comments