Unable to make y-axis to begin at zero on Charts.js v2.1.4

爷,独闯天下 提交于 2020-01-05 04:05:10

问题


When using the following code to generate a chart, I am not able to set the scale to begin at zero, even setting that option.

Here is the code (JSFiddle)

var simpleChart = new Chart(document.getElementById('chart'), {
    type: 'bar',
    data: {
      labels: ['Online', 'Offline'],
      datasets: [{
        label: "# of servers",
        data: [1, 7],
        backgroundColor: [
            '#009245',
            '#c02a31'
        ]
      }]
    },
    options: {
        title: {
            display: false
        },
        scales: {
            yAxes: [{
                beginAtZero: true
            }]
        }
    }
});

Is this a bug? What should I do to be able to set the scale to really start at 0?

Also, how can I disable that "# of servers" label from being rendered?


回答1:


I had the same problems. You have to define "ticks" within the yAxes-definition:

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

Updated fiddle: https://jsfiddle.net/r90Ljzvo/5/



来源:https://stackoverflow.com/questions/37573689/unable-to-make-y-axis-to-begin-at-zero-on-charts-js-v2-1-4

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