implement chartjs zoom with buttons
Asked Answered
C

3

6

Is it possible to implement zoom buttons for quick zooming (+ and - buttons) with chartjs Now I use this code:

                    pan: {
                    enabled: true,
                    mode: 'xy',
                    rangeMin: {
                        // Format of min pan range depends on scale type
                        x: null,
                        y: null
                    },
                    rangeMax: {
                        // Format of max pan range depends on scale type
                        x: null,
                        y: null
                    }
                },
                zoom: {
                    enabled: true,
                    mode: 'xy',
                    rangeMin: {
                        // Format of min pan range depends on scale type
                        x: 1,
                        y: 1
                    },
                    rangeMax: {
                        // Format of max pan range depends on scale type
                        x: 5,
                        y: 5
                    }

only by mouse wheel (as default) Thanks

Crosseye answered 9/4, 2018 at 7:27 Comment(0)
M
0

You can use an event handler to check/update the ticks of the xAxes, to achieve a similar effect:

$('#zoom_in').click(function(){
    ticks = myChart.chart.options.scales.xAxes[0].ticks;

    /* if x-axis is categorical, use the data.labels to determine current axis range, e.g.:
    labels = myChart.chart.data.labels; 
    startTick = labels.indexOf(ticks.min);
    endTick = labels.indexOf(ticks.max);
    */

    // below assumes ticks.min and ticks.max are numeric
    var diff = ticks.max - ticks.min;
    var factor = 0.05;

    var newMin = ticks.min + factor * diff;
    var newMax = ticks.max - factor * diff;

    ticks.min = newMin;
    ticks.max = newMax;  
    myChart.update();
});
Muncy answered 19/3, 2020 at 2:43 Comment(0)
C
0

Values ​​need to be changed to chart.config.options.scales.x

 document.getElementById("lastDays").onclick = ()=> {
        ticks = chart.config.options.scales.x;
        ticks.min = lastDaysMs;
        ticks.max = nowTime;
        chart.update();
      };
Corene answered 11/7, 2022 at 11:15 Comment(0)
A
0

There are several options to zoom in, out and reset. All answers above seem correct. Nevertheless, I found the easiest one to be

  1. Get the chart in a variable.
  2. zoomIn --> myChart.zoom(1.1) // 1.1 is just a random zoom in value to scale up
  3. zoomOut --> myChart.zoom(0.9) // scale down
  4. reset --> myChart.resetZoom() // reset the chart.
Abamp answered 4/8, 2023 at 12:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.