feat: allow fixing the vertical order of Sankey nodes#7873
Open
adamreeve wants to merge 5 commits into
Open
Conversation
camdecoster
requested changes
Jul 13, 2026
camdecoster
left a comment
Contributor
There was a problem hiding this comment.
This seems great. I've requested a few small changes. What do you think of adding support for link sort?
| right: d3Sankey.sankeyRight, | ||
| center: d3Sankey.sankeyCenter | ||
| }[trace.node.align]; | ||
| var input_sort = trace.node.sort === 'input'; |
Contributor
There was a problem hiding this comment.
Suggested change
| var input_sort = trace.node.sort === 'input'; | |
| const inputNodeSort = trace.node.sort === 'input'; |
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.'); | ||
| } | ||
|
|
Contributor
There was a problem hiding this comment.
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); | ||
| } |
Contributor
There was a problem hiding this comment.
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 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' |
Contributor
There was a problem hiding this comment.
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.' |
Contributor
There was a problem hiding this comment.
I think this mock can be deleted since all of the other sankey mocks use the auto sort.
| } | ||
| ], | ||
| "layout": { | ||
| "title": { "text": "Sankey with fixed vertical node ordering" }, |
Contributor
There was a problem hiding this comment.
Suggested change
| "title": { "text": "Sankey with fixed vertical node ordering" }, | |
| "title": { "text": "Sankey with vertical node ordering matching the input array" }, |
Comment on lines
+505
to
+508
| var errors = []; | ||
| spyOn(Lib, 'error').and.callFake(function (msg) { | ||
| errors.push(msg); | ||
| }); |
Contributor
There was a problem hiding this comment.
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); | |
| }); |
| expect(d3SelectAll('.sankey .node-rect').size()).toBeGreaterThan(0); | ||
|
|
||
| // An error is logged about the fallback | ||
| expect(errors.length).toBe(1); |
Contributor
There was a problem hiding this comment.
Suggested change
| expect(errors.length).toBe(1); | |
| expect(warnings.length).toBe(1); |
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4373
Follow up to #7830
This exposes the
nodeSortmethod added tod3-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
nodeSortmethod 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 passnullinstead of the defaultundefinedis 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
nodeSortoption is not supported byd3-sankey-circular, so is not used for circular Sankey diagrams (see #7689 (comment) for more context).