Chart js data to start at zero
Asked Answered
S

3

5

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

Soutor answered 3/6, 2017 at 17:9 Comment(3)
that should work for sure. where did you add the options?Multifarious
Thanks, I didn't add the options at the right place but with the data... @ℊααndSoutor
The answers below didn't work for me, so I added a 0|0 data point with { x: 0, y: 0, r: 0 }, with datasets > data.Forsta
S
12

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

Sarsaparilla answered 3/6, 2017 at 17:28 Comment(1)
If you still have problems, your options are probably not being read, or are configured wrong, check out this example with options: github.com/gor181/react-chartjs-2/blob/master/example/src/…Sarsaparilla
L
3

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,
         }
       }
    };
Laius answered 14/4, 2022 at 8:6 Comment(0)
I
2

Replaced by:

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

It worked!

Illuminate answered 6/5, 2021 at 11:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.