import React, { useRef, useEffect, useState } from 'react'; import { Line } from 'react-chartjs-2'; import ChartJS from 'chart.js/auto'; import zoomPlugin from 'chartjs-plugin-zoom'; ChartJS.register(zoomPlugin); interface TimeSeriesEntry { time: string; value: number; } interface EnergyLineChartProps { consumption: TimeSeriesEntry[]; generation: TimeSeriesEntry[]; } const EnergyLineChart = ({ consumption, generation }: EnergyLineChartProps) => { const chartRef = useRef(null); // Generate sorted unique time labels from both series const allTimes = Array.from(new Set([ ...consumption.map(d => d.time), ...generation.map(d => d.time), ])).sort(); // e.g., ["00:00", "00:30", "01:00", ...] // Map times to values const consumptionMap = Object.fromEntries(consumption.map(d => [d.time, d.value])); const generationMap = Object.fromEntries(generation.map(d => [d.time, d.value])); const [startIndex, setStartIndex] = useState(0); const [endIndex, setEndIndex] = useState(allTimes.length - 1); useEffect(() => { if (typeof window !== 'undefined') { import('hammerjs'); } }, []); const filteredLabels = allTimes.slice(startIndex, endIndex + 1); const filteredConsumption = filteredLabels.map(t => consumptionMap[t] ?? null); const filteredGeneration = filteredLabels.map(t => generationMap[t] ?? null); const allValues = [...filteredConsumption, ...filteredGeneration].filter(v => v !== null) as number[]; const maxValue = allValues.length > 0 ? Math.max(...allValues) : 0; const yAxisSuggestedMax = maxValue * 1.15; const data = { labels: filteredLabels, datasets: [ { label: 'Consumption', data: filteredConsumption, borderColor: '#8884d8', tension: 0.4, fill: false, }, { label: 'Generation', data: filteredGeneration, borderColor: '#82ca9d', tension: 0.4, fill: false, }, ], }; const options = { responsive: true, maintainAspectRatio: false, plugins: { legend: { position: 'top' as const }, zoom: { zoom: { wheel: { enabled: true }, pinch: { enabled: true }, mode: 'x' as const, }, pan: { enabled: true, mode: 'x' as const }, }, tooltip: { enabled: true, mode: 'index' as const, intersect: false }, }, scales: { x: { title: { display: true, text: 'Time (HH:MM)', font: { weight: 'bold' as const, // ✅ FIX: cast as 'const' }, }, }, y: { beginAtZero: true, suggestedMax: yAxisSuggestedMax, title: { display: true, text: 'Power (kW)', font: { weight: 'bold' as const, // ✅ FIX: cast as 'const' }, }, }, }, } as const; // ✅ Ensures compatibility with chart.js types const handleResetZoom = () => { chartRef.current?.resetZoom(); }; return (

Energy Consumption & Generation

{/* Time range selectors */}
); }; export default EnergyLineChart;