Skip to content

fix: stackOffset expand should not override numerical XAxis domain#7152

Merged
PavelVanecek merged 1 commit intorecharts:mainfrom
SeaL773:fix/expand-domain-number-xaxis
Mar 22, 2026
Merged

fix: stackOffset expand should not override numerical XAxis domain#7152
PavelVanecek merged 1 commit intorecharts:mainfrom
SeaL773:fix/expand-domain-number-xaxis

Conversation

@SeaL773
Copy link
Copy Markdown
Contributor

@SeaL773 SeaL773 commented Mar 21, 2026

Summary

Fixes #6517

Root Cause

In combineAxisDomain (src/state/selectors/axisSelectors.ts), when stackOffsetType === '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 the type === '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 isCategorical check (already computed in the function):```diff

  • if (stackOffsetType === 'expand') {
  • if (stackOffsetType === 'expand' && !isCategorical) {

## Tests Added
3 new tests in `test/chart/AreaChart.stacked.spec.tsx`:
- Area curves render when XAxis type is number with expand offset
- Y axis domain is correctly `[0, 1]`
- X axis domain is NOT overridden to `[0, 1]`

All 38 tests pass.

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

* **Bug Fixes**
  * Corrected axis domain calculation for stacked area charts with categorical X-axes when using the 'expand' stack offset setting.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

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
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 21, 2026

Walkthrough

Modified the combineAxisDomain function in axisSelectors to conditionally apply the expand domain only when the stack offset type is 'expand' and the axis is not categorical. Added a test case for numeric X-axis with stacked expand charts to validate the fix.

Changes

Cohort / File(s) Summary
Axis Selector Logic
src/state/selectors/axisSelectors.ts
Updated combineAxisDomain to return expandDomain only when stackOffsetType === 'expand' AND axis is non-categorical; otherwise returns numericalDomain.
Stacked Area Chart Tests
test/chart/AreaChart.stacked.spec.tsx
Replaced placeholder test with active test suite for numeric X-axis with stackOffset="expand", verifying Y-axis domain is [0, 1] and X-axis domain is not overridden.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • Ts strict #6750: Changes axisSelectors domain logic for stack offset handling and axis type differentiation.

Suggested reviewers

  • PavelVanecek
  • ckifer
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: stackOffset expand should not override numerical XAxis domain' accurately and specifically describes the main bug fix in the changeset.
Description check ✅ Passed The PR description provides clear summary, root cause analysis, fix explanation, and test details. All key sections are covered with specific information about the issue and solution.
Linked Issues check ✅ Passed The PR directly addresses issue #6517 by adding the isCategorical check to prevent expand domain from overriding numerical XAxis domains, with corresponding tests verifying the fix.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the expand domain issue for numerical XAxis. No unrelated modifications or out-of-scope changes detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

You can get early access to new features in CodeRabbit.

Enable the early_access setting to enable early access features such as new models, tools, and more.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 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 from numericData and 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 createSelectorTestCase to 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

📥 Commits

Reviewing files that changed from the base of the PR and between b39edbf and 728bf6b.

📒 Files selected for processing (2)
  • src/state/selectors/axisSelectors.ts
  • test/chart/AreaChart.stacked.spec.tsx

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 21, 2026

Bundle Report

Changes will increase total bundle size by 93 bytes (0.0%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
recharts/bundle-cjs 1.3MB 18 bytes (0.0%) ⬆️
recharts/bundle-es6 1.13MB 18 bytes (0.0%) ⬆️
recharts/bundle-umd 546.68kB 3 bytes (0.0%) ⬆️
recharts/bundle-treeshaking-polar 448.01kB 18 bytes (0.0%) ⬆️
recharts/bundle-treeshaking-treemap 354.03kB 18 bytes (0.01%) ⬆️
recharts/bundle-treeshaking-cartesian 643.62kB 18 bytes (0.0%) ⬆️

Affected Assets, Files, and Routes:

view changes for bundle: recharts/bundle-treeshaking-treemap

Assets Changed:

Asset Name Size Change Total Size Change (%)
bundle.js 18 bytes 354.03kB 0.01%
view changes for bundle: recharts/bundle-es6

Assets Changed:

Asset Name Size Change Total Size Change (%)
state/selectors/axisSelectors.js 18 bytes 59.24kB 0.03%
view changes for bundle: recharts/bundle-treeshaking-cartesian

Assets Changed:

Asset Name Size Change Total Size Change (%)
bundle.js 18 bytes 643.62kB 0.0%
view changes for bundle: recharts/bundle-umd

Assets Changed:

Asset Name Size Change Total Size Change (%)
Recharts.js 3 bytes 546.68kB 0.0%
view changes for bundle: recharts/bundle-cjs

Assets Changed:

Asset Name Size Change Total Size Change (%)
state/selectors/axisSelectors.js 18 bytes 69.27kB 0.03%
view changes for bundle: recharts/bundle-treeshaking-polar

Assets Changed:

Asset Name Size Change Total Size Change (%)
bundle.js 18 bytes 448.01kB 0.0%

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 21, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.61%. Comparing base (b39edbf) to head (728bf6b).
⚠️ Report is 3 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@PavelVanecek
Copy link
Copy Markdown
Collaborator

Hi @SeaL773 how did you test this change? Do you have a repro, and screenshots before/after?

@SeaL773
Copy link
Copy Markdown
Contributor Author

SeaL773 commented Mar 22, 2026

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: stackOffset="expand" with <XAxis type="number"> renders an empty chart. The XAxis domain is incorrectly overridden to [0, 1]:

Snipaste_2026-03-22_01-00-34

After (with fix): Removing type="number" from the same StackBlitz shows the chart renders correctly, the fix ensures type="number" behaves the same way by not overriding the XAxis domain.

Tests included in this PR (test/chart/AreaChart.stacked.spec.tsx):

Test evidence (before/after):

Without fix, test fails:
✓ should render area curves when XAxis type is number (19ms)
✓ should have Y axis domain [0, 1] for expand offset (21ms)
✗ should not override X axis domain to [0, 1] (27ms)   ← FAIL

Tests: 1 failed | 37 passed (38)
With fix, all tests pass:
✓ should render area curves when XAxis type is number
✓ should have Y axis domain [0, 1] for expand offset
✓ should not override X axis domain to [0, 1]

Tests: 38 passed (38)

@PavelVanecek
Copy link
Copy Markdown
Collaborator

This is what the example looks like after the fix:

image

because numerical axis goes to [0, auto] by default

If I switch to <XAxis type="number" dataKey="ts" domain={['dataMin', 'dataMax']} /> I get:

image

Which isn't a very pretty chart but it does render.

@PavelVanecek PavelVanecek merged commit 86ca8de into recharts:main Mar 22, 2026
52 checks passed
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.

Stacked % Charts don't render for type number on X-Axis

2 participants