import { CartesianGrid, Line, LineChart, XAxis, YAxis } from 'recharts' import { ChartContainer, ChartLegend, ChartLegendContent, ChartTooltip, ChartTooltipContent, } from '@/components/ui/chart' import { chartTimeData, cn, formatShortDate, toFixedWithoutTrailingZeros, useYaxisWidth, } from '@/lib/utils' import { useStore } from '@nanostores/react' import { $chartTime } from '@/lib/stores' import { SystemStatsRecord } from '@/types' import { useMemo, useRef } from 'react' export default function TemperatureChart({ ticks, systemData, }: { ticks: number[] systemData: SystemStatsRecord[] }) { const chartRef = useRef(null) const yAxisWidth = useYaxisWidth(chartRef) const chartTime = useStore($chartTime) /** Format temperature data for chart and assign colors */ const newChartData = useMemo(() => { const chartData = { data: [], colors: {} } as { data: Record[] colors: Record } const tempSums = {} as Record for (let data of systemData) { let newData = { created: data.created } as Record let keys = Object.keys(data.stats?.t ?? {}) for (let i = 0; i < keys.length; i++) { let key = keys[i] newData[key] = data.stats.t![key] tempSums[key] = (tempSums[key] ?? 0) + newData[key] } chartData.data.push(newData) } const keys = Object.keys(tempSums).sort((a, b) => tempSums[b] - tempSums[a]) for (let key of keys) { chartData.colors[key] = `hsl(${((keys.indexOf(key) * 360) / keys.length) % 360}, 60%, 55%)` } return chartData }, [systemData]) const yAxisSet = useMemo(() => yAxisWidth !== 180, [yAxisWidth]) return (
{/* {!yAxisSet && } */} toFixedWithoutTrailingZeros(value, 2)} tickLine={false} axisLine={false} unit={' °C'} /> b.value - a.value} content={ formatShortDate(data[0].payload.created)} indicator="line" /> } /> {Object.keys(newChartData.colors).map((key) => ( ))} } />
) }