Skip to content

Extend onZoom and onPan callbacks for syncing multiple charts #933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 10 additions & 4 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { getState, type OriginalScaleLimits, type ScaleRange, type State, type UpdatedScaleLimits } from './state.js'
import { directionEnabled, getEnabledScalesByPoint } from './utils.js'
import type { Chart, Point, Scale, UpdateMode } from 'chart.js'
import type { LimitOptions, ZoomTrigger } from './options.js'
import type { LimitOptions, PanTrigger, ZoomTrigger } from './options.js'
import type { ZoomAmount } from './types.js'

function shouldUpdateScaleLimits(
Expand Down Expand Up @@ -78,18 +78,21 @@
const xEnabled = x !== 1
const yEnabled = y !== 1
const enabledScales = getEnabledScalesByPoint(zoomOptions, focalPoint, chart)
const actualAmount = { x: 1, y: 1 }

for (const scale of enabledScales) {
if (scale.isHorizontal() && xEnabled) {
actualAmount.x = x
doZoom(scale, x, focalPoint, limits)
} else if (!scale.isHorizontal() && yEnabled) {
actualAmount.y = y
doZoom(scale, y, focalPoint, limits)
}
}

chart.update(transition)

zoomOptions?.onZoom?.({ chart, trigger })
zoomOptions?.onZoom?.({ chart, trigger, amount: actualAmount })
}

export function zoomRect(
Expand Down Expand Up @@ -207,7 +210,7 @@

type PanAmount = number | Partial<Point>

export function pan(chart: Chart, delta: PanAmount, enabledScales?: Scale[], transition: UpdateMode = 'none') {
export function pan(chart: Chart, delta: PanAmount, enabledScales?: Scale[], transition: UpdateMode = 'none', trigger: PanTrigger = "other") {

Check failure on line 213 in src/core.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

Strings must use singlequote

Check failure on line 213 in src/core.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

Replace `chart:·Chart,·delta:·PanAmount,·enabledScales?:·Scale[],·transition:·UpdateMode·=·'none',·trigger:·PanTrigger·=·"other"` with `⏎··chart:·Chart,⏎··delta:·PanAmount,⏎··enabledScales?:·Scale[],⏎··transition:·UpdateMode·=·'none',⏎··trigger:·PanTrigger·=·'other'⏎`

Check failure on line 213 in src/core.ts

View workflow job for this annotation

GitHub Actions / ci (windows-latest)

Strings must use singlequote

Check failure on line 213 in src/core.ts

View workflow job for this annotation

GitHub Actions / ci (windows-latest)

Replace `chart:·Chart,·delta:·PanAmount,·enabledScales?:·Scale[],·transition:·UpdateMode·=·'none',·trigger:·PanTrigger·=·"other"` with `␍⏎··chart:·Chart,␍⏎··delta:·PanAmount,␍⏎··enabledScales?:·Scale[],␍⏎··transition:·UpdateMode·=·'none',␍⏎··trigger:·PanTrigger·=·'other'␍⏎`
const { x = 0, y = 0 } = typeof delta === 'number' ? { x: delta, y: delta } : delta
const state = getState(chart)
const {
Expand All @@ -221,18 +224,21 @@
const yEnabled = y !== 0

const scales = enabledScales || Object.values(chart.scales)
const actualDelta = { x: 0, y: 0 }

for (const scale of scales) {
if (scale.isHorizontal() && xEnabled) {
actualDelta.x = x
panScale(scale, x, limits, state)
} else if (!scale.isHorizontal() && yEnabled) {
actualDelta.y = y
panScale(scale, y, limits, state)
}
}

chart.update(transition)

onPan?.({ chart })
onPan?.({ chart, trigger, delta: actualDelta })
}

export function getInitialScaleBounds(chart: Chart) {
Expand Down
5 changes: 3 additions & 2 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
export type ModifierKey = 'ctrl' | 'alt' | 'shift' | 'meta'
export type DrawTime = 'afterDraw' | 'afterDatasetsDraw' | 'beforeDraw' | 'beforeDatasetsDraw'
export type ZoomTrigger = 'api' | 'drag' | 'wheel' | 'pinch'
export type PanTrigger = 'api' | 'drag' | 'wheel' | 'other'

type RejectableStartEvent<T = Event | HammerInput> = (context: {
chart: Chart
Expand Down Expand Up @@ -120,7 +121,7 @@
/**
* Function called while the user is zooming
*/
onZoom?: (context: { chart: Chart; trigger: ZoomTrigger }) => void
onZoom?: (context: { chart: Chart; trigger: ZoomTrigger; amount?: { x: number, y: number } }) => void

Check failure on line 124 in src/options.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

Replace `,` with `;`

Check failure on line 124 in src/options.ts

View workflow job for this annotation

GitHub Actions / ci (windows-latest)

Replace `,` with `;`

/**
* Function called once zooming is completed
Expand Down Expand Up @@ -172,7 +173,7 @@
/**
* Function called while the user is panning
*/
onPan?: GenericEvent
onPan?: (context: { chart: Chart; trigger: PanTrigger; delta: { x: number, y: number } }) => void

Check failure on line 176 in src/options.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

Replace `,` with `;`

Check failure on line 176 in src/options.ts

View workflow job for this annotation

GitHub Actions / ci (windows-latest)

Replace `,` with `;`

/**
* Function called once panning is completed
Expand Down
2 changes: 1 addition & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
}

const bindApi = (chart: Chart) => {
chart.pan = (delta, panScales, transition) => pan(chart, delta, panScales, transition)
chart.pan = (delta, panScales, transition) => pan(chart, delta, panScales, transition, "api")

Check failure on line 53 in src/plugin.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

Strings must use singlequote

Check failure on line 53 in src/plugin.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

Replace `"api"` with `'api'`

Check failure on line 53 in src/plugin.ts

View workflow job for this annotation

GitHub Actions / ci (windows-latest)

Strings must use singlequote

Check failure on line 53 in src/plugin.ts

View workflow job for this annotation

GitHub Actions / ci (windows-latest)

Replace `"api"` with `'api'`
chart.zoom = (args, transition) => zoom(chart, args, transition)
chart.zoomRect = (p0, p1, transition) => zoomRect(chart, p0, p1, transition)
chart.zoomScale = (id, range, transition) => zoomScale(chart, id, range, transition)
Expand Down
Loading