Skip to content

Commit ca62548

Browse files
authored
Add a sample for drag-to-zoom in docs (#475)
1 parent 7289efd commit ca62548

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

docs/.vuepress/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ module.exports = {
6969
'bar',
7070
'log',
7171
'time',
72+
'drag',
7273
'api',
7374
],
7475
}

docs/samples/drag.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Drag To Zoom
2+
3+
Zooming is performed by clicking and selecting an area over the chart with the mouse.
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: false,
60+
},
61+
zoom: {
62+
enabled: true,
63+
mode: 'xy',
64+
drag: {
65+
borderColor: 'rgb(54, 162, 235)',
66+
borderWidth: 1,
67+
backgroundColor: 'rgba(54, 162, 235, 0.3)'
68+
}
69+
}
70+
};
71+
// </block:zoom>
72+
73+
const zoomStatus = () => zoomOptions.zoom.enabled ? 'enabled' : 'disabled';
74+
75+
// <block:config:1>
76+
const config = {
77+
type: 'scatter',
78+
data: data,
79+
options: {
80+
scales: scales,
81+
plugins: {
82+
zoom: zoomOptions,
83+
title: {
84+
display: true,
85+
position: 'bottom',
86+
text: (ctx) => 'Zoom: ' + zoomStatus()
87+
}
88+
},
89+
}
90+
};
91+
// </block:config>
92+
93+
const actions = [
94+
{
95+
name: 'Toggle zoom',
96+
handler(chart) {
97+
zoomOptions.zoom.enabled = !zoomOptions.zoom.enabled;
98+
chart.update();
99+
}
100+
}, {
101+
name: 'Reset zoom',
102+
handler(chart) {
103+
chart.resetZoom();
104+
}
105+
}
106+
];
107+
108+
module.exports = {
109+
actions,
110+
config,
111+
};
112+
```

0 commit comments

Comments
 (0)