Skip to content

Commit 3db67e0

Browse files
authored
Add isZoomingOrPanning method (#823)
1 parent 0b79870 commit 3db67e0

File tree

6 files changed

+33
-4
lines changed

6 files changed

+33
-4
lines changed

docs/guide/developers.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ Returns the initial scale bounds of each scale before any zooming or panning too
4242

4343
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.
4444

45+
### `chart.isZoomingOrPanning(): boolean`
46+
47+
Returns whether the user is currently in the middle of a drag operation or pan operation.
48+
4549
## Custom Scales
4650

4751
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.

src/core.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,8 @@ export function isZoomedOrPanned(chart) {
225225

226226
return false;
227227
}
228+
229+
export function isZoomingOrPanning(chart) {
230+
const state = getState(chart);
231+
return state.panning || state.dragging;
232+
}

src/plugin.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Hammer from 'hammerjs';
22
import {addListeners, computeDragRect, removeListeners} from './handlers';
33
import {startHammer, stopHammer} from './hammer';
4-
import {pan, zoom, resetZoom, zoomScale, getZoomLevel, getInitialScaleBounds, isZoomedOrPanned, zoomRect} from './core';
4+
import {pan, zoom, resetZoom, zoomScale, getZoomLevel, getInitialScaleBounds, isZoomedOrPanned, isZoomingOrPanning, zoomRect} from './core';
55
import {panFunctions, zoomFunctions, zoomRectFunctions} from './scale.types';
66
import {getState, removeState} from './state';
77
import {version} from '../package.json';
@@ -83,11 +83,11 @@ export default {
8383
chart.getZoomLevel = () => getZoomLevel(chart);
8484
chart.getInitialScaleBounds = () => getInitialScaleBounds(chart);
8585
chart.isZoomedOrPanned = () => isZoomedOrPanned(chart);
86+
chart.isZoomingOrPanning = () => isZoomingOrPanning(chart);
8687
},
8788

8889
beforeEvent(chart) {
89-
const state = getState(chart);
90-
if (state.panning || state.dragging) {
90+
if (isZoomingOrPanning(chart)) {
9191
// cancel any event handling while panning or dragging
9292
return false;
9393
}

src/state.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ export function getState(chart) {
77
originalScaleLimits: {},
88
updatedScaleLimits: {},
99
handlers: {},
10-
panDelta: {}
10+
panDelta: {},
11+
dragging: false,
12+
panning: false
1113
};
1214
chartStates.set(chart, state);
1315
}

test/specs/zoom.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,10 +791,19 @@ describe('zoom', function() {
791791
};
792792
const pt2 = {x: pt.x + 20, y: pt.y + 20};
793793

794+
expect(chart.isZoomingOrPanning()).toBe(false);
795+
794796
jasmine.dispatchEvent(chart, 'mousedown', pt);
795797
jasmine.dispatchEvent(chart, 'mousemove', pt2);
798+
799+
expect(chart.isZoomingOrPanning()).toBe(true);
800+
796801
jasmine.dispatchEvent(chart, 'mouseup', pt2);
797802

803+
// Drag state isn't cleared until a timeout fires (later), so we can't
804+
// easily test this here.
805+
// expect(chart.isZoomingOrPanning()).toBe(false);
806+
798807
expect(startSpy).toHaveBeenCalled();
799808
expect(zoomSpy).toHaveBeenCalled();
800809
});
@@ -830,10 +839,17 @@ describe('zoom', function() {
830839
};
831840
const pt2 = {x: pt.x + 20, y: pt.y + 20};
832841

842+
expect(chart.isZoomingOrPanning()).toBe(false);
843+
833844
jasmine.dispatchEvent(chart, 'mousedown', pt);
845+
846+
expect(chart.isZoomingOrPanning()).toBe(false);
847+
834848
jasmine.dispatchEvent(chart, 'mousemove', pt2);
835849
jasmine.dispatchEvent(chart, 'mouseup', pt2);
836850

851+
expect(chart.isZoomingOrPanning()).toBe(false);
852+
837853
expect(rejectSpy).toHaveBeenCalled();
838854
expect(zoomSpy).not.toHaveBeenCalled();
839855
expect(doneSpy).not.toHaveBeenCalled();

types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ declare module 'chart.js' {
2727
getZoomLevel(): number;
2828
getInitialScaleBounds(): Record<string, {min: number, max: number}>;
2929
isZoomedOrPanned(): boolean;
30+
isZoomingOrPanning(): boolean;
3031
}
3132
}
3233

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

0 commit comments

Comments
 (0)