Files
CPLv4.0/components/main/kabelueberwachung/kue705FO/Charts/LoopMeasurementChart/LoopMeasurementChart.tsx
ISA 94b40c9b67 feat: Uhrzeit in Chart-Tooltips hinzugefügt
- Tooltip-Format geändert auf `dd.MM.yyyy HH:mm`, um Datum und Uhrzeit anzuzeigen
- X-Achse zeigt weiterhin nur das Datum (`dd.MM.yyyy`), aber Tooltips enthalten auch die Uhrzeit
- Tooltip `callbacks.label` angepasst, um Uhrzeit (`HH:mm`) bei Mouse-Hover zusätzlich anzuzeigen
- Sicherstellung, dass alle Linien (i, a, m, g) die korrekte Uhrzeit im Tooltip anzeigen
2025-02-21 11:56:39 +01:00

170 lines
5.4 KiB
TypeScript

// components/main/kabelueberwachung/kue705FO/Charts/LoopMeasurementChart/LoopMeasurementChart.tsx
import React, { useEffect, useRef } from "react";
import { useSelector } from "react-redux";
import { RootState } from "../../../../../../redux/store";
import Chart from "chart.js/auto";
import "chartjs-adapter-moment";
import zoomPlugin from "chartjs-plugin-zoom"; // Zoom-Plugin importieren
Chart.register(zoomPlugin); // Plugin registrieren
const LoopMeasurementChart = () => {
const chartRef = useRef<HTMLCanvasElement>(null);
const chartInstance = useRef<Chart | null>(null);
// Daten & Datum aus Redux abrufen
const { chartData, vonDatum, bisDatum } = useSelector(
(state: RootState) => state.kabelueberwachungChart
);
const selectedMode = useSelector(
(state: RootState) => state.kabelueberwachungChart.selectedMode
);
useEffect(() => {
if (chartRef.current) {
if (chartInstance.current) {
chartInstance.current.destroy();
}
console.log("Chart Data:", chartData);
console.log("Von Datum:", vonDatum, "Bis Datum:", bisDatum);
console.log("Selected Mode:", selectedMode);
// Daten filtern basierend auf vonDatum und bisDatum
const filteredData = chartData.filter((entry) => {
const timestampMs = new Date(entry.t).getTime();
return (
timestampMs >= new Date(vonDatum).getTime() &&
timestampMs <= new Date(bisDatum).getTime()
);
});
// Basis-Datasets für i (Minimum) und a (Maximum)
const datasets = [
{
label: "Messwert Minimum (kOhm)",
data: filteredData.map((entry) => ({
x: new Date(entry.t).getTime(),
y: entry.i,
})),
borderColor: "rgba(75, 192, 192, 1)", // Türkis
backgroundColor: "rgba(75, 192, 192, 0.2)",
fill: false,
},
{
label: "Messwert Maximum (kOhm)",
data: filteredData.map((entry) => ({
x: new Date(entry.t).getTime(),
y: entry.a,
})),
borderColor: "rgba(192, 75, 75, 1)", // Rot
backgroundColor: "rgba(192, 75, 75, 0.2)",
fill: false,
},
];
// Falls DIA0: `m` als aktueller Messwert verwenden
if (
selectedMode === "DIA0" &&
filteredData.some((entry) => entry.m !== undefined)
) {
datasets.push({
label: "Messwert Aktuell (m)",
data: filteredData.map((entry) => ({
x: new Date(entry.t).getTime(),
y: entry.m ?? NaN,
})),
borderColor: "rgba(255, 165, 0, 1)", // Orange
backgroundColor: "rgba(255, 165, 0, 0.2)",
fill: false,
});
}
// Falls DIA1 oder DIA2: `g` als Durchschnittswert verwenden
if (
(selectedMode === "DIA1" || selectedMode === "DIA2") &&
filteredData.some((entry) => entry.g !== undefined)
) {
datasets.push({
label: "Messwert Durchschnitt (g)",
data: filteredData.map((entry) => ({
x: new Date(entry.t).getTime(),
y: entry.g ?? NaN,
})),
borderColor: "rgba(75, 75, 192, 1)", // Blau
backgroundColor: "rgba(75, 75, 192, 0.2)",
fill: false,
});
}
const ctx = chartRef.current.getContext("2d");
if (ctx) {
chartInstance.current = new Chart(ctx, {
type: "line",
data: { datasets },
options: {
responsive: true,
maintainAspectRatio: false,
elements: {
line: { spanGaps: true },
},
scales: {
x: {
type: "time",
time: {
unit: "day",
tooltipFormat: "dd.MM.yyyy HH:mm", // Ändert das Format für Tooltips (inkl. Uhrzeit)
displayFormats: { day: "dd.MM.yyyy" }, // Achse bleibt nur Datum
},
title: { display: true, text: "Zeit (Datum)" },
min: new Date(vonDatum).getTime(),
max: new Date(bisDatum).getTime(),
},
y: {
ticks: {
callback: (value) => value.toFixed(2) + " kOhm",
},
},
},
plugins: {
tooltip: {
callbacks: {
label: (tooltipItem) => {
const date = new Date(tooltipItem.raw.x);
const timeString = `${date
.getHours()
.toString()
.padStart(2, "0")}:${date
.getMinutes()
.toString()
.padStart(2, "0")}`;
return `${
tooltipItem.dataset.label
}: ${tooltipItem.raw.y.toFixed(2)} kOhm `;
},
},
},
zoom: {
pan: { enabled: true, mode: "x" },
zoom: {
wheel: { enabled: true },
pinch: { enabled: true },
mode: "x",
},
},
},
},
});
}
}
}, [chartData, vonDatum, bisDatum, selectedMode]);
return (
<div style={{ position: "relative", width: "100%", height: "400px" }}>
<canvas ref={chartRef} />
</div>
);
};
export default LoopMeasurementChart;