chartjs-plugin-zoom icon indicating copy to clipboard operation
chartjs-plugin-zoom copied to clipboard

ZoomIn ZoomOut on button click

Open LipSoft opened this issue 10 years ago • 15 comments

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 avatar Oct 04 '16 10:10 LipSoft

@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.

etimberg avatar Oct 04 '16 13:10 etimberg

+1 for the feature request.

@LipSoft could you please share your code for the zoom reset button? Thanks!

ondrejtichacek avatar Oct 14 '16 15:10 ondrejtichacek

@ondrejtichacek

I reset zoom like this -->

function resetZoom() {
  window.scatterChart.resetZoom()
}
var ctx = $("#myChart");

window.scatterChart = new Chart(ctx, {});

LipSoft avatar Oct 15 '16 21:10 LipSoft

+1 for this feature request! Thanks for the plugin :)

hornergraphic avatar Feb 14 '17 19:02 hornergraphic

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

kartik-ramachandran avatar Dec 12 '18 00:12 kartik-ramachandran

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.

ravau avatar Jun 07 '19 17:06 ravau

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)

jledentu avatar Nov 20 '19 09:11 jledentu

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

benmccann avatar Nov 20 '19 22:11 benmccann

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

JelleBlaauw avatar Jul 14 '20 09:07 JelleBlaauw

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.

Vijay-Gaikwad avatar Aug 30 '20 20:08 Vijay-Gaikwad

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.

cederlof avatar Sep 29 '20 14:09 cederlof

Is feature added natively or stll we have to add functions to chartjs plugin zoom files?

Priyajit08 avatar Oct 08 '20 00:10 Priyajit08

Please take look of message. Appear a easy solution. https://github.com/chartjs/chartjs-plugin-zoom/issues/123#issuecomment-544372830

carlosanpardo avatar Apr 09 '21 20:04 carlosanpardo

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

Worked like a charm. I am using v0.7.7 and I had problem with getBoundingClientRect. But this solution worked . thanks

shuhad avatar Apr 28 '23 10:04 shuhad

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

Worked 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

FelipeD4ntas avatar Sep 28 '23 15:09 FelipeD4ntas