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,24 @@ 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
+ var totalOutflow = sum ( node . sourceLinks , function ( n ) {
74
+ return n . value ;
75
+ } ) ;
76
+ node . sourceLinks . forEach ( function ( link ) {
77
+ link . outConcentration = link . value / totalOutflow ;
78
+ } ) ;
79
+ var totalInflow = sum ( node . targetLinks , function ( n ) {
80
+ return n . value ;
81
+ } ) ;
82
+ node . targetLinks . forEach ( function ( link ) {
83
+ link . inConcentration = link . value / totalInflow ;
84
+ } ) ;
85
+ } ) ;
86
+ }
87
+ computeLinkConcentrations ( ) ;
88
+
70
89
return {
71
90
circular : circular ,
72
91
key : traceIndex ,
@@ -121,7 +140,9 @@ function linkModel(d, l, i) {
121
140
valueSuffix : d . valueSuffix ,
122
141
sankey : d . sankey ,
123
142
parent : d ,
124
- interactionState : d . interactionState
143
+ interactionState : d . interactionState ,
144
+ inConcentration : l . inConcentration ,
145
+ outConcentration : l . outConcentration
125
146
} ;
126
147
}
127
148
@@ -273,6 +294,27 @@ function linkPath() {
273
294
return path ;
274
295
}
275
296
297
+ function linkColor ( s , gd ) {
298
+ if ( s . size ( ) ) {
299
+ s . each ( function ( d , i ) {
300
+ var gradientID = 'linkfill-' + i ;
301
+
302
+ console . log ( d )
303
+ var startColor = tinycolor ( d . link . source . color ) ;
304
+ startColor . setAlpha ( d . inConcentration ) ;
305
+
306
+ var endColor = tinycolor ( d . link . target . color ) ;
307
+ endColor . setAlpha ( d . outConcentration ) ;
308
+
309
+ var colorRange = [ startColor , endColor ] ;
310
+ if ( d . link . circular ) colorRange = colorRange . reverse ( )
311
+
312
+ var colorscale = [ [ 0 , colorRange [ 0 ] . toRgb ( ) ] , [ 1 , colorRange [ 1 ] . toRgb ( ) ] ] ;
313
+ Drawing . gradient ( d3 . select ( this ) , gd , gradientID , 'horizontalreversed' , colorscale , 'fill' ) ;
314
+ } ) ;
315
+ }
316
+ }
317
+
276
318
function nodeModel ( d , n , i ) {
277
319
var tc = tinycolor ( n . color ) ;
278
320
var zoneThicknessPad = c . nodePadAcross ;
@@ -568,7 +610,7 @@ function switchToSankeyFormat(nodes) {
568
610
}
569
611
570
612
// scene graph
571
- module . exports = function ( svg , calcData , layout , callbacks ) {
613
+ module . exports = function ( gd , svg , calcData , layout , callbacks ) {
572
614
573
615
var styledData = calcData
574
616
. filter ( function ( d ) { return unwrap ( d ) . trace . visible ; } )
@@ -616,6 +658,7 @@ module.exports = function(svg, calcData, layout, callbacks) {
616
658
. attr ( 'd' , linkPath ( ) )
617
659
. call ( attachPointerEvents , sankey , callbacks . linkEvents ) ;
618
660
661
+
619
662
sankeyLink
620
663
. style ( 'stroke' , function ( d ) {
621
664
return salientEnough ( d ) ? Color . tinyRGB ( tinycolor ( d . linkLineColor ) ) : d . tinyColorHue ;
@@ -633,6 +676,9 @@ module.exports = function(svg, calcData, layout, callbacks) {
633
676
return salientEnough ( d ) ? d . linkLineWidth : 1 ;
634
677
} ) ;
635
678
679
+ // Color logic based on concentration
680
+ sankeyLink . call ( linkColor , gd ) ;
681
+
636
682
sankeyLink . transition ( )
637
683
. ease ( c . ease ) . duration ( c . duration )
638
684
. attr ( 'd' , linkPath ( ) ) ;
0 commit comments