"use client"; import React, { useMemo, useState } from "react"; import { Cell, PieChart as CorePieChart, Label, Legend, Pie, ResponsiveContainer, Tooltip } from "recharts"; // plane imports import { TPieChartProps } from "@plane/types"; // local components import { getLegendProps } from "../components/legend"; import { CustomActiveShape } from "./active-shape"; import { CustomPieChartTooltip } from "./tooltip"; export const PieChart = React.memo((props: TPieChartProps) => { const { data, dataKey, cells, className, innerRadius, legend, margin, outerRadius, showTooltip = true, showLabel, customLabel, centerLabel, cornerRadius, paddingAngle, tooltipLabel, } = props; // states const [activeIndex, setActiveIndex] = useState(null); const [activeLegend, setActiveLegend] = useState(null); const renderCells = useMemo( () => cells.map((cell, index) => ( setActiveIndex(index)} onMouseLeave={() => setActiveIndex(null)} /> )), [activeLegend, cells] ); return (
setActiveIndex(null)} data={data} dataKey={dataKey} cx="50%" cy="50%" blendStroke activeShape={} innerRadius={innerRadius} outerRadius={outerRadius} cornerRadius={cornerRadius} paddingAngle={paddingAngle} labelLine={false} label={ showLabel ? ({ payload, ...props }) => ( {customLabel?.(payload.count) ?? payload.count} ) : undefined } > {renderCells} {centerLabel && ( {legend && ( // @ts-expect-error recharts types are not up to date { // @ts-expect-error recharts types are not up to date const key: string | undefined = payload.payload?.key; if (!key) return; setActiveLegend(key); setActiveIndex(null); }} onMouseLeave={() => setActiveLegend(null)} {...getLegendProps(legend)} /> )} {showTooltip && ( { if (!active || !payload || !payload.length) return null; const cellData = cells.find((c) => c.key === payload[0].payload.key); if (!cellData) return null; const label = tooltipLabel ? typeof tooltipLabel === "function" ? tooltipLabel(payload[0]?.payload?.payload) : tooltipLabel : dataKey; return ; }} /> )}
); }); PieChart.displayName = "PieChart";