Skip to content

Commit 7094887

Browse files
committedFeb 26, 2019
sankey: check for circularity after grouping
1 parent ffbafa7 commit 7094887

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed
 

‎src/traces/sankey/calc.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ function convertToD3Sankey(trace) {
6060
}
6161

6262
// Process links
63+
var groupedLinks = {
64+
source: [],
65+
target[]
66+
};
6367
for(i = 0; i < linkSpec.value.length; i++) {
6468
var val = linkSpec.value[i];
6569
// remove negative values, but keep zeros with special treatment
@@ -103,6 +107,9 @@ function convertToD3Sankey(trace) {
103107
target: target,
104108
value: +val
105109
});
110+
111+
groupedLinks.source.push(source);
112+
groupedLinks.target.push(target);
106113
}
107114

108115
// Process nodes
@@ -122,7 +129,14 @@ function convertToD3Sankey(trace) {
122129
});
123130
}
124131

132+
// Check if we have circularity on the resulting graph
133+
var circular = false;
134+
if(circularityPresent(totalCount, groupedLinks.source, groupedLinks.target)) {
135+
circular = true;
136+
}
137+
125138
return {
139+
circular: circular,
126140
links: links,
127141
nodes: nodes,
128142

@@ -132,9 +146,7 @@ function convertToD3Sankey(trace) {
132146
};
133147
}
134148

135-
function circularityPresent(nodeList, sources, targets) {
136-
137-
var nodeLen = nodeList.length;
149+
function circularityPresent(nodeLen, sources, targets) {
138150
var nodes = Lib.init2dArray(nodeLen, 0);
139151

140152
for(var i = 0; i < Math.min(sources.length, targets.length); i++) {
@@ -156,15 +168,10 @@ function circularityPresent(nodeList, sources, targets) {
156168
}
157169

158170
module.exports = function calc(gd, trace) {
159-
var circular = false;
160-
if(circularityPresent(trace.node.label, trace.link.source, trace.link.target)) {
161-
circular = true;
162-
}
163-
164171
var result = convertToD3Sankey(trace);
165172

166173
return wrap({
167-
circular: circular,
174+
circular: result.circular,
168175
_nodes: result.nodes,
169176
_links: result.links,
170177

‎test/jasmine/tests/sankey_test.js

+22
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,28 @@ describe('sankey tests', function() {
485485
.catch(failTest)
486486
.then(done);
487487
});
488+
489+
it('switches from normal to circular Sankey on grouping', function(done) {
490+
var mockCopy = Lib.extendDeep({}, mock);
491+
492+
Plotly.plot(gd, mockCopy)
493+
.then(function() {
494+
expect(gd.calcdata[0][0].circular).toBe(false);
495+
496+
// Group two nodes to create circularity
497+
return Plotly.restyle(gd, 'node.groups', [[[1, 3]]]);
498+
})
499+
.then(function() {
500+
expect(gd.calcdata[0][0].circular).toBe(true);
501+
// Group two nodes to that do not create circularity
502+
return Plotly.restyle(gd, 'node.groups', [[[1, 4]]]);
503+
})
504+
.then(function() {
505+
expect(gd.calcdata[0][0].circular).toBe(false);
506+
done();
507+
});
508+
});
509+
488510
});
489511

490512
describe('Test hover/click interactions:', function() {

0 commit comments

Comments
 (0)
Please sign in to comment.