ChartJS:如何设置固定 Y 轴最大值和最小值最大值、如何设置、最小值、ChartJS

2023-09-07 11:41:53 作者:药药药切克闹我说二逼你说到

我有一个折线图,我正在尝试设置 固定 最大值和最小值.我已经尝试过在 SO 上找到的其他建议,但它仍然不起作用.我的图表会根据传入的数据不断重新设置最大值和最小值.

I have a line chart which I'm trying to set a fixed max and min values. I have tried the other suggestions found on SO, but it still doesn't work. My chart keeps re-setting the max and min values depending on the incoming data.

这是我传递给折线图的当前选项:

Here's my current options that I pass to my Line chart:

var options = {
        responsive: true,
        animation: false
};

我也尝试了这些设置:

var options = {
    scales: {
        animation: false,
        yAxes: [{
            display: true,
            ticks: {
                suggestedMin: -1,
                suggestedMax: 1
            }
        }]
    }
};

知道我做错了什么吗?

谢谢!

推荐答案

要修复 yAxes 比例的最小值和最大值,请使用以下代码.

To fix min and max value for yAxes scale use the below code.

options:{
            scales: {
                yAxes : [{
                    ticks : {
                        max : 1,    
                        min : -1
                    }
                }]
            }
        }

suggestedMax 仅在传入数据低于建议最大值时才有效.suggestedMin 仅在传入数据大于建议的最小值时起作用.

suggestedMax will only works when incoming data is lower than suggestedMax value. suggestedMin will works only when incoming data is greater than suggestedMin value.