Skip to content

Commit d38ee88

Browse files
Return trigger, delta and amount in onZoom and onPan callbacks.
1 parent 137d924 commit d38ee88

3 files changed

Lines changed: 12 additions & 7 deletions

File tree

src/core.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { panFunctions, updateRange, zoomFunctions, zoomRectFunctions } from './s
33
import { getState, type OriginalScaleLimits, type ScaleRange, type State, type UpdatedScaleLimits } from './state.js'
44
import { directionEnabled, getEnabledScalesByPoint } from './utils.js'
55
import type { Chart, Point, Scale, UpdateMode } from 'chart.js'
6-
import type { LimitOptions, ZoomTrigger } from './options.js'
6+
import type { LimitOptions, PanTrigger, ZoomTrigger } from './options.js'
77
import type { ZoomAmount } from './types.js'
88

99
function shouldUpdateScaleLimits(
@@ -78,18 +78,21 @@ export function zoom(chart: Chart, amount: ZoomAmount, transition: UpdateMode =
7878
const xEnabled = x !== 1
7979
const yEnabled = y !== 1
8080
const enabledScales = getEnabledScalesByPoint(zoomOptions, focalPoint, chart)
81+
const actualAmount = { x: 1, y: 1 }
8182

8283
for (const scale of enabledScales) {
8384
if (scale.isHorizontal() && xEnabled) {
85+
actualAmount.x = x
8486
doZoom(scale, x, focalPoint, limits)
8587
} else if (!scale.isHorizontal() && yEnabled) {
88+
actualAmount.y = y
8689
doZoom(scale, y, focalPoint, limits)
8790
}
8891
}
8992

9093
chart.update(transition)
9194

92-
zoomOptions?.onZoom?.({ chart, trigger })
95+
zoomOptions?.onZoom?.({ chart, trigger, amount: actualAmount })
9396
}
9497

9598
export function zoomRect(
@@ -207,7 +210,7 @@ function panScale(scale: Scale, delta: number, limits: LimitOptions, state: Stat
207210

208211
type PanAmount = number | Partial<Point>
209212

210-
export function pan(chart: Chart, delta: PanAmount, enabledScales?: Scale[], transition: UpdateMode = 'none') {
213+
export function pan(chart: Chart, delta: PanAmount, enabledScales?: Scale[], transition: UpdateMode = 'none', trigger: PanTrigger = "other") {
211214
const { x = 0, y = 0 } = typeof delta === 'number' ? { x: delta, y: delta } : delta
212215
const state = getState(chart)
213216
const {
@@ -232,7 +235,7 @@ export function pan(chart: Chart, delta: PanAmount, enabledScales?: Scale[], tra
232235

233236
chart.update(transition)
234237

235-
onPan?.({ chart })
238+
onPan?.({ chart, trigger, delta })
236239
}
237240

238241
export function getInitialScaleBounds(chart: Chart) {

src/options.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import type { Chart, Color, Point } from 'chart.js'
2+
import type { PanAmount, ZoomAmount } from './types'
23

34
export type Mode = 'x' | 'y' | 'xy'
45
export type ModeFn = (context: { chart: Chart }) => Mode
56
export type ModeOption = Mode | ModeFn
67
export type ModifierKey = 'ctrl' | 'alt' | 'shift' | 'meta'
78
export type DrawTime = 'afterDraw' | 'afterDatasetsDraw' | 'beforeDraw' | 'beforeDatasetsDraw'
89
export type ZoomTrigger = 'api' | 'drag' | 'wheel' | 'pinch'
10+
export type PanTrigger = 'api' | 'drag' | 'wheel' | 'other'
911

1012
type RejectableStartEvent<T = Event | HammerInput> = (context: {
1113
chart: Chart
@@ -120,7 +122,7 @@ export interface ZoomOptions {
120122
/**
121123
* Function called while the user is zooming
122124
*/
123-
onZoom?: (context: { chart: Chart; trigger: ZoomTrigger }) => void
125+
onZoom?: (context: { chart: Chart; trigger: ZoomTrigger; amount?: ZoomAmount }) => void
124126

125127
/**
126128
* Function called once zooming is completed
@@ -172,7 +174,7 @@ export interface PanOptions {
172174
/**
173175
* Function called while the user is panning
174176
*/
175-
onPan?: GenericEvent
177+
onPan?: (context: { chart: Chart; trigger: PanTrigger; delta: PanAmount }) => void
176178

177179
/**
178180
* Function called once panning is completed

src/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function draw(chart: Chart, caller: string, options: ZoomPluginOptions) {
5050
}
5151

5252
const bindApi = (chart: Chart) => {
53-
chart.pan = (delta, panScales, transition) => pan(chart, delta, panScales, transition)
53+
chart.pan = (delta, panScales, transition) => pan(chart, delta, panScales, transition, "api")
5454
chart.zoom = (args, transition) => zoom(chart, args, transition)
5555
chart.zoomRect = (p0, p1, transition) => zoomRect(chart, p0, p1, transition)
5656
chart.zoomScale = (id, range, transition) => zoomScale(chart, id, range, transition)

0 commit comments

Comments
 (0)