From 7817fbb610212f8ff6b2cd5bc675044013ae6b74 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Sun, 1 Sep 2019 17:29:51 -0700 Subject: [PATCH] Fix sankey.linkSort when non-null. --- README.md | 4 ++++ src/sankey.js | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/README.md b/README.md index 24dda12..6f2f08a 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,10 @@ For convenience, a link’s source and target may be initialized using numeric o * *link*.width - the link’s width (proportional to *link*.value) * *link*.index - the zero-based index of *link* within the array of links +# sankey.linkSort([sort]) [<>](https://github.com/d3/d3-sankey/blob/master/src/sankey.js "Source") + +If *sort* is specified, sets the link sort method and returns this Sankey generator. If *sort* is not specified, returns the current link sort method, which defaults to *undefined*, indicating that vertical order of links within each node will be determined automatically by the layout. If *sort* is null, the order is fixed by the input. Otherwise, the specified *sort* function determines the order; the function is passed two links, and must return a value less than 0 if the first link should be above the second, and a value greater than 0 if the second link should be above the first, or 0 if the order is not specified. + # sankey.nodeId([id]) [<>](https://github.com/d3/d3-sankey/blob/master/src/sankey.js "Source") If *id* is specified, sets the node id accessor to the specified function and returns this Sankey generator. If *id* is not specified, returns the current node id accessor, which defaults to the numeric *node*.index: diff --git a/src/sankey.js b/src/sankey.js index ae7cae4..56ad642 100644 --- a/src/sankey.js +++ b/src/sankey.js @@ -138,6 +138,12 @@ export default function Sankey() { source.sourceLinks.push(link); target.targetLinks.push(link); } + if (linkSort != null) { + for (const {sourceLinks, targetLinks} of nodes) { + sourceLinks.sort(linkSort); + targetLinks.sort(linkSort); + } + } } function computeNodeValues({nodes}) {