chartjs-plugin-zoom
A zoom and pan plugin for Chart.js >= 3.0.0-beta.13.
For Chart.js 2.6.0 to 2.9.x support, use version 0.7.7 of this plugin.
Panning can be done via the mouse or with a finger.
Zooming is done via the mouse wheel or via a pinch gesture. Hammer.js is used for gesture recognition.
Live Codepen Demo
Installation
Run npm install --save chartjs-plugin-zoom
to install with npm
.
If including via a <script>
tag, make sure to include Hammer.js
as well:
<script src="https://cdn.jsdelivr.net/npm/chart.js@next"></script>
<script src="https://cdn.jsdelivr.net/npm/hammerjs@2.0.8"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-zoom@next"></script>
Configuration
To configure the zoom and pan plugin, you can simply add new config options to your chart config.
plugins: {
zoom: {
pan: {
enabled: true,
mode: 'xy',
overScaleMode: 'xy',
rangeMin: {
x: null,
y: null
},
rangeMax: {
x: null,
y: null
},
speed: 20,
threshold: 10,
onPan: function({chart}) { console.log(`I'm panning!!!`); },
onPanComplete: function({chart}) { console.log(`I was panned!!!`); },
onPanRejected: function({chart, event}) { console.log(`I didn't start panning!`); }
},
zoom: {
enabled: true,
drag: true,
mode: 'xy',
overScaleMode: 'xy',
rangeMin: {
x: null,
y: null
},
rangeMax: {
x: null,
y: null
},
speed: 0.1,
threshold: 2,
sensitivity: 3,
onZoom: function({chart}) { console.log(`I'm zooming!!!`); },
onZoomComplete: function({chart}) { console.log(`I was zoomed!!!`); },
onZoomRejected: function({chart, event}) { console.log(`I didn't start zooming!`); }
}
}
}
API
chart.resetZoom()
Programmatically resets the zoom to the default state. See samples/zoom-time.html for an example.
Documentation
You can find documentation for Chart.js at www.chartjs.org/docs.
Examples for this plugin are available in the samples folder.
Prior to v0.4.0, this plugin was known as 'Chart.Zoom.js'. Old versions are still available on npm under that name.
Contributing
Before submitting an issue or a pull request to the project, please take a moment to look over the contributing guidelines first.
License
chartjs-plugin-zoom.js is available under the MIT license.