Skip to content

Commit 03a3cb5

Browse files
committed
merge @Plotly/d3-sankey into plotly.js (es6 -> es5, tests in jasmine)
1 parent 3ea5c82 commit 03a3cb5

File tree

5 files changed

+466
-12
lines changed

5 files changed

+466
-12
lines changed

package-lock.json

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@
5656
]
5757
},
5858
"dependencies": {
59+
"d3-array": "1",
60+
"d3-collection": "1",
61+
"d3-interpolate": "1",
5962
"3d-view": "^2.0.0",
60-
"@plotly/d3-sankey": "^0.5.1",
6163
"alpha-shape": "^1.0.0",
6264
"array-range": "^1.0.1",
6365
"canvas-fit": "^1.5.0",

src/traces/sankey/d3-sankey.js

Lines changed: 356 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,356 @@
1+
/**
2+
Copyright 2015, Mike Bostock
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without modification,
6+
are permitted provided that the following conditions are met:
7+
8+
* Redistributions of source code must retain the above copyright notice, this
9+
list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above copyright notice,
12+
this list of conditions and the following disclaimer in the documentation
13+
and/or other materials provided with the distribution.
14+
15+
* Neither the name of the author nor the names of contributors may be used to
16+
endorse or promote products derived from this software without specific prior
17+
written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
30+
*/
31+
32+
/**
33+
The following is a fork of https://github.com/plotly/d3-sankey which itself
34+
was a fork of https://github.com/d3/d3-sankey from Mike Bostock.
35+
*/
36+
37+
'use strict';
38+
39+
var d3array = require('d3-array');
40+
var ascending = d3array.ascending;
41+
var min = d3array.min;
42+
var sum = d3array.sum;
43+
var max = d3array.max;
44+
var nest = require('d3-collection').nest;
45+
var interpolateNumber = require('d3-interpolate').interpolateNumber;
46+
47+
module.exports = function() {
48+
var sankey = {},
49+
nodeWidth = 24,
50+
nodePadding = 8,
51+
size = [1, 1],
52+
nodes = [],
53+
links = [],
54+
maxPaddedSpace = 2 / 3; // Defined as a fraction of the total available space
55+
56+
sankey.nodeWidth = function(_) {
57+
if(!arguments.length) return nodeWidth;
58+
nodeWidth = +_;
59+
return sankey;
60+
};
61+
62+
sankey.nodePadding = function(_) {
63+
if(!arguments.length) return nodePadding;
64+
nodePadding = +_;
65+
return sankey;
66+
};
67+
68+
sankey.nodes = function(_) {
69+
if(!arguments.length) return nodes;
70+
nodes = _;
71+
return sankey;
72+
};
73+
74+
sankey.links = function(_) {
75+
if(!arguments.length) return links;
76+
links = _;
77+
return sankey;
78+
};
79+
80+
sankey.size = function(_) {
81+
if(!arguments.length) return size;
82+
size = _;
83+
return sankey;
84+
};
85+
86+
sankey.layout = function(iterations) {
87+
computeNodeLinks();
88+
computeNodeValues();
89+
computeNodeBreadths();
90+
computeNodeDepths(iterations);
91+
computeLinkDepths();
92+
return sankey;
93+
};
94+
95+
sankey.relayout = function() {
96+
computeLinkDepths();
97+
return sankey;
98+
};
99+
100+
sankey.link = function() {
101+
var curvature = 0.5;
102+
103+
function link(d) {
104+
var x0 = d.source.x + d.source.dx,
105+
x1 = d.target.x,
106+
xi = interpolateNumber(x0, x1),
107+
x2 = xi(curvature),
108+
x3 = xi(1 - curvature),
109+
y0a = d.source.y + d.sy,
110+
y0b = y0a + d.dy,
111+
y1a = d.target.y + d.ty,
112+
y1b = y1a + d.dy;
113+
return 'M' + x0 + ',' + y0a +
114+
'C' + x2 + ',' + y0a +
115+
' ' + x3 + ',' + y1a +
116+
' ' + x1 + ',' + y1a +
117+
'L' + x1 + ',' + y1b +
118+
'C' + x3 + ',' + y1b +
119+
' ' + x2 + ',' + y0b +
120+
' ' + x0 + ',' + y0b +
121+
'Z';
122+
}
123+
124+
link.curvature = function(_) {
125+
if(!arguments.length) return curvature;
126+
curvature = +_;
127+
return link;
128+
};
129+
130+
return link;
131+
};
132+
133+
// Populate the sourceLinks and targetLinks for each node.
134+
// Also, if the source and target are not objects, assume they are indices.
135+
function computeNodeLinks() {
136+
nodes.forEach(function(node) {
137+
node.sourceLinks = [];
138+
node.targetLinks = [];
139+
});
140+
links.forEach(function(link, i) {
141+
var source = link.source,
142+
target = link.target;
143+
if(typeof source === 'number') source = link.source = nodes[link.source];
144+
if(typeof target === 'number') target = link.target = nodes[link.target];
145+
link.originalIndex = i;
146+
source.sourceLinks.push(link);
147+
target.targetLinks.push(link);
148+
});
149+
}
150+
151+
// Compute the value (size) of each node by summing the associated links.
152+
function computeNodeValues() {
153+
nodes.forEach(function(node) {
154+
node.value = Math.max(
155+
sum(node.sourceLinks, value),
156+
sum(node.targetLinks, value)
157+
);
158+
});
159+
}
160+
161+
// Iteratively assign the breadth (x-position) for each node.
162+
// Nodes are assigned the maximum breadth of incoming neighbors plus one;
163+
// nodes with no incoming links are assigned breadth zero, while
164+
// nodes with no outgoing links are assigned the maximum breadth.
165+
function computeNodeBreadths() {
166+
var remainingNodes = nodes,
167+
nextNodes,
168+
x = 0;
169+
170+
function processNode(node) {
171+
node.x = x;
172+
node.dx = nodeWidth;
173+
node.sourceLinks.forEach(function(link) {
174+
if(nextNodes.indexOf(link.target) < 0) {
175+
nextNodes.push(link.target);
176+
}
177+
});
178+
}
179+
180+
while(remainingNodes.length) {
181+
nextNodes = [];
182+
remainingNodes.forEach(processNode);
183+
remainingNodes = nextNodes;
184+
++x;
185+
}
186+
187+
//
188+
moveSinksRight(x);
189+
scaleNodeBreadths((size[0] - nodeWidth) / (x - 1));
190+
}
191+
192+
// function moveSourcesRight() {
193+
// nodes.forEach(function(node) {
194+
// if (!node.targetLinks.length) {
195+
// node.x = min(node.sourceLinks, function(d) { return d.target.x; }) - 1;
196+
// }
197+
// });
198+
// }
199+
200+
function moveSinksRight(x) {
201+
nodes.forEach(function(node) {
202+
if(!node.sourceLinks.length) {
203+
node.x = x - 1;
204+
}
205+
});
206+
}
207+
208+
function scaleNodeBreadths(kx) {
209+
nodes.forEach(function(node) {
210+
node.x *= kx;
211+
});
212+
}
213+
214+
function computeNodeDepths(iterations) {
215+
var nodesByBreadth = nest()
216+
.key(function(d) { return d.x; })
217+
.sortKeys(ascending)
218+
.entries(nodes)
219+
.map(function(d) { return d.values; });
220+
221+
//
222+
initializeNodeDepth();
223+
resolveCollisions();
224+
for(var alpha = 1; iterations > 0; --iterations) {
225+
relaxRightToLeft(alpha *= 0.99);
226+
resolveCollisions();
227+
relaxLeftToRight(alpha);
228+
resolveCollisions();
229+
}
230+
231+
function initializeNodeDepth() {
232+
var L = max(nodesByBreadth, function(nodes) {
233+
return nodes.length;
234+
});
235+
var maxNodePadding = maxPaddedSpace * size[1] / (L - 1);
236+
if(nodePadding > maxNodePadding) nodePadding = maxNodePadding;
237+
var ky = min(nodesByBreadth, function(nodes) {
238+
return (size[1] - (nodes.length - 1) * nodePadding) / sum(nodes, value);
239+
});
240+
241+
nodesByBreadth.forEach(function(nodes) {
242+
nodes.forEach(function(node, i) {
243+
node.y = i;
244+
node.dy = node.value * ky;
245+
});
246+
});
247+
248+
links.forEach(function(link) {
249+
link.dy = link.value * ky;
250+
});
251+
}
252+
253+
function relaxLeftToRight(alpha) {
254+
nodesByBreadth.forEach(function(nodes) {
255+
nodes.forEach(function(node) {
256+
if(node.targetLinks.length) {
257+
var y = sum(node.targetLinks, weightedSource) / sum(node.targetLinks, value);
258+
node.y += (y - center(node)) * alpha;
259+
}
260+
});
261+
});
262+
263+
function weightedSource(link) {
264+
return center(link.source) * link.value;
265+
}
266+
}
267+
268+
function relaxRightToLeft(alpha) {
269+
nodesByBreadth.slice().reverse().forEach(function(nodes) {
270+
nodes.forEach(function(node) {
271+
if(node.sourceLinks.length) {
272+
var y = sum(node.sourceLinks, weightedTarget) / sum(node.sourceLinks, value);
273+
node.y += (y - center(node)) * alpha;
274+
}
275+
});
276+
});
277+
278+
function weightedTarget(link) {
279+
return center(link.target) * link.value;
280+
}
281+
}
282+
283+
function resolveCollisions() {
284+
nodesByBreadth.forEach(function(nodes) {
285+
var node,
286+
dy,
287+
y0 = 0,
288+
n = nodes.length,
289+
i;
290+
291+
// Push any overlapping nodes down.
292+
nodes.sort(ascendingDepth);
293+
for(i = 0; i < n; ++i) {
294+
node = nodes[i];
295+
dy = y0 - node.y;
296+
if(dy > 0) node.y += dy;
297+
y0 = node.y + node.dy + nodePadding;
298+
}
299+
300+
// If the bottommost node goes outside the bounds, push it back up.
301+
dy = y0 - nodePadding - size[1];
302+
if(dy > 0) {
303+
y0 = node.y -= dy;
304+
305+
// Push any overlapping nodes back up.
306+
for(i = n - 2; i >= 0; --i) {
307+
node = nodes[i];
308+
dy = node.y + node.dy + nodePadding - y0;
309+
if(dy > 0) node.y -= dy;
310+
y0 = node.y;
311+
}
312+
}
313+
});
314+
}
315+
316+
function ascendingDepth(a, b) {
317+
return a.y - b.y;
318+
}
319+
}
320+
321+
function computeLinkDepths() {
322+
nodes.forEach(function(node) {
323+
node.sourceLinks.sort(ascendingTargetDepth);
324+
node.targetLinks.sort(ascendingSourceDepth);
325+
});
326+
nodes.forEach(function(node) {
327+
var sy = 0, ty = 0;
328+
node.sourceLinks.forEach(function(link) {
329+
link.sy = sy;
330+
sy += link.dy;
331+
});
332+
node.targetLinks.forEach(function(link) {
333+
link.ty = ty;
334+
ty += link.dy;
335+
});
336+
});
337+
338+
function ascendingSourceDepth(a, b) {
339+
return (a.source.y - b.source.y) || (a.originalIndex - b.originalIndex);
340+
}
341+
342+
function ascendingTargetDepth(a, b) {
343+
return (a.target.y - b.target.y) || (a.originalIndex - b.originalIndex);
344+
}
345+
}
346+
347+
function center(node) {
348+
return node.y + node.dy / 2;
349+
}
350+
351+
function value(link) {
352+
return link.value;
353+
}
354+
355+
return sankey;
356+
};

src/traces/sankey/render.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var d3 = require('d3');
1313
var tinycolor = require('tinycolor2');
1414
var Color = require('../../components/color');
1515
var Drawing = require('../../components/drawing');
16-
var d3sankey = require('@plotly/d3-sankey').sankey;
16+
var d3sankey = require('./d3-sankey.js');
1717
var d3Force = require('d3-force');
1818
var Lib = require('../../lib');
1919
var isArrayOrTypedArray = Lib.isArrayOrTypedArray;

0 commit comments

Comments
 (0)