Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add panning to chart in chartjs?

I have created a bar chart with chartjs. I am also using chartjs-plugin-zoom. I have tried several ways but can’t find a way to add panning functionality to chart. Does anyone know how can I add this?

Here is my code:

var myChart = new Chart(ctx, {
        type: 'bar',
        data: {
                labels: [1, 2, 3, 4, 5...],
                datasets: [{
                        label: '# of Votes',
                        data: [12, 19, 3, 5, 2...]
                }]
        },
        options: {
                zoom: {
                        enabled: true,
                        mode: 'x',
                }
        }
});
like image 503
Juan Payne Avatar asked Jul 18 '17 20:07

Juan Payne


People also ask

How do I add a padding in Chartjs?

Lets say you wanted to add 50px of padding to the left side of the chart canvas, you would do: let chart = new Chart(ctx, { type: 'line', data: data, options: { layout: { padding: { left: 50 } } } }); Copied!

How do you zoom in on Chartjs?

By default, you can draw a view box or use the mouse wheel to zoom in or out. Double click to fit the chart in the canvas.

Does Chartjs use canvas?

Accessible ChartsChart. js charts are rendered on user provided canvas elements. Thus, it is up to the user to create the canvas element in a way that is accessible. The canvas element has support in all browsers and will render on screen but the canvas content will not be accessible to screen readers.


1 Answers

ꜰɪʀꜱᴛ

add hammer.js to your project :

<script src="http://cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.8/hammer.min.js"></script>

ꜱᴇᴄᴏɴᴅ

enable pan option for your chart, like so :

options: {
   pan: {
      enabled: true,
      mode: 'x',
   },
   ...
}

see demo on jsFiddle

like image 84
ɢʀᴜɴᴛ Avatar answered Sep 30 '22 17:09

ɢʀᴜɴᴛ