Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
321 changes: 321 additions & 0 deletions build/d3-sankey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,321 @@
// https://github.com/d3/d3-sankey Version 0.5.0. Copyright 2018 Mike Bostock.
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-array'), require('d3-collection'), require('d3-interpolate')) :
typeof define === 'function' && define.amd ? define(['exports', 'd3-array', 'd3-collection', 'd3-interpolate'], factory) :
(factory((global.d3 = global.d3 || {}),global.d3,global.d3,global.d3));
}(this, (function (exports,d3Array,d3Collection,d3Interpolate) { 'use strict';

var sankey = function() {
var sankey = {},
nodeWidth = 24,
nodePadding = 8,
size = [1, 1],
nodes = [],
links = [],
maxPaddedSpace = 2 / 3; // Defined as a fraction of the total available space

sankey.nodeWidth = function(_) {
if (!arguments.length) return nodeWidth;
nodeWidth = +_;
return sankey;
};

sankey.nodePadding = function(_) {
if (!arguments.length) return nodePadding;
nodePadding = +_;
return sankey;
};

sankey.nodes = function(_) {
if (!arguments.length) return nodes;
nodes = _;
return sankey;
};

sankey.links = function(_) {
if (!arguments.length) return links;
links = _;
return sankey;
};

sankey.size = function(_) {
if (!arguments.length) return size;
size = _;
return sankey;
};

sankey.layout = function(iterations) {
computeNodeLinks();
computeNodeValues();
computeNodeBreadths();
computeNodeDepths(iterations);
computeLinkDepths();
return sankey;
};

sankey.relayout = function() {
computeLinkDepths();
return sankey;
};

sankey.link = function() {
var curvature = .5;

function link(d) {
var x0 = d.source.x + d.source.dx,
x1 = d.target.x,
xi = d3Interpolate.interpolateNumber(x0, x1),
x2 = xi(curvature),
x3 = xi(1 - curvature),
y0a = d.source.y + d.sy,
y0b = y0a + d.dy,
y1a = d.target.y + d.ty,
y1b = y1a + d.dy;
return "M" + x0 + "," + y0a
+ "C" + x2 + "," + y0a
+ " " + x3 + "," + y1a
+ " " + x1 + "," + y1a
+ "L" + x1 + "," + y1b
+ "C" + x3 + "," + y1b
+ " " + x2 + "," + y0b
+ " " + x0 + "," + y0b
+ "Z";
}

link.curvature = function(_) {
if (!arguments.length) return curvature;
curvature = +_;
return link;
};

return link;
};

// Populate the sourceLinks and targetLinks for each node.
// Also, if the source and target are not objects, assume they are indices.
function computeNodeLinks() {
nodes.forEach(function(node) {
node.sourceLinks = [];
node.targetLinks = [];
});
links.forEach(function(link, i) {
var source = link.source,
target = link.target;
if (typeof source === "number") source = link.source = nodes[link.source];
if (typeof target === "number") target = link.target = nodes[link.target];
link.originalIndex = i;
source.sourceLinks.push(link);
target.targetLinks.push(link);
});
}

// Compute the value (size) of each node by summing the associated links.
function computeNodeValues() {
nodes.forEach(function(node) {
node.value = Math.max(
d3Array.sum(node.sourceLinks, value),
d3Array.sum(node.targetLinks, value)
);
});
}

// Iteratively assign the breadth (x-position) for each node.
// Nodes are assigned the maximum breadth of incoming neighbors plus one;
// nodes with no incoming links are assigned breadth zero, while
// nodes with no outgoing links are assigned the maximum breadth.
function computeNodeBreadths() {
var remainingNodes = nodes,
nextNodes,
x = 0;

while (remainingNodes.length) {
nextNodes = [];
remainingNodes.forEach(function(node) {
node.x = x;
node.dx = nodeWidth;
node.sourceLinks.forEach(function(link) {
if (nextNodes.indexOf(link.target) < 0) {
nextNodes.push(link.target);
}
});
});
remainingNodes = nextNodes;
++x;
}

//
moveSinksRight(x);
scaleNodeBreadths((size[0] - nodeWidth) / (x - 1));
}

// function moveSourcesRight() {
// nodes.forEach(function(node) {
// if (!node.targetLinks.length) {
// node.x = min(node.sourceLinks, function(d) { return d.target.x; }) - 1;
// }
// });
// }

function moveSinksRight(x) {
nodes.forEach(function(node) {
if (!node.sourceLinks.length) {
node.x = x - 1;
}
});
}

function scaleNodeBreadths(kx) {
nodes.forEach(function(node) {
node.x *= kx;
});
}

function computeNodeDepths(iterations) {
var nodesByBreadth = d3Collection.nest()
.key(function(d) { return d.x; })
.sortKeys(d3Array.ascending)
.entries(nodes)
.map(function(d) { return d.values; });

//
initializeNodeDepth();
resolveCollisions();
for (var alpha = 1; iterations > 0; --iterations) {
relaxRightToLeft(alpha *= .99);
resolveCollisions();
relaxLeftToRight(alpha);
resolveCollisions();
}

function initializeNodeDepth() {
var L = d3Array.max(nodesByBreadth, function(nodes) {
return nodes.length;
});
var maxNodePadding = maxPaddedSpace * size[1] / (L - 1);
if(nodePadding > maxNodePadding) nodePadding = maxNodePadding;
var ky = d3Array.min(nodesByBreadth, function(nodes) {
return (size[1] - (nodes.length - 1) * nodePadding) / d3Array.sum(nodes, value);
});

nodesByBreadth.forEach(function(nodes) {
nodes.forEach(function(node, i) {
node.y = i;
node.dy = node.value * ky;
});
});

links.forEach(function(link) {
link.dy = link.value * ky;
});
}

function relaxLeftToRight(alpha) {
nodesByBreadth.forEach(function(nodes) {
nodes.forEach(function(node) {
if (node.targetLinks.length) {
var y = d3Array.sum(node.targetLinks, weightedSource) / d3Array.sum(node.targetLinks, value);
node.y += (y - center(node)) * alpha;
}
});
});

function weightedSource(link) {
return center(link.source) * link.value;
}
}

function relaxRightToLeft(alpha) {
nodesByBreadth.slice().reverse().forEach(function(nodes) {
nodes.forEach(function(node) {
if (node.sourceLinks.length) {
var y = d3Array.sum(node.sourceLinks, weightedTarget) / d3Array.sum(node.sourceLinks, value);
node.y += (y - center(node)) * alpha;
}
});
});

function weightedTarget(link) {
return center(link.target) * link.value;
}
}

function resolveCollisions() {
nodesByBreadth.forEach(function(nodes) {
var node,
dy,
y0 = 0,
n = nodes.length,
i;

// Push any overlapping nodes down.
nodes.sort(ascendingDepth);
for (i = 0; i < n; ++i) {
node = nodes[i];
dy = y0 - node.y;
if (dy > 0) node.y += dy;
y0 = node.y + node.dy + nodePadding;
}

// If the bottommost node goes outside the bounds, push it back up.
dy = y0 - nodePadding - size[1];
if (dy > 0) {
y0 = node.y -= dy;

// Push any overlapping nodes back up.
for (i = n - 2; i >= 0; --i) {
node = nodes[i];
dy = node.y + node.dy + nodePadding - y0;
if (dy > 0) node.y -= dy;
y0 = node.y;
}
}
});
}

function ascendingDepth(a, b) {
return a.y - b.y;
}
}

function computeLinkDepths() {
nodes.forEach(function(node) {
node.sourceLinks.sort(ascendingTargetDepth);
node.targetLinks.sort(ascendingSourceDepth);
});
nodes.forEach(function(node) {
var sy = 0, ty = 0;
node.sourceLinks.forEach(function(link) {
link.sy = sy;
sy += link.dy;
});
node.targetLinks.forEach(function(link) {
link.ty = ty;
ty += link.dy;
});
});

function ascendingSourceDepth(a, b) {
return (a.source.y - b.source.y) || (a.originalIndex - b.originalIndex);
}

function ascendingTargetDepth(a, b) {
return (a.target.y - b.target.y) || (a.originalIndex - b.originalIndex);
}
}

function center(node) {
return node.y + node.dy / 2;
}

function value(link) {
return link.value;
}

return sankey;
};

exports.sankey = sankey;

Object.defineProperty(exports, '__esModule', { value: true });

})));
2 changes: 2 additions & 0 deletions build/d3-sankey.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions src/sankey.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ascending, min, sum} from "d3-array";
import {ascending, min, sum, max} from "d3-array";
import {nest} from "d3-collection";
import {interpolateNumber} from "d3-interpolate";

Expand All @@ -8,7 +8,8 @@ export default function() {
nodePadding = 8,
size = [1, 1],
nodes = [],
links = [];
links = [],
maxPaddedSpace = 2 / 3; // Defined as a fraction of the total available space

sankey.nodeWidth = function(_) {
if (!arguments.length) return nodeWidth;
Expand Down Expand Up @@ -184,6 +185,11 @@ export default function() {
}

function initializeNodeDepth() {
var L = max(nodesByBreadth, function(nodes) {
return nodes.length;
});
var maxNodePadding = maxPaddedSpace * size[1] / (L - 1);
if(nodePadding > maxNodePadding) nodePadding = maxNodePadding;
var ky = min(nodesByBreadth, function(nodes) {
return (size[1] - (nodes.length - 1) * nodePadding) / sum(nodes, value);
});
Expand Down