0

I have a pie chart from react-chartjs-2 package, I want the percentage of each segment to be displayed in each area of the pie chart.

Below is my code and the chart I have so far, the desired chart image is also below.

Thank you.

import React from "react";
import { Chart as ChartJS, ArcElement, Tooltip, Legend } from "chart.js";
import { Pie } from "react-chartjs-2";

ChartJS.register(ArcElement, Tooltip, Legend);

export default function PieChart() {
  return (
    <>
      <Pie
        options={{
          responsive: true,
          maintainAspectRatio: false,
          plugins: {
            legend: { display: false },
            tooltip: false,
          },
          onHover: function (e) {
            e.native.target.style.cursor = "pointer";
          },
          rotation: 270,
          events: ["click"],
          onClick: function (event, element) {
            console.log(element[0].index);
          },
        }}
        data={{
          labels: ["Above 60%", "Below 60%"],
          datasets: [
            {
              data: [20, 10],
              backgroundColor: ["#3A925D", "#FD7E14"],
              borderWidth: 0,
            },
          ],
        }}
      />
    </>
  );

current chart.

enter image description here

desired chart.

enter image description here

Cuado
  • 501
  • 1
  • 5
  • 13
  • I looked through that question before and it did not answer my question. But having studied it again, I discovered that `ChartDataLabels` is now a separate package as `chartjs-plugin-datalabels` – Cuado Dec 20 '22 at 15:24

0 Answers0