d3 / d3-sankey

Visualize flow between nodes in a directed acyclic network.

Home Page:https://observablehq.com/collection/@d3/d3-sankey

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

parameterize the sorting algorithm in resolveCollisions()

damil opened this issue · comments

I'm new to d3-sankey and d3.js in general, thanks for this software, it's just amazingly powerful.
One feature is missing in sankey : the ability to customize the sorting algorithm used in resolveCollisions().
I'm building a diagram where I need vertical nodes to be sorted by name instead of "ascendingBreadth". I modified the source code by putting my own sorting function and it works like a charm; but it would be much better if the sorting function could be passed as a parameter to sankey().

@damil , could you provide me an example of where and how you did this? I need to sort alphabetically (A higher than B, higher than C, etc.).

In function resolveCollisions(), like this :

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

        // Push any overlapping nodes down.
        // ORIG : nodes.sort(ascendingBreadth);
        // MODIFIED LINE BELOW :
        nodes.sort(function(a, b){return a.name < b.name ? -1 : a.name > b.name ? 1 : 0;});

I just ran into a situation where disabling/altering the sorting would be super useful.

EDIT: seems like this pull would fix this: #53

You can now use sankey.nodeSort(null) to disable automatic reordering of nodes within the columns and preserve the input order.