"use client"; import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, ReferenceLine } from "recharts"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Skeleton } from "@/components/ui/skeleton"; import type { TempBucket } from "@/lib/api"; interface Props { data: TempBucket[]; loading?: boolean; } type ChartRow = { time: string; [roomId: string]: string | number }; function formatTime(iso: string) { return new Date(iso).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }); } export function TemperatureTrendChart({ data, loading }: Props) { // Pivot flat rows [{bucket, room_id, avg_temp}] into [{time, "hall-a": 23.1, "hall-b": 24.2}] const bucketMap = new Map(); for (const row of data) { const time = formatTime(row.bucket); if (!bucketMap.has(time)) bucketMap.set(time, { time }); bucketMap.get(time)![row.room_id] = row.avg_temp; } const chartData = Array.from(bucketMap.values()); const roomIds = [...new Set(data.map((d) => d.room_id))].sort(); const LINE_COLORS = ["oklch(0.62 0.17 212)", "oklch(0.7 0.15 162)", "oklch(0.75 0.18 84)"]; return (
Temperature (°C)
{roomIds.map((id, i) => ( {id} ))}
{loading ? ( ) : chartData.length === 0 ? (
Waiting for data...
) : ( [`${value}°C`, name]} /> {roomIds.map((id, i) => ( ))} )}
); }