Adding data labels inside charts in ReactJS is not working?
Asked Answered
P

3

7

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"
}
Polygamous answered 19/6, 2021 at 12:39 Comment(0)
H
35

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} />
Hupp answered 14/7, 2021 at 6:58 Comment(7)
I searched for the whole day and try everything, only ur answer is works thanks!Bruiser
I have a multiple axes chart. Do you know how you could format datalabels for each axes separately?Cutwater
I managed to solve it: you need to add the datalabels for each dataset.Cutwater
And then how could I customize datalabels?Bootlace
``` datalabels: { display: true, color: 'white', }, ``` For me it doesn't workBootlace
How do you do the config then? Because strangely enough, if I use the dataLabels plugin, the spacing between the labels and the bars in the graph is different if I register the plugin this way, as opposed to the Chart.register optionOphite
@TimeBuy try dataLabels: { display: true, color: 'white'} instead of datalabels: {...}Outwardbound
P
10

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

Polygamous answered 26/7, 2021 at 12:43 Comment(2)
weirdly const ChartDataLabels = require("chartjs-plugin-datalabels"); didnt work for me. import doesTurbo
it works by combining with this answer: https://mcmap.net/q/1013303/-adding-data-labels-inside-charts-in-reactjs-is-not-workingAppear
T
0

Import chartjs-plugin-datalabels like this in React project.

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

Chart.register(ChartDataLabels);
Thistledown answered 20/2 at 9:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.