Skip to content
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

Add isZoomingOrPanning method #823

Open
wants to merge 1 commit 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
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be called IsPanningOrDragging?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thinking was, "zoom or pan" describes the plugin's two main pieces of functionality; its isZoomedOrPanned method therefore means, roughly, "has the plugin been activated"; so I called the new method isZoomingOrPanning to check "is the plugin currently being activated / manipulated."

I'm happy with isPanningOrDragging too - whatever makes the most sense for users of the library.

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;
Loading