By Configuring Scales Options (Using beginAtZero)

In this approach, we Utilize the scale arrangement’s beginAtZero: genuine boundary. Regardless of what your information range is, this clear change ensures that the y-hub generally begins at 0.

Syntax:

 options: {
scales: {
y: {
beginAtZero: true
}
}
}

Example: This example shows the implementation of the above-explained approach.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>Chart.js Example 1</title>
    <script src=
"https://cdn.jsdelivr.net/npm/chart.js">
      </script>
</head>
  
<body>
  
    <canvas id="example1" width="300" height="100"></canvas>
  
    <script>
        let ctx1 = document.getElementById('example1').getContext('2d');
        let chart1 = new Chart(ctx1, {
            type: 'bar',
            data: {
                labels: ['Label 1', 'Label 2', 'Label 3'],
                datasets: [{
                    label: 'My Dataset',
                    data: [5, 10, 15],
                    backgroundColor: ['rgba(255, 99, 132, 0.2)',
                        'rgba(54, 162, 235, 0.2)', 'rgba(255, 206, 86, 0.2)'],
                    borderColor: ['rgba(255, 99, 132, 1)', 'rgba(54, 162, 235, 1)',
                        'rgba(255, 206, 86, 1)'],
                    borderWidth: 1
                }]
            },
            options: {
                scales: {
                    y: {
                        beginAtZero: true
                    }
                }
            }
        });
    </script>
  
</body>
  
</html>


Output:

How to Set Start Value as “0” in Chart.js ?

In this article, we are going to discuss how can we set 0 as a start value for any of the axes. we need to set 0 as a start value for a chart in Chart.js.

CDN Link:

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

These are the following approaches for setting a 0 value as a start:

Table of Content

  • By Configuring Scales Options (Using beginAtZero)
  • By Custom Tick Callback
  • By using Minimum Property
  • By Axes Configuration

Similar Reads

Approach 1: By Configuring Scales Options (Using beginAtZero)

In this approach, we Utilize the scale arrangement’s beginAtZero: genuine boundary. Regardless of what your information range is, this clear change ensures that the y-hub generally begins at 0....

Approach 2: By Custom Tick Callback

...

Approach 3: By using Minimum Property

In this approach, we have defined a function to generate desired ticks that is 0....

Approch 4: By Axes Configuration

...

Contact Us