@@ -3,122 +3,152 @@ var units = "Widgets";
3
3
var defaultNodeHeight = 15 ;
4
4
5
5
// set the dimensions and margins of the graph
6
- var margin = { top : 0 , right : 0 , bottom : 0 , left : 0 } ,
7
- height = 300 ;
6
+ var margin = { top : 0 , right : 0 , bottom : 0 , left : 0 } ,
7
+ height = 300 ;
8
8
9
9
// format variables
10
- var formatNumber = d3 . format ( ",.0f" ) , // zero decimal places
11
- format = function ( d ) { return formatNumber ( d ) + " " + units ; } ;
10
+ var formatNumber = d3 . format ( ",.0f" ) , // zero decimal places
11
+ format = function ( d ) {
12
+ return formatNumber ( d ) + " " + units ;
13
+ } ;
12
14
13
15
// append the svg object to the body of the page
14
- var svg2 = d3 . select ( "#sankeyChart" ) . append ( "svg" )
15
- . attr ( "id" , "sankey" )
16
- . attr ( "width" , width )
17
- . attr ( "height" , height + 20 ) ;
16
+ var svg2 = d3
17
+ . select ( "#sankeyChart" )
18
+ . append ( "svg" )
19
+ . attr ( "id" , "sankey" )
20
+ . attr ( "width" , width )
21
+ . attr ( "height" , height + 20 ) ;
18
22
19
23
// Set the sankey diagram properties
20
- var sankey = d3 . sankey ( )
21
- . nodeWidth ( 5 )
22
- . nodePadding ( 40 )
23
- . size ( [ width , height ] ) ;
24
+ var sankey = d3
25
+ . sankey ( )
26
+ . nodeWidth ( 5 )
27
+ . nodePadding ( 40 )
28
+ . size ( [ width , height ] ) ;
24
29
25
30
var path = sankey . link ( ) ;
26
31
27
32
// load the data
28
- function loadSankey ( graph ) {
29
-
33
+ function loadSankey ( graph ) {
30
34
svg2 . selectAll ( "g" ) . remove ( ) ;
31
35
32
36
//uncomment to add filters
33
37
//const defs = svg.append('defs');
34
38
35
39
sankey
36
- . nodes ( graph . nodes )
37
- . links ( graph . links )
38
- . layout ( 32 ) ;
39
-
40
- // add in the links
41
- var link = svg2 . append ( "g" ) . selectAll ( ".link" )
42
- . data ( graph . links )
43
- . enter ( ) . append ( "path" )
44
- . attr ( "class" , "link" )
45
- . attr ( "d" , path )
46
- . style ( "stroke" , "grey" )
47
- . style ( "stroke-width" , function ( d ) { return Math . max ( 1 , d . dy ) ; } )
48
- . sort ( function ( a , b ) { return d3 . descending ( a . dy , b . dy ) ; } ) ;
49
-
50
- // add the link titles
51
- link . append ( "title" )
52
- . text ( function ( d ) {
53
- return d . source . name + " → " +
54
- d . target . name } ) ;
55
-
56
- // add in the nodes
57
- var node = svg2 . append ( "g" ) . selectAll ( ".node" )
58
- . data ( graph . nodes )
59
- . enter ( ) . append ( "g" )
60
- . attr ( "class" , "node" )
61
- . attr ( "transform" , function ( d ) {
62
- if ( d . value == 0 ) {
63
- d . x = 0.0 ;
64
- d . dy = defaultNodeHeight ;
65
- if ( graph . links . length == 0 ) {
66
- d . dy = ( height / graph . nodes . length ) ;
67
- d . y = d . y * ( height / graph . nodes . length ) ;
68
-
69
- }
40
+ . nodes ( graph . nodes )
41
+ . links ( graph . links )
42
+ . layout ( 32 ) ;
43
+
44
+ // add in the links
45
+ var link = svg2
46
+ . append ( "g" )
47
+ . selectAll ( ".link" )
48
+ . data ( graph . links )
49
+ . enter ( )
50
+ . append ( "path" )
51
+ . attr ( "class" , "link" )
52
+ . attr ( "d" , path )
53
+ . style ( "stroke" , "grey" )
54
+ . style ( "stroke-width" , function ( d ) {
55
+ return Math . max ( 1 , d . dy ) ;
56
+ } )
57
+ . sort ( function ( a , b ) {
58
+ return d3 . descending ( a . dy , b . dy ) ;
59
+ } ) ;
60
+
61
+ // add the link titles
62
+ link . append ( "title" ) . text ( function ( d ) {
63
+ return d . source . name + " → " + d . target . name ;
64
+ } ) ;
65
+
66
+ // add in the nodes
67
+ var node = svg2
68
+ . append ( "g" )
69
+ . selectAll ( ".node" )
70
+ . data ( graph . nodes )
71
+ . enter ( )
72
+ . append ( "g" )
73
+ . attr ( "class" , "node" )
74
+ . attr ( "transform" , function ( d ) {
75
+ if ( d . value == 0 ) {
76
+ d . x = 0.0 ;
77
+ d . dy = defaultNodeHeight ;
78
+ if ( graph . links . length == 0 ) {
79
+ d . dy = height / graph . nodes . length ;
80
+ d . y = d . y * ( height / graph . nodes . length ) ;
70
81
}
71
- return "translate(" + d . x + "," + d . y + ")" ; } )
72
- . call ( d3 . drag ( )
82
+ }
83
+ return "translate(" + d . x + "," + d . y + ")" ;
84
+ } )
85
+ . call (
86
+ d3
87
+ . drag ( )
73
88
. subject ( function ( d ) {
74
89
return d ;
75
90
} )
76
91
. on ( "start" , function ( ) {
77
92
this . parentNode . appendChild ( this ) ;
78
93
} )
79
- . on ( "drag" , dragmove ) ) ;
80
-
81
- // add the rectangles for the nodes
82
- node . append ( "rect" )
83
- . attr ( "height" , function ( d ) {
84
- return Math . max ( 1 , d . dy ) ; } )
85
- . attr ( "width" , sankey . nodeWidth ( ) )
86
- . style ( "fill" , function ( d ) {
87
- colorwheel ++ ;
88
- return d . color = color ( colorwheel ) ; } )
89
- . style ( "stroke-width" , 0 )
94
+ . on ( "drag" , dragmove )
95
+ ) ;
96
+
97
+ // add the rectangles for the nodes
98
+ node
99
+ . append ( "rect" )
100
+ . attr ( "height" , function ( d ) {
101
+ return Math . max ( 1 , d . dy ) ;
102
+ } )
103
+ . attr ( "width" , sankey . nodeWidth ( ) )
104
+ . style ( "fill" , function ( d ) {
105
+ colorwheel ++ ;
106
+ return ( d . color = color ( colorwheel ) ) ;
107
+ } )
108
+ . style ( "stroke-width" , 0 )
90
109
. append ( "title" )
91
- . text ( function ( d ) {
92
- return d . name + "\n" + format ( d . value ) ; } ) ;
93
-
94
- // add in the title for the nodes
95
- nodeText = node . append ( "text" )
96
- . attr ( "x" , - 6 )
97
- . attr ( "y" , function ( d ) { return d . dy / 2 ; } )
98
- . attr ( "dy" , ".35em" )
99
- . attr ( "text-anchor" , "end" )
100
- . attr ( "transform" , null )
101
- . text ( function ( d ) { return d . name ; } )
102
- . on ( "contextmenu" , function ( d ) {
103
- contextmenu ( d ) ;
104
- } )
105
- . filter ( function ( d ) { return d . x < width / 2 ; } )
106
- . attr ( "x" , 6 + sankey . nodeWidth ( ) )
107
- . attr ( "text-anchor" , "start" )
108
- . style ( "fill" , function ( d ) {
109
- return d . value > 0 ? "black" : "white" ; } ) ;
110
-
111
-
112
- // the function for moving the nodes
110
+ . text ( function ( d ) {
111
+ return d . name + "\n" + format ( d . value ) ;
112
+ } ) ;
113
+
114
+ // add in the title for the nodes
115
+ nodeText = node
116
+ . append ( "text" )
117
+ . attr ( "x" , - 6 )
118
+ . attr ( "y" , function ( d ) {
119
+ return d . dy / 2 ;
120
+ } )
121
+ . attr ( "dy" , ".35em" )
122
+ . attr ( "text-anchor" , "end" )
123
+ . attr ( "transform" , null )
124
+ . text ( function ( d ) {
125
+ return d . name ;
126
+ } )
127
+ . on ( "contextmenu" , function ( d ) {
128
+ contextmenu ( d ) ;
129
+ } )
130
+ . filter ( function ( d ) {
131
+ return d . x < width / 2 ;
132
+ } )
133
+ . attr ( "x" , 6 + sankey . nodeWidth ( ) )
134
+ . attr ( "text-anchor" , "start" )
135
+ . style ( "fill" , function ( d ) {
136
+ return d . value > 0 ? "black" : "white" ;
137
+ } ) ;
138
+
139
+ // the function for moving the nodes
113
140
function dragmove ( d ) {
114
- d3 . select ( this )
115
- . attr ( "transform" ,
116
- "translate("
117
- + d . x + ","
118
- + ( d . y = Math . max (
119
- 0 , Math . min ( height - d . dy , d3 . event . y ) )
120
- ) + ")" ) ;
141
+ d3
142
+ . select ( this )
143
+ . attr (
144
+ "transform" ,
145
+ "translate(" +
146
+ d . x +
147
+ "," +
148
+ ( d . y = Math . max ( 0 , Math . min ( height - d . dy , d3 . event . y ) ) ) +
149
+ ")"
150
+ ) ;
121
151
sankey . relayout ( ) ;
122
152
link . attr ( "d" , path ) ;
123
153
}
124
- }
154
+ }
0 commit comments