Categories
JavaScript Answers

How to display data values on Chart.js and JavaScript?

Spread the love

Sometimes, we want to display data values on Chart.js and JavaScript.

In this article, we’ll look at how to display data values on Chart.js and JavaScript.

How to display data values on Chart.js and JavaScript?

To display data values on Chart.js and JavaScript, we add the datalabels.formatter method.

For instance, we write

const myBarChart = new Chart(ctx, {
  type: "bar",
  data: yourDataObject,
  options: {
    // ...
    plugins: {
      datalabels: {
        anchor: "end",
        align: "top",
        formatter(value, context) {
          return getValueFormatted(value);
        },
      },
    },
  },
});

to add the datalabels.formatter method to return the formatted value of the value.

value is the display data value of a point.

Conclusion

To display data values on Chart.js and JavaScript, we add the datalabels.formatter method.

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 *