Skip to content

Commit

Permalink
Add isZoomingOrPanning method
Browse files Browse the repository at this point in the history
  • Loading branch information
joshkel committed Jul 29, 2024
1 parent bfd207d commit 3ca579f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 4 deletions.
4 changes: 4 additions & 0 deletions docs/guide/developers.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ Returns the initial scale bounds of each scale before any zooming or panning too

Returns whether the chart has been zoomed or panned - i.e. whether the initial scale of any axis is different to the one used currently.

### `chart.isZoomingOrPanning(): boolean`

Returns whether the user is currently in the middle of a drag operation or pan operation.

## Custom Scales

You can extend chartjs-plugin-zoom with support for [custom scales](https://www.chartjs.org/docs/latest/developers/axes.html) by using the zoom plugin's `zoomFunctions`, `zoomRectFunctions`, and `panFunctions` members. These objects are indexed by scale types (scales' `id` members) and give optional handlers for zoom and pan functionality.
Expand Down
5 changes: 5 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,8 @@ export function isZoomedOrPanned(chart) {

return false;
}

export function isZoomingOrPanning(chart) {
const state = getState(chart);
return state.panning || state.dragging;
}
6 changes: 3 additions & 3 deletions src/plugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Hammer from 'hammerjs';
import {addListeners, computeDragRect, removeListeners} from './handlers';
import {startHammer, stopHammer} from './hammer';
import {pan, zoom, resetZoom, zoomScale, getZoomLevel, getInitialScaleBounds, isZoomedOrPanned, zoomRect} from './core';
import {pan, zoom, resetZoom, zoomScale, getZoomLevel, getInitialScaleBounds, isZoomedOrPanned, isZoomingOrPanning, zoomRect} from './core';
import {panFunctions, zoomFunctions, zoomRectFunctions} from './scale.types';
import {getState, removeState} from './state';
import {version} from '../package.json';
Expand Down Expand Up @@ -83,11 +83,11 @@ export default {
chart.getZoomLevel = () => getZoomLevel(chart);
chart.getInitialScaleBounds = () => getInitialScaleBounds(chart);
chart.isZoomedOrPanned = () => isZoomedOrPanned(chart);
chart.isZoomingOrPanning = () => isZoomingOrPanning(chart);
},

beforeEvent(chart) {
const state = getState(chart);
if (state.panning || state.dragging) {
if (isZoomingOrPanning(chart)) {
// cancel any event handling while panning or dragging
return false;
}
Expand Down
4 changes: 3 additions & 1 deletion src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export function getState(chart) {
originalScaleLimits: {},
updatedScaleLimits: {},
handlers: {},
panDelta: {}
panDelta: {},
dragging: false,
panning: false
};
chartStates.set(chart, state);
}
Expand Down
16 changes: 16 additions & 0 deletions test/specs/zoom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,10 +791,19 @@ describe('zoom', function() {
};
const pt2 = {x: pt.x + 20, y: pt.y + 20};

expect(chart.isZoomingOrPanning()).toBe(false);

jasmine.dispatchEvent(chart, 'mousedown', pt);
jasmine.dispatchEvent(chart, 'mousemove', pt2);

expect(chart.isZoomingOrPanning()).toBe(true);

jasmine.dispatchEvent(chart, 'mouseup', pt2);

// Drag state isn't cleared until a timeout fires (later), so we can't
// easily test this here.
// expect(chart.isZoomingOrPanning()).toBe(false);

expect(startSpy).toHaveBeenCalled();
expect(zoomSpy).toHaveBeenCalled();
});
Expand Down Expand Up @@ -830,10 +839,17 @@ describe('zoom', function() {
};
const pt2 = {x: pt.x + 20, y: pt.y + 20};

expect(chart.isZoomingOrPanning()).toBe(false);

jasmine.dispatchEvent(chart, 'mousedown', pt);

expect(chart.isZoomingOrPanning()).toBe(false);

jasmine.dispatchEvent(chart, 'mousemove', pt2);
jasmine.dispatchEvent(chart, 'mouseup', pt2);

expect(chart.isZoomingOrPanning()).toBe(false);

expect(rejectSpy).toHaveBeenCalled();
expect(zoomSpy).not.toHaveBeenCalled();
expect(doneSpy).not.toHaveBeenCalled();
Expand Down
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ declare module 'chart.js' {
getZoomLevel(): number;
getInitialScaleBounds(): Record<string, {min: number, max: number}>;
isZoomedOrPanned(): boolean;
isZoomingOrPanning(): boolean;
}
}

Expand Down Expand Up @@ -56,3 +57,4 @@ export function resetZoom(chart: Chart, mode?: UpdateMode): void;
export function getZoomLevel(chart: Chart): number;
export function getInitialScaleBounds(chart: Chart): Record<string, {min: number, max: number}>;
export function isZoomedOrPanned(chart: Chart): boolean;
export function isZoomingOrPanning(chart: Chart): boolean;

0 comments on commit 3ca579f

Please sign in to comment.