score:4
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/Chart.Zoom.js@0.3.0/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>
score:1
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.
Source: stackoverflow.com
Related Query
- Add zoom event handler to charts for chartjs with chartjs-plugin-zoom
- Chartjs with zoom plugin zooms too much with wheel just with single turn
- How to add left padding for my charts done in ChartJs and my Google Map so it is not glued to the limit of the page on the left
- add a unit to label with chartjs plugin datalabels
- Customize fill colors for ChartJS 3.x using beforRender plugin event
- Chartjs random colors for each part of pie chart with data dynamically from database
- ChartJS - Donut charts with multiple rings
- How to add label for ChartJs Legend
- Chart.js - Line charts with different colors for each border lines
- Chartjs - data format for bar chart with multi-level x-axes
- implement chartjs zoom with buttons
- How to get onClick Event for a Label in ChartJS and React?
- Add mouseout event handler to legends in Chart.js
- How to add ChartJS code in Html2Pdf to view image
- How to remove bars for those bars with zero value in Chartjs bar chart?
- UnitStepSize for regular time interval with Chartjs
- how to set color for each item in tooltip with ChartJS
- chartjs plugin datalabels does not show value on charts
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- Setting Common labels and background color common for all the charts in ChartJs
- Chartjs - Add backgroundColor for labels radar chart
- How can I load multiple Chartjs charts with different data on the same page?
- Chartjs not working with d3 from csv source
- Zoom is very slow in ChartJS Zoom Plugin
- Zoom chartjs with many X values
- Generate multiple line charts in Django with ChartJS
- ChartJS Version 3 - common legend for multiple line charts
- How to create a custom tooltip for chartJS graph with data in JSON array in JavaScript?
- Map JSON for Chartjs with Angular 7
- Issue with chartjs linear gradient for the mixed bar chart in ReactJS is not calculated for each individual Bars
More Query from same tag
- Stacked Horizontal Bars not in align Chart JS
- django + chart.js : Cannot get two bar charts on the same graph with appropriate data
- Load JSON to display data, using ng-repeat {ANGULAR.JS}
- ChartJS - Show percentage on hover (AngularJS)
- react-chartjs-2 keep x axis always 0% to 100%
- Accordion won't take up height of chart for first click
- How to make the x-axis start from zero in chartjs-plugin-streaming?
- How to Create Single line bubble Chartjs chart?
- multiple line charts with independent data Javascript (Chart.js or google-vizualisation)
- How to grouping Label In Char JS
- Is Chartjs compatible with PhoneGap?
- Getting a Chart is not defined ReferenceError with in React + React-chartjs
- How to access the Chart object in Javascript DOM?
- How to realize a discontinuous section (jumps) in a continuous line plot with Chart.js?
- chat,js stacked bar chart (make one dataset into stacked)
- How to install Chart.js without a library?
- ChartJS - Highlight Weekends and Lowest and Highest Values
- How to use a string returned from php by ajax as data for a chart.js chart?
- Can't apply options of datalabels chartjs plugin in Vue
- Change height of chartjs dynamically
- Angular chart.js onClick is not working - how to get all elements of a bar when it is clicked on
- chart js: when all the values passed to data are zeros, nothing is showing
- Chart.js in Angular Radar Chart
- generate multiple charts via loop for chart js
- chart.js hide gridline inside chartarea but keep y-axis border
- Chart.js - Add text/label to bubble chart elements without using tooltips?
- How to highlight a section of a stack in all bars in a stacked barchart in chartjs
- How to set defaults for scales in ChartJS?
- Chart.js canvas : can't center it
- Set min, max and number of steps in radar chart.js