Skip to content

Commit

Permalink
Add a sample for drag-to-zoom in docs (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
jledentu authored Apr 28, 2021
1 parent 7289efd commit ca62548
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ module.exports = {
'bar',
'log',
'time',
'drag',
'api',
],
}
Expand Down
112 changes: 112 additions & 0 deletions docs/samples/drag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Drag To Zoom

Zooming is performed by clicking and selecting an area over the chart with the mouse.

```js chart-editor
// <block:data:1>
const NUMBER_CFG = {count: 20, min: -100, max: 100};
const data = {
datasets: [{
label: 'My First dataset',
borderColor: Utils.randomColor(0.4),
backgroundColor: Utils.randomColor(0.1),
pointBorderColor: Utils.randomColor(0.7),
pointBackgroundColor: Utils.randomColor(0.5),
pointBorderWidth: 1,
data: Utils.points(NUMBER_CFG),
}, {
label: 'My Second dataset',
borderColor: Utils.randomColor(0.4),
backgroundColor: Utils.randomColor(0.1),
pointBorderColor: Utils.randomColor(0.7),
pointBackgroundColor: Utils.randomColor(0.5),
pointBorderWidth: 1,
data: Utils.points(NUMBER_CFG),
}]
};
// </block:data>

// <block:scales:2>
const scaleOpts = {
reverse: true,
ticks: {
callback: (val, index, ticks) => index === 0 || index === ticks.length - 1 ? null : val,
},
grid: {
borderColor: Utils.randomColor(1),
color: 'rgba( 0, 0, 0, 0.1)',
},
title: {
display: true,
text: (ctx) => ctx.scale.axis + ' axis',
}
};
const scales = {
x: {
position: 'top',
},
y: {
position: 'right',
},
};
Object.keys(scales).forEach(scale => Object.assign(scales[scale], scaleOpts));
// </block:scales>

// <block:zoom:0>
const dragColor = Utils.randomColor(0.4);
const zoomOptions = {
pan: {
enabled: false,
},
zoom: {
enabled: true,
mode: 'xy',
drag: {
borderColor: 'rgb(54, 162, 235)',
borderWidth: 1,
backgroundColor: 'rgba(54, 162, 235, 0.3)'
}
}
};
// </block:zoom>

const zoomStatus = () => zoomOptions.zoom.enabled ? 'enabled' : 'disabled';

// <block:config:1>
const config = {
type: 'scatter',
data: data,
options: {
scales: scales,
plugins: {
zoom: zoomOptions,
title: {
display: true,
position: 'bottom',
text: (ctx) => 'Zoom: ' + zoomStatus()
}
},
}
};
// </block:config>

const actions = [
{
name: 'Toggle zoom',
handler(chart) {
zoomOptions.zoom.enabled = !zoomOptions.zoom.enabled;
chart.update();
}
}, {
name: 'Reset zoom',
handler(chart) {
chart.resetZoom();
}
}
];

module.exports = {
actions,
config,
};
```

0 comments on commit ca62548

Please sign in to comment.