-
-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: allow fixing the vertical order of Sankey nodes #7873
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v4.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - Add `sort` option to Sankey nodes [[#7873](https://github.com/plotly/plotly.js/pull/7873)] |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -40,6 +40,7 @@ function sankeyModel(layout, d, traceIndex) { | |||||||||||||||||||
| right: d3Sankey.sankeyRight, | ||||||||||||||||||||
| center: d3Sankey.sankeyCenter | ||||||||||||||||||||
| }[trace.node.align]; | ||||||||||||||||||||
| var input_sort = trace.node.sort === 'input'; | ||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| var width = layout.width * (domain.x[1] - domain.x[0]); | ||||||||||||||||||||
| var height = layout.height * (domain.y[1] - domain.y[0]); | ||||||||||||||||||||
|
|
@@ -48,6 +49,10 @@ function sankeyModel(layout, d, traceIndex) { | |||||||||||||||||||
| var links = calcData._links; | ||||||||||||||||||||
| var circular = calcData.circular; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if(circular && input_sort) { | ||||||||||||||||||||
| Lib.error('Circular Sankey diagrams do not support the "input" node.sort mode; falling back to the default sort.'); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
Comment on lines
+52
to
+55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
| // Select Sankey generator | ||||||||||||||||||||
| var sankey; | ||||||||||||||||||||
| if(circular) { | ||||||||||||||||||||
|
|
@@ -70,6 +75,11 @@ function sankeyModel(layout, d, traceIndex) { | |||||||||||||||||||
| .nodes(nodes) | ||||||||||||||||||||
| .links(links); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| var graph = sankey(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if(sankey.nodePadding() < nodePad) { | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this mock can be deleted since all of the other sankey mocks use the auto sort. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "data": [ | ||
| { | ||
| "type": "sankey", | ||
| "node": { | ||
| "label": ["0", "1", "2", "3", "4", "5"], | ||
| "align": "center", | ||
| "sort": "auto" | ||
| }, | ||
| "link": { | ||
| "source": [0, 0, 0, 0, 1, 4], | ||
| "target": [1, 2, 3, 4, 5, 5], | ||
| "value": [1, 2, 1, 1, 1, 1] | ||
| } | ||
| } | ||
| ], | ||
| "layout": { | ||
| "title": { "text": "Sankey with automatic vertical node ordering" }, | ||
| "width": 800, | ||
| "height": 800 | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,22 @@ | ||||||
| { | ||||||
| "data": [ | ||||||
| { | ||||||
| "type": "sankey", | ||||||
| "node": { | ||||||
| "label": ["0", "1", "2", "3", "4", "5"], | ||||||
| "align": "center", | ||||||
| "sort": "input" | ||||||
| }, | ||||||
| "link": { | ||||||
| "source": [0, 0, 0, 0, 1, 4], | ||||||
| "target": [1, 2, 3, 4, 5, 5], | ||||||
| "value": [1, 2, 1, 1, 1, 1] | ||||||
| } | ||||||
| } | ||||||
| ], | ||||||
| "layout": { | ||||||
| "title": { "text": "Sankey with fixed vertical node ordering" }, | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| "width": 800, | ||||||
| "height": 800 | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -501,6 +501,27 @@ describe('sankey tests', function () { | |||||||||||||||||
| .then(done, done.fail); | ||||||||||||||||||
| }); | ||||||||||||||||||
|
|
||||||||||||||||||
| it('falls back to the default sort for circular Sankey with node.sort set', function (done) { | ||||||||||||||||||
| var errors = []; | ||||||||||||||||||
| spyOn(Lib, 'error').and.callFake(function (msg) { | ||||||||||||||||||
| errors.push(msg); | ||||||||||||||||||
| }); | ||||||||||||||||||
|
Comment on lines
+505
to
+508
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| var mockCircularCopy = Lib.extendDeep({}, mockCircular); | ||||||||||||||||||
| mockCircularCopy.data[0].node.sort = 'input'; | ||||||||||||||||||
|
|
||||||||||||||||||
| Plotly.newPlot(gd, mockCircularCopy) | ||||||||||||||||||
| .then(function () { | ||||||||||||||||||
| // The plot renders successfully | ||||||||||||||||||
| expect(gd.calcdata[0][0].circular).toBe(true); | ||||||||||||||||||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
| }) | ||||||||||||||||||
| .then(done, done.fail); | ||||||||||||||||||
| }); | ||||||||||||||||||
|
|
||||||||||||||||||
| it('can create groups, restyle groups and properly update DOM', function (done) { | ||||||||||||||||||
| var mockCircularCopy = Lib.extendDeep({}, mockCircular); | ||||||||||||||||||
| var firstGroup = [ | ||||||||||||||||||
|
|
||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.