improve chart scaling + add space below docker net chart for tooltip

This commit is contained in:
Henry Dollman
2024-08-05 19:17:54 -04:00
parent f36b0a4528
commit fdf0ce22dc
6 changed files with 61 additions and 45 deletions

View File

@@ -1,7 +1,13 @@
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, cn, formatShortDate, useYaxisWidth } from '@/lib/utils' import {
chartTimeData,
cn,
formatShortDate,
toFixedWithoutTrailingZeros,
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'
@@ -44,13 +50,8 @@ export default function BandwidthChart({
<YAxis <YAxis
className="tracking-tighter" className="tracking-tighter"
width={yAxisWidth} 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) => toFixedWithoutTrailingZeros(value, 2)}
if (value >= 100) {
return value.toFixed(0)
}
return value.toFixed((value * 100) % 1 === 0 ? 1 : 2)
}}
tickLine={false} tickLine={false}
axisLine={false} axisLine={false}
unit={' MB/s'} unit={' MB/s'}

View File

@@ -6,7 +6,13 @@ import {
ChartTooltipContent, ChartTooltipContent,
} from '@/components/ui/chart' } from '@/components/ui/chart'
import { useMemo, useRef } from 'react' import { useMemo, useRef } from 'react'
import { chartTimeData, cn, formatShortDate, useYaxisWidth } from '@/lib/utils' import {
chartTimeData,
cn,
formatShortDate,
toFixedWithoutTrailingZeros,
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'
@@ -87,10 +93,7 @@ export default function ContainerMemChart({
axisLine={false} axisLine={false}
unit={' GB'} unit={' GB'}
width={yAxisWidth} width={yAxisWidth}
tickFormatter={(value) => { tickFormatter={(value) => toFixedWithoutTrailingZeros(value / 1024, 2)}
value = value / 1024
return value.toFixed((value * 100) % 1 === 0 ? 1 : 2)
}}
/> />
<XAxis <XAxis
dataKey="time" dataKey="time"

View File

@@ -6,7 +6,13 @@ import {
ChartTooltipContent, ChartTooltipContent,
} from '@/components/ui/chart' } from '@/components/ui/chart'
import { useMemo, useRef } from 'react' import { useMemo, useRef } from 'react'
import { chartTimeData, cn, formatShortDate, useYaxisWidth } from '@/lib/utils' import {
chartTimeData,
cn,
formatShortDate,
toFixedWithoutTrailingZeros,
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'
@@ -86,8 +92,8 @@ export default function ContainerCpuChart({
width={yAxisWidth} width={yAxisWidth}
tickLine={false} tickLine={false}
axisLine={false} axisLine={false}
unit={' MB'} unit={' MB/s'}
tickFormatter={(x) => (x % 1 === 0 ? x : x.toFixed(1))} tickFormatter={(value) => toFixedWithoutTrailingZeros(value, 2)}
/> />
<XAxis <XAxis
dataKey="time" dataKey="time"
@@ -104,17 +110,8 @@ export default function ContainerCpuChart({
// cursor={false} // cursor={false}
animationEasing="ease-out" animationEasing="ease-out"
animationDuration={150} animationDuration={150}
labelFormatter={(_, data) => { labelFormatter={(_, data) => formatShortDate(data[0].payload.time)}
return (
<span>
{formatShortDate(data[0].payload.time)}
<br />
<small className="opacity-70">Total MB received / transmitted</small>
</span>
)
}}
// @ts-ignore // @ts-ignore
itemSorter={(a, b) => b.value - a.value} itemSorter={(a, b) => b.value - a.value}
content={ content={
<ChartTooltipContent <ChartTooltipContent
@@ -125,10 +122,10 @@ 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 {received.toLocaleString()} MB/s
<span className="opacity-70 ml-0.5"> rx </span> <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/s<span className="opacity-70 ml-0.5"> tx</span>
</span> </span>
) )
} catch (e) { } catch (e) {

View File

@@ -1,7 +1,13 @@
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, cn, formatShortDate, useYaxisWidth } from '@/lib/utils' import {
chartTimeData,
cn,
formatShortDate,
toFixedWithoutTrailingZeros,
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'
@@ -48,13 +54,8 @@ export default function DiskIoChart({
<YAxis <YAxis
className="tracking-tighter" className="tracking-tighter"
width={yAxisWidth} 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) => toFixedWithoutTrailingZeros(value, 2)}
if (value >= 100) {
return value.toFixed(0)
}
return value.toFixed((value * 100) % 1 === 0 ? 1 : 2)
}}
tickLine={false} tickLine={false}
axisLine={false} axisLine={false}
unit={' MB/s'} unit={' MB/s'}

View File

@@ -246,13 +246,23 @@ export default function ServerDetail({ name }: { name: string }) {
<BandwidthChart ticks={ticks} systemData={systemStats} /> <BandwidthChart ticks={ticks} systemData={systemStats} />
</ChartCard> </ChartCard>
{hasDockerStats && ( {hasDockerStats && dockerNetChartData.length > 0 && (
<ChartCard <>
title="Docker Network I/O" <ChartCard
description="Includes traffic between internal services" title="Docker Network I/O"
> description="Includes traffic between internal services"
<ContainerNetChart chartData={dockerNetChartData} ticks={ticks} /> >
</ChartCard> <ContainerNetChart chartData={dockerNetChartData} ticks={ticks} />
</ChartCard>
{/* add space for tooltip if more than 12 containers */}
{Object.keys(dockerNetChartData[0]).length > 12 && (
<div
style={{
height: (Object.keys(dockerNetChartData[0]).length - 13) * 18,
}}
/>
)}
</>
)} )}
</div> </div>
) )

View File

@@ -191,10 +191,10 @@ export function useYaxisWidth(chartRef: React.RefObject<HTMLDivElement>) {
const yAxisElement = chartRef?.current?.querySelector('.yAxis') const yAxisElement = chartRef?.current?.querySelector('.yAxis')
if (yAxisElement) { if (yAxisElement) {
// console.log('yAxisElement', yAxisElement) // console.log('yAxisElement', yAxisElement)
setYAxisWidth(yAxisElement.getBoundingClientRect().width + 22)
clearInterval(interval) clearInterval(interval)
setYAxisWidth(yAxisElement.getBoundingClientRect().width + 22)
} }
}, 0) }, 16)
return () => clearInterval(interval) return () => clearInterval(interval)
}, []) }, [])
return yAxisWidth return yAxisWidth
@@ -217,3 +217,7 @@ export function useClampedIsInViewport(options: HookOptions): [boolean | null, C
return [wasInViewportAtleastOnce, wrappedTargetRef] return [wasInViewportAtleastOnce, wrappedTargetRef]
} }
export function toFixedWithoutTrailingZeros(num: number, digits: number) {
return parseFloat(num.toFixed(digits)).toString()
}