图表js数据从零开始

Chart js data to start at zero

我就是这样设置数据的:

    const data = {
        labels: ['February', 'March'],
        datasets: [
            {
                label: 'My First dataset',
                backgroundColor: 'rgba(255,99,132,0.2)',
                borderColor: 'rgba(255,99,132,1)',
                borderWidth: 1,
                hoverBackgroundColor: 'rgba(255,99,132,0.4)',
                hoverBorderColor: 'rgba(255,99,132,1)',
                data: [5, 9]
            }
        ]
    };

但是第一个元素设置了轴的起点:

但我希望它从零开始

添加以下内容没有帮助:

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

我在文档中找不到其他设置来执行此操作。

顺便说一句,我正在使用这个:https://www.npmjs.com/package/react-chartjs-2

尝试将最小值添加到您的选项中:

    var options = {
      scales: {
        yAxes: [{
          ticks: {
            beginAtZero: true,
            min: 0
          }    
        }]
      }
    };

现场直播:Chart.js Start at zero

替换为:

const options = {
  scales: {
    y: {
      beginAtZero: true
    }
  }
};

成功了!

根据chart.js 3.7.1 版本

直接在 x 或 y 中写“beginAtZero”而不是在刻度中

   const options = {
    scales: {
         x: {
           beginAtZero: true,            
         },
         y: {
           beginAtZero: true,
         }
       }
    };