"use client";
import { PieChart, Pie, Cell, ResponsiveContainer, Tooltip, Legend } from "recharts";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
interface DonutChartProps {
data: Array<{ name: string; value: number; color?: string }>;
title?: string;
centerText?: {
title: string;
value: string | number;
};
colors?: string[];
height?: number;
className?: string;
}
const CustomTooltip = ({ active, payload }: any) => {
if (active && payload && payload.length) {
const data = payload[0];
return (
{data.name}
{data.value}
{" "}
sessions ({((data.value / data.payload.total) * 100).toFixed(1)}%)
);
}
return null;
};
const CustomLegend = ({ payload }: any) => {
return (
{payload.map((entry: any, index: number) => (
))}
);
};
const CenterLabel = ({ centerText, total }: any) => {
if (!centerText) return null;
return (
{centerText.value}
{centerText.title}
);
};
export default function ModernDonutChart({
data,
title,
centerText,
colors = [
"rgb(37, 99, 235)", // Blue (primary)
"rgb(107, 114, 128)", // Gray
"rgb(236, 72, 153)", // Pink
"rgb(34, 197, 94)", // Lime green
"rgb(168, 85, 247)", // Purple
],
height = 300,
className,
}: DonutChartProps) {
const total = data.reduce((sum, item) => sum + item.value, 0);
const dataWithTotal = data.map(item => ({ ...item, total }));
return (
{title && (
{title}
)}
{dataWithTotal.map((entry, index) => (
|
))}
} />
} />
);
}