Skip to content

Commit

Permalink
Add sankey.linkSort.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Mar 11, 2019
1 parent 80df172 commit 60ea055
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/sankey.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default function Sankey() {
id = defaultId,
align = justify,
sort,
linkSort,
nodes = defaultNodes,
links = defaultLinks,
iterations = 6;
Expand Down Expand Up @@ -91,6 +92,10 @@ export default function Sankey() {
return arguments.length ? (links = typeof _ === "function" ? _ : constant(_), sankey) : links;
};

sankey.linkSort = function(_) {
return arguments.length ? (linkSort = _, sankey) : linkSort;
};

sankey.size = function(_) {
return arguments.length ? (x0 = y0 = 0, x1 = +_[0], y1 = +_[1], sankey) : [x1 - x0, y1 - y0];
};
Expand Down Expand Up @@ -182,11 +187,11 @@ export default function Sankey() {
for (var i = 0, n = iterations; i < n; ++i) {
const a = Math.pow(0.99, i);
const b = (i + 1) / n;
reorderLinks();
reorderLinks(graph);
relaxRightToLeft(a);
resolveCollisionsTopToBottom(b);
resolveCollisionsBottomToTop(b);
reorderLinks();
reorderLinks(graph);
relaxLeftToRight(a);
resolveCollisionsTopToBottom(b);
resolveCollisionsBottomToTop(b);
Expand All @@ -210,14 +215,10 @@ export default function Sankey() {
graph.links.forEach(function(link) {
link.width = link.value * ky;
});
}

function reorderLinks() {
columns.forEach(function(nodes) {
nodes.forEach(function(node) {
node.sourceLinks.sort(ascendingTargetBreadth);
node.targetLinks.sort(ascendingSourceBreadth);
});
if (linkSort != null) graph.nodes.forEach(function(node) {
node.sourceLinks.sort(linkSort);
node.targetLinks.sort(linkSort);
});
}

Expand Down Expand Up @@ -296,11 +297,15 @@ export default function Sankey() {
}
}

function computeLinkBreadths(graph) {
graph.nodes.forEach(function(node) {
function reorderLinks(graph) {
if (linkSort === undefined) graph.nodes.forEach(function(node) {
node.sourceLinks.sort(ascendingTargetBreadth);
node.targetLinks.sort(ascendingSourceBreadth);
});
}

function computeLinkBreadths(graph) {
reorderLinks(graph);
graph.nodes.forEach(function(node) {
var y0 = node.y0, y1 = y0;
node.sourceLinks.forEach(function(link) {
Expand Down

0 comments on commit 60ea055

Please sign in to comment.