shinzou
shinzou

Reputation: 6192

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

Upvotes: 5

Views: 14596

Answers (3)

sidd 5312
sidd 5312

Reputation: 191

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,
         }
       }
    };

Upvotes: 3

LTL
LTL

Reputation: 21

Replaced by:

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

It worked!

Upvotes: 2

Keno
Keno

Reputation: 3945

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

Upvotes: 12

Related Questions