mirror of
https://github.com/fankes/beszel.git
synced 2025-10-19 17:59:28 +08:00
improve y axis annoyances on charts
This commit is contained in:
@@ -82,6 +82,7 @@ export default function ContainerCpuChart({
|
|||||||
>
|
>
|
||||||
<CartesianGrid vertical={false} />
|
<CartesianGrid vertical={false} />
|
||||||
<YAxis
|
<YAxis
|
||||||
|
className="tracking-tighter"
|
||||||
// domain={[0, (max: number) => Math.max(Math.ceil(max), 0.4)]}
|
// domain={[0, (max: number) => Math.max(Math.ceil(max), 0.4)]}
|
||||||
width={yAxisWidth}
|
width={yAxisWidth}
|
||||||
tickLine={false}
|
tickLine={false}
|
||||||
|
@@ -88,6 +88,7 @@ export default function ContainerMemChart({
|
|||||||
>
|
>
|
||||||
<CartesianGrid vertical={false} />
|
<CartesianGrid vertical={false} />
|
||||||
<YAxis
|
<YAxis
|
||||||
|
className="tracking-tighter"
|
||||||
// domain={[0, (max: number) => Math.ceil(max)]}
|
// domain={[0, (max: number) => Math.ceil(max)]}
|
||||||
tickLine={false}
|
tickLine={false}
|
||||||
axisLine={false}
|
axisLine={false}
|
||||||
|
@@ -88,6 +88,7 @@ export default function ContainerCpuChart({
|
|||||||
>
|
>
|
||||||
<CartesianGrid vertical={false} />
|
<CartesianGrid vertical={false} />
|
||||||
<YAxis
|
<YAxis
|
||||||
|
className="tracking-tighter"
|
||||||
// domain={[0, (max: number) => Math.max(Math.ceil(max), 0.4)]}
|
// domain={[0, (max: number) => Math.max(Math.ceil(max), 0.4)]}
|
||||||
width={yAxisWidth}
|
width={yAxisWidth}
|
||||||
tickLine={false}
|
tickLine={false}
|
||||||
|
@@ -21,10 +21,6 @@ export default function CpuChart({
|
|||||||
|
|
||||||
const yAxisSet = useMemo(() => yAxisWidth !== 180, [yAxisWidth])
|
const yAxisSet = useMemo(() => yAxisWidth !== 180, [yAxisWidth])
|
||||||
|
|
||||||
// if (!systemData.length || !ticks.length) {
|
|
||||||
// return <Spinner />
|
|
||||||
// }
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div ref={chartRef}>
|
<div ref={chartRef}>
|
||||||
<ChartContainer
|
<ChartContainer
|
||||||
@@ -36,6 +32,7 @@ export default function CpuChart({
|
|||||||
<AreaChart accessibilityLayer data={systemData} margin={{ top: 10 }}>
|
<AreaChart accessibilityLayer data={systemData} margin={{ top: 10 }}>
|
||||||
<CartesianGrid vertical={false} />
|
<CartesianGrid vertical={false} />
|
||||||
<YAxis
|
<YAxis
|
||||||
|
className="tracking-tighter"
|
||||||
// domain={[0, (max: number) => Math.ceil(max)]}
|
// domain={[0, (max: number) => Math.ceil(max)]}
|
||||||
width={yAxisWidth}
|
width={yAxisWidth}
|
||||||
tickLine={false}
|
tickLine={false}
|
||||||
|
@@ -1,13 +1,7 @@
|
|||||||
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 {
|
import { chartTimeData, cn, formatShortDate, toFixedFloat, useYaxisWidth } from '@/lib/utils'
|
||||||
chartTimeData,
|
|
||||||
cn,
|
|
||||||
formatShortDate,
|
|
||||||
toFixedWithoutTrailingZeros,
|
|
||||||
useYaxisWidth,
|
|
||||||
} from '@/lib/utils'
|
|
||||||
import { useMemo, useRef } 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'
|
||||||
@@ -21,20 +15,15 @@ export default function MemChart({
|
|||||||
ticks: number[]
|
ticks: number[]
|
||||||
systemData: SystemStatsRecord[]
|
systemData: SystemStatsRecord[]
|
||||||
}) {
|
}) {
|
||||||
const chartTime = useStore($chartTime)
|
|
||||||
const chartRef = useRef<HTMLDivElement>(null)
|
const chartRef = useRef<HTMLDivElement>(null)
|
||||||
const yAxisWidth = useYaxisWidth(chartRef)
|
const yAxisWidth = useYaxisWidth(chartRef)
|
||||||
|
const chartTime = useStore($chartTime)
|
||||||
|
|
||||||
const yAxisSet = useMemo(() => yAxisWidth !== 180, [yAxisWidth])
|
const yAxisSet = useMemo(() => yAxisWidth !== 180, [yAxisWidth])
|
||||||
|
|
||||||
// const totalMem = useMemo(() => {
|
const totalMem = useMemo(() => {
|
||||||
// const maxMem = Math.ceil(systemData[0]?.stats.m)
|
return toFixedFloat(systemData.at(-1)?.stats.m ?? 0, 1)
|
||||||
// return maxMem > 2 && maxMem % 2 !== 0 ? maxMem + 1 : maxMem
|
}, [systemData])
|
||||||
// }, [systemData])
|
|
||||||
|
|
||||||
// if (!systemData.length || !ticks.length) {
|
|
||||||
// return <Spinner />
|
|
||||||
// }
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div ref={chartRef}>
|
<div ref={chartRef}>
|
||||||
@@ -53,14 +42,17 @@ export default function MemChart({
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<CartesianGrid vertical={false} />
|
<CartesianGrid vertical={false} />
|
||||||
|
{totalMem && (
|
||||||
<YAxis
|
<YAxis
|
||||||
// use "ticks" instead of domain / tickcount if need more control
|
// use "ticks" instead of domain / tickcount if need more control
|
||||||
domain={[0, () => toFixedWithoutTrailingZeros(systemData.at(-1)?.stats.m ?? 0.04, 1)]}
|
domain={[0, totalMem]}
|
||||||
|
className="tracking-tighter"
|
||||||
width={yAxisWidth}
|
width={yAxisWidth}
|
||||||
tickLine={false}
|
tickLine={false}
|
||||||
axisLine={false}
|
axisLine={false}
|
||||||
unit={' GB'}
|
unit={' GB'}
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
<XAxis
|
<XAxis
|
||||||
dataKey="created"
|
dataKey="created"
|
||||||
domain={[ticks[0], ticks.at(-1)!]}
|
domain={[ticks[0], ticks.at(-1)!]}
|
||||||
@@ -93,8 +85,7 @@ export default function MemChart({
|
|||||||
fill="hsl(var(--chart-2))"
|
fill="hsl(var(--chart-2))"
|
||||||
fillOpacity={0.4}
|
fillOpacity={0.4}
|
||||||
stroke="hsl(var(--chart-2))"
|
stroke="hsl(var(--chart-2))"
|
||||||
stackId="a"
|
stackId="1"
|
||||||
// animationDuration={1200}
|
|
||||||
isAnimationActive={false}
|
isAnimationActive={false}
|
||||||
/>
|
/>
|
||||||
<Area
|
<Area
|
||||||
@@ -105,8 +96,7 @@ export default function MemChart({
|
|||||||
fillOpacity={0.2}
|
fillOpacity={0.2}
|
||||||
strokeOpacity={0.3}
|
strokeOpacity={0.3}
|
||||||
stroke="hsl(var(--chart-2))"
|
stroke="hsl(var(--chart-2))"
|
||||||
stackId="a"
|
stackId="1"
|
||||||
// animationDuration={1200}
|
|
||||||
isAnimationActive={false}
|
isAnimationActive={false}
|
||||||
/>
|
/>
|
||||||
</AreaChart>
|
</AreaChart>
|
||||||
|
@@ -38,6 +38,7 @@ export default function SwapChart({
|
|||||||
<AreaChart accessibilityLayer data={systemData} margin={{ top: 10 }}>
|
<AreaChart accessibilityLayer data={systemData} margin={{ top: 10 }}>
|
||||||
<CartesianGrid vertical={false} />
|
<CartesianGrid vertical={false} />
|
||||||
<YAxis
|
<YAxis
|
||||||
|
className="tracking-tighter"
|
||||||
domain={[0, () => toFixedWithoutTrailingZeros(systemData.at(-1)?.stats.s ?? 0.04, 2)]}
|
domain={[0, () => toFixedWithoutTrailingZeros(systemData.at(-1)?.stats.s ?? 0.04, 2)]}
|
||||||
width={yAxisWidth}
|
width={yAxisWidth}
|
||||||
tickLine={false}
|
tickLine={false}
|
||||||
|
@@ -192,7 +192,7 @@ export function useYaxisWidth(chartRef: React.RefObject<HTMLDivElement>) {
|
|||||||
if (yAxisElement) {
|
if (yAxisElement) {
|
||||||
// console.log('yAxisElement', yAxisElement)
|
// console.log('yAxisElement', yAxisElement)
|
||||||
clearInterval(interval)
|
clearInterval(interval)
|
||||||
setYAxisWidth(yAxisElement.getBoundingClientRect().width + 24)
|
setYAxisWidth(yAxisElement.getBoundingClientRect().width + 22)
|
||||||
}
|
}
|
||||||
}, 16)
|
}, 16)
|
||||||
return () => clearInterval(interval)
|
return () => clearInterval(interval)
|
||||||
@@ -221,3 +221,7 @@ export function useClampedIsInViewport(options: HookOptions): [boolean | null, C
|
|||||||
export function toFixedWithoutTrailingZeros(num: number, digits: number) {
|
export function toFixedWithoutTrailingZeros(num: number, digits: number) {
|
||||||
return parseFloat(num.toFixed(digits)).toString()
|
return parseFloat(num.toFixed(digits)).toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function toFixedFloat(num: number, digits: number) {
|
||||||
|
return parseFloat(num.toFixed(digits))
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user