1
- import { storiesOf } from '@storybook/react'
1
+ import { Meta } from '@storybook/react'
2
2
import { generateSankeyData , randColor } from '@nivo/generators'
3
3
import { SankeyNodeMinimal } from 'd3-sankey'
4
4
// @ts -ignore
5
5
import { Sankey } from '../src'
6
+ import { PropsWithChildren } from 'react'
7
+
8
+ export default {
9
+ component : Sankey ,
10
+ title : 'Sankey' ,
11
+ } as Meta
6
12
7
13
const sankeyData = generateSankeyData ( { nodeCount : 11 , maxIterations : 2 } )
8
14
const commonProperties = {
@@ -13,41 +19,25 @@ const commonProperties = {
13
19
colors : { scheme : 'category10' as const } ,
14
20
}
15
21
16
- const stories = storiesOf ( 'Sankey' , module )
17
-
18
- stories . add ( 'default' , ( ) => < Sankey { ...commonProperties } /> )
22
+ export const Defaut = ( ) => < Sankey { ...commonProperties } />
19
23
20
- stories . add ( 'custom align (end)' , ( ) => < Sankey { ...commonProperties } align = "end" /> )
24
+ export const CustomAlign = ( ) => < Sankey { ...commonProperties } align = "end" />
21
25
22
- stories . add ( 'outside labels' , ( ) => < Sankey { ...commonProperties } labelPosition = "outside" /> )
26
+ export const OutsideLabels = ( ) => < Sankey { ...commonProperties } labelPosition = "outside" />
23
27
24
- stories . add ( 'vertical labels' , ( ) => (
28
+ export const VerticalLabels = ( ) => (
25
29
< Sankey { ...commonProperties } labelOrientation = "vertical" labelPadding = { 20 } />
26
- ) )
30
+ )
27
31
28
- stories . add ( 'contracting links' , ( ) => < Sankey { ...commonProperties } linkContract = { 10 } /> )
32
+ export const ContractingLinks = ( ) => < Sankey { ...commonProperties } linkContract = { 10 } />
29
33
30
- stories . add ( 'click listener (console)' , ( ) => (
34
+ export const ClickHandler = ( ) => (
31
35
< Sankey { ...commonProperties } onClick = { ( data , event ) => console . log ( { data, event } ) } />
32
- ) )
36
+ )
33
37
34
- stories . add ( 'label formatter' , ( ) => (
35
- < Sankey { ...commonProperties } label = { node => `${ node . id } 😁` } />
36
- ) )
37
-
38
- stories . add ( 'custom tooltip' , ( ) => (
39
- < Sankey
40
- { ...commonProperties }
41
- nodeTooltip = { ( { node } ) => < span > Custom tooltip for node: { node . label } </ span > }
42
- linkTooltip = { ( { link } ) => (
43
- < span >
44
- Custom tooltip for link: { link . source . label } to { link . target . label }
45
- </ span >
46
- ) }
47
- />
48
- ) )
38
+ export const CustomLabel = ( ) => < Sankey { ...commonProperties } label = { node => `${ node . id } 😁` } />
49
39
50
- stories . add ( 'with formatted values' , ( ) => (
40
+ export const FormattedValues = ( ) => (
51
41
< Sankey
52
42
{ ...commonProperties }
53
43
valueFormat = { value =>
@@ -56,7 +46,40 @@ stories.add('with formatted values', () => (
56
46
} ) } ₽`
57
47
}
58
48
/>
59
- ) )
49
+ )
50
+
51
+ const CustomTooltipContainer = ( { children } : PropsWithChildren < any > ) => (
52
+ < div
53
+ style = { {
54
+ padding : 9 ,
55
+ background : '#eeeeee' ,
56
+ borderRadius : '2px' ,
57
+ border : '1px solid #aaaaaa' ,
58
+ } }
59
+ >
60
+ { children }
61
+ </ div >
62
+ )
63
+
64
+ export const CustomTooltips = ( ) => (
65
+ < Sankey
66
+ { ...commonProperties }
67
+ nodeTooltip = { ( { node } ) => (
68
+ < CustomTooltipContainer >
69
+ Custom tooltip for node:
70
+ < br />
71
+ < strong > { node . label } </ strong >
72
+ </ CustomTooltipContainer >
73
+ ) }
74
+ linkTooltip = { ( { link } ) => (
75
+ < CustomTooltipContainer >
76
+ Custom tooltip for link:
77
+ < br />
78
+ < strong > { link . source . label } </ strong > to < strong > { link . target . label } </ strong >
79
+ </ CustomTooltipContainer >
80
+ ) }
81
+ />
82
+ )
60
83
61
84
const dataWithRandLinkColors = ( data : typeof sankeyData ) => ( {
62
85
nodes : data . nodes . map ( node => ( {
@@ -70,14 +93,14 @@ const dataWithRandLinkColors = (data: typeof sankeyData) => ({
70
93
} ) ) ,
71
94
} )
72
95
73
- stories . add ( 'with custom node & link coloring' , ( ) => (
96
+ export const CustomNodeAndLinkColors = ( ) => (
74
97
< Sankey
75
98
{ ...commonProperties }
76
99
data = { dataWithRandLinkColors ( sankeyData ) }
77
100
enableLinkGradient = { true }
78
101
colors = { node => node . nodeColor }
79
102
/>
80
- ) )
103
+ )
81
104
82
105
const minNodeValueOnTop = (
83
106
nodeA : SankeyNodeMinimal < any , any > ,
@@ -88,11 +111,12 @@ const minNodeValueOnTop = (
88
111
return 0
89
112
}
90
113
91
- stories . add ( 'with reverse sort ordering (min node value on top)' , ( ) => (
114
+ // min node value on top
115
+ export const WithReverseSortOrdering = ( ) => (
92
116
< Sankey { ...commonProperties } sort = { minNodeValueOnTop } />
93
- ) )
117
+ )
94
118
95
- stories . add ( 'sort links by input' , ( ) => (
119
+ export const SortLinksByInput = ( ) => (
96
120
< Sankey
97
121
{ ...commonProperties }
98
122
data = { {
@@ -117,4 +141,4 @@ stories.add('sort links by input', () => (
117
141
sort = "input"
118
142
enableLinkGradient
119
143
/>
120
- ) )
144
+ )
0 commit comments