Skip to content

Commit c228b37

Browse files
committed
sankey: forEach -> for loops, no FX on link hover if concentrationscale
1 parent a791291 commit c228b37

File tree

4 files changed

+54
-26
lines changed

4 files changed

+54
-26
lines changed

src/traces/sankey/plot.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,21 @@ function linkHoveredStyle(d, sankey, visitNodes, sankeyLink) {
7272

7373
var label = sankeyLink.datum().link.label;
7474

75-
sankeyLink.style('fill-opacity', 0.4);
75+
sankeyLink.style('fill-opacity', function(l) {
76+
if(!l.link.concentrationscale) {
77+
return 0.4;
78+
}
79+
});
7680

7781
if(label) {
7882
ownTrace(sankey, d)
7983
.selectAll('.' + cn.sankeyLink)
8084
.filter(function(l) {return l.link.label === label;})
81-
.style('fill-opacity', 0.4);
85+
.style('fill-opacity', function(l) {
86+
if(!l.link.concentrationscale) {
87+
return 0.4;
88+
}
89+
});
8290
}
8391

8492
if(visitNodes) {
@@ -143,6 +151,7 @@ module.exports = function plot(gd, calcData) {
143151

144152
var sourceLabel = _(gd, 'source:') + ' ';
145153
var targetLabel = _(gd, 'target:') + ' ';
154+
var concentrationLabel = _(gd, 'concentration:') + ' ';
146155
var incomingLabel = _(gd, 'incoming flow count:') + ' ';
147156
var outgoingLabel = _(gd, 'outgoing flow count:') + ' ';
148157

@@ -172,7 +181,8 @@ module.exports = function plot(gd, calcData) {
172181
text: [
173182
d.link.label || '',
174183
sourceLabel + d.link.source.label,
175-
targetLabel + d.link.target.label
184+
targetLabel + d.link.target.label,
185+
d.link.concentrationscale ? concentrationLabel + d3.format('%0.2f')(d.link.flow.labelConcentration) : ''
176186
].filter(renderableValuePresent).join('<br>'),
177187
color: castHoverOption(obj, 'bgcolor') || Color.addOpacity(d.tinyColorHue, 1),
178188
borderColor: castHoverOption(obj, 'bordercolor'),
@@ -190,7 +200,9 @@ module.exports = function plot(gd, calcData) {
190200
gd: gd
191201
});
192202

193-
makeTranslucent(tooltip, 0.65);
203+
if(!d.link.concentrationscale) {
204+
makeTranslucent(tooltip, 0.65);
205+
}
194206
makeTextContrasty(tooltip);
195207
};
196208

src/traces/sankey/render.js

+36-21
Original file line numberDiff line numberDiff line change
@@ -69,53 +69,68 @@ function sankeyModel(layout, d, traceIndex) {
6969
}
7070

7171
function computeLinkConcentrations() {
72-
graph.nodes.forEach(function(node) {
72+
var i, j, k;
73+
for(i = 0; i < graph.nodes.length; i++) {
74+
var node = graph.nodes[i];
7375
// Links connecting the same two nodes are part of a flow
7476
var flows = {};
75-
node.targetLinks.forEach(function(link) {
76-
var flowKey = link.source.pointNumber + ':' + link.target.pointNumber;
77+
var flowKey;
78+
var link;
79+
for(j = 0; j < node.targetLinks.length; j++) {
80+
link = node.targetLinks[j];
81+
flowKey = link.source.pointNumber + ':' + link.target.pointNumber;
7782
if(!flows.hasOwnProperty(flowKey)) flows[flowKey] = [];
7883
flows[flowKey].push(link);
79-
});
84+
}
8085

8186
// Compute statistics for each flow
82-
Object.keys(flows).forEach(function(flowKey) {
87+
var keys = Object.keys(flows);
88+
for(j = 0; j < keys.length; j++) {
89+
flowKey = keys[j];
8390
var flowLinks = flows[flowKey];
8491

8592
// Find the total size of the flow and total size per label
8693
var total = 0;
8794
var totalPerLabel = {};
88-
flowLinks.forEach(function(link) {
95+
for(k = 0; k < flowLinks.length; k++) {
96+
link = flowLinks[k];
8997
if(!totalPerLabel[link.label]) totalPerLabel[link.label] = 0;
9098
totalPerLabel[link.label] += link.value;
9199
total += link.value;
92-
});
100+
}
93101

94102
// Find the ratio of the link's value and the size of the flow
95-
flowLinks.forEach(function(link) {
103+
for(k = 0; k < flowLinks.length; k++) {
104+
link = flowLinks[k];
96105
link.flow = {
97106
value: total,
98107
labelConcentration: totalPerLabel[link.label] / total,
99108
concentration: link.value / total,
100109
links: flowLinks
101110
};
102-
});
103-
});
111+
}
112+
}
104113

105114
// 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) {
115+
var totalOutflow = 0;
116+
for(j = 0; j < node.sourceLinks.length; j++) {
117+
totalOutflow += node.sourceLinks[j].value;
118+
}
119+
for(j = 0; j < node.sourceLinks.length; j++) {
120+
link = node.sourceLinks[j];
110121
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) {
122+
}
123+
124+
var totalInflow = 0;
125+
for(j = 0; j < node.targetLinks.length; j++) {
126+
totalInflow += node.targetLinks[j].value;
127+
}
128+
129+
for(j = 0; j < node.targetLinks.length; j++) {
130+
link = node.targetLinks[j];
116131
link.concenrationIn = link.value / totalInflow;
117-
});
118-
});
132+
}
133+
}
119134
}
120135
computeLinkConcentrations();
121136

-129 Bytes
Loading

test/image/mocks/sankey_link_concentration.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"color": "white",
99
"width": 2
1010
},
11+
"color": "black",
1112
"label": ["process0", "process1", "process2", "process3", "process4"]
1213
},
1314
"link": {
@@ -30,7 +31,7 @@
3031
10, 5, 10, 20,
3132
0, 10, 10, 10,
3233
15, 5,
33-
5
34+
20
3435

3536
],
3637
"label": [

0 commit comments

Comments
 (0)