简体   繁体   English

Chart.js 自动缩放

[英]Chart.js automatic scaling

I've been testing out chart.js for maybe including it in a project.我一直在测试 chart.js 以便将它包含在一个项目中。 I just have one smallish issue with it.我只有一个小问题。 It doesn't scale some datasets properly.它不能正确缩放某些数据集。 I found somewhat similar questions here in SO, but nothing really that would solve my issue.我在 SO 中发现了一些类似的问题,但没有什么能真正解决我的问题。

So this is what my chart looks like with some datasets:所以这就是我的图表在一些数据集上的样子: 在此处输入图片说明

For some reason, the max values (2.2) look ugly as hell.出于某种原因,最大值(2.2)看起来很难看。 The reason is, that the dataset has a set of three duplicate values in this case (2.2, 2.2, 2.2).原因是,在这种情况下,数据集具有一组三个重复值(2.2、2.2、2.2)。 I would like there to be some room on top of this series, so that it would look a bit more reasonable.我希望在这个系列的顶部有一些空间,这样它看起来会更合理一些。

How do you properly add "padding" on top the the series line?您如何在系列线的顶部正确添加“填充”? I would like a solution where I could rely on chart.js to find out the max value, and then maybe update that with some padding on the scale(like x + 10).我想要一个解决方案,我可以依靠 chart.js 来找出最大值,然后可能会在比例尺上使用一些填充(例如 x + 10)来更新它。 Issue is, that I cant just hard code the min max values, because I can have over 200 different values that can be selected, and they can vary A LOT in scale.问题是,我不能只是硬编码最小最大值,因为我可以选择 200 多个不同的值,并且它们可以在规模上变化很多。

Here is how I render the chart for now:这是我现在渲染图表的方式:

    return new Chart(context, {
    type: 'line',
    data: { 
        labels: labels.data,
        datasets: dataSets
    },
    options: {
        responsive: true,
        maintainAspectRatio: false,
        scales: {
            ticks: {
                beginAtZero: false
            },
            yAxes: [{
                ticks: {
                    beginAtZero: false
                },
                id: 'A',
                type: 'linear',
                position: 'left',
            }, {
                ticks: {
                    beginAtZero: false
                },
                display: displayBAxis,
                id: 'B',
                type: 'linear',
                position: 'right',
            }]
        }
    }
});

This may not be the cleanest way but you can play with it as you see fit.这可能不是最干净的方式,但您可以根据自己的喜好进行操作。

In short, you need to create a function that finds the max value out of all your dataSets arrays and in yAxes.ticks set max to that functions return value.简而言之,您需要创建一个函数,该函数从所有 dataSets 数组中找到最大值,并在 yAxes.ticks 中将 max 设置为该函数的返回值。

More details below更多详情如下

Referencing here first:先参考这里:

data:{
    labels: labels.data,
    datasets: dataSets //talking about this first
}

Create a function that will go through these arrays and get the max of all arrays.创建一个函数,该函数将遍历这些数组并获取所有数组的最大值。 I did it using this but you can do it better I'm sure:我是用这个做的,但你可以做得更好,我敢肯定:

function getMax(){
    var max = 0;
    dataSets.forEach(function(x, i){        
        max = Math.max(max, Math.max.apply(null, x.data));
    });

    return max + 2;
}

Then in your return in yAxes you should be able to have:然后在您return yAxes 时,您应该能够拥有:

ticks: {
    beginAtZero: false,
    max: getMax()
},

You can find the min and max of your dataset, then add/subtract a percentage of the difference (range).您可以找到数据集的最小值和最大值,然后添加/减去差异(范围)的百分比。

For example, say you use 1% of the range.例如,假设您使用范围的 1%。

[0.1, 0.2, 0.5] gives (1%)x(0.5-0.1)=0.004, so [min,max]=[0.096,0.504]. [0.1, 0.2, 0.5] 给出 (1%)x(0.5-0.1)=0.004,所以 [min,max]=[0.096,0.504]。

[1500, 1800, 3500, 3600] gives (1%)x(3600-1500)=21, so [min,max]=[1479,3621]. [1500, 1800, 3500, 3600] 给出 (1%)x(3600-1500)=21,所以 [min,max]=[1479,3621]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM