Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Always show 0 on Bar chart axis

Open borgar opened this issue 13 years ago • 12 comments

Bar chart axis should always include a 0.

Bars must begin at zero because of the way we read them. We compare their heights (vertical bars) or lengths (horizontal bars), which only represent the differences in values accurately when they begin at zero. For example if the scale begins at 50, a bar that represents a value of 70 would be twice as high or long as a bar that represents a value of 60, resulting in an inaccurate representation of the difference between them.

— Stephen Few

If there is a need to break this it should be set up as an option override.

Test: http://jsfiddle.net/QwCKB/

borgar avatar Mar 18 '13 11:03 borgar

I've harcoded this in my fork:

airblade avatar Jun 03 '13 12:06 airblade

I agree. On the list of todos for the next major release.

nnnick avatar Jun 05 '13 21:06 nnnick

Wow, can't believe this isn't available! I'll look elsewhere for bar charts. :-(

jameshfisher avatar Jul 12 '13 00:07 jameshfisher

This is a most flabbergasting flaw. Unbelievable that this beautiful plugin missed a fundamental business criteria like that. Both bars and lines need this option of course.

alexanderlind avatar Nov 22 '13 10:11 alexanderlind

Line charts need to start at 0 as well. Keep your original code though, Fox News will love it. xD

shawnwhinnery avatar Nov 25 '13 23:11 shawnwhinnery

I'm not sure if I agree that line charts should always start at zero, visual context at a small size is difficult when a scale a lot larger than the change itself.

Though there is a new option for doing this - pass scaleBeginAtZero: true into your options for chart types.

For bar charts, this now defaults to true.

nnnick avatar Jun 29 '14 19:06 nnnick

This new feauter should be in http://www.chartjs.org/docs/#line-chart documentation.

axelwass avatar Jan 14 '15 18:01 axelwass

For chartjs 2.0 I've found the solution. Put this in the options object. Have fun!

scales: {
    yAxes: [{
      id: 'y-axis-0',
      gridLines: {
        display: true,
        lineWidth: 1,
        color: "rgba(0,0,0,0.30)"
      },
      ticks: {
        beginAtZero:true,
        mirror:false,
        suggestedMin: 0,
        suggestedMax: 500,
      },
      afterBuildTicks: function(chart) {

      }
    }],
    xAxes: [{
      id: 'x-axis-0',
      gridLines: {
        display: false
      },
      ticks: {
        beginAtZero: true
      }
    }]
}

BennyPolak avatar Jul 20 '16 15:07 BennyPolak

hi, I've tried your solution but it didn't work for me - I found on stack other answer (http://stackoverflow.com/questions/43040867/show-bar-with-zero-value-in-chartjs-v2/), but it's more hack than solution - can you show on jsfiddle how exactly you soultion works?

bartekmaciejewski avatar Mar 27 '17 12:03 bartekmaciejewski

same here- doesn't work for me, and I would like my y axis to begin at zero!

nickgrossman avatar Jun 21 '17 21:06 nickgrossman

@bartekmaciejewski @nickgrossman Not sure if you still need it, it works for me with:

var config = {
  type: 'bar',
  data: {
    ...
  },
  options: {
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true
        }
      }]
    }
  }
};

chesstrian avatar Feb 08 '18 22:02 chesstrian

@chesstrian the solution is not working for me, but i am able to find a solution from a site https://www.dyclassroom.com/chartjs/how-to-create-a-bar-graph-using-chartjs

where they are using min option for y axis, which can solve few of our requirements

yAxes: [{ ticks: { min: 0 } }]

tmukka avatar Mar 12 '18 05:03 tmukka