Skip to content

Commit

Permalink
fix: numeric scale panning with dynamic limits (#915)
Browse files Browse the repository at this point in the history
  • Loading branch information
MindFreeze authored Feb 15, 2025
1 parent 679efbb commit 77ed88c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/scale.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ export function updateRange(
scale: Scale,
{ min, max }: ScaleRange,
limits?: LimitOptions,
zoom: boolean | 'pan' = false
zoom = false,
pan = false
): boolean {
const state = getState(scale.chart)
const { options: scaleOpts } = scale
Expand All @@ -143,7 +144,7 @@ export function updateRange(
const minLimit = getLimit(state, scale, scaleLimits, 'min', -Infinity)
const maxLimit = getLimit(state, scale, scaleLimits, 'max', Infinity)

if (zoom === 'pan' && (min < minLimit || max > maxLimit)) {
if (pan && (min < minLimit || max > maxLimit)) {
// At limit: No change but return true to indicate no need to store the delta.
return true
}
Expand Down Expand Up @@ -246,7 +247,7 @@ const OFFSETS: Record<TimeUnit, number> = {
year: 182 * 24 * 60 * 60 * 1000, // 182 d
}

function panNumericalScale(scale: Scale, delta: number, limits: LimitOptions, pan = false) {
function panNumericalScale(scale: Scale, delta: number, limits: LimitOptions, canZoom = false) {
const { min: prevStart, max: prevEnd } = scale
let offset = 0
if (isTimeScale(scale)) {
Expand All @@ -260,7 +261,7 @@ function panNumericalScale(scale: Scale, delta: number, limits: LimitOptions, pa
// with min === max or because the chart has 0 plottable area).
return true
}
return updateRange(scale, { min: newMin, max: newMax }, limits, pan ? 'pan' : false)
return updateRange(scale, { min: newMin, max: newMax }, limits, canZoom, true)
}

function panNonLinearScale(scale: Scale, delta: number, limits: LimitOptions) {
Expand Down
8 changes: 4 additions & 4 deletions test/specs/pan.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ describe('pan', function () {
expect(scale.min).toBe(1)
expect(scale.max).toBe(3)
chart.pan(-2000)
expect(scale.min).toBe(2)
expect(scale.max).toBe(4)
expect(scale.min).toBe(1)
expect(scale.max).toBe(3)
chart.pan(-2000)
expect(scale.min).toBe(2)
expect(scale.max).toBe(4)
expect(scale.min).toBe(1)
expect(scale.max).toBe(3)
chart.pan(50)
expect(scale.min).toBeLessThan(2)
expect(scale.max).toBe(scale.min + 2)
Expand Down

0 comments on commit 77ed88c

Please sign in to comment.