"use client" import type { CSSProperties } from "react"; import { CartesianGrid, Legend, Line, LineChart, Tooltip, XAxis, YAxis } from "recharts"; import { ChartContainer, type ChartConfig } from "./lib/chart"; import type { ContentType } from "recharts/types/component/DefaultLegendContent"; type LineChartProps = { chartConfig: ChartConfig | undefined; chartData: ChartDataType | undefined; chartTitle: string; chartClassName?: string; graphSize?: number; yFieldText?: string; showLegend?: boolean; } /** 차트 기본 스타일 */ const customStyles = { legendContainer: { display: "flex", justifyContent: "center", gap: "20px", fontSize: "9px", fontWeight : "bold", marginTop: "10px", } as CSSProperties, }; type LegendEntry = { value: string; color: string; type?: string; payload?: unknown; }; type LegendProps = { payload?: LegendEntry[]; }; // 커스텀 툴팁 const CustomTooltip = ({ active, payload, label }: { active?: boolean; payload?: Array<{ name: string; value: number; color: string }>; label?: string }) => { if (active && payload && payload.length) { return (

{label}

{payload.map((item, index) => (
{/* 색상 박스 추가 (범례 색상과 동일) */}
{` ${item.name} : ${item.value}`}
))}
); } return null; }; // 커스텀 범례 (글씨 크기 키우기 + 스타일 추가) const CustomLegend = (props: LegendProps) => { const { payload } = props; if (!payload) return null; return (
{payload.map((entry: LegendEntry, index: number) => (
{entry.value}
))}
); }; type DataType = {label: string; val: string}; type ChartDataType = { [key: string]: DataType[]; }; export default function Linechart({ chartConfig, chartData, chartTitle, chartClassName = 'w-full h-full', graphSize = 40, yFieldText, showLegend = true }: LineChartProps) { if (!chartData || Object.keys(chartData).length === 0) { return
데이터가 없습니다.
; } // 모든 데이터셋이 비어있는지 확인 const hasNoData = Object.values(chartData).every(dataset => !dataset || dataset.length === 0); if (hasNoData) { return
데이터가 없습니다.
; } // 데이터 병합 및 변환 const mergedData = Object.entries(chartData).reduce>((acc, [key, dataset]) => { dataset.forEach((entry, index) => { if (!acc[index]) { acc[index] = { label: entry.label }; } acc[index][key] = parseFloat(entry.val); }); return acc; }, []); // 데이터의 최대값 계산 const getMaxValue = (data: ChartDataType): number => { return Math.max(...Object.values(data).flatMap(dataset => dataset.map(entry => { const val = parseFloat(entry.val); return isNaN(val) ? 0 : val; }) )); }; const lineMaxValue = getMaxValue(chartData); return (
{chartTitle && chartTitle !== "" && chartTitle !== "undefined" ? (
{chartTitle}
) : null}
} /> {showLegend && } {Object.keys(chartData).map((key) => { const config = chartConfig?.[key]; return ( ); })}
); }