Skip to content

fix(hooks): order data-snap points by pixel and skip values the scale cannot place - #8309

Merged
PavelVanecek merged 2 commits into
recharts:mainfrom
sagarrsharmaa:fix/data-snap-pixel-order
Nov 29, 2026
Merged

PavelVanecek merged 2 commits into
recharts:mainfrom
sagarrsharmaa:fix/data-snap-pixel-order

Conversation

@sagarrsharmaa

@sagarrsharmaa sagarrsharmaa commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Description

createCategoricalInverse now pairs each value with its pixel position, skips values the scale can't place (null, or a category outside the domain), and sorts by pixel before the binary search. Before, it searched values that were pre-sorted by number, and gave unplaceable values pixel 0.

Related Issue

Fixes #8308

Motivation and Context

useXAxisInverseDataSnapScale / useYAxisInverseDataSnapScale returned the wrong data point when:

  • a series has gaps (null): on a YAxis with [10, null, 20, 30], the pixel of 10 snapped to 30, 20 to 10, and 30 to null;
  • a categorical axis has number-like values that aren't ascending (e.g. years newest first): the pixel of 2022 snapped to 2023.

selectSortedDataPoints sorts numerically and keeps nullish values, but a categorical axis keeps domain order and null got pixel 0, so the bisect saw an unsorted list. The plain inverse hooks on categorical axes use the same helper; their band order was already sorted, so their results don't change.

This touches the same lines as #8307 (band centre offset), so whichever lands second will need a small rebase.

How Has This Been Tested?

  • New unit tests in test/util/scale/createCategoricalInverse.spec.ts and hook tests in test/hooks/useAxisScale.spec.tsx. All 5 fail on main and pass with the fix.
  • npm run test -- test/util test/hooks test/state: 1885 passed.
  • check-types-lib, check-types-test, eslint and prettier pass on the changed files.

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
    • Category-axis snapping now selects the closest category based on its displayed position, even when category order differs.
    • Unplaceable categories are skipped, and snapping returns no result when no category can be placed.
    • Axis data snapping handles non-ascending category order and avoids selecting points with null values, choosing the closest available non-null value instead.

… cannot place

createCategoricalInverse binary-searches pixel positions, which only
works when they're in order. The data-snap selector feeds it values from
selectSortedDataPoints, which sorts by numeric value and keeps nullish
values. On a categorical axis the numeric order isn't the on-screen
order, and null was mapped to pixel 0, so the search ran over an
unsorted list: years listed newest first snapped to the wrong year, and
a Line with a gap snapped to the wrong value or to null.

Pair each value with its pixel, drop values the scale can't place, and
sort by pixel before searching.

Fixes recharts#8308
@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The categorical inverse scale now sorts valid scale positions before bisecting. It skips values without valid numeric positions and selects or clamps to the closest placed value. Tests cover unordered categories and null-valued data.

Changes

Categorical inverse snapping

Layer / File(s) Summary
Order and select placeable values
src/util/scale/createCategoricalInverse.ts
The inverse helper filters out scale results that are not well-behaved numbers, sorts the remaining values by pixel position, and bisects that ordered list. It clamps to the first or last placed value and returns undefined when a neighbor is absent.
Verify helper and hook behavior
test/util/scale/createCategoricalInverse.spec.ts, test/hooks/useAxisScale.spec.tsx
Tests cover unplaceable values, categories whose domain order differs from pixel order, the case where no values can be placed, and X- and Y-axis inverse data snapping.

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • recharts/recharts#8332: This PR targets the same issue and helper, with overlapping changes to pair values with pixel positions, skip unplaceable entries, and sort before bisecting.
  • recharts/recharts#8307: This PR also changes pixel positions in createCategoricalInverse, adding a half-bandwidth offset before nearest-neighbor search.

Suggested reviewers: pavelvanecek

Merge Risk: Minimal · up to 5c5e1

This change fixes snapping on categorical axes when categories are not in ascending order or when data contains null gaps. No actionable merge risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the two main changes: data-snap points are ordered by pixel, and values the scale cannot place are skipped. Both match the changeset, which sorts retained values by pixel in createCate…
Description check Passed The description follows the template and covers the required sections. It explains the change and the root cause, which is the unsorted lookup and the pixel-0 mapping for unplaceable values. It links …
Linked Issues check Passed Issue #8308 requires the data-snap inverse to return the data point closest to a pixel in on-screen order, and never to snap to a value the scale cannot place (such as null). The whole-PR diff of src/…
Out of Scope Changes check Passed The whole-PR changes are limited to src/util/scale/createCategoricalInverse.ts (pixel pairing, sorting by pixel, skipping unplaceable values, null-guarding neighbours, importing isWellBehavedNumber) a…
Full details: Title check

Explanation

The title names the two main changes: data-snap points are ordered by pixel, and values the scale cannot place are skipped. Both match the changeset, which sorts retained values by pixel in createCategoricalInverse and excludes null or out-of-domain values. The title is specific and concise, and it uses the conventional fix(scope) prefix.

Full details: Description check

Explanation

The description follows the template and covers the required sections. It explains the change and the root cause, which is the unsorted lookup and the pixel-0 mapping for unplaceable values. It links the issue (Fixes #8308) and gives motivation with concrete examples. It describes the testing done, marks the bug fix type, and checks the tests box. Some template items are not filled in: the storybook/VR and documentation boxes are unchecked, and no screenshots section is included. The description is mostly complete, so the check passes.

Full details: Linked Issues check

Explanation

Issue #8308 requires the data-snap inverse to return the data point closest to a pixel in on-screen order, and never to snap to a value the scale cannot place (such as null). The whole-PR diff of src/util/scale/createCategoricalInverse.ts at the reviewed head meets both points. (1) Each domain value is paired with its pixel (scale(value) plus the existing band offset), and the pairs are sorted by pixel before bisect. The search therefore runs over ascending on-screen positions even when the domain is non-ascending (e.g. 2024, 2022, 2023, 2021). (2) Values whose scale result fails isWellBehavedNumber are skipped, so null gaps never enter the search and are never mapped to pixel 0. Clamp paths return the first or last placed value, and the nearest-neighbour branch returns the value for the nearer pixel. The PR adds unit tests in test/util/scale/createCategoricalInverse.spec.ts and hook tests in test/hooks/useAxisScale.spec.tsx for both reported cases. Tests were not re-run in this review. The hook files are unchanged, so hook behaviour depends on this helper.

Full details: Out of Scope Changes check

Explanation

The whole-PR changes are limited to src/util/scale/createCategoricalInverse.ts (pixel pairing, sorting by pixel, skipping unplaceable values, null-guarding neighbours, importing isWellBehavedNumber) and to two test files that cover this helper and the two hooks named in #8308. The band offset line appears as unchanged context in the diff, so it is existing behaviour and not a new change from this PR. Each changed line serves #8308. The PR description notes that the plain categorical inverse hooks share this helper and keep their results because their band order was already sorted. No unrelated change was found. The #8307 overlap is a rebase concern, not a scope issue. The incremental diff from the previous reviewed head could not be read, so this verdict rests on the whole-PR diff.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b0799491-235e-4445-945b-80525ec6474b
Commits

Reviewing files that changed from the base of the PR and between 1e7e8cf and 5c5e10e.

Files selected for processing: 3

  • src/util/scale/createCategoricalInverse.ts
  • test/hooks/useAxisScale.spec.tsx
  • test/util/scale/createCategoricalInverse.spec.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@flakiness-io

flakiness-io Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Test Results Summary

This comment shows the latest test results for this pull request.

Commit Status Test Results CI Runs
5c5e10e ✅ Passed 3984 passed · 30 skipped 9 runs

View full report

ℹ️ About Flakiness.io in GitHub

Your team has set up Flakiness.io pull request comments. This comment is updated when

  • The first test results arrive for this pull request
  • A later run reports more results for the same commit
  • A new commit is pushed and tested

Flakiness.io rewrites the comment in place, so it always reflects the most recently tested commit.

@codecov

codecov Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Bundle Report

Changes will decrease total bundle size by 2.55kB (-0.03%) ⬇️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
recharts/bundle-cjs 1.87MB 653 bytes (0.03%) ⬆️
recharts/bundle-es6 1.68MB 628 bytes (0.04%) ⬆️
recharts/bundle-umd 688.2kB 128 bytes (0.02%) ⬆️
recharts/bundle-treeshaking-cartesian 945.68kB 565 bytes (0.06%) ⬆️
recharts/bundle-treeshaking-polar 658.01kB 565 bytes (0.09%) ⬆️
recharts/bundle-treeshaking-treemap 576.18kB 565 bytes (0.1%) ⬆️
recharts/bundle-treeshaking-sunburst 514.49kB 565 bytes (0.11%) ⬆️
recharts/bundle-treeshaking-sankey* 567.27kB -6.22kB (-1.08%) ⬇️

ℹ️ *Bundle size includes cached data from a previous commit

Affected Assets, Files, and Routes:

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

Assets Changed:

Asset Name Size Change Total Size Change (%)
bundle.js 565 bytes 576.18kB 0.1%
view changes for bundle: recharts/bundle-treeshaking-cartesian

Assets Changed:

Asset Name Size Change Total Size Change (%)
bundle.js 565 bytes 945.68kB 0.06%
view changes for bundle: recharts/bundle-treeshaking-polar

Assets Changed:

Asset Name Size Change Total Size Change (%)
bundle.js 565 bytes 658.01kB 0.09%
view changes for bundle: recharts/bundle-cjs

Assets Changed:

Asset Name Size Change Total Size Change (%)
util/scale/createCategoricalInverse.js 653 bytes 3.5kB 22.91% ⚠️
view changes for bundle: recharts/bundle-treeshaking-sankey

Assets Changed:

Asset Name Size Change Total Size Change (%)
bundle.js -6.22kB 567.27kB -1.08%
view changes for bundle: recharts/bundle-umd

Assets Changed:

Asset Name Size Change Total Size Change (%)
Recharts.js 128 bytes 688.2kB 0.02%
view changes for bundle: recharts/bundle-treeshaking-sunburst

Assets Changed:

Asset Name Size Change Total Size Change (%)
bundle.js 565 bytes 514.49kB 0.11%
view changes for bundle: recharts/bundle-es6

Assets Changed:

Asset Name Size Change Total Size Change (%)
util/scale/createCategoricalInverse.js 628 bytes 3.33kB 23.28% ⚠️

@codecov

codecov Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.11765% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 89.81%. Comparing base (9325530) to head (5c5e10e).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/util/scale/createCategoricalInverse.ts 94.11% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8309      +/-   ##
==========================================
+ Coverage   89.79%   89.81%   +0.02%     
==========================================
  Files         691      691              
  Lines       17965    17973       +8     
  Branches     4872     4867       -5     
==========================================
+ Hits        16131    16143      +12     
+ Misses       1630     1628       -2     
+ Partials      204      202       -2     

☔ View full report in Codecov by Harness.
📢 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.

Conflicts in src/util/scale/createCategoricalInverse.ts and its spec with
the band-centre change on main: keep main's half-bandwidth offset and add
it to each placed pixel before sorting.
@PavelVanecek
PavelVanecek merged commit 10c385e into recharts:main Nov 29, 2026
64 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.

Inverse data-snap hooks return the wrong point for null values and non-ascending categories

2 participants