chartjs-plugin-zoom icon indicating copy to clipboard operation
chartjs-plugin-zoom copied to clipboard

Add mouseButton for zoom reset to Zoom Options

Open bairog opened this issue 3 years ago • 2 comments

It would be very handy to have some settings for zoom reset in Zoom Options. From my point of view it could be at least mouseButton (maybe also modifierKey but I'm not sure about it). Generating special button for zoom reset that simply uses chart.resetZoom(); API call is not always possible (it can break the design of HTLM page for example). Some time ago I even had to modify chartjs-plugin-zoom.js file to add this functionality - but that is not the best practice I think:

chartInstance.$zoom._mouseDownHandler = function (event) {
  	node.addEventListener('mousemove', chartInstance.$zoom._mouseMoveHandler);
  	chartInstance.$zoom._dragZoomStart = event;

  	// CHANGED: middle mouse button is clicked - for resetting zoom
  	if (event.button === 1) {
  		chartInstance.resetZoom();
  	}
  };

Maybe now it's time to add some options? Thank you in advance.

Relates to #325

bairog avatar Sep 29 '21 12:09 bairog

You could probably do something like:

document.addEventListener('click', function(e){
  if(e.button === 1 && e.target.tagName === "canvas"){
    const chartInstance = Chart.getChart(e.target);
    if (chartInstance && chartInstance.resetZoom) {
      chartInstance.resetZoom();
    }
  }
})

kurkle avatar Oct 19 '21 16:10 kurkle

@kurkle Thank you for your workaround. Slightly modified jQuery code (left mouse button double click restores zoom and pan):

var ctx = document.getElementById("myChartCanvasId");
var myChart = new Chart(ctx, {...});
...
$(document).ready(function () {
     $('#myChartCanvasId').dblclick( function (e) {
          //left mouse button double click restores zoom and pan - resetZoom
          if (e.button === 0) {
               if (myChart && myChart.resetZoom) {
                    myChart.resetZoom();
               }
          }
     });
});

bairog avatar Oct 20 '21 05:10 bairog