Skip to main content
PHP Blog

Back to all posts

How to Show the 0 Axis With Chart.js?

Published on
3 min read
How to Show the 0 Axis With Chart.js? image

Best Chart.js Plugins to Buy in October 2025

+
ONE MORE?

To show the 0 axis with Chart.js, you can use the scales option of the configuration object. Here's an example of how to achieve this:

var chartOptions = { scales: { y: { beginAtZero: true } } };

var chartData = { labels: [/* Your labels */], datasets: [ { data: [/* Your data */], // Additional dataset options } ] };

var chart = new Chart("", { type: "", data: chartData, options: chartOptions });

In the above code snippet, replace <chartElement> with the ID or DOM element of the canvas element where you want to render the chart. Also, replace <chartType> with the specific chart type you are using, such as "bar", "line", or "pie".

The crucial part for showing the 0 axis is the definition of beginAtZero: true under the scales.y object. This configuration ensures that the y-axis starts at zero, enabling the 0 axis to be displayed.

Remember to customize the chartData object according to your specific data points and labels, and utilize the appropriate chart type, options, and styling to suit your requirements.

How can I make sure the 0 axis is included in my chart.js visualizations?

To include the 0 axis in your Chart.js visualizations, you need to set the beginAtZero property to true for the corresponding scale. This property ensures that the scale starts from zero.

Here's an example of how you can include the 0 axis for both the X and Y axes:

var chartOptions = { scales: { x: { beginAtZero: true, }, y: { beginAtZero: true, } } };

var chartData = { // Your chart data goes here };

var myChart = new Chart(ctx, { // Chart configuration options go here options: chartOptions, data: chartData });

In this example, ctx is the HTML canvas element's context, chartOptions specifies the options for the chart (including the scale configurations), and chartData represents the actual data that you want to display.

How can I modify the chart.js options to display the 0 axis?

To display the 0 axis on a chart created with Chart.js, you can modify the scales property of the chart's options object. Specifically, you need to set the beginAtZero property of the y-axis to true. Here's an example:

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

var chart = new Chart(ctx, { type: 'line', data: data, options: options });

In this example, the options object is modified to include the scales property. Within the scales property, the y scale is configured with the beginAtZero property set to true.

Make sure to replace ctx, data, and type with the appropriate values for your chart.

What parameters should I pass to chart.js to show the 0 axis?

To display the 0 axis using chart.js, you need to set the suggestedMin and suggestedMax properties of the y-axis scale to define the range of values. Here's an example of how to pass the parameters to show the 0 axis:

options: { scales: { y: { suggestedMin: 0, // Sets the minimum value of the y-axis scale to 0 suggestedMax: 100, // Sets the maximum value of the y-axis scale to 100 (change as needed) } } }

Replace 100 with the maximum value of your dataset, or adjust it to fit your specific requirements. This will ensure that the y-axis includes the 0 value while displaying the other data points accordingly.