ChartJS栏没有显示简单的数据点

时间:2017-03-03 22:32:41

标签: javascript bar-chart chart.js

这是一个简单的chart.js水平条形图我正在使用:

var myBarChart = new Chart(ctx,{
  type: 'horizontalBar',
  data: {
    labels: [
        "Foo",
        "Bar",
        "Baz"
    ],
    datasets: [
    {
        data: [725000, 600000, 900000],
        backgroundColor: [
          "#ccf6ec",
          "#ff6654",
          "#009784"
        ],
        hoverBackgroundColor: [
          "#ccf6ec",
          "#ff6654",
          "#009784"
        ]
    }]
  },
  options: {
    legend: {
      display: false
    },
    scales: {
      xAxes: [{
        display: false
      }],
      yAxes: [{
        display: false
      }],
    }
  }
});

enter image description here

如果我将值更改为彼此更接近,我会看到三个条形图。我需要它来绘制条形图,这样它们总是可见的,即使它们的值差别很大。

我错过了配置吗?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

出于某种原因,chart.js使用最少的数据作为最小刻度值(很可能是由于数据的值)。简单地添加ticks.min: 0显然可以解决问题:



var ctx = document.getElementById('chart')
var myBarChart = new Chart(ctx, {
  type: 'horizontalBar',
  data: {
    labels: [
      "Foo",
      "Bar",
      "Baz"
    ],
    datasets: [{
      data: [725000, 600000, 900000],
      backgroundColor: [
        "#ccf6ec",
        "#ff6654",
        "#009784"
      ],
      hoverBackgroundColor: [
        "#ccf6ec",
        "#ff6654",
        "#009784"
      ]
    }]
  },
  options: {
    legend: {
      display: false
    },
    scales: {
      xAxes: [{
        display: false,
        ticks: {
          min: 0
        }
      }],
      yAxes: [{
        display: false
      }],
    }
  }
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<canvas id="chart" width="400" height="400"></canvas>
&#13;
&#13;
&#13;