Skip to content
This repository has been archived by the owner on Nov 7, 2018. It is now read-only.

Sankey Chart: Add a parameter which allows nodes to be manually placed horizontally. #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 20 additions & 8 deletions sankey/sankey.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ d3.sankey = function() {
};

sankey.nodes = function(_) {
// nodes are a list of objects, with a name parameter identifying a label
// and, optionally, an xpos parameter, identifying the horizontal position
// of the node. E.g.: {name:'foo', xpos:3}
if (!arguments.length) return nodes;
nodes = _;
return sankey;
Expand Down Expand Up @@ -107,16 +110,24 @@ d3.sankey = function() {
// 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.
// nodes with xpos defined override the above rules
function computeNodeBreadths() {
var remainingNodes = nodes,
nextNodes,
x = 0;

x = 0,
maxx = 0;

while (remainingNodes.length) {
nextNodes = [];
remainingNodes.forEach(function(node) {
node.x = x;
node.dx = nodeWidth;
if(node.xpos != null) {
node.x = node.xpos;
if(node.x+1 > maxx)
maxx = node.x+1;
return;
}
node.x = x;
node.sourceLinks.forEach(function(link) {
if (nextNodes.indexOf(link.target) < 0) {
nextNodes.push(link.target);
Expand All @@ -127,22 +138,23 @@ d3.sankey = function() {
++x;
}

//
moveSinksRight(x);
scaleNodeBreadths((size[0] - nodeWidth) / (x - 1));
if(x > maxx)
maxx = x;
moveSinksRight(maxx);
scaleNodeBreadths((size[0] - nodeWidth) / (maxx - 1));
}

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

function moveSinksRight(x) {
nodes.forEach(function(node) {
if (!node.sourceLinks.length) {
if (!node.sourceLinks.length && node.xpos == null) {
node.x = x - 1;
}
});
Expand Down