"use client"; /** * 수계별 유입ㆍ방류수질 분석 */ import React, { useEffect, useState, useMemo } from 'react'; import { useStore } from "zustand"; import { Line } from 'react-chartjs-2'; import BasinSelect from "./basIn-select"; import { Chart as ChartJS, CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend, type ChartOptions, } from 'chart.js'; import type { OperWqltContent } from "../model/fo-interface"; import { OperationApi, basinStore } from "../api/fo-service"; import { defaultInitOperWqltState } from "../api/fo-service"; // Chart.js 등록 ChartJS.register(CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend); type WaterQualityType = 'BOD' | 'TOC' | 'SS' | 'TP' | 'TN'; const FoWqltInflEffl: React.FC = () => { const { selectedBasin, setSelectedBasin } = useStore(basinStore); const [selectedTab, setSelectedTab] = useState('BOD'); const [wqltData, setWqltData] = useState([]); const tabs: WaterQualityType[] = ['BOD', 'TOC', 'SS', 'TP', 'TN']; const onBasinChange = (value: string) => { setSelectedBasin(value); }; useEffect(() => { setWqltData(defaultInitOperWqltState.operWqltStatus); const fetchData = async () => { try { const standardYear = await OperationApi.getStandardYear(); const data = await OperationApi.getDashOperWqlt(standardYear); setWqltData(data); // console.log(wqltData); } catch (e) { console.error("시설 유입ㆍ방류수질 데이터 조회 실패", e); } }; fetchData(); }, []); const chartData = useMemo(() => { const filteredData = wqltData .filter(d => d.rvbsn === selectedBasin) // 해당 유역 .sort((a, b) => Number(a.yr) - Number(b.yr)); const wqltKey = selectedTab.charAt(0).toUpperCase() + selectedTab.slice(1).toLowerCase(); const inflowKey = `sumInfl${wqltKey}` as keyof OperWqltContent; const effluentKey = `sumEffl${wqltKey}` as keyof OperWqltContent; const inflowAvgKey = `avgInfl${wqltKey}` as keyof OperWqltContent; const effluentAvgKey = `avgEffl${wqltKey}` as keyof OperWqltContent; const labels = filteredData.map(d => d.yr ?? ''); const inflowData = filteredData.map(d => d[inflowKey] as number); const effluentData = filteredData.map(d => d[effluentKey] as number); const inflowAvg = filteredData[0]?.[inflowAvgKey] as number ?? 0; const effluentAvg = filteredData[0]?.[effluentAvgKey] as number ?? 0; return { labels, inflowData, effluentData, inflowAvg, effluentAvg }; }, [wqltData, selectedBasin, selectedTab]); // console.log(chartData); // 차트 공통 옵션 // const getChartOptions = (yAxisLabel: string): ChartOptions<'line'> => ({ const getChartOptions = (): ChartOptions<'line'> => ({ responsive: true, maintainAspectRatio: false, interaction: { mode: 'index', intersect: false, }, plugins: { legend: { display: true, position: 'bottom', labels: { usePointStyle: true, boxWidth: 6, color: '#000', font: { size: 8, }, }, }, tooltip: { enabled: true, titleFont: { size: 10 }, bodyFont: { size: 10 }, }, }, scales: { x: { grid: { color: 'rgba(255, 255, 255, 0.1)', tickLength: 4, // 눈금 길이 축소 }, ticks: { color: '#000', font: { size: 8, // 폰트 크기 축소 }, padding: 0, }, }, y: { grid: { color: 'rgba(255, 255, 255, 0.1)', }, ticks: { color: '#000', font: { size: 8, // 폰트 크기 축소 }, maxTicksLimit: 4, // 눈금 개수 제한 padding: 2, }, title: { display: false, }, }, }, layout: { padding: { top: 5, bottom: 0, left: 0, right: 5 } } }); // 유입 데이터 차트 설정 const inflowChartData = { labels: chartData.labels, datasets: [ { label: `유입${selectedTab} (mg/L)`, data: chartData.inflowData, borderColor: '#00D4FF', backgroundColor: '#00D4FF', borderWidth: 1, pointRadius: 2, pointHoverRadius: 4, tension: 0.4, }, { label: '유입 평균 (mg/L)', data: Array(chartData.labels.length).fill(chartData.inflowAvg), borderColor: '#FF1493', backgroundColor: '#FF1493', borderWidth: 1, borderDash: [10, 5], pointRadius: 0, pointHoverRadius: 4, tension: 0, }, ], }; // 방류 데이터 차트 설정 const effluentChartData = { labels: chartData.labels, datasets: [ { label: `방류${selectedTab} (mg/L)`, data: chartData.effluentData, borderColor: '#00FF88', backgroundColor: '#00FF88', borderWidth: 1, pointRadius: 2, pointHoverRadius: 4, tension: 0.4, }, { label: '방류 평균 (mg/L)', data: Array(chartData.labels.length).fill(chartData.effluentAvg), borderColor: '#FFB800', backgroundColor: '#FFB800', borderWidth: 1, borderDash: [10, 5], pointRadius: 0, pointHoverRadius: 4, tension: 0, }, ], }; return (

수계별 유입ㆍ방류수질 분석

{/* 탭 메뉴 */}
{tabs.map((tab) => ( ))}
{/* 차트 영역 */}
{/* 유입 차트 */}
{/* 방류 차트 */}
); }; export default FoWqltInflEffl;