'use client'; import { PieChart as RechartsPieChart, Pie, Cell, Tooltip, Legend, ResponsiveContainer, } from 'recharts'; import type { WidgetConfig, ChartData, PieChartConfig } from '@/lib/pipeline-types'; import { WidgetWrapper } from './WidgetWrapper'; interface PieChartWidgetProps { config: WidgetConfig; data: ChartData | null; loading: boolean; error?: string; onRefresh?: () => void; } // Default colors for pie slices const DEFAULT_COLORS = [ '#3b82f6', // blue '#22c55e', // green '#ef4444', // red '#eab308', // yellow '#8b5cf6', // purple '#ec4899', // pink '#06b6d4', // cyan '#f97316', // orange ]; /** * Pie/Donut chart widget using Recharts. */ export function PieChartWidget({ config, data, loading, error, onRefresh, }: PieChartWidgetProps) { const chartConfig = config.config as unknown as PieChartConfig; const chartData = data?.data || []; const colors = chartConfig.colors || DEFAULT_COLORS; const innerRadius = chartConfig.inner_radius || 0; // 0 = pie, > 0 = donut // Transform data to use consistent keys const transformedData = chartData.map((item) => ({ name: item[chartConfig.label_key] as string, value: item[chartConfig.value_key] as number, })); return ( {transformedData.length === 0 ? (
No data available
) : ( `${name} ${((percent ?? 0) * 100).toFixed(0)}%` : undefined } labelLine={chartConfig.show_labels !== false} > {transformedData.map((_, index) => ( ))} [(value ?? 0).toLocaleString(), 'Count']} /> {chartConfig.show_legend !== false && ( )} )}
); }