"use client"; import React, { useState, useEffect } from "react"; import { ChartConfig, ChartDataSource } from "./types"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Button } from "@/components/ui/button"; import { Switch } from "@/components/ui/switch"; import { Trash2 } from "lucide-react"; interface MultiChartConfigPanelProps { config: ChartConfig; dataSources: ChartDataSource[]; testResults: Map[] }>; // 각 데이터 소스의 테스트 결과 onConfigChange: (config: ChartConfig) => void; } export function MultiChartConfigPanel({ config, dataSources, testResults, onConfigChange, }: MultiChartConfigPanelProps) { const [chartType, setChartType] = useState(config.chartType || "line"); const [mergeMode, setMergeMode] = useState(config.mergeMode || false); const [dataSourceConfigs, setDataSourceConfigs] = useState< Array<{ dataSourceId: string; xAxis: string; yAxis: string[]; label?: string; }> >(config.dataSourceConfigs || []); // 데이터 소스별 사용 가능한 컬럼 const getColumnsForDataSource = (dataSourceId: string): string[] => { const result = testResults.get(dataSourceId); return result?.columns || []; }; // 데이터 소스별 숫자 컬럼 const getNumericColumnsForDataSource = (dataSourceId: string): string[] => { const result = testResults.get(dataSourceId); if (!result || !result.rows || result.rows.length === 0) return []; const firstRow = result.rows[0]; return Object.keys(firstRow).filter((key) => { const value = firstRow[key]; return typeof value === "number" || !isNaN(Number(value)); }); }; // 차트 타입 변경 const handleChartTypeChange = (type: string) => { setChartType(type); onConfigChange({ ...config, chartType: type, mergeMode, dataSourceConfigs, }); }; // 병합 모드 변경 const handleMergeModeChange = (checked: boolean) => { setMergeMode(checked); onConfigChange({ ...config, chartType, mergeMode: checked, dataSourceConfigs, }); }; // 데이터 소스 설정 추가 const handleAddDataSourceConfig = (dataSourceId: string) => { const columns = getColumnsForDataSource(dataSourceId); const numericColumns = getNumericColumnsForDataSource(dataSourceId); const newConfig = { dataSourceId, xAxis: columns[0] || "", yAxis: numericColumns.length > 0 ? [numericColumns[0]] : [], label: dataSources.find((ds) => ds.id === dataSourceId)?.name || "", }; const updated = [...dataSourceConfigs, newConfig]; setDataSourceConfigs(updated); onConfigChange({ ...config, chartType, mergeMode, dataSourceConfigs: updated, }); }; // 데이터 소스 설정 삭제 const handleRemoveDataSourceConfig = (dataSourceId: string) => { const updated = dataSourceConfigs.filter((c) => c.dataSourceId !== dataSourceId); setDataSourceConfigs(updated); onConfigChange({ ...config, chartType, mergeMode, dataSourceConfigs: updated, }); }; // X축 변경 const handleXAxisChange = (dataSourceId: string, xAxis: string) => { const updated = dataSourceConfigs.map((c) => (c.dataSourceId === dataSourceId ? { ...c, xAxis } : c)); setDataSourceConfigs(updated); onConfigChange({ ...config, chartType, mergeMode, dataSourceConfigs: updated, }); }; // Y축 변경 const handleYAxisChange = (dataSourceId: string, yAxis: string) => { const updated = dataSourceConfigs.map((c) => (c.dataSourceId === dataSourceId ? { ...c, yAxis: [yAxis] } : c)); setDataSourceConfigs(updated); onConfigChange({ ...config, chartType, mergeMode, dataSourceConfigs: updated, }); }; // 🆕 개별 차트 타입 변경 const handleIndividualChartTypeChange = (dataSourceId: string, chartType: "bar" | "line" | "area") => { const updated = dataSourceConfigs.map((c) => (c.dataSourceId === dataSourceId ? { ...c, chartType } : c)); setDataSourceConfigs(updated); onConfigChange({ ...config, chartType: "mixed", // 혼합 모드로 설정 mergeMode, dataSourceConfigs: updated, }); }; // 설정되지 않은 데이터 소스 (테스트 완료된 것만) const availableDataSources = dataSources.filter( (ds) => testResults.has(ds.id!) && !dataSourceConfigs.some((c) => c.dataSourceId === ds.id), ); return (
{/* 차트 타입 선택 */}
{/* 데이터 병합 모드 */} {dataSourceConfigs.length > 1 && (

여러 데이터 소스를 하나의 라인/바로 합쳐서 표시

)} {/* 데이터 소스별 설정 */}
{availableDataSources.length > 0 && ( )}
{dataSourceConfigs.length === 0 ? (

데이터 소스를 추가하고 API 테스트를 실행한 후
위 드롭다운에서 차트에 표시할 데이터를 선택하세요

) : ( dataSourceConfigs.map((dsConfig) => { const dataSource = dataSources.find((ds) => ds.id === dsConfig.dataSourceId); const columns = getColumnsForDataSource(dsConfig.dataSourceId); const numericColumns = getNumericColumnsForDataSource(dsConfig.dataSourceId); return (
{/* 헤더 */}
{dataSource?.name || dsConfig.dataSourceId}
{/* X축 */}
{/* Y축 */}
{/* 🆕 개별 차트 타입 (병합 모드가 아닐 때만) */} {!mergeMode && (
)}
); }) )}
{/* 안내 메시지 */} {dataSourceConfigs.length > 0 && (

{mergeMode ? ( <> 🔗 {dataSourceConfigs.length}개의 데이터 소스가 하나의 라인/바로 병합되어 표시됩니다.
⚠️ 중요: 첫 번째 데이터 소스의 X축/Y축 컬럼명이 기준이 됩니다.
다른 데이터 소스에 동일한 컬럼명이 없으면 해당 데이터는 표시되지 않습니다.
💡 컬럼명이 다르면 "컬럼 매핑" 기능을 사용하여 통일하세요.
) : ( <> 💡 {dataSourceConfigs.length}개의 데이터 소스가 하나의 차트에 표시됩니다.
각 데이터 소스마다 다른 차트 타입(바/라인/영역)을 선택할 수 있습니다. )}

)}
); }