import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from 'recharts' import { ChartConfig, ChartContainer, ChartTooltip, ChartTooltipContent, } from '@/components/ui/chart' import { useMemo, useRef } from 'react' import { chartTimeData, cn, formatShortDate, toFixedWithoutTrailingZeros, useYaxisWidth, } from '@/lib/utils' // import Spinner from '../spinner' import { useStore } from '@nanostores/react' import { $chartTime } from '@/lib/stores' import { Separator } from '@/components/ui/separator' export default function ContainerCpuChart({ chartData, ticks, }: { chartData: Record[] ticks: number[] }) { const chartTime = useStore($chartTime) const chartRef = useRef(null) const yAxisWidth = useYaxisWidth(chartRef) const yAxisSet = useMemo(() => yAxisWidth !== 180, [yAxisWidth]) const chartConfig = useMemo(() => { let config = {} as Record< string, { label: string color: string } > const totalUsage = {} as Record for (let stats of chartData) { for (let key in stats) { if (!Array.isArray(stats[key])) { continue } if (!(key in totalUsage)) { totalUsage[key] = 0 } totalUsage[key] += stats[key][2] ?? 0 } } let keys = Object.keys(totalUsage) keys.sort((a, b) => (totalUsage[a] > totalUsage[b] ? -1 : 1)) const length = keys.length for (let i = 0; i < length; i++) { const key = keys[i] const hue = ((i * 360) / length) % 360 config[key] = { label: key, color: `hsl(${hue}, 60%, 55%)`, } } return config satisfies ChartConfig }, [chartData]) // if (!chartData.length || !ticks.length) { // return // } return (
{/* {!yAxisSet && } */} Math.max(Math.ceil(max), 0.4)]} width={yAxisWidth} tickLine={false} axisLine={false} unit={' MB/s'} tickFormatter={(value) => toFixedWithoutTrailingZeros(value, 2)} /> formatShortDate(data[0].payload.time)} // @ts-ignore itemSorter={(a, b) => b.value - a.value} content={ { try { const sent = item?.payload?.[key][0] ?? 0 const received = item?.payload?.[key][1] ?? 0 return ( {received.toLocaleString()} MB/s rx {sent.toLocaleString()} MB/s tx ) } catch (e) { return null } }} /> } /> {Object.keys(chartConfig).map((key) => ( data?.[key]?.[2] ?? 0} type="monotoneX" fill={chartConfig[key].color} fillOpacity={0.4} stroke={chartConfig[key].color} stackId="a" /> ))}
) }