@@ -67,6 +67,72 @@ function sankeyModel(layout, d, traceIndex) {
6767 Lib . warn ( 'node.pad was reduced to ' , sankey . nodePadding ( ) , ' to fit within the figure.' ) ;
6868 }
6969
70+ function computeLinkConcentrations ( ) {
71+ var i , j , k ;
72+ for ( i = 0 ; i < graph . nodes . length ; i ++ ) {
73+ var node = graph . nodes [ i ] ;
74+ // Links connecting the same two nodes are part of a flow
75+ var flows = { } ;
76+ var flowKey ;
77+ var link ;
78+ for ( j = 0 ; j < node . targetLinks . length ; j ++ ) {
79+ link = node . targetLinks [ j ] ;
80+ flowKey = link . source . pointNumber + ':' + link . target . pointNumber ;
81+ if ( ! flows . hasOwnProperty ( flowKey ) ) flows [ flowKey ] = [ ] ;
82+ flows [ flowKey ] . push ( link ) ;
83+ }
84+
85+ // Compute statistics for each flow
86+ var keys = Object . keys ( flows ) ;
87+ for ( j = 0 ; j < keys . length ; j ++ ) {
88+ flowKey = keys [ j ] ;
89+ var flowLinks = flows [ flowKey ] ;
90+
91+ // Find the total size of the flow and total size per label
92+ var total = 0 ;
93+ var totalPerLabel = { } ;
94+ for ( k = 0 ; k < flowLinks . length ; k ++ ) {
95+ link = flowLinks [ k ] ;
96+ if ( ! totalPerLabel [ link . label ] ) totalPerLabel [ link . label ] = 0 ;
97+ totalPerLabel [ link . label ] += link . value ;
98+ total += link . value ;
99+ }
100+
101+ // Find the ratio of the link's value and the size of the flow
102+ for ( k = 0 ; k < flowLinks . length ; k ++ ) {
103+ link = flowLinks [ k ] ;
104+ link . flow = {
105+ value : total ,
106+ labelConcentration : totalPerLabel [ link . label ] / total ,
107+ concentration : link . value / total ,
108+ links : flowLinks
109+ } ;
110+ }
111+ }
112+
113+ // Gather statistics of all links at current node
114+ var totalOutflow = 0 ;
115+ for ( j = 0 ; j < node . sourceLinks . length ; j ++ ) {
116+ totalOutflow += node . sourceLinks [ j ] . value ;
117+ }
118+ for ( j = 0 ; j < node . sourceLinks . length ; j ++ ) {
119+ link = node . sourceLinks [ j ] ;
120+ link . concentrationOut = link . value / totalOutflow ;
121+ }
122+
123+ var totalInflow = 0 ;
124+ for ( j = 0 ; j < node . targetLinks . length ; j ++ ) {
125+ totalInflow += node . targetLinks [ j ] . value ;
126+ }
127+
128+ for ( j = 0 ; j < node . targetLinks . length ; j ++ ) {
129+ link = node . targetLinks [ j ] ;
130+ link . concenrationIn = link . value / totalInflow ;
131+ }
132+ }
133+ }
134+ computeLinkConcentrations ( ) ;
135+
70136 return {
71137 circular : circular ,
72138 key : traceIndex ,
@@ -100,6 +166,9 @@ function sankeyModel(layout, d, traceIndex) {
100166
101167function linkModel ( d , l , i ) {
102168 var tc = tinycolor ( l . color ) ;
169+ if ( l . concentrationscale ) {
170+ tc = tinycolor ( l . concentrationscale ( l . flow . labelConcentration ) ) ;
171+ }
103172 var basicKey = l . source . label + '|' + l . target . label ;
104173 var key = basicKey + '__' + i ;
105174
@@ -121,7 +190,8 @@ function linkModel(d, l, i) {
121190 valueSuffix : d . valueSuffix ,
122191 sankey : d . sankey ,
123192 parent : d ,
124- interactionState : d . interactionState
193+ interactionState : d . interactionState ,
194+ flow : l . flow
125195 } ;
126196}
127197
@@ -568,7 +638,7 @@ function switchToSankeyFormat(nodes) {
568638}
569639
570640// scene graph
571- module . exports = function ( svg , calcData , layout , callbacks ) {
641+ module . exports = function ( gd , svg , calcData , layout , callbacks ) {
572642
573643 var styledData = calcData
574644 . filter ( function ( d ) { return unwrap ( d ) . trace . visible ; } )
0 commit comments