Skip to content

fix: keep the original range of a scale another scale's zoom narrowed - #1010

Draft
kurkle wants to merge 4 commits into
chartjs:masterfrom
kurkle:fix/original-range-of-unzoomed-scale
Draft

kurkle wants to merge 4 commits into
chartjs:masterfrom
kurkle:fix/original-range-of-unzoomed-scale

Conversation

@kurkle

@kurkle kurkle commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Builds on #1008 and contains its commits until that is merged; the change here is the last commit.

A scale without bounds of its own fits the data that is in view, so zooming x narrows y. The original range of a scale that was not zoomed was stored again at every zoom and pan, from whatever the scale showed at that moment. Zooming y after x therefore took the narrowed range as y's original (#798):

  1. Data with y from 0 to 100; zoom x so that y fits 0 to 50.
  2. Zoom y.
  3. Zoom x all the way out.
  4. With limits: { y: { min: 'original', max: 'original' } }, y only zooms out to 0..50.

What changes

  • The stored range of a scale that is not zoomed is kept instead of being overwritten. It is taken when nothing is zoomed yet, so it is the range the scale really started with.
  • The refresh before a layout (from fix: keep the original scale limits in step with the data #1008) computes it with all zooms lifted, like the range of a zoomed scale, so it follows the data and the bounds the application sets on the scale.
  • getZoomLevel() only counts the scales that are zoomed. A scale that merely fits what another scale's zoom leaves in view used to enter the result with a ratio against its range one step earlier, which was not a meaningful number; with the range now kept, it would report the fitting as zoom.

Cost

The refresh now also computes the scales that are not zoomed, when another scale is. A chart zoomed along x only therefore pays for a pass over the data of the y scale on each update that comes from the application, as a chart zoomed along both axes already did in #1008. Pan and zoom steps are not affected.

Median time of one call in milliseconds, measured the same way as in #1008 (headless Chromium, line chart without animation, timer resolution 0.1 ms), master against this branch:

Scale Zoomed Points chart.update() master → PR One pan step master → PR
linear x 10 000 0.9 → 1.3 1.0 → 1.0
linear x 100 000 4.9 → 7.4 6.2 → 6.2
linear xy 10 000 0.7 → 1.1 0.8 → 0.8
linear xy 100 000 3.2 → 6.1 4.4 → 4.4
time x 10 000 1.4 → 2.3 1.5 → 1.6
time x 100 000 5.8 → 8.6 6.9 → 7.2
time xy 10 000 1.1 → 1.8 1.1 → 1.1
time xy 100 000 3.5 → 6.5 5.1 → 5.1

Fixes #798

A zoomed scale has its range pinned through its min and max options, so
it stops following the data. The range it would have without the zoom
was stored once, at the first zoom or pan, and never looked at again.
When the data changed while the chart was zoomed:

- the 'original' limits kept the old range, so new data could not be
  reached by panning or zooming out,
- getZoomLevel() and getInitialScaleBounds() described the old data.

Before each layout that the plugin did not start itself, the zoomed
scales now compute their range once more with the options they had
before the zoom. Bounds given in the scale options are kept; only a
bound the scale derives from the data is refreshed. The stored range of
a scale that is not zoomed is dropped instead, since it is the range
the scale already shows.

The limits can now move away from under the current view, so a pan that
brings the view back towards a limit is no longer refused; one that
moves further past it still is.
The chart passes its options along when it initialises a scale, and a
time scale takes `normalized` and its adapter's options from them.
Initialising the scale without them left a zoomed time scale without
`normalized` after an update.

The check for bounds the application set on a zoomed scale is now one
function, used both when the original limits are stored and when they
are refreshed.
A scale without bounds of its own fits the data that is in view, so
zooming x narrows y. The original range of a scale that was not zoomed
was stored again at every zoom and pan, from whatever the scale showed
at that moment. Zooming y after x therefore took the narrowed range as
y's original: with 'original' limits y could never be zoomed back out
to where it started, also after x was.

The stored range of a scale that is not zoomed is now kept. It is taken
when nothing is zoomed yet, and the refresh before a layout computes it
with all zooms lifted, like the range of a zoomed scale.

getZoomLevel() only counts the scales that are zoomed. A scale that
merely fits what another scale's zoom leaves in view used to enter the
result with a ratio against its range one step earlier.
The fitted y scale showed half of its range while x was zoomed to half,
so the level was 2 whether or not y was counted. y now shows a quarter.

This branch has not been deployed

No deployments
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.

Zooming in over axis does not reset y-axis ticks back to initial limits

1 participant