fix: stackOffset expand should not override numerical XAxis domain#7152
Conversation
When stackOffset='expand' is used with XAxis type='number', the combineAxisDomain function was incorrectly returning [0, 1] for the X axis domain. The expand domain should only apply to the value axis (Y axis in horizontal layout), not the categorical axis. This caused all data points to be mapped outside the visible chart area, resulting in nothing being rendered. The fix adds an isCategorical check (already computed in the function) to ensure expandDomain only applies to the value axis. Fixes recharts#6517
WalkthroughModified the Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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 Tip You can get early access to new features in CodeRabbit.Enable the |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
test/chart/AreaChart.stacked.spec.tsx (1)
1292-1297: Tighten the X-axis assertion and verify selector call count.
not.toEqual([0, 1])is permissive. Prefer asserting the concrete expected domain fromnumericDataand include a call-count check on the spy for selector stability.Suggested test refinement
- it('should not override X axis domain to [0, 1]', () => { + it('should keep numeric X axis domain from data', () => { const { spy } = renderTestCase(state => selectAxisDomain(state, 'xAxis', defaultAxisId, false)); - const result = spy.mock.lastCall?.[0]; - expect(result).toBeDefined(); - expect(result).not.toEqual([0, 1]); + expect(spy).toHaveBeenCalledTimes(1); + expectLastCalledWith(spy, [1, 3]); });As per coding guidelines, "Verify the number of selector calls using the spy object from
createSelectorTestCaseto spot unnecessary re-renders and improve performance."🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@test/chart/AreaChart.stacked.spec.tsx` around lines 1292 - 1297, The test currently only asserts the X-axis domain is not [0,1]; update it to assert the concrete expected domain computed from the fixture numericData (use the same min/max logic the selector uses) and verify the selector invocation count for stability: after calling renderTestCase with selectAxisDomain and args ('xAxis', defaultAxisId, false) assert spy.mock.lastCall[0] equals the computed expected domain and assert spy.mock.calls.length equals the expected number of selector evaluations (use the spy returned by createSelectorTestCase/renderTestCase to check call count).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@test/chart/AreaChart.stacked.spec.tsx`:
- Around line 1292-1297: The test currently only asserts the X-axis domain is
not [0,1]; update it to assert the concrete expected domain computed from the
fixture numericData (use the same min/max logic the selector uses) and verify
the selector invocation count for stability: after calling renderTestCase with
selectAxisDomain and args ('xAxis', defaultAxisId, false) assert
spy.mock.lastCall[0] equals the computed expected domain and assert
spy.mock.calls.length equals the expected number of selector evaluations (use
the spy returned by createSelectorTestCase/renderTestCase to check call count).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 40eaf213-c05a-4183-a955-43b1ea686ffa
📒 Files selected for processing (2)
src/state/selectors/axisSelectors.tstest/chart/AreaChart.stacked.spec.tsx
Bundle ReportChanges will increase total bundle size by 93 bytes (0.0%) ⬆️. This is within the configured threshold ✅ Detailed changes
Affected Assets, Files, and Routes:view changes for bundle: recharts/bundle-treeshaking-treemapAssets Changed:
view changes for bundle: recharts/bundle-es6Assets Changed:
view changes for bundle: recharts/bundle-treeshaking-cartesianAssets Changed:
view changes for bundle: recharts/bundle-umdAssets Changed:
view changes for bundle: recharts/bundle-cjsAssets Changed:
view changes for bundle: recharts/bundle-treeshaking-polarAssets Changed:
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7152 +/- ##
=======================================
Coverage 89.61% 89.61%
=======================================
Files 536 536
Lines 40479 40479
Branches 5519 5519
=======================================
Hits 36275 36275
Misses 4196 4196
Partials 8 8 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Hi @SeaL773 how did you test this change? Do you have a repro, and screenshots before/after? |
Hi @PavelVanecek, thanks for the review! The StackBlitz from #6517 demonstrates the bug: https://stackblitz.com/edit/j22xpcdb?file=src%2FExample.tsx Before:
After (with fix): Removing Tests included in this PR ( Test evidence (before/after): |



Summary
Fixes #6517
Root Cause
In
combineAxisDomain(src/state/selectors/axisSelectors.ts), whenstackOffsetType === 'expand', the function returns[0, 1]for all non-category axes. However, the expand domain should only apply to the value axis (YAxis in horizontal layout), not the base axis (XAxis).When
XAxis type='number'is set, it skips thetype === 'category'branch and incorrectly hits the expand domain shortcut, causing the XAxis domain to become[0, 1]while actual data values are far outside this range.Fix
Added
isCategoricalcheck (already computed in the function):```diff