Skip to content

Commit 2ffa563

Browse files
committed
compute link concentration and color links with gradients
1 parent 48a8b1b commit 2ffa563

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/traces/sankey/plot.js

+1
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ module.exports = function plot(gd, calcData) {
288288
};
289289

290290
render(
291+
gd,
291292
svg,
292293
calcData,
293294
{

src/traces/sankey/render.js

+48-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
var c = require('./constants');
1212
var d3 = require('d3');
13+
var sum = require('d3-array').sum;
1314
var tinycolor = require('tinycolor2');
1415
var Color = require('../../components/color');
1516
var Drawing = require('../../components/drawing');
@@ -67,6 +68,24 @@ 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+
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+
7089
return {
7190
circular: circular,
7291
key: traceIndex,
@@ -121,7 +140,9 @@ function linkModel(d, l, i) {
121140
valueSuffix: d.valueSuffix,
122141
sankey: d.sankey,
123142
parent: d,
124-
interactionState: d.interactionState
143+
interactionState: d.interactionState,
144+
inConcentration: l.inConcentration,
145+
outConcentration: l.outConcentration
125146
};
126147
}
127148

@@ -273,6 +294,27 @@ function linkPath() {
273294
return path;
274295
}
275296

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+
276318
function nodeModel(d, n, i) {
277319
var tc = tinycolor(n.color);
278320
var zoneThicknessPad = c.nodePadAcross;
@@ -568,7 +610,7 @@ function switchToSankeyFormat(nodes) {
568610
}
569611

570612
// scene graph
571-
module.exports = function(svg, calcData, layout, callbacks) {
613+
module.exports = function(gd, svg, calcData, layout, callbacks) {
572614

573615
var styledData = calcData
574616
.filter(function(d) {return unwrap(d).trace.visible;})
@@ -616,6 +658,7 @@ module.exports = function(svg, calcData, layout, callbacks) {
616658
.attr('d', linkPath())
617659
.call(attachPointerEvents, sankey, callbacks.linkEvents);
618660

661+
619662
sankeyLink
620663
.style('stroke', function(d) {
621664
return salientEnough(d) ? Color.tinyRGB(tinycolor(d.linkLineColor)) : d.tinyColorHue;
@@ -633,6 +676,9 @@ module.exports = function(svg, calcData, layout, callbacks) {
633676
return salientEnough(d) ? d.linkLineWidth : 1;
634677
});
635678

679+
// Color logic based on concentration
680+
sankeyLink.call(linkColor, gd);
681+
636682
sankeyLink.transition()
637683
.ease(c.ease).duration(c.duration)
638684
.attr('d', linkPath());

0 commit comments

Comments
 (0)