import React from "react"; interface DonutChartProps { data: { label: string; value: number }[]; width?: number; height?: number; colors?: string[]; strokeWidth?: number; onClick?: (item: { label: string; value: number }) => void; showLegend?: boolean; legendPosition?: "top" | "bottom" | "left" | "right"; } const DonutChart: React.FC = ({ data, width = 300, height = 300, colors = ["#FF6384", "#36A2EB", "#FFCE56", "#4BC0C0"], strokeWidth = 20, onClick, showLegend = true, legendPosition = "bottom", }) => { const total = data.reduce((sum, item) => sum + item.value, 0); const radius = (Math.min(width, height) - strokeWidth) / 2; const circumference = 2 * Math.PI * radius; let cumulativeValue = 0; return (
{data.map((item, index) => { const value = (item.value / total) * circumference; const offset = cumulativeValue; cumulativeValue += value; return ( onClick && onClick(item)} style={{ cursor: "pointer", transition: "stroke-dashoffset 0.3s ease, stroke 0.3s ease", filter: "drop-shadow(2px 4px 6px rgba(0, 0, 0, 0.2))", }} /> ); })} {showLegend && (
{data.map((item, index) => (
{item.label} ({item.value})
))}
)}
); }; export default DonutChart;