JavaScript Start bar-chart at 0 using ChartJS chart start with 0

I have a bar chart which use ChartJS that shows data. Based on the bar chart, I have the data: 15, 2, 0, 11.

I can see all of these data in the bar, except 0. Is there a possibility to start the bar chart on 0, so I can also see the data for the fourth column in the bar chart?

options: {
  legend: { display: false },
  scales: {
      yAxes: [{
          display: false,
      }],
      xAxes: [{
          display: true,
      }],
  },
  title: {
    display: false,
  }

}
Answer:1

After hours of working, finally i found a soulution. There is no chartjs configuration to do it and you have to draw it your self. Let do it in onComplete function

var ctx = document.getElementById("myChart").getContext('2d');
var datasets = [15, 2, 0, 11];
var myChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ["1", "2", "3", "4"],
    datasets: [{
      label: 'value',
      backgroundColor: '#F00',
      data: datasets
    }]
  },
  options: {
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true
        }
      }]
    },
    animation: {
      onComplete: function() {
        var dataSet = myChart.getDatasetMeta(0);
        dataSet.data.forEach(elm => { 
          if (datasets[elm._index] == 0) {
            ctx.fillStyle = '#F00';
            ctx.fillRect(elm._model.x - elm._view.width / 2, elm._model.y - 1, elm._view.width, 2);
          }
        })
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas class="canvasChart" id="myChart"></canvas>
Answer:2

Because your Y-axis values starts from 0 to something. So, if your value is 0 in X-Axis than it would be null and won't show you bar of 0. So, if you want 0 to appear in chart your starting point should be less than the 0.

Answer:3

You can try beginAtZero:true config option.

options: {
  legend: { display: false },
  scales: {
      yAxes: [{
          display: false,
          ticks: {
                beginAtZero:true
            }
      }],
      xAxes: [{
          display: true,
      }],
  },
  title: {
    display: false,
  }
}
Answer:4

I believe you said you were using chartsjs. This question already has an answer here.

Answer:5

I have a strange problem with ASP.NETmaster page and code behind. When Page.Header.DataBind() is called somehow all public method on my angular service are called. Here is my angular service: ...

I have a strange problem with ASP.NETmaster page and code behind. When Page.Header.DataBind() is called somehow all public method on my angular service are called. Here is my angular service: ...

  1. page.header.databind()
  2. page.header.databind failed to load viewstate
  3. page header databind in c#
  4. asp.net page.header.databind

function change(element) { document.getElementById('imageReplace').src = element; } function change_1() { document.getElementById('1').style.background = '#111'; document.getElementById('...

function change(element) { document.getElementById('imageReplace').src = element; } function change_1() { document.getElementById('1').style.background = '#111'; document.getElementById('...

  1. javascript takes long time load
  2. javascript window.open takes long time
  3. javascript function takes too long

I'm using Lodash to format a string like DIGITAL & TECHNOLOGY into Digital & Technology. I've tried with the following code: _.startCase(_.lowerCase('DIGITAL & TECHNOLOGY')); However, ...

I'm using Lodash to format a string like DIGITAL & TECHNOLOGY into Digital & Technology. I've tried with the following code: _.startCase(_.lowerCase('DIGITAL & TECHNOLOGY')); However, ...

  1. format strings with python
  2. format strings with printf
  3. format strings in c
  4. format strings in java
  5. format strings in javascript
  6. format strings in python 3
  7. format strings in r
  8. format strings in golang
  9. format strings in excel
  10. format strings python 3
  11. format strings in powershell
  12. format strings in matlab
  13. format strings in ruby
  14. format strings in swift
  15. format strings in js
  16. format strings in php
  17. format strings in sql
  18. format strings in python 2
  19. format strings in scala
  20. format strings in vb.net

I have an angularJS directive, which has it's templateUrl property set to a function, that determines which template to use based on a service function. When the service finishes a certain job (which ...

I have an angularJS directive, which has it's templateUrl property set to a function, that determines which template to use based on a service function. When the service finishes a certain job (which ...

  1. angularjs directive with parameters
  2. angularjs directive with controller
  3. angularjs directive with template
  4. angularjs directive with ng-model
  5. angularjs directive with typescript
  6. angularjs directive with controller and link
  7. angularjs directive with attribute
  8. angularjs directive with html template
  9. angularjs directive with scope
  10. angularjs directive with templateurl
  11. angularjs directive with input
  12. angularjs directive with dynamic template
  13. angularjs directive with params
  14. angularjs directive with arguments
  15. angularjs directive with service
  16. angularjs directive with controller example
  17. angularjs directive with value
  18. angularjs directive with css
  19. angularjs directive with ng-repeat
  20. angularjs directive with function