Skip to content

Commit 723936b

Browse files
authored
Fix resetZoom after manual scale limits update (#553)
* Fix resetZoom after manual scale limits update * Increase tolerance
1 parent 5bbacb0 commit 723936b

5 files changed

Lines changed: 118 additions & 8 deletions

File tree

src/core.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,38 @@ import {panFunctions, updateRange, zoomFunctions} from './scale.types';
33
import {getState} from './state';
44
import {directionEnabled, getEnabledScalesByPoint} from './utils';
55

6+
function shouldUpdateScaleLimits(scale, originalScaleLimits, updatedScaleLimits) {
7+
const {id, options: {min, max}} = scale;
8+
if (!originalScaleLimits[id] || !updatedScaleLimits[id]) {
9+
return true;
10+
}
11+
const previous = updatedScaleLimits[id];
12+
return previous.min !== min || previous.max !== max;
13+
}
14+
15+
function removeMissingScales(limits, scales) {
16+
each(limits, (opt, key) => {
17+
if (!scales[key]) {
18+
delete limits[key];
19+
}
20+
});
21+
}
22+
623
function storeOriginalScaleLimits(chart, state) {
7-
const {originalScaleLimits} = state;
8-
each(chart.scales, function(scale) {
9-
if (!originalScaleLimits[scale.id]) {
24+
const {scales} = chart;
25+
const {originalScaleLimits, updatedScaleLimits} = state;
26+
27+
each(scales, function(scale) {
28+
if (shouldUpdateScaleLimits(scale, originalScaleLimits, updatedScaleLimits)) {
1029
originalScaleLimits[scale.id] = {
1130
min: {scale: scale.min, options: scale.options.min},
1231
max: {scale: scale.max, options: scale.options.max},
1332
};
1433
}
1534
});
16-
each(originalScaleLimits, function(opt, key) {
17-
if (!chart.scales[key]) {
18-
delete originalScaleLimits[key];
19-
}
20-
});
35+
36+
removeMissingScales(originalScaleLimits, scales);
37+
removeMissingScales(updatedScaleLimits, scales);
2138
return originalScaleLimits;
2239
}
2340

src/scale.types.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ export function updateRange(scale, {min, max}, limits, zoom = false) {
5454
}
5555
scaleOpts.min = min;
5656
scaleOpts.max = max;
57+
58+
state.updatedScaleLimits[scale.id] = {min, max};
59+
5760
// return true if the scale range is changed
5861
return scale.parse(min) !== scale.min || scale.parse(max) !== scale.max;
5962
}

src/state.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export function getState(chart) {
55
if (!state) {
66
state = {
77
originalScaleLimits: {},
8+
updatedScaleLimits: {},
89
handlers: {},
910
panDelta: {}
1011
};

test/fixtures/zoom/update-reset.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const labels = ['a', 'b', 'c', 'd'];
2+
const data = [2, 4, 8, 16];
3+
4+
const canvas = document.createElement('canvas');
5+
const side = 256;
6+
canvas.height = 768;
7+
canvas.width = 768;
8+
const ctx = canvas.getContext('2d');
9+
10+
let x = 0;
11+
let y = 0;
12+
function snapshot(chart, descr) {
13+
ctx.drawImage(chart.canvas, x, y, side, side);
14+
ctx.textBaseline = 'middle';
15+
ctx.textAlign = 'center';
16+
ctx.font = 'normal normal 12px arial';
17+
ctx.fillText(descr, x + side / 2, y + side / 2);
18+
19+
x += side;
20+
if (x + side > canvas.width) {
21+
y += side;
22+
x = 0;
23+
}
24+
}
25+
26+
module.exports = {
27+
tolerance: 0.0018,
28+
config: {
29+
type: 'line',
30+
data: {
31+
labels,
32+
datasets: [{
33+
data,
34+
borderColor: 'red',
35+
borderWidth: 10
36+
}]
37+
},
38+
options: {
39+
scales: {
40+
y: {}
41+
},
42+
plugins: {
43+
legend: false,
44+
},
45+
}
46+
},
47+
options: {
48+
spriteText: true,
49+
run(chart) {
50+
snapshot(chart, 'original');
51+
52+
chart.zoomScale('y', {min: 3, max: 9});
53+
snapshot(chart, 'zoom 3..9');
54+
55+
chart.resetZoom();
56+
snapshot(chart, 'reset');
57+
58+
chart.options.scales.y = {
59+
min: 0,
60+
max: 25
61+
};
62+
chart.update();
63+
snapshot(chart, 'update 0..25');
64+
65+
chart.resetZoom();
66+
snapshot(chart, 'reset');
67+
68+
chart.zoomScale('y', {min: 5, max: 15});
69+
snapshot(chart, 'zoom 5..15');
70+
71+
chart.resetZoom();
72+
snapshot(chart, 'reset');
73+
74+
chart.options.scales.y = {
75+
min: 1,
76+
max: 10
77+
};
78+
chart.update();
79+
snapshot(chart, 'update 1..10');
80+
81+
chart.resetZoom();
82+
snapshot(chart, 'reset');
83+
84+
Chart.helpers.clearCanvas(chart.canvas);
85+
chart.ctx.drawImage(canvas, 0, 0, 512, 512);
86+
}
87+
}
88+
};
89+
40.7 KB
Loading

0 commit comments

Comments
 (0)