Skip to content

Commit

Permalink
d3#88 an optional "level" property for sankey nodes
Browse files Browse the repository at this point in the history
If present, this will override the x-position/stack for this node
(otherwise computed during layout in computeNodeBreadths)
  • Loading branch information
thiloplanz committed Sep 25, 2014
1 parent b105f96 commit 8658072
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
10 changes: 10 additions & 0 deletions sankey/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ var sankey = d3.sankey()
```
var path = sankey.link();
```

### Node attributes

Some attributes influence how sankey does its layout (anything else is just passed through).

* `level` can be set to an integer to specify how far right a node is pushed, starting with 0
for the left-most position (a "source"). If unspecified, the default layout position is
"sources" left, "sinks" right, everything else to the right of their "inputs".


16 changes: 9 additions & 7 deletions sankey/sankey.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,34 +107,36 @@ 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.
// This can be overridden by a "level" property of the node
function computeNodeBreadths() {
var remainingNodes = nodes,
nextNodes,
x = 0;
x = 0, maxX = 0;

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

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

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

0 comments on commit 8658072

Please sign in to comment.