Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/guide/developers.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Returns the current zoom level. If this is the same as the chart's initial scal

If the chart has been panned but not zoomed, this method will still return `1.0`.

Only the scales that have been zoomed count. A scale that shows less than its full range because it fits the data another scale's zoom leaves in view does not.

### `chart.getInitialScaleBounds(): Record<string, {min: number | undefined, max: number | undefined}>`

Returns the bounds each scale would have without any zooming or panning. For a scale that derives its range from the data, they follow the data when it changes. This is returned in the format of an object, e.g.
Expand Down
106 changes: 68 additions & 38 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ function shouldUpdateScaleLimits(
updatedScaleLimits: UpdatedScaleLimits
) {
const { id } = scale
if (!originalScaleLimits[id] || !updatedScaleLimits[id]) {
if (!originalScaleLimits[id]) {
return true
}
return !isPinned(scale, updatedScaleLimits[id])
// What is stored for a scale that is not zoomed stays: its current range may
// be fitted to the data another zoomed scale leaves in view, and is then not
// the range to go back to.
return !!updatedScaleLimits[id] && !isPinned(scale, updatedScaleLimits[id])
}

function removeMissingScales(limits: OriginalScaleLimits | UpdatedScaleLimits, scales: Record<string, Scale>) {
Expand Down Expand Up @@ -77,20 +80,55 @@ function setUserBounds(scale: Scale, min: unknown, max: unknown) {
init.call(scale, scale.options, scale.chart.options)
}

const hasBothBounds = (min: unknown, max: unknown) => !isNullOrUndef(min) && !isNullOrUndef(max)

// A scale that is not zoomed has the options the application gave it. If it
// derives a bound from the data, it fits the data the zoomed scales leave in
// view, so its original range needs computing. With both bounds in the options
// there is nothing to compute, and its range is stored when next needed.
function resetOriginalToOptions(scale: Scale, originalScaleLimits: OriginalScaleLimits) {
const { min, max } = scale.options
if (hasBothBounds(min, max)) {
delete originalScaleLimits[scale.id]
} else {
originalScaleLimits[scale.id] = { min: { options: min }, max: { options: max } }
}
}

function computeOriginalRanges(state: State, pinned: Scale[], stale: Scale[]) {
const { originalScaleLimits, updatedScaleLimits } = state

for (const scale of pinned) {
const { min, max } = originalScaleLimits[scale.id]
setUserBounds(scale, min.options, max.options)
}
for (const scale of stale) {
const original = originalScaleLimits[scale.id]
scale.beforeLayout?.()
scale.update(scale.maxWidth, scale.maxHeight)
original.min.scale = scale.min
original.max.scale = scale.max
}
for (const scale of pinned) {
const { min, max } = updatedScaleLimits[scale.id]
setUserBounds(scale, min, max)
}
}

/**
* A zoomed scale has its range pinned through its `min` and `max` options, so
* it no longer follows the data. The range it would have without the zoom is
* still what the `'original'` limits, the zoom level and a reset refer to, and
* that range changes when the data does.
*
* This runs right before a layout: it lets the zoomed scales compute their
* range once with the options they had before the zoom, and keeps the result.
* The layout that follows computes the scales again with the zoomed range.
* This runs right before a layout: it lets the scales compute their range once
* with the options they had before the zoom, and keeps the result. The layout
* that follows computes the scales again with the zoomed range.
*
* A bound the options gave before the zoom does not depend on the data, so a
* scale with both of them has nothing to compute. Its zoom is still lifted for
* the others, because a scale only looks at the data inside the bounds of the
* scale across it.
* A bound the options gave does not depend on the data, so a scale with both
* of them has nothing to compute. Its zoom is still lifted for the others,
* because a scale only looks at the data inside the bounds of the scale across
* it. For the same reason a scale that is not zoomed is computed as well.
*/
export function refreshOriginalScaleLimits(chart: Chart) {
const state = getState(chart)
Expand All @@ -99,40 +137,30 @@ export function refreshOriginalScaleLimits(chart: Chart) {
}

const { originalScaleLimits, updatedScaleLimits } = state
const zoomed: Scale[] = []
for (const scale of Object.values(chart.scales)) {
if (updatedScaleLimits[scale.id] && originalScaleLimits[scale.id]) {
zoomed.push(scale)
} else {
// A scale that is not zoomed shows its original range. A copy of it
// would only go stale, and is stored again when it is next needed.
delete originalScaleLimits[scale.id]
}
}

const pinned = zoomed.filter((scale) => isPinned(scale, updatedScaleLimits[scale.id]))
const stale = pinned.filter((scale) => {
const { min, max } = originalScaleLimits[scale.id]
return isNullOrUndef(min.options) || isNullOrUndef(max.options)
const scales = Object.values(chart.scales)
const pinned = scales.filter((scale) => {
const zoomed = updatedScaleLimits[scale.id]
return originalScaleLimits[scale.id] && zoomed && isPinned(scale, zoomed)
})
if (!stale.length) {
if (!pinned.length) {
// Every scale shows its original range. A copy of it would only go stale,
// and is stored again when it is next needed.
state.originalScaleLimits = {}
return
}

for (const scale of pinned) {
const { min, max } = originalScaleLimits[scale.id]
setUserBounds(scale, min.options, max.options)
for (const scale of scales) {
if (!pinned.includes(scale)) {
resetOriginalToOptions(scale, originalScaleLimits)
}
}
for (const scale of stale) {

const stale = scales.filter((scale) => {
const original = originalScaleLimits[scale.id]
scale.beforeLayout?.()
scale.update(scale.maxWidth, scale.maxHeight)
original.min.scale = scale.min
original.max.scale = scale.max
}
for (const scale of pinned) {
const { min, max } = updatedScaleLimits[scale.id]
setUserBounds(scale, min, max)
return original && !hasBothBounds(original.min.options, original.max.options)
})
if (stale.length) {
computeOriginalRanges(state, pinned, stale)
}
}

Expand Down Expand Up @@ -266,7 +294,9 @@ export function getZoomLevel(chart: Chart) {
let min = 1
let max = 1
for (const scale of Object.values(chart.scales)) {
const origRange = getOriginalRange(state, scale.id)
// Only the scales that are zoomed count. Another one may show less than
// its original range because it fits the data the zoom leaves in view.
const origRange = state.updatedScaleLimits[scale.id] && getOriginalRange(state, scale.id)
if (origRange) {
const level = Math.round((origRange / (scale.max - scale.min)) * 100) / 100
min = Math.min(min, level)
Expand Down
69 changes: 69 additions & 0 deletions test/specs/original.limits.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,75 @@ describe('original scale limits', function () {
expect(initial.y.max).toBe(30)
})

describe('of a scale that is not zoomed', function () {
// y has no bounds of its own, so it fits the data the x zoom leaves in view.
function zoomXThenY() {
const chart = createChart({
zoom: { mode: 'xy' },
limits: { y: { min: 'original', max: 'original' } },
})
chart.zoomScale('x', { min: 10, max: 20 })
expect(chart.scales.y.max).toBe(2)
chart.zoomScale('y', { min: 1.2, max: 1.8 })
return chart
}

it('should not be the range the zoom of another scale left it with', function () {
const chart = zoomXThenY()

expect(chart.getInitialScaleBounds().y).toEqual({ min: 0, max: 10 })
})

it('should let the scale be zoomed back out to the range it started with', function () {
const chart = zoomXThenY()
chart.zoomScale('x', { min: 0, max: 100 })

for (let i = 0; i < 8; i++) {
chart.zoom({ y: 0.1 })
}

expect(chart.scales.y.min).toBe(0)
expect(chart.scales.y.max).toBe(10)
})

it('should follow the data', function () {
const chart = createChart({ zoom: { mode: 'x' } })
chart.zoomScale('x', { min: 10, max: 20 })
setData(chart, 0, 300)

const initial = chart.getInitialScaleBounds()
chart.resetZoom('none')

expect(initial.y).toEqual({ min: 0, max: 30 })
expect(initial).toEqual(bounds(chart))
})

it('should follow the bounds the application gives the scale', function () {
const chart = createChart({ zoom: { mode: 'x' } })
chart.zoomScale('x', { min: 10, max: 20 })
chart.options.scales.y.min = -5
chart.update()

expect(chart.getInitialScaleBounds().y).toEqual({ min: -5, max: 10 })

chart.options.scales.y.max = 50
chart.update()

expect(chart.getInitialScaleBounds().y).toEqual({ min: -5, max: 50 })
})

it('should not count towards the zoom level', function () {
const chart = createChart({ zoom: { mode: 'x' } })
chart.data.datasets[0].data = points(0, 100).map(({ x }) => ({ x, y: (x / 10) ** 2 }))
chart.update()
chart.zoomScale('x', { min: 0, max: 50 })

// y shows a quarter of its 0..100, without having been zoomed.
expect(chart.scales.y.max).toBe(25)
expect(chart.getZoomLevel()).toBe(2)
})
})

it('should keep a bound the scale options gave', function () {
const chart = createChart({ zoom: { mode: 'x' } }, { x: { min: 10 } })
chart.zoom({ x: 1.5 })
Expand Down