Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add zoom event handler to charts for chartjs with chartjs-plugin-zoom

chartjs-plugin-zoom is a zoom and pan plugin for Chart.js

You can call chart.resetZoom() to programmatically resets the zoom to the default state. See this example on jsfiddle.

HTML:

<div class="myChartDiv">
    <canvas id="myChart" width="600" height="400"></canvas>
</div>

<button id="reset_zoom">
    Reset zoom
</button>

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: {
        pan: {
            enabled: true,
            mode: 'x',
        },
        zoom: {
            enabled: true,                      
            mode: 'x',
        }
    }
});

$('#reset_zoom').click(function(){
    myChart.resetZoom();
});

And it looks like:enter image description here

However, I don't want to show the Reset zoom button in the first place. Instead, I would like to hide it first, and then listen for a zoom event (In chartjs-plugin-zoom the event is mouse wheel and pinch for touch screens) to show it.

So the question is, is there a way in chartjs with chartjs-plugin-zoom to add a zoom event handler (wheel and pinch events) to a chart? Thanks.

like image 838
Yuci Avatar asked Jun 14 '17 18:06

Yuci


People also ask

How do I add a plugin to ChartJS?

Plugins can be shared between chart instances: const plugin = { /* plugin implementation */ }; // chart1 and chart2 use "plugin" const chart1 = new Chart(ctx, { plugins: [plugin] }); const chart2 = new Chart(ctx, { plugins: [plugin] }); // chart3 doesn't use "plugin" const chart3 = new Chart(ctx, {}); Copied!

Is ChartJS customizable?

If you have a simple dataset with few customization tweaks, it's a great solution. However, as your data visualization and customization needs grow — the more you'll benefit from getting under the hood of ChartJS. We can refer directly to the ChartJS documentation and customize as much as we need.

Does ChartJS use canvas?

A Node JS renderer for Chart. js using canvas. Provides and alternative to chartjs-node that does not require jsdom (or the global variables that this requires) and allows chartJS as a peer dependency, so you can manage its version yourself.


2 Answers

Unfortunately, you can not add mouse scroll event to the chart, as the chartjs-plugin-zoom will prevent that by default.

However, you can use the following chart plugin, which will get the job done for you ...

plugins: [{
   beforeDraw: function(c) {
      var reset_zoom = document.getElementById("reset_zoom"); //reset button
      var ticks = c.scales['x-axis-0'].ticks.length; //x-axis ticks array
      var labels = c.data.labels.length; //labels array
      if (ticks < labels) reset_zoom.hidden = false;
      else reset_zoom.hidden = true;
   }
}]

add this plugin followed by your chart options.

Basically, the way chartjs-plugin-zoom works is, it removes/adds elements from/to the ticks array on mouse scroll, and the above plugin will check for that, henceforth show / hide the rest zoom button accordingly.

ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ ⧩

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: {
      pan: {
         enabled: true,
         mode: 'x',
      },
      zoom: {
         enabled: true,
         mode: 'x',
      }
   },
   plugins: [{
      beforeDraw: function(c) {
         var reset_zoom = document.getElementById("reset_zoom"); //reset button
         var ticks = c.scales['x-axis-0'].ticks.length; //x-axis ticks array
         var labels = c.data.labels.length; //labels array
         if (ticks < labels) reset_zoom.hidden = false;
         else reset_zoom.hidden = true;
      }
   }]
});

$('#reset_zoom').click(function() {
   myChart.resetZoom();
});
.myChartDiv {
   max-width: 600px;
   max-height: 400px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<script src="https://npmcdn.com/[email protected]/Chart.Zoom.min.js"></script>
<div class="myChartDiv">
   <canvas id="myChart" width="600" height="400"></canvas>
</div>
<button id="reset_zoom" hidden>
   Reset zoom
</button>
like image 77
ɢʀᴜɴᴛ Avatar answered Oct 08 '22 11:10

ɢʀᴜɴᴛ


Another approach to this is to use the zoom plugin onZoomComplete event.

First, you should apply a style to the zoom button that will have it hidden by default.

.reset-zoom {
    display: none;
}

and the button

<button id="reset_zoom" class="reset-zoom">
    Reset zoom
</button>

Then on the chart's zoom options you should add

zoom: {
   enabled: true,
   mode: 'x',
   onZoomComplete: function(myChart) {
         $('#reset_zoom').show();
   }
}

And in order to hide the zoom button again once a resetZoom is called you could add

$('#reset_zoom').click(function(){
    myChart.resetZoom(); 
    $('#reset_zoom').hide();
});

So each time you zoom in the reset zoom button is displayed and once you reset the zoom it is hidden again.

like image 1
Nikos Steiakakis Avatar answered Oct 08 '22 13:10

Nikos Steiakakis