[Chartjs]-Have the current zoom information in chart.js with zoom plugin

0👍

I don’t think you can. The chartjs zoom plugin merely modifies the min/max settings of the scaled axes. You could use that to determine the zoom factor if you know the start min/max values, I guess. The plugin stores the min/max internally for the resetZoom function, but I don’t think you have access to it.

If you, like me, just want to apply the same zoom to another (or in my case a new chart) chart, maybe this will help you (works with chart.js 3/chartjs-plugin-zoom 1.1):

const options = chart?.scales.x.options; // store the options of the old chart
chart?.clear(); // tear down the old chart
chart?.destroy();
chart = renderChart(canvas, data); // create a new one

if(options) {
  chart.zoom({x: 1}); // make sure internal structures of the zoom plugin are set up
  chart.scales.x.options.min = options.min; // restore the zoom
  chart.scales.x.options.max = options.max;
  chart.update(); //rerender the chart
}

Leave a comment