Giter VIP home page Giter VIP logo

Comments (3)

danmana avatar danmana commented on July 26, 2024 2

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.

danmana avatar danmana commented on July 26, 2024 1

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.

anko20094 avatar anko20094 commented on July 26, 2024
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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.