ZoomIn ZoomOut on button click
Hi love your project
So far I have been able to reset zoom on a button , but i would like toknow if there is a ZoomIn/Out event to call on button click .
Since alot of people use trackpads and dont have a mouse wheel i think would be nice future.
@LipSoft this isn't supported at the moment. It should be relatively easy to export the functions that do zooming and panning allowing them to be triggered programatically.
+1 for the feature request.
@LipSoft could you please share your code for the zoom reset button? Thanks!
@ondrejtichacek
I reset zoom like this -->
function resetZoom() {
window.scatterChart.resetZoom()
}
var ctx = $("#myChart");
window.scatterChart = new Chart(ctx, {});
+1 for this feature request! Thanks for the plugin :)
i have the chartjs-plugin-zoom.js file in my solution and just added two more function to the chart instance to zoom in and zoom out after the _WheelHandler function.
chartInstance.ZoomIn = function () {
var rect = document.getElementById(chartInstance.canvas.id).getBoundingClientRect();
var center = {
x: rect.width / 2,
y: rect.height / 2
};
doZoom(chartInstance,1.1,center);
};
chartInstance.ZoomOut = function () {
var rect = document.getElementById(chartInstance.canvas.id).getBoundingClientRect();
var center = {
x: rect.width / 2,
y: rect.height / 2
};
doZoom(chartInstance,0.909,center);
};
and then I just call them on the button click
i have the chartjs-plugin-zoom.js file in my solution and just added two more function to the chart instance to zoom in and zoom out after the _WheelHandler function.
chartInstance.ZoomIn = function () { var rect = document.getElementById(chartInstance.canvas.id).getBoundingClientRect(); var center = { x: rect.width / 2, y: rect.height / 2 }; doZoom(chartInstance,1.1,center); }; chartInstance.ZoomOut = function () { var rect = document.getElementById(chartInstance.canvas.id).getBoundingClientRect(); var center = { x: rect.width / 2, y: rect.height / 2 }; doZoom(chartInstance,0.909,center); };and then I just call them on the button click
Woul love to see working example, cant make it work.
Even if users can handle such use cases by setting min / max manually, I think that exposing a doZoom public method in the plugin API would make life easier. Don't you think @benmccann?
function doZoom(percentZoomX, percentZoomY, focalPoint, options)
There's often a complex calculation in the plugin code resulting in the value of focalPoint that's used (and which has led to some bugs in the past). I think it might be a very difficult value for the user to come up with and so I wonder if there might be a more user-friendly API we could come up with and would be a bit hesitant to make the internal API available
I have added the following public method:
chartInstance.doZoom = function(percentage) {
doZoom(chartInstance, 1 + (percentage / 100), 1 + (percentage / 100), undefined, undefined, true);
// Manually trigger zoom complete callback.
const zoomOptions = chartInstance.$zoom._options.zoom;
if (typeof zoomOptions.onZoomComplete === 'function') {
zoomOptions.onZoomComplete({chart: chartInstance});
}
}
The focalPoint parameter is omitted, because it is calculated from the center in the doZoom function.
This works perfectly fine with version 0.7.4 (I haven't tested it on newer versions).
Usage example:
chart.doZoom(10); // Zoom in
chart.doZoom(-10); // Zoom out
Hey, @JelleBlaauw I tried using 0.7.4 version as well but it is still not working. I checked the object as well but there is no function like doZoom. Every time I get doZoom is not a function. Let me know where I am doing wrong.
Hey, @JelleBlaauw I tried using 0.7.4 version as well but it is still not working. I checked the object as well but there is no function like doZoom. Every time I get doZoom is not a function. Let me know where I am doing wrong.
It works for me if I add the function inside the source code for the plugin, right after chartInstance.resetZoom = ... in the afterInit-function.
Is feature added natively or stll we have to add functions to chartjs plugin zoom files?
Please take look of message. Appear a easy solution. https://github.com/chartjs/chartjs-plugin-zoom/issues/123#issuecomment-544372830
I have added the following public method:
chartInstance.doZoom = function(percentage) { doZoom(chartInstance, 1 + (percentage / 100), 1 + (percentage / 100), undefined, undefined, true); // Manually trigger zoom complete callback. const zoomOptions = chartInstance.$zoom._options.zoom; if (typeof zoomOptions.onZoomComplete === 'function') { zoomOptions.onZoomComplete({chart: chartInstance}); } }The
focalPointparameter is omitted, because it is calculated from the center in thedoZoomfunction. This works perfectly fine with version 0.7.4 (I haven't tested it on newer versions).Usage example:
chart.doZoom(10); // Zoom in chart.doZoom(-10); // Zoom out
Worked like a charm. I am using v0.7.7 and I had problem with getBoundingClientRect. But this solution worked . thanks
I have added the following public method:
chartInstance.doZoom = function(percentage) { doZoom(chartInstance, 1 + (percentage / 100), 1 + (percentage / 100), undefined, undefined, true); // Manually trigger zoom complete callback. const zoomOptions = chartInstance.$zoom._options.zoom; if (typeof zoomOptions.onZoomComplete === 'function') { zoomOptions.onZoomComplete({chart: chartInstance}); } }The
focalPointparameter is omitted, because it is calculated from the center in thedoZoomfunction. This works perfectly fine with version 0.7.4 (I haven't tested it on newer versions). Usage example:chart.doZoom(10); // Zoom in chart.doZoom(-10); // Zoom outWorked like a charm. I am using v0.7.7 and I had problem with getBoundingClientRect. But this solution worked . thanks
Could you tell me how to do this? I can't do it, I try to access doZoom and it says it's internal to the plugin