10
10
11
11
var c = require ( './constants' ) ;
12
12
var d3 = require ( 'd3' ) ;
13
+ var sum = require ( 'd3-array' ) . sum ;
13
14
var tinycolor = require ( 'tinycolor2' ) ;
14
15
var Color = require ( '../../components/color' ) ;
15
16
var Drawing = require ( '../../components/drawing' ) ;
@@ -67,6 +68,57 @@ function sankeyModel(layout, d, traceIndex) {
67
68
Lib . warn ( 'node.pad was reduced to ' , sankey . nodePadding ( ) , ' to fit within the figure.' ) ;
68
69
}
69
70
71
+ function computeLinkConcentrations ( ) {
72
+ graph . nodes . forEach ( function ( node ) {
73
+ // Links connecting the same two nodes are part of a flow
74
+ var flows = { } ;
75
+ node . targetLinks . forEach ( function ( link ) {
76
+ var flowKey = link . source . pointNumber + ':' + link . target . pointNumber ;
77
+ if ( ! flows . hasOwnProperty ( flowKey ) ) flows [ flowKey ] = [ ] ;
78
+ flows [ flowKey ] . push ( link ) ;
79
+ } ) ;
80
+
81
+ // Compute statistics for each flow
82
+ Object . keys ( flows ) . forEach ( function ( flowKey ) {
83
+ var flowLinks = flows [ flowKey ] ;
84
+
85
+ // Find the total size of the flow and total size per label
86
+ var total = 0 ;
87
+ var totalPerLabel = { } ;
88
+ flowLinks . forEach ( function ( link ) {
89
+ if ( ! totalPerLabel [ link . label ] ) totalPerLabel [ link . label ] = 0 ;
90
+ totalPerLabel [ link . label ] += link . value ;
91
+ total += link . value ;
92
+ } ) ;
93
+
94
+ // Find the ratio of the link's value and the size of the flow
95
+ flowLinks . forEach ( function ( link ) {
96
+ link . flow = {
97
+ value : total ,
98
+ labelConcentration : totalPerLabel [ link . label ] / total ,
99
+ concentration : link . value / total ,
100
+ links : flowLinks
101
+ } ;
102
+ } ) ;
103
+ } ) ;
104
+
105
+ // Gather statistics of all links at current node
106
+ var totalOutflow = sum ( node . sourceLinks , function ( n ) {
107
+ return n . value ;
108
+ } ) ;
109
+ node . sourceLinks . forEach ( function ( link ) {
110
+ link . concentrationOut = link . value / totalOutflow ;
111
+ } ) ;
112
+ var totalInflow = sum ( node . targetLinks , function ( n ) {
113
+ return n . value ;
114
+ } ) ;
115
+ node . targetLinks . forEach ( function ( link ) {
116
+ link . concenrationIn = link . value / totalInflow ;
117
+ } ) ;
118
+ } ) ;
119
+ }
120
+ computeLinkConcentrations ( ) ;
121
+
70
122
return {
71
123
circular : circular ,
72
124
key : traceIndex ,
@@ -100,6 +152,9 @@ function sankeyModel(layout, d, traceIndex) {
100
152
101
153
function linkModel ( d , l , i ) {
102
154
var tc = tinycolor ( l . color ) ;
155
+ if ( l . concentrationscale ) {
156
+ tc = tinycolor ( l . concentrationscale ( l . flow . labelConcentration ) ) ;
157
+ }
103
158
var basicKey = l . source . label + '|' + l . target . label ;
104
159
var key = basicKey + '__' + i ;
105
160
@@ -121,7 +176,8 @@ function linkModel(d, l, i) {
121
176
valueSuffix : d . valueSuffix ,
122
177
sankey : d . sankey ,
123
178
parent : d ,
124
- interactionState : d . interactionState
179
+ interactionState : d . interactionState ,
180
+ flow : l . flow
125
181
} ;
126
182
}
127
183
@@ -568,7 +624,7 @@ function switchToSankeyFormat(nodes) {
568
624
}
569
625
570
626
// scene graph
571
- module . exports = function ( svg , calcData , layout , callbacks ) {
627
+ module . exports = function ( gd , svg , calcData , layout , callbacks ) {
572
628
573
629
var styledData = calcData
574
630
. filter ( function ( d ) { return unwrap ( d ) . trace . visible ; } )
@@ -616,6 +672,7 @@ module.exports = function(svg, calcData, layout, callbacks) {
616
672
. attr ( 'd' , linkPath ( ) )
617
673
. call ( attachPointerEvents , sankey , callbacks . linkEvents ) ;
618
674
675
+
619
676
sankeyLink
620
677
. style ( 'stroke' , function ( d ) {
621
678
return salientEnough ( d ) ? Color . tinyRGB ( tinycolor ( d . linkLineColor ) ) : d . tinyColorHue ;
0 commit comments