141 lines
3.6 KiB
TypeScript
141 lines
3.6 KiB
TypeScript
"use client"
|
|
|
|
import type { CSSProperties } from "react";
|
|
import { Pie, PieChart } from "recharts";
|
|
import {
|
|
Card,
|
|
CardContent
|
|
} from "./lib/card";
|
|
import { ChartContainer, ChartTooltip } from "./lib/chart";
|
|
|
|
type ChartDataItem = {
|
|
label: string;
|
|
val: number;
|
|
};
|
|
|
|
type ChartConfigItem = {
|
|
label: string;
|
|
fill: string;
|
|
};
|
|
|
|
type ChartConfig = {
|
|
[key: string]: ChartConfigItem;
|
|
};
|
|
|
|
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",
|
|
justifyContent: "center",
|
|
gap: "20px",
|
|
fontSize: "9px",
|
|
fontWeight: "bold",
|
|
marginTop: "10px",
|
|
} as CSSProperties,
|
|
};
|
|
|
|
// 커스텀 툴팁
|
|
const CustomTooltip = ({ active, payload, label }: {
|
|
active?: boolean;
|
|
payload?: TooltipPayloadItem[];
|
|
label?: string
|
|
}) => {
|
|
if (active && payload && payload.length) {
|
|
return (
|
|
<div className="w-[180px] rounded bg-white p-2.5 shadow-[0px_2px_10px_rgba(0,0,0,0.2)]">
|
|
<p className="text-[12px] font-bold m-3">{label}</p>
|
|
{payload.map((item, index) => (
|
|
<div key={index} style={{ marginTop: "10px", display: "flex", alignItems: "center", gap: "8px" }}>
|
|
<div style={{ width: "10px", height: "10px", backgroundColor: item.payload.fill, borderRadius: "50%" }}></div>
|
|
<span className="text-[11px] font-bold">{` ${item.name} : ${item.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}`}</span>
|
|
</div>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|
|
return null;
|
|
};
|
|
|
|
// 커스텀 범례
|
|
const CustomLegend = (props: LegendProps) => {
|
|
const { payload } = props;
|
|
if (!payload) return null;
|
|
|
|
return (
|
|
<div style={customStyles.legendContainer}>
|
|
{payload.map((entry: LegendEntry, index: number) => (
|
|
<div key={`legend-${index}`} className="flex items-center gap-2">
|
|
<div className="w-[14px] h-[14px] ml-10" style={{ backgroundColor: entry.fill }}></div>
|
|
<span className="text-[12px]">{entry.value}</span>
|
|
</div>
|
|
))}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default function DoughnutChart(props: {
|
|
chartConfig: any;
|
|
chartData: ChartDataItem[] | undefined;
|
|
chartTitle: string
|
|
}) {
|
|
return (
|
|
<Card className="flex flex-col">
|
|
<CardContent className="flex-1 pb-0">
|
|
{props.chartTitle && props.chartTitle !== "" && props.chartTitle !== "undefined" ? (
|
|
<div style={{ textAlign: "left", fontSize: "13px", fontWeight: "bold", marginTop:"13px", marginBottom: "10px" }}>
|
|
{props.chartTitle}
|
|
</div>
|
|
) : null}
|
|
<div className="h-[calc(100%-42px)] justify-center">
|
|
<ChartContainer
|
|
config={props.chartConfig}
|
|
className="mx-auto aspect-square max-h-[300px]"
|
|
>
|
|
<PieChart>
|
|
<ChartTooltip
|
|
cursor={false}
|
|
content={<CustomTooltip />}
|
|
/>
|
|
<Pie
|
|
data={props.chartData?.map(item => ({
|
|
...item,
|
|
fill: props.chartConfig[item.label]?.fill || 'blue'
|
|
}))}
|
|
dataKey="val"
|
|
nameKey="label"
|
|
innerRadius={60}
|
|
/>
|
|
<CustomLegend />
|
|
</PieChart>
|
|
</ChartContainer>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
);
|
|
}
|