242 lines
6.5 KiB
TypeScript
242 lines
6.5 KiB
TypeScript
"use client";
|
|
/**
|
|
* 물재이용 차트
|
|
*/
|
|
import React, { useState, useEffect, useRef } from 'react';
|
|
import { useStore } from "zustand";
|
|
import { Line } from 'react-chartjs-2';
|
|
import { Button } from "@common/components";
|
|
import type { ReuseStatusContent } from "../model/fo-interface";
|
|
import { OperationApi, reuseStore } from "../api/fo-service";
|
|
import {
|
|
Chart as ChartJS,
|
|
CategoryScale,
|
|
LinearScale,
|
|
PointElement,
|
|
LineElement,
|
|
Title,
|
|
Tooltip,
|
|
Legend,
|
|
Filler,
|
|
type ChartOptions
|
|
} from 'chart.js';
|
|
|
|
// Chart.js 플러그인 등록
|
|
ChartJS.register(
|
|
CategoryScale,
|
|
LinearScale,
|
|
PointElement,
|
|
LineElement,
|
|
Title,
|
|
Tooltip,
|
|
Legend,
|
|
Filler // 영역 채우기를 위해 필요
|
|
);
|
|
|
|
|
|
|
|
const ReuseChart: React.FC = () => {
|
|
const [activeIndex, setActiveIndex] = useState(0);
|
|
const intervalRef = useRef<NodeJS.Timeout | null>(null);
|
|
const reuseStatusData = useStore(
|
|
reuseStore,
|
|
(state) => state.reuseStatus) as ReuseStatusContent[];
|
|
|
|
useEffect(() => {
|
|
OperationApi.getReuseStatus().catch((e) => {
|
|
console.error("물재이용 차트 데이터 조회 실패", e);
|
|
});
|
|
}, []);
|
|
|
|
const labels = reuseStatusData?.map((r) => r.yr);
|
|
const sewageData = reuseStatusData?.map((r) => r.sewage);
|
|
const reclaimedData = reuseStatusData?.map((r) => r.reclaimed);
|
|
const rainwaterData = reuseStatusData?.map((r) => r.rainwater);
|
|
|
|
const commonChartData = {
|
|
datasets: [
|
|
{
|
|
label: '재이용량',
|
|
data: sewageData, // 기본은 하수처리수
|
|
borderColor: '#00D4FF', // 선 색상 (밝은 하늘색)
|
|
backgroundColor: 'rgba(0, 212, 255, 0.1)', // 채우기 색상 (투명도 있는 하늘색)
|
|
borderWidth: 3,
|
|
pointBackgroundColor: '#00D4FF',
|
|
pointBorderColor: '#fff',
|
|
pointBorderWidth: 2,
|
|
pointRadius: 6,
|
|
pointHoverRadius: 8,
|
|
fill: true, // 아래 영역 채우기 활성화
|
|
tension: 0.4, // 곡선 부드럽게
|
|
},
|
|
],
|
|
};
|
|
|
|
const commonChartOptions: ChartOptions<'line'> = {
|
|
maintainAspectRatio: false,
|
|
plugins: {
|
|
legend: {
|
|
display: false, // 범례 숨김 (이미지에 없음)
|
|
},
|
|
tooltip: {
|
|
callbacks: {
|
|
label: (context) => {
|
|
let label = context.dataset.label || '';
|
|
if (label) {
|
|
label += ': ';
|
|
}
|
|
if (context.parsed.y !== null) {
|
|
label += new Intl.NumberFormat('ko-KR').format(context.parsed.y) + '㎥';
|
|
}
|
|
return label;
|
|
}
|
|
}
|
|
}
|
|
},
|
|
scales: {
|
|
x: {
|
|
grid: {
|
|
display: true,
|
|
color: '#f0f0f0', // 연한 회색 그리드
|
|
},
|
|
ticks: {
|
|
color: '#666',
|
|
}
|
|
},
|
|
y: {
|
|
min: 1100000, // Y축 최소값 설정 (그래프 모양을 위해)
|
|
grid: {
|
|
color: '#f0f0f0',
|
|
},
|
|
ticks: {
|
|
color: '#666',
|
|
callback: function(value) {
|
|
return new Intl.NumberFormat('ko-KR').format(Number(value)) + '㎥';
|
|
}
|
|
}
|
|
}
|
|
},
|
|
interaction: {
|
|
mode: 'index',
|
|
intersect: false,
|
|
},
|
|
};
|
|
|
|
const chartItems = [
|
|
{
|
|
title: "하수처리수 재이용량 (m³)",
|
|
data: {
|
|
...commonChartData,
|
|
labels,
|
|
datasets: [{ ...commonChartData.datasets[0], data: sewageData }],
|
|
},
|
|
options: commonChartOptions,
|
|
},
|
|
{
|
|
title: "중수도 재이용량 (m³)",
|
|
data: {
|
|
...commonChartData,
|
|
labels,
|
|
datasets: [{ ...commonChartData.datasets[0], data: reclaimedData }],
|
|
},
|
|
options: commonChartOptions,
|
|
},
|
|
{
|
|
title: "빗물 재이용량 (m³)",
|
|
data: {
|
|
...commonChartData,
|
|
labels,
|
|
datasets: [{ ...commonChartData.datasets[0], data: rainwaterData }],
|
|
},
|
|
options: commonChartOptions,
|
|
},
|
|
];
|
|
|
|
const startSlider = () => {
|
|
intervalRef.current = setInterval(() => {
|
|
setActiveIndex(prevIndex => (prevIndex + 1) % chartItems.length);
|
|
}, 7000);
|
|
};
|
|
|
|
const stopSlider = () => {
|
|
if (intervalRef.current) {
|
|
clearInterval(intervalRef.current);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
startSlider();
|
|
return () => stopSlider();
|
|
}, []);
|
|
|
|
const handleIndicatorClick = (index: number) => {
|
|
stopSlider();
|
|
setActiveIndex(index);
|
|
startSlider();
|
|
};
|
|
|
|
const handleMoreClick = () => {
|
|
window.open(process.env.NEXT_PUBLIC_WR, "_blank", "noopener,noreferrer");
|
|
};
|
|
|
|
const viewportRef = useRef<HTMLDivElement>(null);
|
|
|
|
return (
|
|
<div className="card card-border" style={{ height: "250px" }}>
|
|
<div className="card-header text-foreground">
|
|
<h3 className="text-2xl font-medium">{chartItems[activeIndex]?.title}</h3>
|
|
<Button className="h-[3.2rem]" onClick={handleMoreClick}>더보기</Button>
|
|
</div>
|
|
<div className="relative" style={{ maxWidth: 400, height: 180, marginLeft: 20 }}>
|
|
<div className="h-full">
|
|
|
|
{/* ✅ 좌표 기준 (절대 이동 X) */}
|
|
<div
|
|
ref={viewportRef}
|
|
className="w-full overflow-hidden"
|
|
style={{ height: 170}}
|
|
>
|
|
{/* ✅ 이동 대상 */}
|
|
<div
|
|
className="flex h-full transition-transform duration-500 ease-in-out"
|
|
style={{
|
|
width: `${chartItems.length * 100}%`,
|
|
transform: `translateX(-${
|
|
activeIndex * (viewportRef.current?.offsetWidth ?? 0)
|
|
}px)`
|
|
}}
|
|
>
|
|
{chartItems.map((chart, index) => (
|
|
<div
|
|
key={index}
|
|
className="h-full flex-shrink-0"
|
|
style={{ width: `${100 / chartItems.length}%` }}
|
|
>
|
|
<Line
|
|
data={chart.data}
|
|
options={chart.options as ChartOptions<'line'>}
|
|
/>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
<div className="absolute left-1/2 -translate-x-1/2 flex space-x-2" style={{ bottom: '-1px'}}>
|
|
{chartItems.map((_, index) => (
|
|
<button
|
|
key={index}
|
|
onClick={() => handleIndicatorClick(index)}
|
|
className={`w-3 h-3 rounded-full cursor-pointer transition-colors ${
|
|
activeIndex === index ? 'bg-blue-500' : 'bg-gray-300'
|
|
}`}
|
|
aria-label={`Go to slide ${index + 1}`}
|
|
/>
|
|
))}
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default ReuseChart; |