feat: Implementiere dynamischen Import für chartjs-plugin-zoom im Frontend
- Dynamischer Import von chartjs-plugin-zoom im `useEffect` hinzugefügt, um Zugriff auf `window` im Server-Side-Build zu vermeiden. - Zustandsvariable `zoomPluginLoaded` eingeführt, um sicherzustellen, dass das Plugin nur im Client geladen wird. - Verwende `useRef` für Canvas-Referenz, um direkten Zugriff auf das Chart-Element zu ermöglichen. - Optimierung der Chart-Erstellung, um Build-Fehler aufgrund fehlender `window`-Objekte zu verhindern.
This commit is contained in:
@@ -1,11 +1,10 @@
|
|||||||
"use client"; // components/modules/Kue705FO.jsx
|
"use client"; // components/modules/Kue705FO.jsx
|
||||||
import React, { useState, useEffect } from "react";
|
import React, { useState, useEffect, useRef } from "react";
|
||||||
import ReactModal from "react-modal";
|
import ReactModal from "react-modal";
|
||||||
import Chart from "chart.js/auto";
|
import Chart from "chart.js/auto";
|
||||||
import { useSelector } from "react-redux";
|
import { useSelector } from "react-redux";
|
||||||
import KueModal from "../modales/KueModal";
|
import KueModal from "../modales/KueModal";
|
||||||
import "bootstrap-icons/font/bootstrap-icons.css"; // Import Bootstrap Icons
|
import "bootstrap-icons/font/bootstrap-icons.css"; // Import Bootstrap Icons
|
||||||
import zoomPlugin from "chartjs-plugin-zoom";
|
|
||||||
|
|
||||||
function Kue705FO({
|
function Kue705FO({
|
||||||
isolationswert,
|
isolationswert,
|
||||||
@@ -16,6 +15,8 @@ function Kue705FO({
|
|||||||
tdrLocation,
|
tdrLocation,
|
||||||
alarmStatus,
|
alarmStatus,
|
||||||
}) {
|
}) {
|
||||||
|
const chartRef = useRef(null);
|
||||||
|
const [zoomPlugin, setZoomPlugin] = useState(null); // Plugin-Status für Chart.js
|
||||||
const [kueVersion, setKueVersion] = useState("V4.19");
|
const [kueVersion, setKueVersion] = useState("V4.19");
|
||||||
const [currentAlarmStatus, setCurrentAlarmStatus] = useState(false);
|
const [currentAlarmStatus, setCurrentAlarmStatus] = useState(false);
|
||||||
const [currentModulName, setCurrentModulName] = useState(modulName);
|
const [currentModulName, setCurrentModulName] = useState(modulName);
|
||||||
@@ -191,7 +192,15 @@ function Kue705FO({
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Füge das Plugin zu Chart.js hinzu
|
// Füge das Plugin zu Chart.js hinzu
|
||||||
Chart.register(zoomPlugin);
|
useEffect(() => {
|
||||||
|
// Lade das Plugin nur, wenn `window` verfügbar ist
|
||||||
|
if (typeof window !== "undefined") {
|
||||||
|
import("chartjs-plugin-zoom").then((mod) => {
|
||||||
|
setZoomPlugin(mod.default); // Setze das Plugin
|
||||||
|
Chart.register(mod.default); // Plugin zur Chart.js-Instanz registrieren
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
const createChart = (data) => {
|
const createChart = (data) => {
|
||||||
const ctx = document.getElementById("myChart").getContext("2d");
|
const ctx = document.getElementById("myChart").getContext("2d");
|
||||||
@@ -206,7 +215,7 @@ function Kue705FO({
|
|||||||
label: "Isolationswiderstand (MOhm)",
|
label: "Isolationswiderstand (MOhm)",
|
||||||
data: data.map((row) => row.m).reverse(),
|
data: data.map((row) => row.m).reverse(),
|
||||||
borderColor: "#00AEEF",
|
borderColor: "#00AEEF",
|
||||||
borderWidth: 0.5,
|
borderWidth: 2,
|
||||||
fill: false,
|
fill: false,
|
||||||
yAxisID: "y",
|
yAxisID: "y",
|
||||||
},
|
},
|
||||||
@@ -214,7 +223,7 @@ function Kue705FO({
|
|||||||
label: "Schleifenwiderstand (kOhm)",
|
label: "Schleifenwiderstand (kOhm)",
|
||||||
data: data.map((row) => row.n).reverse(),
|
data: data.map((row) => row.n).reverse(),
|
||||||
borderColor: "black",
|
borderColor: "black",
|
||||||
borderWidth: 0.5,
|
borderWidth: 2,
|
||||||
fill: false,
|
fill: false,
|
||||||
yAxisID: "y1",
|
yAxisID: "y1",
|
||||||
},
|
},
|
||||||
@@ -500,6 +509,7 @@ function Kue705FO({
|
|||||||
<h2>Messkurve Slot {slotIndex + 1}</h2>
|
<h2>Messkurve Slot {slotIndex + 1}</h2>
|
||||||
<canvas
|
<canvas
|
||||||
id="myChart"
|
id="myChart"
|
||||||
|
ref={chartRef}
|
||||||
style={{ width: "100%", height: "600px" }}
|
style={{ width: "100%", height: "600px" }}
|
||||||
></canvas>
|
></canvas>
|
||||||
</ReactModal>
|
</ReactModal>
|
||||||
|
|||||||
Reference in New Issue
Block a user