Categories
JavaScript Answers

How to limit labels number on Chart.js line chart with JavaScript?

Spread the love

Sometimes, we want to limit labels number on Chart.js line chart with JavaScript.

In this article, we’ll look at how to limit labels number on Chart.js line chart with JavaScript.

How to limit labels number on Chart.js line chart with JavaScript?

To limit labels number on Chart.js line chart with JavaScript, we can add the maxTicksLimit property.

For instance, we write

const options = {
  scales: {
    x: {
      ticks: {
        maxTicksLimit: 10,
      },
    },
  },
};

const myLineChart = new Chart(ctx, {
  type: "line",
  data,
  options,
});

to create a Chart object with the canvas context ctx and an object with the options property.

We set the options.scales.x.ticks.maxTicksLimit property to 10 to limit the number of x-axis ticks to 10.

Conclusion

To limit labels number on Chart.js line chart with JavaScript, we can add the maxTicksLimit 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 *