// components/main/kabelueberwachung/kue705FO/Charts/TDRChart/TDRChart.tsx "use client"; import React, { useEffect, useRef } from "react"; import { RootState } from "../../../../../../redux/store"; import { useSelector } from "react-redux"; import { Chart, registerables } from "chart.js"; import "chartjs-adapter-date-fns"; import { getColor } from "../../../../../../utils/colors"; import TDRChartActionBar from "./TDRChartActionBar"; const TDRChart: React.FC<{ isFullScreen: boolean }> = ({ isFullScreen }) => { const chartRef = useRef(null); const chartInstance = useRef(null); // 🟢 **Hole den ausgewählten Slot und Messkurve aus Redux** const selectedId = useSelector( (state: RootState) => state.tdrDataById.selectedId ); const selectedSlot = useSelector( (state: RootState) => state.kueChartMode.selectedSlot ); const selectedChartType = useSelector( (state: RootState) => state.kueChartMode.activeMode ); const tdrChartData = useSelector((state: RootState) => selectedId !== null ? state.tdrDataById.dataById[selectedId] || [] : [] ); const referenceChartData = useSelector((state: RootState) => selectedSlot !== null ? state.tdrReferenceChart.referenceData[selectedSlot] || [] : [] ); useEffect(() => { import("chartjs-plugin-zoom").then((zoomPlugin) => { Chart.register(...registerables, zoomPlugin.default); if (chartRef.current && tdrChartData.length > 0) { if (chartInstance.current) { chartInstance.current.destroy(); } const ctx = chartRef.current.getContext("2d"); if (ctx) { chartInstance.current = new Chart(ctx, { type: "line", data: { datasets: [ { label: `Modul ${ selectedSlot !== null ? selectedSlot + 1 : "?" }`, data: tdrChartData, borderColor: getColor("littwin-blue"), borderWidth: 1, tension: 0.1, parsing: { xAxisKey: "d", // Entfernung/distance // statt "t" yAxisKey: "p", // Pegel // statt "m" }, }, { label: "Referenzkurve", data: referenceChartData, borderColor: "black", // Schwarz für Referenzkurve borderWidth: 1, borderDash: [5, 5], // Gepunktete Linie pointRadius: 3, // Punkte für Tooltip sichtbar machen pointHoverRadius: 5, // Größere Punkte beim Hover pointBackgroundColor: "black", tension: 0.1, parsing: { xAxisKey: "d", yAxisKey: "p", }, }, ], }, options: { responsive: true, maintainAspectRatio: false, animation: { duration: 150, // 150 ms Animation }, scales: { x: { type: "linear", title: { display: true, text: "Entfernung (m)", }, }, y: { title: { display: true, text: "Pegel", }, }, }, plugins: { tooltip: { callbacks: { title: () => "", // 🚫 Entfernt die erste Zeile mit der Nummer label: function (context) { const rawData = context.raw as { d: number; p: number }; // 👇 Unterscheide zwischen Mess- und Referenzkurve if (context.dataset.label === "Referenzkurve") { return [ `Referenzwert`, `Entfernung: ${rawData.d.toFixed(0)} Meter`, `Pegel: ${rawData.p.toFixed(2)}`, ]; } else { return [ `Messwert`, `Entfernung: ${rawData.d.toFixed(0)} Meter`, `Pegel: ${rawData.p.toFixed(2)}`, ]; } }, }, }, zoom: { pan: { enabled: true, mode: "xy", }, zoom: { wheel: { enabled: true, }, pinch: { enabled: true, }, mode: "xy", }, }, }, }, }); } } }); }, [JSON.stringify(tdrChartData), selectedSlot, selectedChartType]); return (
{tdrChartData.length === 0 ? (
⚠️ Keine Daten verfügbar für diesen Slot
) : ( )}
); }; export default TDRChart;