Skip to content

Fix Sankey crash when no link is valid - #8213

Open
Pedroveras-dev wants to merge 2 commits into
plotly:mainfrom
Pedroveras-dev:fix/issue-8211
Open

Pedroveras-dev wants to merge 2 commits into
plotly:mainfrom
Pedroveras-dev:fix/issue-8211

Conversation

@Pedroveras-dev

Copy link
Copy Markdown

Fixes #8211

A Sankey trace with no valid link now renders an empty plot. Before this fix, the trace threw RangeError: Invalid array length.

Problem

  • [{ type: 'sankey' }], or a trace where all links have value: 0, makes Plotly.newPlot throw RangeError: Invalid array length
  • The error comes from computeNodeLayers in @plotly/d3-sankey 0.12.3. With zero nodes, max([]) returns undefined, so the column count is NaN. Then new Array(NaN) throws.
  • This is a regression since v4.0.0 (Upgrade plotly/d3-sankey to 0.12.3 #7830). In v3.7.0 the same figure rendered an empty plot.

Solution

sankeyModel in src/traces/sankey/render.js skips the layout when the graph has zero nodes. It uses an empty graph { nodes: [], links: [] } instead.

  • calc.js builds the nodes from the valid links. Thus the trace has nodes only when it has at least one valid link.
  • The guard is in plotly.js and not in @plotly/d3-sankey. This fix needs no new release of the package. It also covers the circular layout generator, because the layout call does not run for the two generators.

Tests

New jasmine tests in test/jasmine/tests/sankey_test.js:

  • renders a Sankey trace without valid links
  • switches between empty and non-empty Sankey on react

Results:

  • sankey jasmine suite (Karma), with the fix: TOTAL: 90 SUCCESS
  • Same suite before the change: 88 tests, all pass
  • New tests without the fix in render.js: 2 FAILED with RangeError: Invalid array length
  • npm run lint: exit 0
  • npm run test-syntax: exit 0
  • npm run typecheck: exit 0

The change adds no mock and moves no image baseline. Existing figures with nodes take the same code path as before.

🐦

d3-sankey 0.12 throws 'Invalid array length' when it lays out a graph
without nodes. Skip the layout and use an empty graph instead, as in
v3.7.0.

Fixes plotly#8211

This branch has not been deployed

No deployments
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.

[BUG]: Sankey crashes instead of showing an empty chart when a filter returns no links

1 participant