"use client" import type { CSSProperties } from "react"; import { Pie, PieChart } from "recharts"; import { Card, CardContent } from "./lib/card"; import { ChartContainer, ChartTooltip, type ChartConfig } from "./lib/chart"; type ChartDataItem = { label: string; val: number; }; type TooltipPayloadItem = { name: string; value: number; payload: { label: string; val: number; fill: string; }; }; type LegendEntry = { value: string; fill: string; type?: string; payload: { label: string; val: number; fill: string; }; }; type LegendProps = { payload?: LegendEntry[]; }; /** 차트 기본 스타일 (라인차트와 동일하게) */ const customStyles = { legendContainer: { display: "flex", flexWrap: "wrap", // 여러 줄로 자동 줄바꿈 justifyContent: "center", alignItems: "center", gap: "12px", // 아이템 간 간격 fontSize: "13px", fontWeight: "normal", marginTop: "16px", marginBottom: "8px", minHeight: "60px", // 범례 영역 높이 통일 } as CSSProperties, }; const renderCustomizedLabel = ({ cx, cy, midAngle, outerRadius, payload, }: any) => { const RADIAN = Math.PI / 180; const radius = outerRadius + 25; const x = cx + radius * Math.cos(-midAngle * RADIAN); const y = cy + radius * Math.sin(-midAngle * RADIAN); return ( cx ? "start" : "end"} dominantBaseline="central" > {`${payload.label}: ${payload.val.toLocaleString()}`} ); }; // 커스텀 툴팁 const CustomTooltip = ({ active, payload, label }: { active?: boolean; payload?: TooltipPayloadItem[]; label?: string }) => { if (active && payload && payload.length) { return (

{label}

{payload.map((item, index) => (
{` ${item.name} : ${item.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}`}
))}
); } return null; }; const CustomLegend = (props: LegendProps) => { const { payload } = props; if (!payload) return null; return (
{payload.map((entry: LegendEntry, index: number) => (
{entry.payload?.label || entry.value}
))}
); }; export default function DoughnutChart(props: { chartConfig: ChartConfig; chartData: ChartDataItem[] | undefined; chartTitle: string }) { const legendData = props.chartData?.map(item => ({ value: item.label, fill: props.chartConfig?.[item.label]?.color || 'blue', payload: { label: item.label, val: item.val, fill: props.chartConfig?.[item.label]?.color || 'blue' } })) ?? []; return ( {props.chartTitle && props.chartTitle !== "" && props.chartTitle !== "undefined" ? (
{props.chartTitle}
) : null}
} /> ({ ...item, fill: props.chartConfig[item.label]?.color || 'blue' }))} dataKey="val" nameKey="label" innerRadius={60} label={(props) => { return renderCustomizedLabel(props); }} >
); }