Disable and enable zoom - Javascript Chart.js

Javascript examples for Chart.js:Chart Configuration

Description

Disable and enable zoom

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>Chart.js zoom event</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.js"></script> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.8/hammer.min.js"></script> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script> 
      <script type="text/javascript" src="https://npmcdn.com/Chart.Zoom.js@0.3.0/Chart.Zoom.min.js"></script> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 
      <style id="compiled-css" type="text/css">

.myChartDiv {/*www .j av a  2s .  c om*/
   max-width: 600px;
   max-height: 400px;
}


      </style> 
   </head> 
   <body> 
      <div class="myChartDiv"> 
         <canvas id="myChart" width="600" height="400"></canvas> 
      </div> 
      <button id="reset_zoom"> Reset zoom </button> 
      <button id="disable_zoom"> Disable zoom </button> 
      <button id="enable_zoom"> Enable zoom </button> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <h1>test</h1> 
      <script type="text/javascript">
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
        datasets: [{
            label: '# of Votes',
            data: [12, 19, 3, 5, 2, 3]
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        },
        // Container for pan options
        pan: {
            // Boolean to enable panning
            enabled: true,
            // Panning directions. Remove the appropriate direction to disable
            // Eg. 'y' would only allow panning in the y direction
            mode: 'x',
            speed: 1
        },
        // Container for zoom options
        zoom: {
            // Boolean to enable zooming
            enabled: true,
            // Zooming directions. Remove the appropriate direction to disable
            // Eg. 'y' would only allow zooming in the y direction
            mode: 'x',
        }
    }
});
$('#reset_zoom').click(function(){
    myChart.resetZoom();
    console.log(myChart);
});
$('#disable_zoom').click(function(){
    myChart.ctx.canvas.removeEventListener('wheel', myChart._wheelHandler);
});
$('#enable_zoom').click(function(){
    myChart.ctx.canvas.addEventListener('wheel', myChart._wheelHandler);
});

      </script>  
   </body>
</html>

Related Tutorials