import { CartesianGrid, Line, LineChart, XAxis, YAxis } from 'recharts' import { ChartContainer, ChartLegend, ChartLegendContent, ChartTooltip, ChartTooltipContent, } from '@/components/ui/chart' import { useYAxisWidth, chartTimeData, cn, formatShortDate, toFixedWithoutTrailingZeros, twoDecimalString, } from '@/lib/utils' import { useStore } from '@nanostores/react' import { $chartTime } from '@/lib/stores' import { SystemStatsRecord } from '@/types' import { useMemo } from 'react' export default function TemperatureChart({ ticks, systemData, }: { ticks: number[] systemData: SystemStatsRecord[] }) { const chartTime = useStore($chartTime) const { yAxisWidth, updateYAxisWidth } = useYAxisWidth() /** 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 colors = Object.keys(newChartData.colors) return (
{/* {!yAxisSet && } */} { const val = toFixedWithoutTrailingZeros(value, 2) return updateYAxisWidth(val + ' °C') }} tickLine={false} axisLine={false} /> b.value - a.value} content={ formatShortDate(data[0].payload.created)} contentFormatter={(item) => twoDecimalString(item.value) + ' °C'} indicator="line" /> } /> {colors.map((key) => ( ))} {colors.length < 12 && } />}
) }