Skip to content

Add test for ticks spacing#7082

Merged
PavelVanecek merged 2 commits intorecharts:mainfrom
VIDHITTS:fix/xaxis-ticks-even-spacing-4271
Mar 7, 2026
Merged

Add test for ticks spacing#7082
PavelVanecek merged 2 commits intorecharts:mainfrom
VIDHITTS:fix/xaxis-ticks-even-spacing-4271

Conversation

@VIDHITTS
Copy link
Copy Markdown
Contributor

@VIDHITTS VIDHITTS commented Mar 2, 2026

Description

When XAxis has type='number' in a horizontal layout, ticks were placed at actual data positions instead of being evenly spaced across the axis range. For example, with data values [1, 2, 3, 100], ticks would cluster at positions 1, 2, 3, and 100 instead of distributing evenly at 0, 25, 50, 75, 100.

Root cause: In combineAxisTicks (axisSelectors.ts) and getTicksOfAxis (ChartUtils.ts), the categoricalDomain branch was always used for axes where isCategoricalAxis() returns true — which includes XAxis in horizontal layout. This branch places ticks at data values rather than using evenly-spaced niceTicks.

Fix: Added a guard !(type === 'number' && niceTicks) to the categoricalDomain branch in both functions. When type='number' and niceTicks are available (i.e., the scale is linear), the categoricalDomain branch is skipped, allowing tick generation to use the evenly-spaced niceTicks instead. This preserves correct behavior for:

  • Category-type axes (unchanged)
  • Time-scale axes with scale="time" (niceTicks is undefined, so categoricalDomain is still used)
  • Tooltip interaction (tooltip selector unchanged — needs data-positioned ticks)
  • Data point positioning via combineGraphicalItemTicks (unchanged)

Related Issue

Fixes #4271

Motivation and Context

Users setting type='number' on XAxis expect evenly-spaced ticks (like YAxis, which defaults to type='number'). Instead, ticks followed data positions, making charts with clustered data look incorrect. The interval='equidistantStartEnd' workaround mentioned in the issue also didn't exist (correct values are equidistantPreserveStart/equidistantPreserveEnd, which control tick filtering, not spacing).

How Has This Been Tested?

  • All 14,106 existing tests pass (287/288 test files — the 1 failure is a pre-existing Vite transform error in test/routes/index.spec.tsx also present on main)
  • Added regression test in XAxis.numberdomain.spec.tsx with clustered data [1, 2, 3, 100] verifying ticks render at [0, 25, 50, 75, 100] with even pixel spacing
  • Verified no regressions in XAxis tick, XAxis datatypes, Tooltip visibility, and Tooltip multipleDataArrays tests

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • I have added a storybook story or VR test, or extended an existing story or VR test to show my changes

Summary by CodeRabbit

  • Bug Fixes

    • Numeric axes now generate evenly spaced "nice" ticks instead of snapping to clustered data points; axis tick handling better distinguishes numeric vs categorical behavior.
  • Tests

    • New test coverage for numeric-domain axes with clustered data verifies tick spacing and computed axis domain.

When XAxis had type='number' in a horizontal layout, ticks were placed
at data positions instead of being evenly spaced. This happened because
the categoricalDomain branch in combineAxisTicks and getTicksOfAxis
was used even for number-type axes.

Fixed by skipping the categoricalDomain branch when type='number' and
niceTicks are available, ensuring ticks fall through to evenly-spaced
tick generation via niceTicks.

Added regression test with clustered data (1, 2, 3, 100) to verify
ticks are evenly spaced at (0, 25, 50, 75, 100) instead of at data
positions.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 2, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f74c02b1-fc7c-44ed-a4ed-d8ee7f547404

📥 Commits

Reviewing files that changed from the base of the PR and between edf6a15 and 49b26dc.

📒 Files selected for processing (2)
  • src/state/selectors/axisSelectors.ts
  • src/util/ChartUtils.ts
✅ Files skipped from review due to trivial changes (1)
  • src/util/ChartUtils.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/state/selectors/axisSelectors.ts

Walkthrough

Updated comments/guard notes in axis tick selection logic and added a test ensuring XAxis with numeric domain produces evenly spaced ticks; no runtime behavior changes in selectors or public APIs except the new test.

Changes

Cohort / File(s) Summary
Axis selector comments
src/state/selectors/axisSelectors.ts
Adjusted guard comments to clarify categorical-branch intent and noted that type='number' with linear/niceTicks should skip this branch. No functional changes.
Chart utils comment
src/util/ChartUtils.ts
Added explanatory comment above categorical axis branch about skipping for numeric axes when niceTicks are available. No functional changes.
Test: numeric-domain XAxis
test/cartesian/XAxis/XAxis.numberdomain.spec.tsx
Added test asserting evenly spaced XAxis ticks for clustered numeric data and verifying computed axis domain ([0,100]).

Sequence Diagram(s)

(Skipped — changes are comment/test additions and do not introduce new multi-component control flow.)

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~15 minutes

Possibly related PRs

Suggested labels

typescript

Suggested reviewers

  • ckifer
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Add test for ticks spacing' directly describes the primary change—adding a regression test for tick spacing behavior—which aligns with the changeset's main addition of the new test file.
Description check ✅ Passed The PR description provides detailed problem statement, root cause analysis, fix explanation, preservation guarantees, test coverage, and verification results. However, it omits checking the Storybook/VR test checkbox despite the template requiring it to be considered.
Linked Issues check ✅ Passed The PR addresses issue #4271 by adding a guard condition to skip the categoricalDomain branch when type='number' and niceTicks are available, ensuring evenly-spaced ticks for numeric XAxis, and includes a regression test validating the fix with clustered data.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing uneven tick spacing: comments clarify the guard logic, and the new test validates the fix. No unrelated modifications or improvements to unrelated areas are present.
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
  • Post copyable unit tests in a comment

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.

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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@test/cartesian/XAxis/XAxis.numberdomain.spec.tsx`:
- Around line 1421-1437: The new test rendering clusteredData must use fake
timers to avoid rAF/timer flakiness: before calling render(...) call
vi.useFakeTimers(), and immediately after the render (after the JSX with
<BarChart ...>, <XAxis ...>, and <Customized component={<ExpectAxisDomain .../>}
/>) invoke vi.runOnlyPendingTimers() to flush timers; ensure you restore timers
at the end of the test if needed. This targets the spec surrounding render,
<BarChart>, <XAxis>, and ExpectAxisDomain assertions.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fead0f4 and edf6a15.

📒 Files selected for processing (3)
  • src/state/selectors/axisSelectors.ts
  • src/util/ChartUtils.ts
  • test/cartesian/XAxis/XAxis.numberdomain.spec.tsx

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.60%. Comparing base (fead0f4) to head (49b26dc).
⚠️ Report is 24 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7082      +/-   ##
==========================================
- Coverage   89.80%   89.60%   -0.20%     
==========================================
  Files         529      534       +5     
  Lines       39911    40250     +339     
  Branches     5447     5477      +30     
==========================================
+ Hits        35843    36068     +225     
- Misses       4059     4174     +115     
+ Partials        9        8       -1     

☔ 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.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 2, 2026

Bundle Report

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

Detailed changes
Bundle name Size Change
recharts/bundle-cjs 1.27MB 361 bytes (0.03%) ⬆️

Affected Assets, Files, and Routes:

view changes for bundle: recharts/bundle-cjs

Assets Changed:

Asset Name Size Change Total Size Change (%)
state/selectors/axisSelectors.js 210 bytes 65.98kB 0.32%
util/ChartUtils.js 151 bytes 20.02kB 0.76%

});
});

describe('XAxis ticks should be evenly spaced with type=number and clustered data', () => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This test is passing even without the code change.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I believe #7042 fixed this upstream, before it reached the getAxisTicks function.

@PavelVanecek PavelVanecek changed the title fix: XAxis ticks not spaced evenly when type='number' (#4271) Add test for ticks spacing Mar 7, 2026
@PavelVanecek PavelVanecek merged commit 9206425 into recharts:main Mar 7, 2026
40 of 41 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.

XAxis ticks not spaced evenly

2 participants