Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding data labels inside charts in ReactJS is not working?

Example How to display data on top of line chart

enter image description here

issues faced while implementing:

chartjs Plugin "chartjs-plugin-datalabels:1.0.0" import is throwed below error

TypeError : Cannot read property 'extend' of undefined.

Above error resolved by changing the labels plugin package to test version 2.0.0-rc.1

Link to codesandbox will find HERE

A similar question was asked here but the solutions couldn't resolve my issue.

Any kind of help will much be appreciated.

import React, { Fragment } from "react";
import { Line } from "react-chartjs-2";
import "chartjs-plugin-datalabels";

const styles = {
  dangerColor: {
    color: "red"
  },
  geen: {
    color: "green"
  }
};

const data = {
  labels: ["Jan", "Feb", "Mar", "Apr", "May"],
  datasets: [
    {
      label: "Avg interest by month",
      data: [0.7, 0.81, 0.71, 0.87, 0.9],
      fill: false,
      backgroundColor: "transparent",
      borderColor: "#06a1e1",
      tension: 0.1,
      borderWidth: 4
    }
  ]
};

const options = {
  maintainAspectRatio: false,
  scales: {
    x: {
      grid: {
        display: false
      }
    },
    y: {
      display: false,
      grid: {
        display: false
      }
    }
  },
  plugins: {
    legend: {
      display: false
    },
    title: {
      display: true,
      text: "Avg interest by month (days)",
      padding: {
        bottom: 30
      },
      weight: "bold",
      color: "#00325c",
      font: {
        size: 13
      },
      align: "start"
    },
    datalabels: {
      display: true,
      color: "white"
    }
  }
};

const LineChart = () => (
  <Fragment>
    <div style={{ height: "300px" }}>
      <Line data={data} options={options} />
    </div>
  </Fragment>
);


export default LineChart;

This is how my Package.json dependencies looks like

{
    "chart.js": "^3.3.2",
    "chartjs-plugin-datalabels": "^2.0.0-rc.1",
    "react": "^17.0.2",
    "react-chartjs-2": "^3.0.3",
    "react-dom": "^17.0.2",
    "react-scripts": "^4.0.3",
    "web-vitals": "^1.1.2"
}
like image 918
Rajath Avatar asked Jun 03 '26 08:06

Rajath


2 Answers

The react-chartjs-2 package holds a plugin property which can be set on the components.

Change the import from:

import "chartjs-plugin-datalabels";

to:

import ChartDataLabels from 'chartjs-plugin-datalabels';

On your component, add the plugin variable which holds the imported value.

from:

<Line data={data} options={options} />

to:

<Line data={data} plugins={[ChartDataLabels]} options={options} />
like image 184
Vadimas Klepko Avatar answered Jun 07 '26 23:06

Vadimas Klepko


I have found the issue and Updated the sandbox

You can find the link here https://codesandbox.io/s/quizzical-hooks-zcg91?file=/src/components/LineChart.jsx

The issue is with the chart.js

import { Chart } from "chart.js";
import ChartDataLabels from "chartjs-plugin-datalabels";
Chart.register(ChartDataLabels);

Plugin register method is updated in chart.js 3.x version

like image 39
Rajath Avatar answered Jun 07 '26 22:06

Rajath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!