fix(Sankey): prevent NaN node positions when link values sum to zero#7185
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
WalkthroughGuard against division-by-zero in Sankey layout: three methods in Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
test/chart/Sankey.spec.tsx (1)
741-767: Good regression test for the all-zero case.The test correctly validates that NaN values don't appear in link paths when all link values are zero.
Consider adding a companion test with mixed link values (e.g.,
{ source: 0, target: 1, value: 100 }, { source: 1, target: 2, value: 0 }) to ensure nodes with positive values still render correctly. This would guard against regressions in theyRatiocalculation for datasets where only some links have zero values.,
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@test/chart/Sankey.spec.tsx` around lines 741 - 767, Add a companion test to Sankey.spec.tsx that renders the Sankey component with mixed link values (e.g., links [{ source: 0, target: 1, value: 100 }, { source: 1, target: 2, value: 0 }]) and assert that nodes with positive aggregated value render (query '.recharts-sankey-node' and expect length > 0), links render without NaN in their path 'd' attribute and have finite 'stroke-width', so we validate the yRatio/layout logic handles mixed zero/non-zero link values correctly when using the Sankey component.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/chart/Sankey.tsx`:
- Around line 207-212: The current yRatio calculation returns 0 for any depth
with sum===0 which makes Math.min pick 0 and collapse positive-valued nodes;
instead, when mapping depthTree use Infinity as the fallback for sum===0 so
Math.min correctly ignores empty depths (use sumBy, getValue, nodePadding,
height in that mapping), then after computing yRatio = Math.min(...candidates)
add a special case: if yRatio === Infinity set yRatio = 0 (handles the all-zero
case). This preserves previous behavior for mixed and all-zero scenarios.
---
Nitpick comments:
In `@test/chart/Sankey.spec.tsx`:
- Around line 741-767: Add a companion test to Sankey.spec.tsx that renders the
Sankey component with mixed link values (e.g., links [{ source: 0, target: 1,
value: 100 }, { source: 1, target: 2, value: 0 }]) and assert that nodes with
positive aggregated value render (query '.recharts-sankey-node' and expect
length > 0), links render without NaN in their path 'd' attribute and have
finite 'stroke-width', so we validate the yRatio/layout logic handles mixed
zero/non-zero link values correctly when using the Sankey component.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 3b81251a-15a3-4d1c-bb0e-e3ab3aa4eb92
📒 Files selected for processing (2)
src/chart/Sankey.tsxtest/chart/Sankey.spec.tsx
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7185 +/- ##
=======================================
Coverage 89.06% 89.06%
=======================================
Files 539 539
Lines 41011 41017 +6
Branches 5553 5557 +4
=======================================
+ Hits 36527 36533 +6
Misses 4476 4476
Partials 8 8 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Bundle ReportChanges will increase total bundle size by 432 bytes (0.01%) ⬆️. This is within the configured threshold ✅ Detailed changes
Affected Assets, Files, and Routes:view changes for bundle: recharts/bundle-umdAssets Changed:
view changes for bundle: recharts/bundle-cjsAssets Changed:
view changes for bundle: recharts/bundle-es6Assets Changed:
|
|
Thanks for the review, Please let me know if any further changes are needed. |
Summary
Fixes a division-by-zero issue in Sankey layout that can produce
NaNnode positions when link values sum to0.Problem
In
relaxLeftToRightandrelaxRightToLeft, node positions are computed using:When
sourceSumortargetSumis0, this results inNaN, which propagates and breaks the layout.Fix
Added guards to handle zero sums:
centerY(node)whensourceSum === 0centerY(node)whentargetSum === 0Tests
Added a regression test to ensure:
NaNnode positions are produced when link values are zeroImpact
Summary by CodeRabbit
Bug Fixes
Tests