Fix: Preserve chart state during zoom, pan, and date changes

- Added React.useMemo to memoize chartData and chartOptions to prevent unnecessary re-renders.
- Ensured chart zoom and pan states are maintained during interactions.
- Improved performance and user experience by avoiding chart
This commit is contained in:
ISA
2025-07-22 10:19:50 +02:00
parent 773e2c12b8
commit ed9f693098
6 changed files with 20 additions and 22 deletions

View File

@@ -4,10 +4,8 @@ import React, { useEffect, useRef } from "react";
import { useDispatch, useSelector } from "react-redux";
import { RootState, AppDispatch } from "@/redux/store";
import { Line } from "react-chartjs-2";
import { Chart } from "chart.js";
import {
Chart as ChartJS,
LineElement,
PointElement,
CategoryScale,
LinearScale,
Tooltip,
@@ -30,20 +28,11 @@ import { getColor } from "@/utils/colors";
import AnalogInputsDatePicker from "./AnalogInputsDatePicker";
// ✅ Nur die Basis-ChartJS-Module registrieren
ChartJS.register(
LineElement,
PointElement,
CategoryScale,
LinearScale,
Tooltip,
Legend,
Filler,
TimeScale
);
Chart.register(CategoryScale, LinearScale, Tooltip, Legend, Filler, TimeScale);
export default function AnalogInputsChart() {
const dispatch = useDispatch<AppDispatch>();
const chartRef = useRef<Line | null>(null);
const chartRef = useRef<any>(null);
// Redux Werte für Chart-Daten
const { zeitraum, vonDatum, bisDatum, data, autoLoad, selectedId } =
@@ -239,12 +228,12 @@ export default function AnalogInputsChart() {
zoom: {
pan: {
enabled: true,
mode: "x",
mode: "x" as const,
},
zoom: {
wheel: { enabled: true },
pinch: { enabled: true },
mode: "x",
mode: "x" as const,
},
},
},
@@ -292,7 +281,7 @@ export default function AnalogInputsChart() {
useEffect(() => {
if (typeof window !== "undefined") {
import("chartjs-plugin-zoom").then((module) => {
ChartJS.register(module.default);
Chart.register(module.default);
});
}
}, []);