"use client" import { Bar, BarChart, CartesianGrid, Legend, XAxis, YAxis } from "recharts"; import { ChartContainer, ChartTooltip, type ChartConfig } from "./lib/chart"; import type { CSSProperties } from "react"; import { formatCommaNumber } from "./lib/function"; const customStyles = { legendContainer: { display: "flex", justifyContent: "center", gap: "20px", fontSize: "9px", fontWeight : "bold", marginTop: "10px", } as CSSProperties, }; type BarChartProps = { chartConfig: ChartConfig | undefined; chartData: any[] | undefined; chartTitle: string; chartClassName?: string; graphSize?: number; yFieldText?: string; showLegend?: boolean; } // 커스텀 툴팁 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: any) => { const { payload } = props; return (
{payload.map((entry: unknown, index: number) => (
{(entry as {value: string}).value}
))}
); }; // chartData의 모든 값 중 최대값 구하기 const getGlobalMax = (data: Record[]) => { if (!data || data.length === 0) return 0; return Math.max( ...data.flatMap(item => Object.entries(item) .filter(([key]) => key !== 'label') .map(([, value]) => value as number) ) ); }; export default function Chart({chartConfig=undefined, chartData=[], chartTitle='차트', chartClassName='w-full h-full', graphSize=40, yFieldText='', showLegend=false} : BarChartProps) { const colors = ["#FF6B6B", "#4ECDC4", "#44ad57", "#FED16A", "#FFEEAD", "#D4A5A5"]; const globalMax = getGlobalMax(chartData); const tickCount = 5; // interval을 globalMax / tickCount로 계산 const interval = globalMax / tickCount; const ticks = Array.from({ length: tickCount + 1 }, (_, i) => Math.round(interval * i)); return ( <> {chartTitle && chartTitle !== "" && chartTitle !== "undefined" ? (
{chartTitle}
): null}
formatCommaNumber(value)} label={yFieldText ? { value: yFieldText, className:"text-[15px] font-bold", position: 'insideLeft', angle: -90, offset: -30 } : undefined} /> }/> {chartData && chartData.length > 0 && ( Object.keys(chartData[0]) .filter(key => key !== "label") .map((key, idx) => ( )) )} {showLegend && }
) }