Skip to content

fix(scale): inverse data-snap handles unordered categories and null gaps (#8308) - #8332

Closed
samiarif02 wants to merge 1 commit into
recharts:mainfrom
samiarif02:fix/inverse-data-snap-unordered-null
Closed

samiarif02 wants to merge 1 commit into
recharts:mainfrom
samiarif02:fix/inverse-data-snap-unordered-null

Conversation

@samiarif02

@samiarif02 samiarif02 commented Nov 28, 2026 •

Copy link
Copy Markdown

What is the current behavior?

createCategoricalInverse (used by useXAxisInverseDataSnapScale / useYAxisInverseDataSnapScale) maps each sorted data value to its pixel position and feeds those pixels to bisect, which assumes a monotonic array. The data order is not the pixel order, so the inverse returns the wrong point in two common cases (#8308):

  1. A categorical axis whose numeric values are not in ascending order (e.g. years listed newest first). With years [2024, 2022, 2023, 2021] the pixel of 2022 returned 2023.
  2. A series with a null gap. null has no real position — its scaled value is NaN, or on a continuous scale a coerced pixel that belongs to no data point — so it displaced the nearest real point (the pixel of 10 snapped to 30, the pixel of 30 snapped to null).

What is the expected behavior?

The inverse returns the data point whose pixel is nearest the given coordinate, and never a null/undefined gap.

Fix

Pair each value with its pixel position, drop values that have no finite position (null/undefined gaps), and sort the pairs by pixel. The pixel array bisect sees is then genuinely monotonic, and the nearest-neighbour lookup returns the correct point. This is self-contained in createCategoricalInverse; bisect is unchanged.

Tests

Added two cases to test/util/scale/createCategoricalInverse.spec.ts (non-ascending numeric categories, and a null gap on a categorical axis). Both fail on main (expected 2023 to be 2022, expected null to be 10) and pass with the fix. The scale/hooks suites (test/util/scale, test/hooks/useAxisInverseBandScale) stay green (137 tests), tsc is clean and eslint passes.

Summary by CodeRabbit

  • Bug Fixes
    • Categorical scale lookups now return the nearest category based on its actual position, including when categories are unordered or data contains gaps.

createCategoricalInverse mapped each sorted data value to its pixel
position and fed those pixels to bisect, which assumes a monotonic
array. The data order is not the pixel order, so two cases returned the
wrong point:

- a categorical axis whose numeric values are not ascending (e.g. years
  newest first): the pixel of one category snapped to its neighbour;
- a series with a null gap: null has no real position (NaN, or a coerced
  pixel on a continuous scale), so it displaced the nearest real point.

Pair each value with its pixel, drop values with no finite position
(null/undefined gaps), and sort by pixel so the array bisect sees is
monotonic. Nearest-neighbour lookup then returns the correct point.

Fixes recharts#8308
@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

createCategoricalInverse now sorts valid category-position pairs by pixel before lookup. Regression tests cover unordered numeric categories and null gaps.

Changes

Categorical inverse lookup

Layer / File(s) Summary
Pixel-ordered inverse lookup
src/util/scale/createCategoricalInverse.ts, test/util/scale/createCategoricalInverse.spec.ts
The helper filters nullish values and non-finite positions, sorts the remaining pairs by pixel, and performs nearest-neighbor lookup on those pairs. Tests cover unordered categories and null gaps.

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

Change: Bug fix

Possibly related issues

  • Issue 8308 reports incorrect inverse data-snap results for non-ascending categories and null gaps. This change updates createCategoricalInverse for those cases.

Possibly related PRs

  • recharts/recharts#8309: This PR makes an overlapping change to createCategoricalInverse for unordered categories and null gaps.

Suggested reviewers: pavelvanecek, 13sharad

Merge Risk: Minimal · up to 38cfa

This change makes inverse lookup on categorical scales return the nearest data point for unordered categories and skip null gaps. No actionable merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the fix (inverse data-snap handling of unordered categories and null gaps) in the scale code, which matches the changeset. It is specific and states the primary change. It also include…
Description check Passed The description follows the template. It has a current behavior section, expected behavior, a fix section, and a tests section with specific results. It links issue #8308 in the body, which covers the…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the fix (inverse data-snap handling of unordered categories and null gaps) in the scale code, which matches the changeset. It is specific and states the primary change. It also includes the issue number, which is acceptable.

Full details: Description check

Explanation

The description follows the template. It has a current behavior section, expected behavior, a fix section, and a tests section with specific results. It links issue #8308 in the body, which covers the Related Issue section. The Motivation and Context content is present, though under different headings. The Screenshots section is absent, which is acceptable for a non-visual fix. The Types of changes checkbox and the Checklist are not filled in, and the Related Issue link is not in the template's placeholder format.

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: 932fccde-2f3e-4f4a-b37c-b299fea03073
Commits

Reviewing files that changed from the base of the PR and between 60ae30b and 38cfacc.

Files selected for processing: 2

  • src/util/scale/createCategoricalInverse.ts
  • 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.

@PavelVanecek

Copy link
Copy Markdown
Collaborator

Duplicate of #8309 ?

@codecov

codecov Bot commented Nov 28, 2026

Copy link
Copy Markdown

Bundle Report

Changes will increase total bundle size by 3.68kB (0.05%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
recharts/bundle-cjs 1.88MB 878 bytes (0.05%) ⬆️
recharts/bundle-es6 1.68MB 878 bytes (0.05%) ⬆️
recharts/bundle-umd 688.24kB 166 bytes (0.02%) ⬆️
recharts/bundle-treeshaking-cartesian 946.0kB 878 bytes (0.09%) ⬆️
recharts/bundle-treeshaking-polar 658.32kB 878 bytes (0.13%) ⬆️

Affected Assets, Files, and Routes:

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

Assets Changed:

Asset Name Size Change Total Size Change (%)
bundle.js 878 bytes 946.0kB 0.09%
view changes for bundle: recharts/bundle-treeshaking-polar

Assets Changed:

Asset Name Size Change Total Size Change (%)
bundle.js 878 bytes 658.32kB 0.13%
view changes for bundle: recharts/bundle-es6

Assets Changed:

Asset Name Size Change Total Size Change (%)
util/scale/createCategoricalInverse.js 878 bytes 3.58kB 32.54% ⚠️
view changes for bundle: recharts/bundle-umd

Assets Changed:

Asset Name Size Change Total Size Change (%)
Recharts.js 166 bytes 688.24kB 0.02%
view changes for bundle: recharts/bundle-cjs

Assets Changed:

Asset Name Size Change Total Size Change (%)
util/scale/createCategoricalInverse.js 878 bytes 3.73kB 30.81% ⚠️

@codecov

codecov Bot commented Nov 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.79%. Comparing base (60ae30b) to head (38cfacc).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #8332   +/-   ##
=======================================
  Coverage   89.79%   89.79%           
=======================================
  Files         691      691           
  Lines       17965    17972    +7     
  Branches     4872     4874    +2     
=======================================
+ Hits        16131    16138    +7     
  Misses       1630     1630           
  Partials      204      204           

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

@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
38cfacc ✅ 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.

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.

2 participants