且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将起始值设置为“0"在图表中?

更新时间:2023-02-07 13:41:12

对于 Chart.js 2.*,刻度从零开始的选项列在 线性刻度的配置选项.这用于数值数据,这很可能是您的 y 轴的情况.所以,你需要使用这个:

For Chart.js 2.*, the option for the scale to begin at zero is listed under the configuration options of the linear scale. This is used for numerical data, which should most probably be the case for your y-axis. So, you need to use this:

options: {
    scales: {
        yAxes: [{
            ticks: {
                beginAtZero: true
            }
        }]
    }
}

此处还提供了一个示例折线图,该选项用于 y 轴.如果您的数值数据在 x 轴上,请使用 xAxes 而不是 yAxes.请注意,数组(和复数)用于 yAxes(或 xAxes),因为您也可以有多个轴.

A sample line chart is also available here where the option is used for the y-axis. If your numerical data is on the x-axis, use xAxes instead of yAxes. Note that an array (and plural) is used for yAxes (or xAxes), because you may as well have multiple axes.