chart.js - 用按钮实现chartjs缩放

标签 chart.js zooming

是否可以使用chartjs实现用于快速缩放的缩放按钮(+和-按钮)
现在我使用这个代码:

                    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
                    }

仅通过鼠标滚轮(默认)
谢谢

最佳答案

您可以使用事件处理程序来检查/更新 xAxes 的刻度,以实现类似的效果:

$('#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();
});

关于chart.js - 用按钮实现chartjs缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49727597/

相关文章:

javascript - Chart Js使用图形线条样式更新图形的图例框

javascript - ChartJS - 覆盖属性(AngularJS)

android - 放大动画

javascript - d3js 中的缩放线

android - 如何在 Android 的 web View 中显示内置缩放控件

javascript - 如何从图表中删除 y 轴标签?

javascript - 如何在 chart.js (chartjs.org) 中为所有图表类型添加标签/图例?

javascript - 如何动态填充我的 ChartJS 饼图

javascript - 将缩放和旋转值重置为零 Onclick 按钮

css - 在浏览器中自动缩放 Bootstrap 元素