import { PieChart as RechartsPieChart, Pie, Cell, Tooltip, Legend, ResponsiveContainer, } from 'recharts' import { cn } from '../core/cn' const DEFAULT_COLORS = [ '#3b82f6', '#10b981', '#f59e0b', '#ef4444', '#8b5cf6', '#ec4899', '#06b6d4', '#f97316', ] interface PieChartProps { data: Record[] nameKey: string valueKey: string colors?: string[] donut?: boolean innerRadius?: number outerRadius?: number showLegend?: boolean showLabels?: boolean height?: number | string className?: string valueFormatter?: (value: number) => string } function PieChartComponent({ data, nameKey, valueKey, colors = DEFAULT_COLORS, donut = false, innerRadius, outerRadius = 100, showLegend = true, showLabels = true, height = 300, className, valueFormatter = (v) => v.toLocaleString(), }: PieChartProps) { // Ensure numeric values for Recharts Pie const pieData = data.map(row => ({ ...row, [valueKey]: Number(row[valueKey]) || 0, })) const resolvedInnerRadius = donut ? (innerRadius ?? 50) : (innerRadius ?? 0) const labelRenderer = showLabels ? ({ name, percent }: Record) => `${name ?? ''} ${(((percent as number) ?? 0) * 100).toFixed(0)}%` : undefined return ( {pieData.map((_, i) => ( ))} [valueFormatter(value as number)]} /> {showLegend && } ) } export const PieChart = PieChartComponent export type { PieChartProps }