Comments (3)
While the plugin does not support scrolling with the mouse wheel, it is possible to implement it.
All you need is to call the plugins doZoom
method
const chart = new Chart(...);
const plugin = Chart.plugins.getAll().find((p) => p.id === "crosshair");
plugin.doZoom(chart, myZoomStart, myZoomEnd);
Here is a simple version that zooms by 10% https://codepen.io/danmana/pen/GRZQNpR?editors=1010
You might want to implement a smarter way to calculate myZoomStart/End that takes into account the current mouse position (see center variable below)
canvas.addEventListener("wheel", (event) => {
const delta = event.deltaY;
const xscale = chart.scales['x-axis-0'];
const start = chart.crosshair.start === undefined ? xscale.min : chart.crosshair.start;
const end = chart.crosshair.end === undefined ? xscale.max : chart.crosshair.end;
const ZOOM_FACTOR = 0.1; // 10%
const increment = Math.sign(delta) * Math.abs(end - start) * ZOOM_FACTOR / 2;
// might want to use chart.crosshair.x and center to zoom around the current mouse position
const center = xscale.getValueForPixel(chart.crosshair.x);
const myZoomStart = start + increment;
const myZoomEnd = end - increment;
console.log(delta, start, end, center, increment, myZoomStart, myZoomEnd);
plugin.doZoom(
chart,
myZoomStart,
myZoomEnd
);
});
from chartjs-plugin-crosshair.
Chart.js 3.x.x update
Obtaining the plugin is a bit easier now:
const plugin = Chart.registry.getPlugin('crosshair');
Note: there were some changes in Chart.js v3 regarding scales. Besides that, the code above is unchanged.
const xscale = chart.scales['x'];
See example: https://codepen.io/danmana/pen/QWQjVZb?editors=1010
from chartjs-plugin-crosshair.
const plugin = Chart.plugins.getAll().find((p) => p.id === "crosshair");
How should code been rewriting for versions 3.x.x?
from chartjs-plugin-crosshair.
Related Issues (20)
- Spent hours but couldn't get it to working... Help Needed... HOT 2
- Links to Documentation are broken HOT 1
- Get dot on vertical line HOT 1
- Module '"chartjs-plugin-crosshair"' has no exported member 'CrosshairPlugin' and Interpolate HOT 6
- Unhandled Runtime Error TypeError: Cannot read properties of undefined (reading 'syncEventHandler') HOT 1
- error TS2339: Property 'interpolate' does not exist on type 'InteractionModeMap' HOT 1
- Working solution for modern chart.js setup (React) HOT 2
- Compatibility with Chart.js 4.x
- Cannot read properties of undefined (reading 'enabled') at Object.afterDraw HOT 6
- Is this repo still maintained? HOT 2
- Plugin does not support dynamic scale keys
- Disable and enable crosshair HOT 1
- Hi i use plugin chartjs angular, i have error chart.crosshair is undefined how to fix this HOT 1
- How to update zoomed graph datapoints if dataset is is shifted in x axis ?
- Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'syncEventHandler')
- Vertical line not showing in the second chart while hover on the first chart. HOT 3
- s/xscaleType/xScaleType
- What is the status of this library for the Angular wrapper (ng2-charts)?
- Follow Mouse with Pan
- Sync is not working HOT 24
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chartjs-plugin-crosshair.