Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chart js data to start at zero

That's how I set the data:

    const data = {
        labels: ['February', 'March'],
        datasets: [
            {
                label: 'My First dataset',
                backgroundColor: 'rgba(255,99,132,0.2)',
                borderColor: 'rgba(255,99,132,1)',
                borderWidth: 1,
                hoverBackgroundColor: 'rgba(255,99,132,0.4)',
                hoverBorderColor: 'rgba(255,99,132,1)',
                data: [5, 9]
            }
        ]
    };

But the first element sets the beginning of the axis:

enter image description here

But I want it to start from zero

adding the following doesn't help:

options: {
  scales: {
        yAxes: [{
            ticks: {
                beginAtZero: true
            }
        }]
    }
}

I couldn't find another setting to do that on the docs.

I'm using this btw: https://www.npmjs.com/package/react-chartjs-2

like image 235
shinzou Avatar asked Jun 03 '17 17:06

shinzou


People also ask

How do you start a bar chart with zeros?

Simple. Set the axis start point to zero (Select axis, press Ctrl+1, and from Axis options set minimum to 0). So there you go.

Should you start your charts at zero?

Data in a line chart is encoded by position (x, y coordinates), whereas in a bar chart data is represented by length. This subtle difference changes the way a reader uses the chart, meaning that in a line chart it's ok to start the axis at a value other than zero, despite many claims that they are always misleading.

How do you start a zero in ChartJS?

To start the y-axis scale start from 0 you can use the beginAtZero property of Chart. js as shown in the below example. Here we have used beginAtZero: true property of Chart. js to set the minimum scale value to zero.


3 Answers

Try Adding min to your options:

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

enter image description here

Live Copepen: Chart.js Start at zero

like image 84
Keno Avatar answered Nov 20 '22 06:11

Keno


Replaced by:

const options = {
  scales: {
    y: {
      beginAtZero: true
    }
  }
};

It worked!

like image 26
LTL Avatar answered Nov 20 '22 05:11

LTL


As per chart.js 3.7.1 version

Directly write "beginAtZero" in x or y not in ticks

   const options = {
    scales: {
         x: {
           beginAtZero: true,            
         },
         y: {
           beginAtZero: true,
         }
       }
    };
like image 28
sidd 5312 Avatar answered Nov 20 '22 05:11

sidd 5312