차트property 추가 (setXAxisYearTick - 날짜 x축일경우 (년도-월)별로 만들어주는 속성)

This commit is contained in:
2025-08-26 17:39:07 +09:00
parent 2f4cfa0d78
commit aeaf507c47
2 changed files with 45 additions and 16 deletions
@@ -6,6 +6,7 @@ import {
ChartContainer, ChartContainer,
type ChartConfig type ChartConfig
} from "./lib/chart"; } from "./lib/chart";
import { get } from "http";
type LineChartProps = { type LineChartProps = {
chartConfig: ChartConfig | undefined; chartConfig: ChartConfig | undefined;
@@ -15,6 +16,7 @@ type LineChartProps = {
graphSize?: number; graphSize?: number;
yFieldText?: string; yFieldText?: string;
showLegend?: boolean; showLegend?: boolean;
setXAxisYearTick?: boolean;
} }
/** 차트 기본 스타일 */ /** 차트 기본 스타일 */
@@ -141,7 +143,8 @@ export default function Linechart({
chartClassName = 'w-full h-full', chartClassName = 'w-full h-full',
graphSize = 40, graphSize = 40,
yFieldText, yFieldText,
showLegend = true showLegend = true,
setXAxisYearTick = false,
}: LineChartProps) { }: LineChartProps) {
if (!chartData || Object.keys(chartData).length === 0) { if (!chartData || Object.keys(chartData).length === 0) {
return <div>데이터가 없습니다.</div>; return <div>데이터가 없습니다.</div>;
@@ -164,18 +167,24 @@ export default function Linechart({
return acc; return acc;
}, []); }, []);
// 데이터의 최대값 계산 const getTicks = (data : any []) => {
const getMaxValue = (data: ChartDataType): number => { const monthSet = new Set<string>();
return Math.max(...Object.values(data).flatMap(dataset => const rtnData : string[] = [];
dataset.map(entry => { for(const item of data){
const val = parseFloat(entry.val); const month = String(item.label).slice(0,7);
return isNaN(val) ? 0 : val; if(!monthSet.has(month)){
}) monthSet.add(month);
)); rtnData.push(item.label);
}; }
}
const lineMaxValue = getMaxValue(chartData); return rtnData;
}
let monthTicks : string[] = [];
if(setXAxisYearTick){
monthTicks = getTicks(mergedData);
}
return ( return (
<div className={chartClassName}> <div className={chartClassName}>
{chartTitle && chartTitle !== "" && chartTitle !== "undefined" ? ( {chartTitle && chartTitle !== "" && chartTitle !== "undefined" ? (
@@ -192,8 +201,27 @@ export default function Linechart({
margin={{ top: 5, right: 30, left: 20, bottom: 5 }} margin={{ top: 5, right: 30, left: 20, bottom: 5 }}
> >
<CartesianGrid vertical={false} /> <CartesianGrid vertical={false} />
<XAxis dataKey="label" fontSize="8px" fontWeight="bold" />
<YAxis yAxisId="left" className="text-[12px] font-bold" tickFormatter={(value) => value.toLocaleString()} tickCount={6} orientation="left" label={{ value: yFieldText, className:"text-[12px] font-bold", angle: -90, position: 'insideLeft' }} /> {setXAxisYearTick ?
<XAxis
dataKey="label"
fontSize="8px"
fontWeight="bold"
ticks={monthTicks}
tickFormatter={(value) => value.slice(0,7)}
/> :
<XAxis
dataKey="label"
fontSize="8px"
fontWeight="bold"
/>
}
<YAxis
yAxisId="left"
className="text-[12px] font-bold"
orientation="left"
label={{ value: yFieldText, className:"text-[12px] font-bold", angle: -90, position: 'insideLeft' }} />
<Tooltip content={<CustomTooltip />} /> <Tooltip content={<CustomTooltip />} />
{showLegend && <Legend content={CustomLegend as ContentType}/>} {showLegend && <Legend content={CustomLegend as ContentType}/>}
{Object.keys(chartData).map((key) => { {Object.keys(chartData).map((key) => {
@@ -56,6 +56,7 @@ type MainChartProps = {
yFieldText? : string | undefined; yFieldText? : string | undefined;
yFieldText2? : string | undefined; yFieldText2? : string | undefined;
showLegend? : boolean; showLegend? : boolean;
setXAxisYearTick ?: boolean;
} }
function MainChart(props: MainChartProps) { function MainChart(props: MainChartProps) {
@@ -65,7 +66,7 @@ function MainChart(props: MainChartProps) {
); );
} else if( props.chartForm == CHART_FORM.LINE ){ } else if( props.chartForm == CHART_FORM.LINE ){
return ( return (
<LineChart chartConfig={props.chartConfig} chartData={props.chartData} chartTitle={props.chartTitle} chartClassName={props.chartClassName} graphSize={props.graphSize} yFieldText={props.yFieldText} showLegend={props.showLegend}/> <LineChart chartConfig={props.chartConfig} chartData={props.chartData} chartTitle={props.chartTitle} chartClassName={props.chartClassName} graphSize={props.graphSize} yFieldText={props.yFieldText} showLegend={props.showLegend} setXAxisYearTick={props.setXAxisYearTick}/>
); );
} else if( props.chartForm == CHART_FORM.DN ){ } else if( props.chartForm == CHART_FORM.DN ){
return ( return (