Skip to content

Commit 5bbacb0

Browse files
authored
Add getZoomLevel function (#536)
1 parent cca9af4 commit 5bbacb0

5 files changed

Lines changed: 46 additions & 16 deletions

File tree

docs/samples/basic.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,18 @@ const zoomOptions = {
6868
enabled: true
6969
},
7070
mode: 'xy',
71+
onZoomComplete({chart}) {
72+
// This update is needed to display up to date zoom level in the title.
73+
// Without this, previous zoom level is displayed.
74+
// The reason is: title uses the same beforeUpdate hook, and is evaluated before zoom.
75+
chart.update('none');
76+
}
7177
}
7278
};
7379
// </block:zoom>
7480

7581
const panStatus = () => zoomOptions.pan.enabled ? 'enabled' : 'disabled';
76-
const zoomStatus = () => zoomOptions.zoom.wheel.enabled ? 'enabled' : 'disabled';
82+
const zoomStatus = (chart) => (zoomOptions.zoom.wheel.enabled ? 'enabled' : 'disabled') + ' (' + chart.getZoomLevel() + 'x)';
7783

7884
// <block:config:1>
7985
const config = {
@@ -86,7 +92,7 @@ const config = {
8692
title: {
8793
display: true,
8894
position: 'bottom',
89-
text: (ctx) => 'Zoom: ' + zoomStatus() + ', Pan: ' + panStatus()
95+
text: (ctx) => 'Zoom: ' + zoomStatus(ctx.chart) + ', Pan: ' + panStatus()
9096
}
9197
},
9298
onClick(e) {

src/core.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {each, callback as call, sign} from 'chart.js/helpers';
1+
import {each, callback as call, sign, valueOrDefault} from 'chart.js/helpers';
22
import {panFunctions, updateRange, zoomFunctions} from './scale.types';
33
import {getState} from './state';
44
import {directionEnabled, getEnabledScalesByPoint} from './utils';
@@ -102,9 +102,9 @@ export function zoomScale(chart, scaleId, range, transition = 'none') {
102102
chart.update(transition);
103103
}
104104

105-
106105
export function resetZoom(chart, transition = 'default') {
107-
const originalScaleLimits = storeOriginalScaleLimits(chart, getState(chart));
106+
const state = getState(chart);
107+
const originalScaleLimits = storeOriginalScaleLimits(chart, state);
108108

109109
each(chart.scales, function(scale) {
110110
const scaleOptions = scale.options;
@@ -117,6 +117,29 @@ export function resetZoom(chart, transition = 'default') {
117117
}
118118
});
119119
chart.update(transition);
120+
call(state.options.zoom.onZoomComplete, [{chart}]);
121+
}
122+
123+
function getOriginalRange(state, scaleId) {
124+
const original = state.originalScaleLimits[scaleId];
125+
if (!original) {
126+
return;
127+
}
128+
const {min, max} = original;
129+
return valueOrDefault(max.options, max.scale) - valueOrDefault(min.options, min.scale);
130+
}
131+
132+
export function getZoomLevel(chart) {
133+
const state = getState(chart);
134+
let min = 1;
135+
let max = 1;
136+
each(chart.scales, function(scale) {
137+
const origRange = getOriginalRange(state, scale.id);
138+
const level = Math.round(origRange / (scale.max - scale.min) * 100) / 100;
139+
min = Math.min(min, level);
140+
max = Math.max(max, level);
141+
});
142+
return min < 1 ? min : max;
120143
}
121144

122145
function panScale(scale, delta, limits, state) {

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} from './core';
4+
import {pan, zoom, resetZoom, zoomScale, getZoomLevel} from './core';
55
import {panFunctions, zoomFunctions} from './scale.types';
66
import {getState, removeState} from './state';
77
import {version} from '../package.json';
@@ -50,6 +50,7 @@ export default {
5050
chart.zoom = (args, transition) => zoom(chart, args, transition);
5151
chart.zoomScale = (id, range, transition) => zoomScale(chart, id, range, transition);
5252
chart.resetZoom = (transition) => resetZoom(chart, transition);
53+
chart.getZoomLevel = () => getZoomLevel(chart);
5354
},
5455

5556
beforeEvent(chart) {

src/scale.types.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {valueOrDefault} from 'chart.js/helpers';
12
import {getState} from './state';
23

34
function zoomDelta(scale, zoom, center) {
@@ -14,26 +15,23 @@ function zoomDelta(scale, zoom, center) {
1415
};
1516
}
1617

17-
function getLimit(chartState, scale, scaleLimits, prop, fallback) {
18+
function getLimit(state, scale, scaleLimits, prop, fallback) {
1819
let limit = scaleLimits[prop];
1920
if (limit === 'original') {
20-
const original = chartState.originalScaleLimits[scale.id][prop];
21-
limit = original.options !== null && original.options !== undefined ? original.options : original.scale;
21+
const original = state.originalScaleLimits[scale.id][prop];
22+
limit = valueOrDefault(original.options, original.scale);
2223
}
23-
if (limit === null || limit === undefined) {
24-
limit = fallback;
25-
}
26-
return limit;
24+
return valueOrDefault(limit, fallback);
2725
}
2826

2927
export function updateRange(scale, {min, max}, limits, zoom = false) {
30-
const chartState = getState(scale.chart);
28+
const state = getState(scale.chart);
3129
const {id, axis, options: scaleOpts} = scale;
3230

3331
const scaleLimits = limits && (limits[id] || limits[axis]) || {};
3432
const {minRange = 0} = scaleLimits;
35-
const minLimit = getLimit(chartState, scale, scaleLimits, 'min', -Infinity);
36-
const maxLimit = getLimit(chartState, scale, scaleLimits, 'max', Infinity);
33+
const minLimit = getLimit(state, scale, scaleLimits, 'min', -Infinity);
34+
const maxLimit = getLimit(state, scale, scaleLimits, 'max', Infinity);
3735

3836
const cmin = Math.max(min, minLimit);
3937
const cmax = Math.min(max, maxLimit);

types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ declare module 'chart.js' {
2323
zoom(zoom: ZoomAmount, useTransition?: boolean, mode?: UpdateMode): void;
2424
zoomScale(id: string, range: ScaleRange, mode?: UpdateMode): void;
2525
resetZoom(mode?: UpdateMode): void;
26+
getZoomLevel(): number;
2627
}
2728
}
2829

@@ -46,3 +47,4 @@ export function pan(chart: Chart, amount: PanAmount, scales?: Scale[], mode?: Up
4647
export function zoom(chart: Chart, amount: ZoomAmount, mode?: UpdateMode): void;
4748
export function zoomScale(chart: Chart, scaleId: string, range: ScaleRange, mode?: UpdateMode): void;
4849
export function resetZoom(chart: Chart, mode?: UpdateMode): void;
50+
export function getZoomLevel(chart): number;

0 commit comments

Comments
 (0)