Skip to content

Commit 253d01a

Browse files
authored
Fix delta calculation at direction change (#494)
1 parent 48e16ec commit 253d01a

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

src/core.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {each, callback as call} from 'chart.js/helpers';
1+
import {each, callback as call, sign} from 'chart.js/helpers';
22
import {panFunctions, updateRange, zoomFunctions} from './scale.types';
33
import {getState} from './state';
44
import {directionEnabled, getEnabledScalesByPoint} from './utils';
@@ -117,7 +117,10 @@ export function resetZoom(chart, transition = 'default') {
117117
function panScale(scale, delta, limits) {
118118
const {panDelta} = getState(scale.chart);
119119
// Add possible cumulative delta from previous pan attempts where scale did not change
120-
delta += panDelta[scale.id] || 0;
120+
const storedDelta = panDelta[scale.id] || 0;
121+
if (sign(storedDelta) === sign(delta)) {
122+
delta += storedDelta;
123+
}
121124
const fn = panFunctions[scale.type] || panFunctions.default;
122125
if (call(fn, [scale, delta, limits])) {
123126
// The scale changed, reset cumulative delta

src/scale.types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function panCategoryScale(scale, delta, limits) {
8282
const range = Math.max(max - min, 1);
8383
// How many pixels of delta is required before making a step. stepSize, but limited to max 1/10 of the scale length.
8484
const stepDelta = Math.round(scaleLength(scale) / Math.max(range, 10));
85-
const stepSize = Math.ceil(Math.abs(delta / stepDelta));
85+
const stepSize = Math.round(Math.abs(delta / stepDelta));
8686
let applied;
8787
if (delta < -stepDelta) {
8888
max = Math.min(max + stepSize, lastLabelIndex);

test/fixtures/pan/category-x-5.png

150 Bytes
Loading

test/specs/pan.spec.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ describe('pan', function() {
22
describe('auto', jasmine.fixture.specs('pan'));
33

44
const data = {
5+
labels: ['a', 'b', 'c', 'd', 'e'],
56
datasets: [{
67
data: [{
78
x: 1,
@@ -16,6 +17,83 @@ describe('pan', function() {
1617
}]
1718
};
1819

20+
describe('delta', function() {
21+
it('should be applied cumulatively', function() {
22+
const chart = window.acquireChart({
23+
type: 'line',
24+
data,
25+
options: {
26+
plugins: {
27+
zoom: {
28+
pan: {
29+
enabled: true,
30+
mode: 'x',
31+
}
32+
}
33+
},
34+
scales: {
35+
x: {
36+
min: 1,
37+
max: 2
38+
}
39+
}
40+
}
41+
});
42+
const scale = chart.scales.x;
43+
expect(scale.min).toBe(1);
44+
expect(scale.max).toBe(2);
45+
chart.pan(20);
46+
expect(scale.min).toBe(1);
47+
expect(scale.max).toBe(2);
48+
chart.pan(20);
49+
expect(scale.min).toBe(1);
50+
expect(scale.max).toBe(2);
51+
chart.pan(20);
52+
expect(scale.min).toBe(0);
53+
expect(scale.max).toBe(1);
54+
});
55+
56+
it('should not give credit', function() {
57+
const chart = window.acquireChart({
58+
type: 'scatter',
59+
data,
60+
options: {
61+
plugins: {
62+
zoom: {
63+
limits: {
64+
x: {
65+
max: 4
66+
}
67+
},
68+
pan: {
69+
enabled: true,
70+
mode: 'x',
71+
}
72+
}
73+
},
74+
scales: {
75+
x: {
76+
min: 1,
77+
max: 3
78+
}
79+
}
80+
}
81+
});
82+
const scale = chart.scales.x;
83+
expect(scale.min).toBe(1);
84+
expect(scale.max).toBe(3);
85+
chart.pan(-2000);
86+
expect(scale.min).toBe(2);
87+
expect(scale.max).toBe(4);
88+
chart.pan(-2000);
89+
expect(scale.min).toBe(2);
90+
expect(scale.max).toBe(4);
91+
chart.pan(50);
92+
expect(scale.min).toBeLessThan(2);
93+
expect(scale.max).toBe(scale.min + 2);
94+
});
95+
});
96+
1997
describe('events', function() {
2098
it('should call onPanStart', function(done) {
2199
const startSpy = jasmine.createSpy('started');

0 commit comments

Comments
 (0)