How to useyAxes Option in Chart.js

  • In this approach, we are using the yAxes option in Chart.js which configures the scale property to include the yAxes array.
  • By using this array, the scaleLabel property is used to set the Y-axis title.
  • We have given the title “Number of Visitors“.
  • This option is mainly used with Chart.js Version 2.

Syntax:

scales: {
yAxes: [{
scaleLabel: {
display: true,
labelString: 'Y-axis Title'
}
}]
}

Example: Below is the implementation of the above-discussed approach.

HTML




<!DOCTYPE html>
<html>
<head>
    <script src=
"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js">
      </script>
    <title>Example 2</title>
</head>
 
<body>
    <h1 style="color:green;">w3wiki</h1>
    <h3>Approach 2: Using yAxes Option </h3>
    <canvas id="myChart" width="400" height="200"></canvas>
    <script>
        let ctx = document.getElementById('myChart').getContext('2d');
        let myChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: ['January', 'February', 'March', 'April', 'May'],
                datasets: [{
                    label: 'w3wiki Data',
                    data: [12, 19, 3, 5, 2],
                    backgroundColor: 'rgba(75, 192, 192, 0.2)',
                    borderColor: 'rgba(75, 192, 192, 1)',
                    borderWidth: 1
                }]
            },
            options: {
                scales: {
                    yAxes: [{
                        scaleLabel: {
                            display: true,
                            labelString: 'Number of Views'
                        }
                    }]
                },
                legend: {
                    display: true,
                    position: 'top'
                }
            }
        });
    </script>
</body>
 
</html>


Output:



How to Set Chart.js Y-axis Title ?

Chart.js is the JavaScript library to represent the data in visual forms in terms of various charts like Bar, Pies, etc. We can set and customize the axes of the chart by setting the Y-axis title. This provides better information about the vertical axis of the Chart. In this article, we will see how we can set the Chart.js Y-axis title by using two different approaches.

Below are the possible approaches:

Table of Content

  • Using Chart.js Global Options
  • Using yAxes Option

Similar Reads

CDN link

...

Approach 1: Using Chart.js Global Options

In this approach, we are using the options object which is from Chart.js Global. There is a scale configuration in which we can customize the y-axis by setting its title. For the below example, we have set the title as “More Popular” which is displayed on the y-axis of the Bar chart....

Approach 2: Using yAxes Option

...

Contact Us