FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat: Allow fixing the vertical order of Sankey links, nodes by adamreeve · Pull Request #7873 · plotly/plotly.js · GitHub

feat: Allow fixing the vertical order of Sankey links, nodes - #7873

Merged
camdecoster merged 9 commits into
plotly:v4.0from
adamreeve:sankey-order
Jul 20, 2026
Merged

feat: Allow fixing the vertical order of Sankey links, nodes#7873
camdecoster merged 9 commits into
plotly:v4.0from
adamreeve:sankey-order

Conversation

adamreeve commented Jun 28, 2026
edited by camdecoster
Loading

Copy link
Copy Markdown
Contributor

Fixes #4373

Follow up to #7830

This exposes the nodeSort method added to d3-sankey, to allow disabling the default ordering algorithm and enforcing that the vertical order should match the order of nodes in the input data.

Note that the nodeSort method in d3-sankey can be a method that compared two nodes (see the docs), but for many users I think just providing a way to pass null instead of the default undefined is sufficient. But maybe this could be future-proofed somehow to support using a comparison method in future? Although I'm not sure how feasible it is to make that work in plotly.

The nodeSort option is not supported by d3-sankey-circular, so is not used for circular Sankey diagrams (see #7689 (comment) for more context).

EDIT (by @camdecoster): I added the corresponding sort option for links as well. Everything that @adamreeve said above is true for this attribute (linkSort) as well.

adamreeve marked this pull request as ready for review June 29, 2026 02:16

camdecoster left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This seems great. I've requested a few small changes. What do you think of adding support for link sort?

Comment thread src/traces/sankey/render.js Outdated
right: d3Sankey.sankeyRight,
center: d3Sankey.sankeyCenter
}[trace.node.align];
var input_sort = trace.node.sort === 'input';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
var input_sort = trace.node.sort === 'input';
const inputNodeSort = trace.node.sort === 'input';

Comment thread src/traces/sankey/render.js Outdated
Comment on lines +52 to +55
if(circular && input_sort) {
Lib.error('Circular Sankey diagrams do not support the "input" node.sort mode; falling back to the default sort.');
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
if(circular && input_sort) {
Lib.error('Circular Sankey diagrams do not support the "input" node.sort mode; falling back to the default sort.');
}

// d3-sankey-circular does not support the nodeSort method
if(!circular) {
sankey.nodeSort(input_sort ? null : undefined);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
}
if (inputNodeSort) {
if (circular) {
Lib.warn('Circular Sankey diagrams do not support the "input" node.sort mode; falling back to the default sort.');
} else {
// Passing null keeps nodes in their input order
sankey.nodeSort(null);
}
}

Comment thread src/traces/sankey/attributes.js Outdated
Comment on lines +178 to +180
'If the value is `auto` (the default), the vertical order of nodes will be determined automatically',
'by the layout.',
'If the value is `input`, the vertical order is kept the same as the order in the input node array'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
'If the value is `auto` (the default), the vertical order of nodes will be determined automatically',
'by the layout.',
'If the value is `input`, the vertical order is kept the same as the order in the input node array'
'For `auto` (the default), the vertical order of nodes will be determined automatically by the layout.',
'For `input`, the vertical order of nodes is kept the same as the order in the input array.'

Comment thread test/image/mocks/sankey_auto_sort.json Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I think this mock can be deleted since all of the other sankey mocks use the auto sort.

Comment thread test/image/mocks/sankey_input_sort.json Outdated
}
],
"layout": {
"title": { "text": "Sankey with fixed vertical node ordering" },

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
"title": { "text": "Sankey with fixed vertical node ordering" },
"title": { "text": "Sankey with vertical node ordering matching the input array" },

Comment thread test/jasmine/tests/sankey_test.js Outdated
Comment on lines +505 to +508
var errors = [];
spyOn(Lib, 'error').and.callFake(function (msg) {
errors.push(msg);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
var errors = [];
spyOn(Lib, 'error').and.callFake(function (msg) {
errors.push(msg);
});
var warnings = [];
spyOn(Lib, 'warn').and.callFake(function (msg) {
warnings.push(msg);
});

Comment thread test/jasmine/tests/sankey_test.js Outdated
expect(d3SelectAll('.sankey .node-rect').size()).toBeGreaterThan(0);

// An error is logged about the fallback
expect(errors.length).toBe(1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
expect(errors.length).toBe(1);
expect(warnings.length).toBe(1);

Copy link
Copy Markdown
Contributor

FYI, we're tying to get an RC of v4.0 out this week, so I'm going to jump in and make some changes. I'd rather let you finish things up, but schedules, right? Your work has been very helpful and appreciated.

camdecoster added this to the v4.0.0 milestone Jul 20, 2026
camdecoster changed the title feat: allow fixing the vertical order of Sankey nodes feat: Allow fixing the vertical order of Sankey links, nodes Jul 20, 2026
camdecoster merged commit ccb2569 into plotly:v4.0 Jul 20, 2026
164 of 165 checks passed

adamreeve commented Jul 20, 2026
edited
Loading

Copy link
Copy Markdown
Contributor Author

OK thanks @camdecoster! Your changes all make sense to me but I've just been quite busy with work stuff lately. I'm not familiar with the link sort feature but am happy to take a look into that. I see you added the link sort here too, thanks!

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL