146 lines
4.8 KiB
TypeScript
146 lines
4.8 KiB
TypeScript
"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 (
|
|
<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.color, borderRadius: "50%" }}></div>
|
|
<span className="text-[11px] font-bold">{` ${item.name} : ${item.value}`}</span>
|
|
</div>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|
|
return null;
|
|
};
|
|
|
|
// 커스텀 범례 (글씨 크기 키우기 + 스타일 추가)
|
|
const CustomLegend = (props: any) => {
|
|
const { payload } = props;
|
|
return (
|
|
<div style={customStyles.legendContainer}>
|
|
{payload.map((entry: unknown, index: number) => (
|
|
<div key={`legend-${index}`} className="flex items-center gap-2">
|
|
<div className="w-[14px] h-[14px] ml-10" style={{ backgroundColor: (entry as {color: string}).color }}></div>
|
|
<span className="text-[12px]">{(entry as {value: string}).value}</span>
|
|
</div>
|
|
))}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
// chartData의 모든 값 중 최대값 구하기
|
|
const getGlobalMax = (data: Record<string, number>[]) => {
|
|
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" ? (
|
|
<div className="!mt-3 text-[2.5rem] font-bold">
|
|
{chartTitle}
|
|
</div>): null}
|
|
<div className="flex justify-center">
|
|
<ChartContainer config={chartConfig ?? {}} className={chartClassName + " !w-[90%]"}>
|
|
<BarChart accessibilityLayer data={chartData} className="!h-[88%]">
|
|
<CartesianGrid vertical={false} className="!h-[85%]" />
|
|
<XAxis
|
|
dataKey="label"
|
|
tickLine={true}
|
|
tickMargin={10}
|
|
axisLine={true}
|
|
fontSize={13}
|
|
fontWeight={500}
|
|
/>
|
|
<YAxis
|
|
tickLine={true}
|
|
tickMargin={10}
|
|
axisLine={true}
|
|
fontSize={12}
|
|
fontWeight={500}
|
|
domain={[0, globalMax]}
|
|
ticks={ticks}
|
|
tickFormatter={(value) => formatCommaNumber(value)}
|
|
label={yFieldText ? { value: yFieldText, className:"text-[15px] font-bold", position: 'insideLeft', angle: -90, offset: -30 } : undefined}
|
|
/>
|
|
<ChartTooltip cursor={false} content={<CustomTooltip active={undefined} payload={undefined} label={undefined} />}/>
|
|
{chartData && chartData.length > 0 && (
|
|
Object.keys(chartData[0])
|
|
.filter(key => key !== "label")
|
|
.map((key, idx) => (
|
|
<Bar
|
|
dataKey={key}
|
|
radius={5}
|
|
key={key}
|
|
barSize={graphSize}
|
|
fill={colors[idx % colors.length]}
|
|
/>
|
|
))
|
|
)}
|
|
{showLegend && <Legend content={CustomLegend}/>}
|
|
</BarChart>
|
|
</ChartContainer>
|
|
</div>
|
|
</>
|
|
)
|
|
} |