Categories
JavaScript Answers

How to hide labels on y axis in Chart.js with JavaScript?

Spread the love

Sometimes, we want to hide labels on y axis in Chart.js with JavaScript.

In this article, we’ll look at how to hide labels on y axis in Chart.js with JavaScript.

How to hide labels on y axis in Chart.js with JavaScript?

To hide labels on y axis in Chart.js with JavaScript, we set the display property.

For instance, we write

const ctx = document.getElementById("log");
const chart = new Chart(ctx, {
  type: "line",
  options: {
    scales: {
      xAxes: [
        {
          display: false,
        },
      ],
      yAxes: [
        {
          display: false,
        },
      ],
    },
  },
  data: {
    labels: ["Item 1", "Item 2", "Item 3"],
    datasets: [
      {
        fill: false,
        borderWidth: 1,
        data: [10, 20, 30],
      },
    ],
  },
});

to create a chart with the Chart constructor by calling it with an object that sets the options.scale.xAxes to an array with an object that has display set to false to hide the x-axis labels.

Likewise, we do the same with yAxes to hide the y-axis labels.

Conclusion

To hide labels on y axis in Chart.js with JavaScript, we set the display property.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *