Skip to content

Commit f0c2687

Browse files
committed
fix: enabling pan after chart initialization
1 parent 58d8a3e commit f0c2687

File tree

9 files changed

+142
-65
lines changed

9 files changed

+142
-65
lines changed

.eslintrc.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
extends:
22
- chartjs
3-
- plugin:es/no-new-in-es2019
43
- plugin:markdown/recommended
54

65
env:
7-
es6: true
6+
es2021: true
87
browser: true
98
node: true
109

1110
parserOptions:
12-
ecmaVersion: 2018
1311
sourceType: module
1412
ecmaFeatures:
1513
impliedStrict: true
1614
modules: true
1715

18-
plugins: ['html', 'es']
16+
plugins: ['html']
1917

2018
rules:
2119
complexity: ["warn", 10]

docs/.vuepress/config.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,15 @@ module.exports = {
150150
'drag/timeseries',
151151
]
152152
},
153-
'api',
153+
{
154+
title: 'Pan',
155+
children: [
156+
'pan/region',
157+
'pan/toggle',
158+
]
159+
},
154160
'fetch-data',
155-
'pan-region',
161+
'api',
156162
],
157163
}
158164
}

docs/samples/fetch-data.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Fetch data
1+
# Fetch Data
22

33
```js chart-editor
44
// <block:data:2>
File renamed without changes.

docs/samples/pan/toggle.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Pan Toggle
2+
3+
In this example pan is initially disabled.
4+
5+
```js chart-editor
6+
// <block:data:1>
7+
const NUMBER_CFG = {count: 20, min: -100, max: 100};
8+
const data = {
9+
datasets: [{
10+
label: 'My First dataset',
11+
borderColor: Utils.randomColor(0.4),
12+
backgroundColor: Utils.randomColor(0.1),
13+
pointBorderColor: Utils.randomColor(0.7),
14+
pointBackgroundColor: Utils.randomColor(0.5),
15+
pointBorderWidth: 1,
16+
data: Utils.points(NUMBER_CFG),
17+
}, {
18+
label: 'My Second dataset',
19+
borderColor: Utils.randomColor(0.4),
20+
backgroundColor: Utils.randomColor(0.1),
21+
pointBorderColor: Utils.randomColor(0.7),
22+
pointBackgroundColor: Utils.randomColor(0.5),
23+
pointBorderWidth: 1,
24+
data: Utils.points(NUMBER_CFG),
25+
}]
26+
};
27+
// </block:data>
28+
29+
// <block:scales:2>
30+
const scaleOpts = {
31+
grid: {
32+
borderColor: Utils.randomColor(1),
33+
color: 'rgba( 0, 0, 0, 0.1)',
34+
},
35+
title: {
36+
display: true,
37+
text: (ctx) => ctx.scale.axis + ' axis',
38+
}
39+
};
40+
const scales = {
41+
x: {
42+
position: 'top',
43+
},
44+
y: {
45+
position: 'right',
46+
},
47+
};
48+
Object.keys(scales).forEach(scale => Object.assign(scales[scale], scaleOpts));
49+
// </block:scales>
50+
51+
// <block:zoom:0>
52+
const zoomOptions = {
53+
limits: {
54+
x: {min: -200, max: 200, minRange: 50},
55+
y: {min: -200, max: 200, minRange: 50}
56+
},
57+
pan: {
58+
enabled: false,
59+
mode: 'xy',
60+
},
61+
zoom: {
62+
wheel: {
63+
enabled: false,
64+
},
65+
pinch: {
66+
enabled: true
67+
},
68+
}
69+
};
70+
// </block:zoom>
71+
72+
// <block:config:1>
73+
const config = {
74+
type: 'scatter',
75+
data: data,
76+
options: {
77+
scales: scales,
78+
plugins: {
79+
zoom: zoomOptions,
80+
},
81+
},
82+
};
83+
// </block:config>
84+
85+
const actions = [
86+
{
87+
name: 'Toggle pan',
88+
handler(chart) {
89+
chart.options.plugins.zoom.pan.enabled = !chart.options.plugins.zoom.pan.enabled;
90+
chart.update();
91+
}
92+
},
93+
{
94+
name: 'Reset zoom',
95+
handler(chart) {
96+
chart.resetZoom('zoom');
97+
}
98+
}
99+
];
100+
101+
module.exports = {
102+
actions,
103+
config,
104+
};
105+
```

package-lock.json

Lines changed: 0 additions & 56 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
"date-fns": "^2.21.1",
5454
"eslint": "^8.2.0",
5555
"eslint-config-chartjs": "^0.3.0",
56-
"eslint-plugin-es": "^4.1.0",
5756
"eslint-plugin-html": "^6.1.2",
5857
"eslint-plugin-markdown": "^2.0.1",
5958
"hammer-simulator": "^0.0.1",

src/hammer.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,20 @@ export function stopHammer(chart) {
159159
hammers.delete(chart);
160160
}
161161
}
162+
163+
export function hammerOptionsChanged(oldOptions, newOptions) {
164+
const {pan: oldPan, zoom: oldZoom} = oldOptions;
165+
const {pan: newPan, zoom: newZoom} = newOptions;
166+
167+
if (oldZoom?.zoom?.pinch?.enabled !== newZoom?.zoom?.pinch?.enabled) {
168+
return true;
169+
}
170+
if (oldPan?.enabled !== newPan?.enabled) {
171+
return true;
172+
}
173+
if (oldPan?.threshold !== newPan?.threshold) {
174+
return true;
175+
}
176+
177+
return false;
178+
}

src/plugin.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Hammer from 'hammerjs';
22
import {addListeners, computeDragRect, removeListeners} from './handlers';
3-
import {startHammer, stopHammer} from './hammer';
3+
import {hammerOptionsChanged, startHammer, stopHammer} from './hammer';
44
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';
@@ -96,7 +96,15 @@ export default {
9696

9797
beforeUpdate: function(chart, args, options) {
9898
const state = getState(chart);
99+
const previousOptions = state.options;
99100
state.options = options;
101+
102+
// Hammer needs to be restarted when certain options change.
103+
if (hammerOptionsChanged(previousOptions, options)) {
104+
stopHammer(chart);
105+
startHammer(chart, options);
106+
}
107+
100108
addListeners(chart, options);
101109
},
102110

0 commit comments

Comments
 (0)