"use client";
import {
Cell,
Legend,
Pie,
PieChart as RechartsPieChart,
ResponsiveContainer,
Tooltip,
Text,
} from "recharts";
// Define a generic type for chart data
interface ChartDataItem {
[key: string]: string | number;
}
// Define tooltip props
interface CustomTooltipProps {
active?: boolean;
payload?: Array<{
name: string;
value: number;
color: string;
}>;
valueFormatter?: (value: number) => string;
}
// Custom tooltip component for the pie chart
const CustomTooltip = ({
active,
payload,
valueFormatter,
}: CustomTooltipProps) => {
if (active && payload && payload.length) {
return (
{payload[0].name}
{valueFormatter
? valueFormatter(payload[0].value)
: `${payload[0].value}%`}
);
}
return null;
};
interface PieChartProps {
data: ChartDataItem[];
category: string;
index: string;
colors?: string[];
valueFormatter?: (value: number) => string;
className?: string;
innerRadius?: number;
outerRadius?: string | number;
paddingAngle?: number;
showLabel?: boolean;
showLegend?: boolean;
centerText?: string;
}
// Define label props
interface CustomizedLabelProps {
cx: number;
cy: number;
midAngle: number;
innerRadius: number;
outerRadius: number;
percent: number;
// Removing the unused 'name' parameter
}
const RADIAN = Math.PI / 180;
const renderCustomizedLabel = ({
cx,
cy,
midAngle,
innerRadius,
outerRadius,
percent,
}: CustomizedLabelProps) => {
const radius =
Number(innerRadius) + (Number(outerRadius) - Number(innerRadius)) * 0.5;
const x = cx + radius * Math.cos(-midAngle * RADIAN);
const y = cy + radius * Math.sin(-midAngle * RADIAN);
return (
cx ? "start" : "end"}
dominantBaseline="central"
fontSize={12}
fontWeight={500}
>
{`${(percent * 100).toFixed(0)}%`}
);
};
export function PieChart({
data,
category,
index,
colors = ["#3b82f6", "#64748b", "#10b981", "#f59e0b", "#94a3b8"],
valueFormatter = (value: number) => `${value}`,
className,
innerRadius = 0,
outerRadius = "80%",
paddingAngle = 2,
showLabel = true,
showLegend = true,
centerText,
}: PieChartProps) {
return (
{data.map((entry, index) => (
|
))}
{centerText && (
{centerText}
)}
} />
{showLegend && (
);
}
export function DonutChart(props: PieChartProps) {
return (
);
}