import React, { useMemo } from "react"; import { ChartContainer, ChartTooltip } from "@/src/components/ui/chart"; import { Label, Pie, PieChart as PieChartComponent } from "recharts"; import { type ChartProps } from "@/src/features/widgets/chart-library/chart-props"; /** * PieChart component * @param data - Data to be displayed. Expects an array of objects with dimension and metric properties. * @param config - Configuration object for the chart. Can include theme settings for light and dark modes. * @param accessibilityLayer - Boolean to enable or disable the accessibility layer. Default is true. */ export const PieChart: React.FC = ({ data, config = { metric: { theme: { light: "hsl(var(--chart-1))", dark: "hsl(var(--chart-1))", }, }, }, accessibilityLayer = true, }) => { // Calculate total metric value for center label const totalValue = useMemo(() => { return data.reduce((acc, curr) => acc + (curr.metric as number), 0); }, [data]); // Transform data for PieChart const chartData = useMemo(() => { return data.map((item, index) => ({ name: item.dimension || "Unknown", value: item.metric, fill: `hsl(var(--chart-${(index % 4) + 1}))`, })); }, [data]); return ( {/* Label in the center of the donut */} {data.length > 0 && ( ); }; export default PieChart;