简体   繁体   English

Chart.js beginAtZero不起作用

[英]Chart.js beginAtZero doesn't work

I've got problems with Chart.js because the bars aren't starting at zero even though I set the beginAtZero argument. 我遇到了Chart.js问题,因为即使我设置了beginAtZero参数,柱线也不是从零开始。

I tried multiple ways for the right position of the code, but it doesn't seem to work. 我尝试了多种方法来确保代码的正确位置,但似乎不起作用。

Does anyone have an idea of how to fix my problem? 有谁知道如何解决我的问题?

Here is the code: 这是代码:

var chartdata = {
  labels: answer,
  datasets: [{
      label: 'Count',
      backgroundColor: 'rgba(200, 200, 200, 0.75)',
      borderColor: 'rgba(200, 200, 200, 0.75)',
      hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
      hoverBorderColor: 'rgba(200, 200, 200, 1)',
      data: count
    }
  ]
};

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

var ctx = $("#mycanvas");

var barGraph = new Chart(ctx, {
  type: 'bar',
  data: chartdata
  options: options
});

It looks like you are using Chart.js v1 instead of v2. 看起来您正在使用Chart.js v1而不是v2。

First, update your the Chart.js code to v2, preferably the latest version . 首先,将Chart.js代码更新为v2,最好是最新版本

Then, change the following code: 然后,更改以下代码:

var barGraph = new Chart(ctx, {
    type: 'bar',
    data: chartdata
    options: options
});

to: 至:

var barGraph = Chart.Bar(ctx, {
  data: chartdata,
  options: options
});

beginAtZero: true should then work as expected. beginAtZero: true应会按预期工作。

Working JSFiddle: https://jsfiddle.net/ko54hp6n/ 工作的JSFiddle: https ://jsfiddle.net/ko54hp6n/


For an alternative to beginAtZero: true , you can also use the min: 0 ticks configuration option to start the bar axis at zero: 对于beginAtZero: true的替代方法,您还可以使用min: 0 ticks配置选项将钢筋轴从零开始:

var options = {
  scales: {
    yAxes: [{
      ticks: {
        min: 0
      }
    }]
  }
}

Working JSFiddle: https://jsfiddle.net/ko54hp6n/1/ 工作的JSFiddle: https ://jsfiddle.net/ko54hp6n/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM