109 lines
3.1 KiB
TypeScript
109 lines
3.1 KiB
TypeScript
// components/main/kabelueberwachung/kue705FO/Charts/LoopMeasurementChart/LoopMeasurementChart.tsx
|
|
import React, { useEffect, useRef } from "react";
|
|
import { useSelector } from "react-redux";
|
|
import Chart from "chart.js/auto";
|
|
import "chartjs-adapter-moment";
|
|
|
|
const LoopMeasurementChart = () => {
|
|
const chartRef = useRef<HTMLCanvasElement>(null);
|
|
const chartInstance = useRef<Chart | null>(null);
|
|
|
|
// Daten aus dem Redux-Store abrufen
|
|
const chartData =
|
|
useSelector(
|
|
(state: { chartData: { data: any[] } }) => state.chartData.data
|
|
) ?? [];
|
|
|
|
// Ermittlung des ausgewählten Modus (DIA0, DIA1, DIA2)
|
|
const selectedMode = useSelector(
|
|
(state: { chartMode: { mode: "DIA0" | "DIA1" | "DIA2" } }) =>
|
|
state.chartMode?.mode ?? "DIA0"
|
|
);
|
|
|
|
useEffect(() => {
|
|
if (chartRef.current) {
|
|
if (chartInstance.current) {
|
|
chartInstance.current.destroy();
|
|
}
|
|
|
|
// Datenvorbereitung: Konvertieren der Zeitstempel in Millisekunden
|
|
const processedData = chartData.map((entry) => ({
|
|
...entry,
|
|
timestampMs: new Date(entry.t).getTime(),
|
|
}));
|
|
|
|
const datasets = [
|
|
{
|
|
label: "Messwert Minimum (kOhm)",
|
|
data: processedData.map((entry) => ({
|
|
x: entry.timestampMs,
|
|
y: entry.i,
|
|
})),
|
|
borderColor: "rgba(75, 192, 192, 1)",
|
|
backgroundColor: "rgba(75, 192, 192, 0.2)",
|
|
fill: true,
|
|
},
|
|
{
|
|
label: "Messwert Maximum (kOhm)",
|
|
data: processedData.map((entry) => ({
|
|
x: entry.timestampMs,
|
|
y: entry.a,
|
|
})),
|
|
borderColor: "rgba(192, 75, 75, 1)",
|
|
backgroundColor: "rgba(192, 75, 75, 0.2)",
|
|
fill: true,
|
|
},
|
|
{
|
|
label:
|
|
selectedMode === "DIA0"
|
|
? "Messwert Mittelwert (m)"
|
|
: "Messwert Durchschnitt (g)",
|
|
data: processedData.map((entry) => ({
|
|
x: entry.timestampMs,
|
|
y: selectedMode === "DIA0" ? entry.m : entry.g,
|
|
})),
|
|
borderColor: "rgba(75, 75, 192, 1)",
|
|
backgroundColor: "rgba(75, 75, 192, 0.2)",
|
|
fill: true,
|
|
},
|
|
];
|
|
|
|
const ctx = chartRef.current.getContext("2d");
|
|
if (ctx) {
|
|
chartInstance.current = new Chart(ctx, {
|
|
type: "line",
|
|
data: { datasets },
|
|
options: {
|
|
responsive: true,
|
|
maintainAspectRatio: false,
|
|
scales: {
|
|
x: {
|
|
type: "time",
|
|
time: {
|
|
unit: "day",
|
|
tooltipFormat: "dd.MM.yyyy HH:mm",
|
|
displayFormats: { day: "dd.MM.yyyy" },
|
|
},
|
|
title: { display: true, text: "Zeit" },
|
|
},
|
|
y: {
|
|
title: { display: true, text: "kOhm" },
|
|
min: 0,
|
|
max: 80,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
}
|
|
}, [chartData, selectedMode]);
|
|
|
|
return (
|
|
<div style={{ position: "relative", width: "100%", height: "400px" }}>
|
|
<canvas ref={chartRef} />
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default LoopMeasurementChart;
|