Imran
Imran

Reputation: 1104

Why ChartJS's Bar Chart does not render bar for a specific value?

I am using ChartJS v 2.8.0

I am using following data, but it does not show bar for "4.01":

data:[9.24, 6.28, 5.65, 6.74, 4.01, 17.15, 4.01]

var options = {
  type: 'bar',
  data: {
    labels:["Jan", "Feb", "Mar", "Apr", "May", "June", "July"],
    datasets: [
        {
          label: '# of Votes',
          data:[9.24, 6.28, 5.65, 6.74, 4.01, 17.15, 4.01],
        borderWidth: 1
        }
        ]
  },
  options: {
    scales: {
        yAxes: [{
        ticks: {
                    reverse: false
        }, 
      }],
        xAxes: [{
      }]
    }
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
canvas { background-color : #eee;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>
<body>
    <canvas id="chartJSContainer" width="600" height="200"></canvas>
</body>

When I use slightly different data it works perfect:

data:[9.24, 6.28, 5.65, 6.74, 4.01, 17.15, 3]

var options = {
  type: 'bar',
  data: {
    labels:["Jan", "Feb", "Mar", "Apr", "May", "June", "July"],
    datasets: [
        {
          label: '# of Votes',
          data:[9.24, 6.28, 5.65, 6.74, 4.01, 17.15, 3],
        borderWidth: 1
        }
        ]
  },
  options: {
    scales: {
        yAxes: [{
        ticks: {
                    reverse: false
        }, 
      }],
        xAxes: [{
      }]
    }
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
canvas { background-color : #eee;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>
<body>
    <canvas id="chartJSContainer" width="600" height="300"></canvas>
</body>

Am I doing something wrong or is it some kind of bug? How to fix it?

Upvotes: 0

Views: 243

Answers (1)

shrys
shrys

Reputation: 5940

You need to use beginAtZero: true in yAxes.ticks for both charts which will allow you to specify that the scale starts at 0.

var options = {
  type: 'bar',
  data: {
    labels: ["Jan", "Feb", "Mar", "Apr", "May", "June", "July"],
    datasets: [{
      label: '# of Votes',
      data: [9.24, 6.28, 5.65, 6.74, 4.01, 17.15, 3],
      borderWidth: 1
    }]
  },
  options: {
    scales: {
      yAxes: [{
        ticks: {
          reverse: false,
          beginAtZero: true
        },
      }],
      xAxes: [{}]
    }
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
canvas {
  background-color: #eee;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>

<body>
  <canvas id="chartJSContainer" width="600" height="200"></canvas>
</body>
var options = {
  type: 'bar',
  data: {
    labels: ["Jan", "Feb", "Mar", "Apr", "May", "June", "July"],
    datasets: [{
      label: '# of Votes',
      data: [9.24, 6.28, 5.65, 6.74, 4.01, 17.15, 4.01],
      borderWidth: 1
    }]
  },
  options: {
    scales: {
      yAxes: [{
        ticks: {
          reverse: false,
          beginAtZero: true
        },
      }],
      xAxes: [{}]
    }
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
canvas {
  background-color: #eee;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>

<body>
  <canvas id="chartJSContainer" width="600" height="200"></canvas>
</body>

Upvotes: 2

Related Questions