MCPcopy Create free account
hub / github.com/plotly/plotly.js / convertToD3Sankey

Function convertToD3Sankey

src/traces/sankey/calc.js:11–146  ·  view source on GitHub ↗
(trace)

Source from the content-addressed store, hash-verified

9var Colorscale = require('../../components/colorscale');
10
11function convertToD3Sankey(trace) {
12 var nodeSpec = trace.node;
13 var linkSpec = trace.link;
14
15 var links = [];
16 var hasLinkColorArray = isArrayOrTypedArray(linkSpec.color);
17 var hasLinkHoverColorArray = isArrayOrTypedArray(linkSpec.hovercolor);
18 var hasLinkCustomdataArray = isArrayOrTypedArray(linkSpec.customdata);
19 var linkedNodes = {};
20
21 var components = {};
22 var componentCount = linkSpec.colorscales.length;
23 var i;
24 for(i = 0; i < componentCount; i++) {
25 var cscale = linkSpec.colorscales[i];
26 var specs = Colorscale.extractScale(cscale, {cLetter: 'c'});
27 var scale = Colorscale.makeColorScaleFunc(specs);
28 components[cscale.label] = scale;
29 }
30
31 var maxNodeId = 0;
32 for(i = 0; i < linkSpec.value.length; i++) {
33 if(linkSpec.source[i] > maxNodeId) maxNodeId = linkSpec.source[i];
34 if(linkSpec.target[i] > maxNodeId) maxNodeId = linkSpec.target[i];
35 }
36 var nodeCount = maxNodeId + 1;
37 trace.node._count = nodeCount;
38
39 // Group nodes
40 var j;
41 var groups = trace.node.groups;
42 var groupLookup = {};
43 for(i = 0; i < groups.length; i++) {
44 var group = groups[i];
45 // Build a lookup table to quickly find in which group a node is
46 for(j = 0; j < group.length; j++) {
47 var nodeIndex = group[j];
48 var groupIndex = nodeCount + i;
49 if(groupLookup.hasOwnProperty(nodeIndex)) {
50 Lib.warn('Node ' + nodeIndex + ' is already part of a group.');
51 } else {
52 groupLookup[nodeIndex] = groupIndex;
53 }
54 }
55 }
56
57 // Process links
58 var groupedLinks = {
59 source: [],
60 target: []
61 };
62 for(i = 0; i < linkSpec.value.length; i++) {
63 var val = linkSpec.value[i];
64 // remove negative values, but keep zeros with special treatment
65 var source = linkSpec.source[i];
66 var target = linkSpec.target[i];
67 if(!(val > 0 && isIndex(source, nodeCount) && isIndex(target, nodeCount))) {
68 continue;

Callers 1

calc.jsFile · 0.85

Calls 3

isIndexFunction · 0.85
circularityPresentFunction · 0.85
isArrayOrTypedArrayFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…