1010
1111var c = require ( './constants' ) ;
1212var d3 = require ( 'd3' ) ;
13+ var sum = require ( 'd3-array' ) . sum ;
1314var tinycolor = require ( 'tinycolor2' ) ;
1415var Color = require ( '../../components/color' ) ;
1516var Drawing = require ( '../../components/drawing' ) ;
@@ -67,6 +68,57 @@ function sankeyModel(layout, d, traceIndex) {
6768 Lib . warn ( 'node.pad was reduced to ' , sankey . nodePadding ( ) , ' to fit within the figure.' ) ;
6869 }
6970
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+
70122 return {
71123 circular : circular ,
72124 key : traceIndex ,
@@ -100,6 +152,9 @@ function sankeyModel(layout, d, traceIndex) {
100152
101153function linkModel ( d , l , i ) {
102154 var tc = tinycolor ( l . color ) ;
155+ if ( l . concentrationscale ) {
156+ tc = tinycolor ( l . concentrationscale ( l . flow . labelConcentration ) ) ;
157+ }
103158 var basicKey = l . source . label + '|' + l . target . label ;
104159 var key = basicKey + '__' + i ;
105160
@@ -121,7 +176,8 @@ function linkModel(d, l, i) {
121176 valueSuffix : d . valueSuffix ,
122177 sankey : d . sankey ,
123178 parent : d ,
124- interactionState : d . interactionState
179+ interactionState : d . interactionState ,
180+ flow : l . flow
125181 } ;
126182}
127183
@@ -568,7 +624,7 @@ function switchToSankeyFormat(nodes) {
568624}
569625
570626// scene graph
571- module . exports = function ( svg , calcData , layout , callbacks ) {
627+ module . exports = function ( gd , svg , calcData , layout , callbacks ) {
572628
573629 var styledData = calcData
574630 . filter ( function ( d ) { return unwrap ( d ) . trace . visible ; } )
@@ -616,6 +672,7 @@ module.exports = function(svg, calcData, layout, callbacks) {
616672 . attr ( 'd' , linkPath ( ) )
617673 . call ( attachPointerEvents , sankey , callbacks . linkEvents ) ;
618674
675+
619676 sankeyLink
620677 . style ( 'stroke' , function ( d ) {
621678 return salientEnough ( d ) ? Color . tinyRGB ( tinycolor ( d . linkLineColor ) ) : d . tinyColorHue ;
0 commit comments