Skip to content

Commit 6547b6e

Browse files
authored
chore: add sample for rejecting drag start outside chartArea (#890)
1 parent 08844c2 commit 6547b6e

File tree

3 files changed

+123
-5
lines changed

3 files changed

+123
-5
lines changed

docs/.vuepress/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,10 @@ module.exports = {
145145
title: 'Drag to Zoom',
146146
children: [
147147
'drag/category',
148-
'drag/linear',
149148
'drag/linear-ratio',
149+
'drag/linear',
150150
'drag/log',
151+
'drag/reject-outside',
151152
'drag/time',
152153
'drag/timeseries',
153154
]

docs/samples/drag/reject-outside.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Reject outside chartArea
2+
3+
Zooming is performed by clicking and selecting an area over the chart with the mouse. Pan is activated by keeping `ctrl` pressed.
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+
reverse: true,
32+
ticks: {
33+
callback: (val, index, ticks) => index === 0 || index === ticks.length - 1 ? null : val,
34+
},
35+
grid: {
36+
borderColor: Utils.randomColor(1),
37+
color: 'rgba( 0, 0, 0, 0.1)',
38+
},
39+
title: {
40+
display: true,
41+
text: (ctx) => ctx.scale.axis + ' axis',
42+
}
43+
};
44+
const scales = {
45+
x: {
46+
position: 'top',
47+
},
48+
y: {
49+
position: 'right',
50+
},
51+
};
52+
Object.keys(scales).forEach(scale => Object.assign(scales[scale], scaleOpts));
53+
// </block:scales>
54+
55+
// <block:zoom:0>
56+
const dragColor = Utils.randomColor(0.4);
57+
const zoomOptions = {
58+
pan: {
59+
enabled: true,
60+
mode: 'xy',
61+
modifierKey: 'ctrl',
62+
},
63+
zoom: {
64+
mode: 'xy',
65+
// here is the magic!
66+
onZoomStart: ({chart, point}) => chart.isPointInArea(point),
67+
drag: {
68+
enabled: true,
69+
borderColor: 'rgb(54, 162, 235)',
70+
borderWidth: 1,
71+
backgroundColor: 'rgba(54, 162, 235, 0.3)'
72+
}
73+
}
74+
};
75+
// </block:zoom>
76+
77+
const zoomStatus = () => zoomOptions.zoom.drag.enabled ? 'enabled' : 'disabled';
78+
79+
// <block:config:1>
80+
const config = {
81+
type: 'scatter',
82+
data: data,
83+
options: {
84+
scales: scales,
85+
plugins: {
86+
zoom: zoomOptions,
87+
title: {
88+
display: true,
89+
position: 'bottom',
90+
text: (ctx) => 'Zoom: ' + zoomStatus()
91+
}
92+
},
93+
}
94+
};
95+
// </block:config>
96+
97+
const actions = [
98+
{
99+
name: 'Toggle zoom',
100+
handler(chart) {
101+
zoomOptions.zoom.drag.enabled = !zoomOptions.zoom.drag.enabled;
102+
chart.update();
103+
}
104+
}, {
105+
name: 'Reset zoom',
106+
handler(chart) {
107+
chart.resetZoom();
108+
}
109+
}
110+
];
111+
112+
module.exports = {
113+
actions,
114+
config,
115+
};
116+
```

src/hammer.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {callback as call} from 'chart.js/helpers';
1+
import {callback as call, getRelativePosition} from 'chart.js/helpers';
22
import Hammer from 'hammerjs';
33
import {pan, zoom} from './core';
44
import {getState} from './state';
@@ -66,9 +66,10 @@ function handlePinch(chart, state, e) {
6666
}
6767
}
6868

69-
function startPinch(chart, state) {
69+
function startPinch(chart, state, event) {
7070
if (state.options.zoom.pinch.enabled) {
71-
call(state.options.zoom.onZoomStart, [{chart}]);
71+
const point = getRelativePosition(event, chart);
72+
call(state.options.zoom.onZoomStart, [{chart, event, point}]);
7273
state.scale = 1;
7374
}
7475
}
@@ -128,7 +129,7 @@ export function startHammer(chart, options) {
128129
const mc = new Hammer.Manager(canvas);
129130
if (zoomOptions && zoomOptions.pinch.enabled) {
130131
mc.add(new Hammer.Pinch());
131-
mc.on('pinchstart', () => startPinch(chart, state));
132+
mc.on('pinchstart', (e) => startPinch(chart, state, e));
132133
mc.on('pinch', (e) => handlePinch(chart, state, e));
133134
mc.on('pinchend', (e) => endPinch(chart, state, e));
134135
}

0 commit comments

Comments
 (0)