mirror of
https://github.com/fankes/beszel.git
synced 2025-10-19 17:59:28 +08:00
adapt y axis width in recharts
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from 'recharts'
|
import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from 'recharts'
|
||||||
|
|
||||||
import { ChartContainer, ChartTooltip, ChartTooltipContent } from '@/components/ui/chart'
|
import { ChartContainer, ChartTooltip, ChartTooltipContent } from '@/components/ui/chart'
|
||||||
import { chartTimeData, formatShortDate } from '@/lib/utils'
|
import { chartTimeData, formatShortDate, useYaxisWidth } from '@/lib/utils'
|
||||||
import Spinner from '../spinner'
|
import Spinner from '../spinner'
|
||||||
import { useStore } from '@nanostores/react'
|
import { useStore } from '@nanostores/react'
|
||||||
import { $chartTime } from '@/lib/stores'
|
import { $chartTime } from '@/lib/stores'
|
||||||
import { SystemStatsRecord } from '@/types'
|
import { SystemStatsRecord } from '@/types'
|
||||||
|
import { useRef } from 'react'
|
||||||
|
|
||||||
export default function BandwidthChart({
|
export default function BandwidthChart({
|
||||||
ticks,
|
ticks,
|
||||||
@@ -14,6 +15,8 @@ export default function BandwidthChart({
|
|||||||
ticks: number[]
|
ticks: number[]
|
||||||
systemData: SystemStatsRecord[]
|
systemData: SystemStatsRecord[]
|
||||||
}) {
|
}) {
|
||||||
|
const chartRef = useRef<HTMLDivElement>(null)
|
||||||
|
const yAxisWidth = useYaxisWidth(chartRef)
|
||||||
const chartTime = useStore($chartTime)
|
const chartTime = useStore($chartTime)
|
||||||
|
|
||||||
if (!systemData.length || !ticks.length) {
|
if (!systemData.length || !ticks.length) {
|
||||||
@@ -21,6 +24,7 @@ export default function BandwidthChart({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<div ref={chartRef}>
|
||||||
<ChartContainer config={{}} className="h-full w-full absolute aspect-auto">
|
<ChartContainer config={{}} className="h-full w-full absolute aspect-auto">
|
||||||
<AreaChart
|
<AreaChart
|
||||||
accessibilityLayer
|
accessibilityLayer
|
||||||
@@ -35,7 +39,7 @@ export default function BandwidthChart({
|
|||||||
<CartesianGrid vertical={false} />
|
<CartesianGrid vertical={false} />
|
||||||
<YAxis
|
<YAxis
|
||||||
className="tracking-tighter"
|
className="tracking-tighter"
|
||||||
width={75}
|
width={yAxisWidth}
|
||||||
domain={[0, (max: number) => (max <= 0.4 ? 0.4 : Math.ceil(max))]}
|
domain={[0, (max: number) => (max <= 0.4 ? 0.4 : Math.ceil(max))]}
|
||||||
tickFormatter={(value) => {
|
tickFormatter={(value) => {
|
||||||
if (value >= 100) {
|
if (value >= 100) {
|
||||||
@@ -89,5 +93,6 @@ export default function BandwidthChart({
|
|||||||
/>
|
/>
|
||||||
</AreaChart>
|
</AreaChart>
|
||||||
</ChartContainer>
|
</ChartContainer>
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@@ -5,8 +5,8 @@ import {
|
|||||||
ChartTooltip,
|
ChartTooltip,
|
||||||
ChartTooltipContent,
|
ChartTooltipContent,
|
||||||
} from '@/components/ui/chart'
|
} from '@/components/ui/chart'
|
||||||
import { useMemo } from 'react'
|
import { useMemo, useRef } from 'react'
|
||||||
import { chartTimeData, formatShortDate } from '@/lib/utils'
|
import { chartTimeData, formatShortDate, useYaxisWidth } from '@/lib/utils'
|
||||||
import Spinner from '../spinner'
|
import Spinner from '../spinner'
|
||||||
import { useStore } from '@nanostores/react'
|
import { useStore } from '@nanostores/react'
|
||||||
import { $chartTime } from '@/lib/stores'
|
import { $chartTime } from '@/lib/stores'
|
||||||
@@ -18,6 +18,8 @@ export default function ContainerCpuChart({
|
|||||||
chartData: Record<string, number | string>[]
|
chartData: Record<string, number | string>[]
|
||||||
ticks: number[]
|
ticks: number[]
|
||||||
}) {
|
}) {
|
||||||
|
const chartRef = useRef<HTMLDivElement>(null)
|
||||||
|
const yAxisWidth = useYaxisWidth(chartRef)
|
||||||
const chartTime = useStore($chartTime)
|
const chartTime = useStore($chartTime)
|
||||||
|
|
||||||
const chartConfig = useMemo(() => {
|
const chartConfig = useMemo(() => {
|
||||||
@@ -60,6 +62,7 @@ export default function ContainerCpuChart({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<div ref={chartRef}>
|
||||||
<ChartContainer config={chartConfig} className="h-full w-full absolute aspect-auto">
|
<ChartContainer config={chartConfig} className="h-full w-full absolute aspect-auto">
|
||||||
<AreaChart
|
<AreaChart
|
||||||
accessibilityLayer
|
accessibilityLayer
|
||||||
@@ -72,7 +75,7 @@ export default function ContainerCpuChart({
|
|||||||
<CartesianGrid vertical={false} />
|
<CartesianGrid vertical={false} />
|
||||||
<YAxis
|
<YAxis
|
||||||
// domain={[0, (max: number) => Math.max(Math.ceil(max), 0.4)]}
|
// domain={[0, (max: number) => Math.max(Math.ceil(max), 0.4)]}
|
||||||
width={47}
|
width={yAxisWidth}
|
||||||
tickLine={false}
|
tickLine={false}
|
||||||
axisLine={false}
|
axisLine={false}
|
||||||
unit={'%'}
|
unit={'%'}
|
||||||
@@ -114,5 +117,6 @@ export default function ContainerCpuChart({
|
|||||||
))}
|
))}
|
||||||
</AreaChart>
|
</AreaChart>
|
||||||
</ChartContainer>
|
</ChartContainer>
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@@ -5,8 +5,8 @@ import {
|
|||||||
ChartTooltip,
|
ChartTooltip,
|
||||||
ChartTooltipContent,
|
ChartTooltipContent,
|
||||||
} from '@/components/ui/chart'
|
} from '@/components/ui/chart'
|
||||||
import { useMemo } from 'react'
|
import { useMemo, useRef } from 'react'
|
||||||
import { chartTimeData, formatShortDate } from '@/lib/utils'
|
import { chartTimeData, formatShortDate, useYaxisWidth } from '@/lib/utils'
|
||||||
import Spinner from '../spinner'
|
import Spinner from '../spinner'
|
||||||
import { useStore } from '@nanostores/react'
|
import { useStore } from '@nanostores/react'
|
||||||
import { $chartTime } from '@/lib/stores'
|
import { $chartTime } from '@/lib/stores'
|
||||||
@@ -18,6 +18,8 @@ export default function ContainerMemChart({
|
|||||||
chartData: Record<string, number | string>[]
|
chartData: Record<string, number | string>[]
|
||||||
ticks: number[]
|
ticks: number[]
|
||||||
}) {
|
}) {
|
||||||
|
const chartRef = useRef<HTMLDivElement>(null)
|
||||||
|
const yAxisWidth = useYaxisWidth(chartRef)
|
||||||
const chartTime = useStore($chartTime)
|
const chartTime = useStore($chartTime)
|
||||||
|
|
||||||
const chartConfig = useMemo(() => {
|
const chartConfig = useMemo(() => {
|
||||||
@@ -60,6 +62,7 @@ export default function ContainerMemChart({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<div ref={chartRef}>
|
||||||
<ChartContainer config={chartConfig} className="h-full w-full absolute aspect-auto">
|
<ChartContainer config={chartConfig} className="h-full w-full absolute aspect-auto">
|
||||||
<AreaChart
|
<AreaChart
|
||||||
accessibilityLayer
|
accessibilityLayer
|
||||||
@@ -77,7 +80,7 @@ export default function ContainerMemChart({
|
|||||||
tickLine={false}
|
tickLine={false}
|
||||||
axisLine={false}
|
axisLine={false}
|
||||||
unit={' GB'}
|
unit={' GB'}
|
||||||
width={70}
|
width={yAxisWidth}
|
||||||
tickFormatter={(value) => {
|
tickFormatter={(value) => {
|
||||||
value = value / 1024
|
value = value / 1024
|
||||||
return value.toFixed((value * 100) % 1 === 0 ? 1 : 2)
|
return value.toFixed((value * 100) % 1 === 0 ? 1 : 2)
|
||||||
@@ -119,5 +122,6 @@ export default function ContainerMemChart({
|
|||||||
))}
|
))}
|
||||||
</AreaChart>
|
</AreaChart>
|
||||||
</ChartContainer>
|
</ChartContainer>
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@@ -5,8 +5,8 @@ import {
|
|||||||
ChartTooltip,
|
ChartTooltip,
|
||||||
ChartTooltipContent,
|
ChartTooltipContent,
|
||||||
} from '@/components/ui/chart'
|
} from '@/components/ui/chart'
|
||||||
import { useMemo } from 'react'
|
import { useMemo, useRef } from 'react'
|
||||||
import { chartTimeData, formatShortDate } from '@/lib/utils'
|
import { chartTimeData, formatShortDate, useYaxisWidth } from '@/lib/utils'
|
||||||
import Spinner from '../spinner'
|
import Spinner from '../spinner'
|
||||||
import { useStore } from '@nanostores/react'
|
import { useStore } from '@nanostores/react'
|
||||||
import { $chartTime } from '@/lib/stores'
|
import { $chartTime } from '@/lib/stores'
|
||||||
@@ -19,6 +19,8 @@ export default function ContainerCpuChart({
|
|||||||
chartData: Record<string, number | number[]>[]
|
chartData: Record<string, number | number[]>[]
|
||||||
ticks: number[]
|
ticks: number[]
|
||||||
}) {
|
}) {
|
||||||
|
const chartRef = useRef<HTMLDivElement>(null)
|
||||||
|
const yAxisWidth = useYaxisWidth(chartRef)
|
||||||
const chartTime = useStore($chartTime)
|
const chartTime = useStore($chartTime)
|
||||||
|
|
||||||
const chartConfig = useMemo(() => {
|
const chartConfig = useMemo(() => {
|
||||||
@@ -60,6 +62,7 @@ export default function ContainerCpuChart({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<div ref={chartRef}>
|
||||||
<ChartContainer config={{}} className="h-full w-full absolute aspect-auto">
|
<ChartContainer config={{}} className="h-full w-full absolute aspect-auto">
|
||||||
<AreaChart
|
<AreaChart
|
||||||
accessibilityLayer
|
accessibilityLayer
|
||||||
@@ -72,7 +75,7 @@ export default function ContainerCpuChart({
|
|||||||
<CartesianGrid vertical={false} />
|
<CartesianGrid vertical={false} />
|
||||||
<YAxis
|
<YAxis
|
||||||
// domain={[0, (max: number) => Math.max(Math.ceil(max), 0.4)]}
|
// domain={[0, (max: number) => Math.max(Math.ceil(max), 0.4)]}
|
||||||
width={75}
|
width={yAxisWidth}
|
||||||
tickLine={false}
|
tickLine={false}
|
||||||
axisLine={false}
|
axisLine={false}
|
||||||
unit={' MB'}
|
unit={' MB'}
|
||||||
@@ -114,7 +117,8 @@ export default function ContainerCpuChart({
|
|||||||
const received = item?.payload?.[key][1] ?? 0
|
const received = item?.payload?.[key][1] ?? 0
|
||||||
return (
|
return (
|
||||||
<span className="flex">
|
<span className="flex">
|
||||||
{received.toLocaleString()} MB<span className="opacity-70 ml-0.5"> rx </span>
|
{received.toLocaleString()} MB
|
||||||
|
<span className="opacity-70 ml-0.5"> rx </span>
|
||||||
<Separator orientation="vertical" className="h-3 mx-1.5 bg-primary/40" />
|
<Separator orientation="vertical" className="h-3 mx-1.5 bg-primary/40" />
|
||||||
{sent.toLocaleString()} MB<span className="opacity-70 ml-0.5"> tx</span>
|
{sent.toLocaleString()} MB<span className="opacity-70 ml-0.5"> tx</span>
|
||||||
</span>
|
</span>
|
||||||
@@ -143,5 +147,6 @@ export default function ContainerCpuChart({
|
|||||||
))}
|
))}
|
||||||
</AreaChart>
|
</AreaChart>
|
||||||
</ChartContainer>
|
</ChartContainer>
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@@ -1,11 +1,12 @@
|
|||||||
import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from 'recharts'
|
import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from 'recharts'
|
||||||
|
|
||||||
import { ChartContainer, ChartTooltip, ChartTooltipContent } from '@/components/ui/chart'
|
import { ChartContainer, ChartTooltip, ChartTooltipContent } from '@/components/ui/chart'
|
||||||
import { chartTimeData, formatShortDate } from '@/lib/utils'
|
import { chartTimeData, formatShortDate, useYaxisWidth } from '@/lib/utils'
|
||||||
import Spinner from '../spinner'
|
import Spinner from '../spinner'
|
||||||
import { useStore } from '@nanostores/react'
|
import { useStore } from '@nanostores/react'
|
||||||
import { $chartTime } from '@/lib/stores'
|
import { $chartTime } from '@/lib/stores'
|
||||||
import { SystemStatsRecord } from '@/types'
|
import { SystemStatsRecord } from '@/types'
|
||||||
|
import { useRef } from 'react'
|
||||||
|
|
||||||
export default function CpuChart({
|
export default function CpuChart({
|
||||||
ticks,
|
ticks,
|
||||||
@@ -14,6 +15,8 @@ export default function CpuChart({
|
|||||||
ticks: number[]
|
ticks: number[]
|
||||||
systemData: SystemStatsRecord[]
|
systemData: SystemStatsRecord[]
|
||||||
}) {
|
}) {
|
||||||
|
const chartRef = useRef<HTMLDivElement>(null)
|
||||||
|
const yAxisWidth = useYaxisWidth(chartRef)
|
||||||
const chartTime = useStore($chartTime)
|
const chartTime = useStore($chartTime)
|
||||||
|
|
||||||
if (!systemData.length || !ticks.length) {
|
if (!systemData.length || !ticks.length) {
|
||||||
@@ -21,12 +24,13 @@ export default function CpuChart({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<div ref={chartRef}>
|
||||||
<ChartContainer config={{}} className="h-full w-full absolute aspect-auto">
|
<ChartContainer config={{}} className="h-full w-full absolute aspect-auto">
|
||||||
<AreaChart accessibilityLayer data={systemData} margin={{ top: 10 }}>
|
<AreaChart accessibilityLayer data={systemData} margin={{ top: 10 }}>
|
||||||
<CartesianGrid vertical={false} />
|
<CartesianGrid vertical={false} />
|
||||||
<YAxis
|
<YAxis
|
||||||
// domain={[0, (max: number) => Math.ceil(max)]}
|
// domain={[0, (max: number) => Math.ceil(max)]}
|
||||||
width={48}
|
width={yAxisWidth}
|
||||||
tickLine={false}
|
tickLine={false}
|
||||||
axisLine={false}
|
axisLine={false}
|
||||||
unit={'%'}
|
unit={'%'}
|
||||||
@@ -66,5 +70,6 @@ export default function CpuChart({
|
|||||||
/>
|
/>
|
||||||
</AreaChart>
|
</AreaChart>
|
||||||
</ChartContainer>
|
</ChartContainer>
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@@ -1,8 +1,8 @@
|
|||||||
import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from 'recharts'
|
import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from 'recharts'
|
||||||
|
|
||||||
import { ChartContainer, ChartTooltip, ChartTooltipContent } from '@/components/ui/chart'
|
import { ChartContainer, ChartTooltip, ChartTooltipContent } from '@/components/ui/chart'
|
||||||
import { chartTimeData, formatShortDate } from '@/lib/utils'
|
import { chartTimeData, formatShortDate, useYaxisWidth } from '@/lib/utils'
|
||||||
import { useMemo } from 'react'
|
import { useMemo, useRef } from 'react'
|
||||||
import Spinner from '../spinner'
|
import Spinner from '../spinner'
|
||||||
import { useStore } from '@nanostores/react'
|
import { useStore } from '@nanostores/react'
|
||||||
import { $chartTime } from '@/lib/stores'
|
import { $chartTime } from '@/lib/stores'
|
||||||
@@ -15,6 +15,8 @@ export default function DiskChart({
|
|||||||
ticks: number[]
|
ticks: number[]
|
||||||
systemData: SystemStatsRecord[]
|
systemData: SystemStatsRecord[]
|
||||||
}) {
|
}) {
|
||||||
|
const chartRef = useRef<HTMLDivElement>(null)
|
||||||
|
const yAxisWidth = useYaxisWidth(chartRef)
|
||||||
const chartTime = useStore($chartTime)
|
const chartTime = useStore($chartTime)
|
||||||
|
|
||||||
const diskSize = useMemo(() => {
|
const diskSize = useMemo(() => {
|
||||||
@@ -35,6 +37,7 @@ export default function DiskChart({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<div ref={chartRef}>
|
||||||
<ChartContainer config={{}} className="h-full w-full absolute aspect-auto">
|
<ChartContainer config={{}} className="h-full w-full absolute aspect-auto">
|
||||||
<AreaChart
|
<AreaChart
|
||||||
accessibilityLayer
|
accessibilityLayer
|
||||||
@@ -49,7 +52,7 @@ export default function DiskChart({
|
|||||||
<CartesianGrid vertical={false} />
|
<CartesianGrid vertical={false} />
|
||||||
<YAxis
|
<YAxis
|
||||||
className="tracking-tighter"
|
className="tracking-tighter"
|
||||||
width={diskSize >= 1000 ? 75 : 65}
|
width={yAxisWidth}
|
||||||
domain={[0, diskSize]}
|
domain={[0, diskSize]}
|
||||||
tickCount={9}
|
tickCount={9}
|
||||||
tickLine={false}
|
tickLine={false}
|
||||||
@@ -89,5 +92,6 @@ export default function DiskChart({
|
|||||||
/>
|
/>
|
||||||
</AreaChart>
|
</AreaChart>
|
||||||
</ChartContainer>
|
</ChartContainer>
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@@ -1,11 +1,12 @@
|
|||||||
import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from 'recharts'
|
import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from 'recharts'
|
||||||
|
|
||||||
import { ChartContainer, ChartTooltip, ChartTooltipContent } from '@/components/ui/chart'
|
import { ChartContainer, ChartTooltip, ChartTooltipContent } from '@/components/ui/chart'
|
||||||
import { chartTimeData, formatShortDate } from '@/lib/utils'
|
import { chartTimeData, formatShortDate, useYaxisWidth } from '@/lib/utils'
|
||||||
import Spinner from '../spinner'
|
import Spinner from '../spinner'
|
||||||
import { useStore } from '@nanostores/react'
|
import { useStore } from '@nanostores/react'
|
||||||
import { $chartTime } from '@/lib/stores'
|
import { $chartTime } from '@/lib/stores'
|
||||||
import { SystemStatsRecord } from '@/types'
|
import { SystemStatsRecord } from '@/types'
|
||||||
|
import { useRef } from 'react'
|
||||||
|
|
||||||
export default function DiskIoChart({
|
export default function DiskIoChart({
|
||||||
ticks,
|
ticks,
|
||||||
@@ -14,6 +15,8 @@ export default function DiskIoChart({
|
|||||||
ticks: number[]
|
ticks: number[]
|
||||||
systemData: SystemStatsRecord[]
|
systemData: SystemStatsRecord[]
|
||||||
}) {
|
}) {
|
||||||
|
const chartRef = useRef<HTMLDivElement>(null)
|
||||||
|
const yAxisWidth = useYaxisWidth(chartRef)
|
||||||
const chartTime = useStore($chartTime)
|
const chartTime = useStore($chartTime)
|
||||||
|
|
||||||
if (!systemData.length || !ticks.length) {
|
if (!systemData.length || !ticks.length) {
|
||||||
@@ -21,6 +24,7 @@ export default function DiskIoChart({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<div ref={chartRef}>
|
||||||
<ChartContainer config={{}} className="h-full w-full absolute aspect-auto">
|
<ChartContainer config={{}} className="h-full w-full absolute aspect-auto">
|
||||||
<AreaChart
|
<AreaChart
|
||||||
accessibilityLayer
|
accessibilityLayer
|
||||||
@@ -35,7 +39,7 @@ export default function DiskIoChart({
|
|||||||
<CartesianGrid vertical={false} />
|
<CartesianGrid vertical={false} />
|
||||||
<YAxis
|
<YAxis
|
||||||
className="tracking-tighter"
|
className="tracking-tighter"
|
||||||
width={75}
|
width={yAxisWidth}
|
||||||
domain={[0, (max: number) => (max <= 0.4 ? 0.4 : Math.ceil(max))]}
|
domain={[0, (max: number) => (max <= 0.4 ? 0.4 : Math.ceil(max))]}
|
||||||
tickFormatter={(value) => {
|
tickFormatter={(value) => {
|
||||||
if (value >= 100) {
|
if (value >= 100) {
|
||||||
@@ -89,5 +93,6 @@ export default function DiskIoChart({
|
|||||||
/>
|
/>
|
||||||
</AreaChart>
|
</AreaChart>
|
||||||
</ChartContainer>
|
</ChartContainer>
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@@ -1,8 +1,8 @@
|
|||||||
import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from 'recharts'
|
import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from 'recharts'
|
||||||
|
|
||||||
import { ChartContainer, ChartTooltip, ChartTooltipContent } from '@/components/ui/chart'
|
import { ChartContainer, ChartTooltip, ChartTooltipContent } from '@/components/ui/chart'
|
||||||
import { chartTimeData, formatShortDate } from '@/lib/utils'
|
import { chartTimeData, formatShortDate, useYaxisWidth } from '@/lib/utils'
|
||||||
import { useMemo } from 'react'
|
import { useMemo, useRef } from 'react'
|
||||||
import Spinner from '../spinner'
|
import Spinner from '../spinner'
|
||||||
import { useStore } from '@nanostores/react'
|
import { useStore } from '@nanostores/react'
|
||||||
import { $chartTime } from '@/lib/stores'
|
import { $chartTime } from '@/lib/stores'
|
||||||
@@ -16,6 +16,8 @@ export default function MemChart({
|
|||||||
systemData: SystemStatsRecord[]
|
systemData: SystemStatsRecord[]
|
||||||
}) {
|
}) {
|
||||||
const chartTime = useStore($chartTime)
|
const chartTime = useStore($chartTime)
|
||||||
|
const chartRef = useRef<HTMLDivElement>(null)
|
||||||
|
const yAxisWidth = useYaxisWidth(chartRef)
|
||||||
|
|
||||||
const totalMem = useMemo(() => {
|
const totalMem = useMemo(() => {
|
||||||
const maxMem = Math.ceil(systemData[0]?.stats.m)
|
const maxMem = Math.ceil(systemData[0]?.stats.m)
|
||||||
@@ -27,6 +29,7 @@ export default function MemChart({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<div ref={chartRef}>
|
||||||
<ChartContainer config={{}} className="h-full w-full absolute aspect-auto">
|
<ChartContainer config={{}} className="h-full w-full absolute aspect-auto">
|
||||||
<AreaChart
|
<AreaChart
|
||||||
accessibilityLayer
|
accessibilityLayer
|
||||||
@@ -39,9 +42,8 @@ export default function MemChart({
|
|||||||
<YAxis
|
<YAxis
|
||||||
// use "ticks" instead of domain / tickcount if need more control
|
// use "ticks" instead of domain / tickcount if need more control
|
||||||
domain={[0, totalMem]}
|
domain={[0, totalMem]}
|
||||||
|
width={yAxisWidth}
|
||||||
tickLine={false}
|
tickLine={false}
|
||||||
width={totalMem >= 100 ? 65 : 58}
|
|
||||||
// allowDecimals={false}
|
|
||||||
axisLine={false}
|
axisLine={false}
|
||||||
unit={' GB'}
|
unit={' GB'}
|
||||||
/>
|
/>
|
||||||
@@ -93,5 +95,6 @@ export default function MemChart({
|
|||||||
/>
|
/>
|
||||||
</AreaChart>
|
</AreaChart>
|
||||||
</ChartContainer>
|
</ChartContainer>
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@@ -6,6 +6,7 @@ import { AlertRecord, ChartTimeData, ChartTimes, SystemRecord } from '@/types'
|
|||||||
import { RecordModel, RecordSubscription } from 'pocketbase'
|
import { RecordModel, RecordSubscription } from 'pocketbase'
|
||||||
import { WritableAtom } from 'nanostores'
|
import { WritableAtom } from 'nanostores'
|
||||||
import { timeDay, timeHour } from 'd3-time'
|
import { timeDay, timeHour } from 'd3-time'
|
||||||
|
import { useEffect, useState } from 'react'
|
||||||
|
|
||||||
export function cn(...inputs: ClassValue[]) {
|
export function cn(...inputs: ClassValue[]) {
|
||||||
return twMerge(clsx(inputs))
|
return twMerge(clsx(inputs))
|
||||||
@@ -179,3 +180,20 @@ export const chartTimeData: ChartTimeData = {
|
|||||||
getOffset: (endTime: Date) => timeDay.offset(endTime, -30),
|
getOffset: (endTime: Date) => timeDay.offset(endTime, -30),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Hacky solution to set the correct width of the yAxis in recharts */
|
||||||
|
export function useYaxisWidth(chartRef: React.RefObject<HTMLDivElement>) {
|
||||||
|
const [yAxisWidth, setYAxisWidth] = useState(90)
|
||||||
|
useEffect(() => {
|
||||||
|
let interval = setInterval(() => {
|
||||||
|
// console.log('chartRef', chartRef.current)
|
||||||
|
const yAxisElement = chartRef?.current?.querySelector('.yAxis')
|
||||||
|
if (yAxisElement) {
|
||||||
|
console.log('yAxisElement', yAxisElement)
|
||||||
|
setYAxisWidth(yAxisElement.getBoundingClientRect().width + 22)
|
||||||
|
clearInterval(interval)
|
||||||
|
}
|
||||||
|
}, 16)
|
||||||
|
}, [])
|
||||||
|
return yAxisWidth
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user