ChartJS: How to set fixed Y axis max and min

本秂侑毒 提交于 2020-05-12 11:54:07

问题


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
};

I tried those settings too:

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

Any idea what I'm doing wrong?

Thanks!


回答1:


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

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

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




回答2:


For Chart.js version < 3.0:

Look at Sanjay Dutt answer.

For Chart.js version >= 3.0:

Due to breaking changes in version 3.X, the above mentioned answer doesn't work anymore. Chart.js created a Migration Guide to solve the upcoming problems. The minimum and maximum values can now be configured directly to the options.scales:

options : {
    scales : {
        y : {
            min: -1,
            max: 1,
        }
    }
}


来源:https://stackoverflow.com/questions/41216308/chartjs-how-to-set-fixed-y-axis-max-and-min

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!