Die Variable 'chartData' wurde in 'loopMeasurementCurveChartData' umbenannt, um den Inhalt und Zweck der Daten klarer zu definieren. Diese Änderung verbessert die Lesbarkeit und Wartbarkeit des Codes, indem sie die Bedeutung der Variablen präziser beschreibt.
95 lines
2.8 KiB
TypeScript
95 lines
2.8 KiB
TypeScript
// components/main/kabelueberwachung/kue705FO/Charts/TDRChart/TDRChart.tsx
|
|
import React, { useEffect, useRef } from "react";
|
|
import { useSelector } from "react-redux";
|
|
import { Chart, registerables } from "chart.js";
|
|
import "chartjs-adapter-date-fns";
|
|
|
|
const TDRChart: React.FC = () => {
|
|
const chartRef = useRef<HTMLCanvasElement>(null);
|
|
const chartInstance = useRef<Chart | null>(null);
|
|
|
|
// TDR-Daten aus dem Redux Store selektieren
|
|
const loopMeasurementCurveChartData = useSelector(
|
|
(state: any) => state.tdrChartData.data
|
|
);
|
|
|
|
useEffect(() => {
|
|
// Importiere und registriere das Zoom-Plugin innerhalb des useEffect-Hooks
|
|
import("chartjs-plugin-zoom").then((zoomPlugin) => {
|
|
Chart.register(...registerables, zoomPlugin.default);
|
|
|
|
if (chartRef.current && loopMeasurementCurveChartData.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: "TDR Entfernung (km)",
|
|
data: loopMeasurementCurveChartData,
|
|
borderColor: "rgba(255, 99, 132, 1)",
|
|
backgroundColor: "rgba(255, 99, 132, 0.2)",
|
|
tension: 0.1,
|
|
parsing: {
|
|
xAxisKey: "t", // Schlüssel für den Zeitstempel
|
|
yAxisKey: "m", // Schlüssel für den Messwert
|
|
},
|
|
},
|
|
],
|
|
},
|
|
options: {
|
|
responsive: true,
|
|
maintainAspectRatio: false,
|
|
scales: {
|
|
x: {
|
|
type: "linear", // Lineare Skalierung für numerische Daten
|
|
title: {
|
|
display: true,
|
|
text: "Entfernung (m)",
|
|
},
|
|
},
|
|
y: {
|
|
title: {
|
|
display: true,
|
|
text: "Pegel",
|
|
},
|
|
},
|
|
},
|
|
plugins: {
|
|
zoom: {
|
|
pan: {
|
|
enabled: true,
|
|
mode: "xy",
|
|
},
|
|
zoom: {
|
|
wheel: {
|
|
enabled: true,
|
|
},
|
|
pinch: {
|
|
enabled: true,
|
|
},
|
|
mode: "xy",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
}
|
|
});
|
|
}, [loopMeasurementCurveChartData]);
|
|
|
|
return (
|
|
<div>
|
|
<canvas ref={chartRef} style={{ width: "100%", height: "20rem" }} />
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default TDRChart;
|