Categories
JavaScript Answers

How to set bar width with Chart.js 2 and JavaScript?

Spread the love

Sometimes, we want to set bar width with Chart.js 2 and JavaScript.

In this article, we’ll look at how to set bar width with Chart.js 2 and JavaScript.

How to set bar width with Chart.js 2 and JavaScript?

To set bar width with Chart.js 2 and JavaScript, we set the barThickness and maxBarThickness properties.

For instance, we write

const options = {
  type: "bar",
  data: [
    //...
  ],
  options: {
    scales: {
      xAxes: [
        {
          barThickness: 6,
          maxBarThickness: 8,
        },
      ],
    },
  },
};

to set the barThickness to the bar thickness in pixels.

And we set the maxBarThickness to the max bar thickness in pixels.

Conclusion

To set bar width with Chart.js 2 and JavaScript, we set the barThickness and maxBarThickness properties.

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 *