Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions draftlogs/8213_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix Sankey traces throwing "Invalid array length" when no link is valid [[#8213](https://github.com/plotly/plotly.js/pull/8213)], with thanks to @Pedroveras-dev for the contribution!
3 changes: 2 additions & 1 deletion src/traces/sankey/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ function sankeyModel(layout, d, traceIndex) {
applyInputSort('link');
applyInputSort('node');

var graph = sankey();
// d3-sankey throws on an empty graph
const graph = nodes.length ? sankey() : { nodes: [], links: [] };

if(sankey.nodePadding() < nodePad) {
Lib.warn('node.pad was reduced to ', sankey.nodePadding(), ' to fit within the figure.');
Expand Down
47 changes: 47 additions & 0 deletions test/jasmine/tests/sankey_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,53 @@ describe('sankey tests', function () {
.then(done, done.fail);
});

it('renders a Sankey trace without valid links', (done) => {
const assertEmpty = () => {
expect(d3SelectAll('.sankey .sankey-node').size()).toBe(0);
expect(d3SelectAll('.sankey .sankey-link').size()).toBe(0);
};

Plotly.newPlot(gd, [{ type: 'sankey' }])
.then(assertEmpty)
.then(() =>
Plotly.newPlot(gd, [
{
type: 'sankey',
node: { label: ['a', 'b'] },
link: { source: [0], target: [1], value: [0] }
}
])
)
.then(assertEmpty)
.then(done, done.fail);
});

it('switches between empty and non-empty Sankey on react', (done) => {
const emptyCopy = Lib.extendDeep({}, mock);
emptyCopy.data[0].link = { source: [], target: [], value: [] };

Plotly.newPlot(gd, Lib.extendDeep({}, mock))
.then(() => {
expect(d3SelectAll('.sankey .sankey-node').size()).toBeGreaterThan(0);
expect(d3SelectAll('.sankey .sankey-link').size()).toBeGreaterThan(0);

return Plotly.react(gd, emptyCopy);
})
.then(delay(1000))
.then(() => {
expect(d3SelectAll('.sankey .sankey-node').size()).toBe(0);
expect(d3SelectAll('.sankey .sankey-link').size()).toBe(0);

return Plotly.react(gd, Lib.extendDeep({}, mock));
})
.then(delay(1000))
.then(() => {
expect(d3SelectAll('.sankey .sankey-node').size()).toBeGreaterThan(0);
expect(d3SelectAll('.sankey .sankey-link').size()).toBeGreaterThan(0);
})
.then(done, done.fail);
});

it('falls back to the default sort for circular Sankey with node.sort set', (done) => {
const warnings = [];
spyOn(Lib, 'warn').and.callFake((msg) => {
Expand Down