Skip to content

Commit 6456d46

Browse files
authored
fix: stop zooming (panning) at limit (#868)
1 parent ec1c125 commit 6456d46

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
"url": "https://github.com/chartjs/chartjs-plugin-zoom.git"
1515
},
1616
"scripts": {
17+
"autobuild": "rollup -c -w",
1718
"build": "rollup -c",
1819
"dev": "karma start --auto-watch --no-single-run --browsers chrome",
1920
"dev:ff": "karma start --auto-watch --no-single-run --browsers firefox",
2021
"docs": "npm run build && vuepress build docs --no-cache",
21-
"docs:dev": "npm run build && vuepress dev docs --no-cache",
22+
"docs:dev": "concurrently \"npm run autobuild\" \"vuepress dev docs --no-cache\"",
2223
"lint-js": "eslint \"samples/**/*.html\" \"test/**/*.js\" \"src/**/*.js\"",
2324
"lint-md": "eslint \"**/*.md\"",
2425
"lint-tsc": "tsc",

src/scale.types.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,14 @@ export function updateRange(scale, {min, max}, limits, zoom = false) {
133133
return true;
134134
}
135135

136-
const range = zoom ? Math.max(max - min, minRange) : scale.max - scale.min;
136+
const scaleRange = scale.max - scale.min;
137+
const range = zoom ? Math.max(max - min, minRange) : scaleRange;
138+
139+
if (zoom && range === minRange && scaleRange <= minRange) {
140+
// At range limit: No change but return true to indicate no need to store the delta.
141+
return true;
142+
}
143+
137144
const offset = (range - max + min) / 2;
138145
min -= offset;
139146
max += offset;

test/specs/api.spec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,48 @@ describe('api', function() {
152152
expect(chart.scales.x.max).toBe(100);
153153
});
154154

155+
it('should no-op whan laready at limit', function() {
156+
const chart = window.acquireChart({
157+
type: 'scatter',
158+
options: {
159+
scales: {
160+
x: {
161+
min: 0,
162+
max: 100
163+
},
164+
y: {
165+
min: 0,
166+
max: 100
167+
}
168+
},
169+
plugins: {
170+
zoom: {
171+
mode: 'x',
172+
limits: {
173+
x: {
174+
min: 0,
175+
max: 100,
176+
minRange: 50
177+
}
178+
}
179+
}
180+
}
181+
}
182+
});
183+
184+
chart.zoom({x: 1.5, focalPoint: {x: chart.scales.x.getPixelForValue(100)}});
185+
expect(chart.scales.x.min).toBe(50);
186+
expect(chart.scales.x.max).toBe(100);
187+
188+
chart.zoom({x: 1.5, focalPoint: {x: chart.scales.x.getPixelForValue(100)}});
189+
expect(chart.scales.x.min).toBe(50);
190+
expect(chart.scales.x.max).toBe(100);
191+
192+
chart.zoom({x: 1.5, focalPoint: {x: chart.scales.x.getPixelForValue(50)}});
193+
expect(chart.scales.x.min).toBe(50);
194+
expect(chart.scales.x.max).toBe(100);
195+
});
196+
155197
it('should honor zoom changes against a limit', function() {
156198
const chart = window.acquireChart({
157199
type: 'scatter',

0 commit comments

Comments
 (0)