-차트 데이터 개수만큼 차트가 그려지도록 수정정
This commit is contained in:
@@ -91,10 +91,16 @@ export default function Linechart({
|
||||
yFieldText,
|
||||
showLegend = true
|
||||
}: LineChartProps) {
|
||||
if(!chartData || Object.keys(chartData).length === 0){
|
||||
return <div>데이터가 없습니다.</div>
|
||||
if (!chartData || Object.keys(chartData).length === 0) {
|
||||
return <div>데이터가 없습니다.</div>;
|
||||
}
|
||||
|
||||
// 모든 데이터셋이 비어있는지 확인
|
||||
const hasNoData = Object.values(chartData).every(dataset => !dataset || dataset.length === 0);
|
||||
if (hasNoData) {
|
||||
return <div>데이터가 없습니다.</div>;
|
||||
}
|
||||
|
||||
// 데이터 병합 및 변환
|
||||
const mergedData = Object.entries(chartData).reduce<Array<{[key: string]: string | number}>>((acc, [key, dataset]) => {
|
||||
dataset.forEach((entry, index) => {
|
||||
@@ -106,6 +112,17 @@ export default function Linechart({
|
||||
return acc;
|
||||
}, []);
|
||||
|
||||
// 데이터의 최대값 계산
|
||||
const getMaxValue = (data: ChartDataType, keyPrefix: string) => {
|
||||
return Math.max(...Object.values(data).map(item =>
|
||||
Object.keys(item)
|
||||
.filter(key => key.includes(keyPrefix))
|
||||
.map(key => item[key] as unknown as number)
|
||||
).flat());
|
||||
};
|
||||
|
||||
const lineMaxValue = getMaxValue(chartData, 'lineVal');
|
||||
|
||||
return (
|
||||
<div className={chartClassName}>
|
||||
{chartTitle && chartTitle !== "" && chartTitle !== "undefined" ? (
|
||||
@@ -122,8 +139,8 @@ export default function Linechart({
|
||||
margin={{ top: 5, right: 30, left: 20, bottom: 5 }}
|
||||
>
|
||||
<CartesianGrid vertical={false} />
|
||||
<XAxis dataKey="label" fontSize="8px" fontWeight="bold"/>
|
||||
<YAxis yAxisId="left" className="text-[12px] font-bold" orientation="left" label={{ value: yFieldText, className:"text-[12px] foont-bold", angle: -90, position: 'insideLeft' }} />
|
||||
<XAxis dataKey="label" fontSize="8px" fontWeight="bold" />
|
||||
<YAxis yAxisId="left" className="text-[12px] font-bold" domain={[0, Math.ceil(lineMaxValue * 1.1)]} tickCount={6} orientation="left" label={{ value: yFieldText, className:"text-[12px] font-bold", angle: -90, position: 'insideLeft' }} />
|
||||
<Tooltip content={<CustomTooltip />} />
|
||||
{showLegend && <Legend content={CustomLegend as ContentType}/>}
|
||||
{Object.keys(chartData).map((key) => {
|
||||
|
||||
Reference in New Issue
Block a user