Skip to content

Commit 04a186c

Browse files
committed
move function defined in convert-to-d3-sankey.js into calc.js
1 parent 2382d74 commit 04a186c

File tree

2 files changed

+70
-81
lines changed

2 files changed

+70
-81
lines changed

src/traces/sankey/calc.js

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,76 @@ var tarjan = require('strongly-connected-components');
1212
var Lib = require('../../lib');
1313
var wrap = require('../../lib/gup').wrap;
1414

15-
var convertToD3Sankey = require('./convert-to-d3-sankey');
15+
var isArrayOrTypedArray = Lib.isArrayOrTypedArray;
16+
var isIndex = Lib.isIndex;
17+
18+
19+
function convertToD3Sankey(trace) {
20+
var nodeSpec = trace.node;
21+
var linkSpec = trace.link;
22+
23+
var links = [];
24+
var hasLinkColorArray = isArrayOrTypedArray(linkSpec.color);
25+
var linkedNodes = {};
26+
27+
var nodeCount = nodeSpec.label.length;
28+
var i;
29+
for(i = 0; i < linkSpec.value.length; i++) {
30+
var val = linkSpec.value[i];
31+
// remove negative values, but keep zeros with special treatment
32+
var source = linkSpec.source[i];
33+
var target = linkSpec.target[i];
34+
if(!(val > 0 && isIndex(source, nodeCount) && isIndex(target, nodeCount))) {
35+
continue;
36+
}
37+
38+
source = +source;
39+
target = +target;
40+
linkedNodes[source] = linkedNodes[target] = true;
41+
42+
var label = '';
43+
if(linkSpec.label && linkSpec.label[i]) label = linkSpec.label[i];
44+
45+
links.push({
46+
pointNumber: i,
47+
label: label,
48+
color: hasLinkColorArray ? linkSpec.color[i] : linkSpec.color,
49+
source: source,
50+
target: target,
51+
value: +val
52+
});
53+
}
54+
55+
var hasNodeColorArray = isArrayOrTypedArray(nodeSpec.color);
56+
var nodes = [];
57+
var removedNodes = false;
58+
var nodeIndices = {};
59+
60+
for(i = 0; i < nodeCount; i++) {
61+
if(linkedNodes[i]) {
62+
var l = nodeSpec.label[i];
63+
nodeIndices[i] = nodes.length;
64+
nodes.push({
65+
pointNumber: i,
66+
label: l,
67+
color: hasNodeColorArray ? nodeSpec.color[i] : nodeSpec.color
68+
});
69+
} else removedNodes = true;
70+
}
71+
72+
// need to re-index links now, since we didn't put all the nodes in
73+
if(removedNodes) {
74+
for(i = 0; i < links.length; i++) {
75+
links[i].source = nodeIndices[links[i].source];
76+
links[i].target = nodeIndices[links[i].target];
77+
}
78+
}
79+
80+
return {
81+
links: links,
82+
nodes: nodes
83+
};
84+
}
1685

1786
function circularityPresent(nodeList, sources, targets) {
1887

src/traces/sankey/convert-to-d3-sankey.js

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)