Skip to content

Commit dd0c8b3

Browse files
joshkelkurkle
andauthored
Add a function for getting zoom range (#780)
* Add a function for getting current zoom range * Change API to be more in line with existing Update types for getInitialScaleBounds - its implementation shows that it may return undefined. * Update docs --------- Co-authored-by: Jukka Kurkela <[email protected]>
1 parent 4c794cb commit dd0c8b3

File tree

5 files changed

+59
-4
lines changed

5 files changed

+59
-4
lines changed

docs/guide/developers.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Returns the current zoom level. If this is the same as the chart's initial scal
2626

2727
If the chart has been panned but not zoomed, this method will still return `1.0`.
2828

29-
### `chart.getInitialScaleBounds(): Record<string, {min: number, max: number}>`
29+
### `chart.getInitialScaleBounds(): Record<string, {min: number | undefined, max: number | undefined}>`
3030

3131
Returns the initial scale bounds of each scale before any zooming or panning took place. This is returned in the format of an object, e.g.
3232

@@ -38,6 +38,20 @@ Returns the initial scale bounds of each scale before any zooming or panning too
3838
}
3939
```
4040

41+
### `chart.getZoomedScaleBounds(): Record<string, {min: number, max: number}>`
42+
43+
Returns the updated scale bounds of each scale after any zooming or panning took place. This is returned in the format of an object, e.g.
44+
45+
```json
46+
{
47+
x: {min: 25, max: 75},
48+
y1: {min: 60, max: 90},
49+
y2: undefined
50+
}
51+
```
52+
53+
Scale IDs that have not been zoomed will be `undefined` within the returned object.
54+
4155
### `chart.isZoomedOrPanned(): boolean`
4256

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

src/core.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export function resetZoom(chart, transition = 'default') {
127127
delete scaleOptions.min;
128128
delete scaleOptions.max;
129129
}
130+
delete state.updatedScaleLimits[scale.id];
130131
});
131132
chart.update(transition);
132133
call(state.options.zoom.onZoomComplete, [{chart}]);
@@ -209,6 +210,16 @@ export function getInitialScaleBounds(chart) {
209210
return scaleBounds;
210211
}
211212

213+
export function getZoomedScaleBounds(chart) {
214+
const state = getState(chart);
215+
const scaleBounds = {};
216+
for (const scaleId of Object.keys(chart.scales)) {
217+
scaleBounds[scaleId] = state.updatedScaleLimits[scaleId];
218+
}
219+
220+
return scaleBounds;
221+
}
222+
212223
export function isZoomedOrPanned(chart) {
213224
const scaleBounds = getInitialScaleBounds(chart);
214225
for (const scaleId of Object.keys(chart.scales)) {

src/plugin.js

Lines changed: 2 additions & 1 deletion
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, isZoomingOrPanning, zoomRect} from './core';
4+
import {pan, zoom, resetZoom, zoomScale, getZoomLevel, getInitialScaleBounds, getZoomedScaleBounds, 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';
@@ -82,6 +82,7 @@ export default {
8282
chart.resetZoom = (transition) => resetZoom(chart, transition);
8383
chart.getZoomLevel = () => getZoomLevel(chart);
8484
chart.getInitialScaleBounds = () => getInitialScaleBounds(chart);
85+
chart.getZoomedScaleBounds = () => getZoomedScaleBounds(chart);
8586
chart.isZoomedOrPanned = () => isZoomedOrPanned(chart);
8687
chart.isZoomingOrPanning = () => isZoomingOrPanning(chart);
8788
},

test/specs/api.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,31 @@ describe('api', function() {
342342
expect(chart.isZoomedOrPanned()).toBe(false);
343343
});
344344
});
345+
346+
describe('getZoomedScaleBounds', function() {
347+
it('should return the zoom range, or undefined if not zoomed', function() {
348+
const chart = window.acquireChart({
349+
type: 'scatter',
350+
options: {
351+
scales: {
352+
x: {
353+
min: 0,
354+
max: 100
355+
},
356+
y: {
357+
min: 0,
358+
max: 100
359+
}
360+
}
361+
}
362+
});
363+
expect(chart.getZoomedScaleBounds().x).toBeUndefined();
364+
365+
chart.zoom(1.5);
366+
expect(chart.getZoomedScaleBounds().x).toEqual({min: 25, max: 75});
367+
368+
chart.resetZoom();
369+
expect(chart.getZoomedScaleBounds().x).toBeUndefined();
370+
});
371+
});
345372
});

types/index.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ declare module 'chart.js' {
2525
zoomScale(id: string, range: ScaleRange, mode?: UpdateMode): void;
2626
resetZoom(mode?: UpdateMode): void;
2727
getZoomLevel(): number;
28-
getInitialScaleBounds(): Record<string, {min: number, max: number}>;
28+
getInitialScaleBounds(): Record<string, {min: number | undefined, max: number | undefined}>;
29+
getZoomedScaleBounds(): Record<string, ScaleRange | undefined>;
2930
isZoomedOrPanned(): boolean;
3031
isZoomingOrPanning(): boolean;
3132
}
@@ -55,6 +56,7 @@ export function zoomRect(chart: Chart, p0: Point, p1: Point, mode?: UpdateMode):
5556
export function zoomScale(chart: Chart, scaleId: string, range: ScaleRange, mode?: UpdateMode): void;
5657
export function resetZoom(chart: Chart, mode?: UpdateMode): void;
5758
export function getZoomLevel(chart: Chart): number;
58-
export function getInitialScaleBounds(chart: Chart): Record<string, {min: number, max: number}>;
59+
export function getInitialScaleBounds(chart: Chart): Record<string, {min: number | undefined, max: number | undefined}>;
60+
export function getZoomedScaleBounds(chart: Chart): Record<string, ScaleRange | undefined>;
5961
export function isZoomedOrPanned(chart: Chart): boolean;
6062
export function isZoomingOrPanning(chart: Chart): boolean;

0 commit comments

Comments
 (0)