mirror of
https://github.com/fankes/beszel.git
synced 2025-10-20 10:19:27 +08:00
change twoDecimalString to use customizable digits
This commit is contained in:
@@ -7,7 +7,7 @@ import {
|
|||||||
cn,
|
cn,
|
||||||
formatShortDate,
|
formatShortDate,
|
||||||
toFixedWithoutTrailingZeros,
|
toFixedWithoutTrailingZeros,
|
||||||
twoDecimalString,
|
decimalString,
|
||||||
chartMargin,
|
chartMargin,
|
||||||
} from '@/lib/utils'
|
} from '@/lib/utils'
|
||||||
// import Spinner from '../spinner'
|
// import Spinner from '../spinner'
|
||||||
@@ -103,7 +103,7 @@ export default function AreaChartDefault({
|
|||||||
content={
|
content={
|
||||||
<ChartTooltipContent
|
<ChartTooltipContent
|
||||||
labelFormatter={(_, data) => formatShortDate(data[0].payload.created)}
|
labelFormatter={(_, data) => formatShortDate(data[0].payload.created)}
|
||||||
contentFormatter={(item) => twoDecimalString(item.value) + unit}
|
contentFormatter={(item) => decimalString(item.value) + unit}
|
||||||
indicator="line"
|
indicator="line"
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
|
@@ -11,7 +11,7 @@ import {
|
|||||||
chartTimeData,
|
chartTimeData,
|
||||||
cn,
|
cn,
|
||||||
formatShortDate,
|
formatShortDate,
|
||||||
twoDecimalString,
|
decimalString,
|
||||||
chartMargin,
|
chartMargin,
|
||||||
} from '@/lib/utils'
|
} from '@/lib/utils'
|
||||||
// import Spinner from '../spinner'
|
// import Spinner from '../spinner'
|
||||||
@@ -116,7 +116,7 @@ export default function ContainerCpuChart({
|
|||||||
content={
|
content={
|
||||||
<ChartTooltipContent
|
<ChartTooltipContent
|
||||||
filter={filter}
|
filter={filter}
|
||||||
contentFormatter={(item) => twoDecimalString(item.value) + '%'}
|
contentFormatter={(item) => decimalString(item.value) + '%'}
|
||||||
indicator="line"
|
indicator="line"
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
|
@@ -12,7 +12,7 @@ import {
|
|||||||
cn,
|
cn,
|
||||||
formatShortDate,
|
formatShortDate,
|
||||||
toFixedWithoutTrailingZeros,
|
toFixedWithoutTrailingZeros,
|
||||||
twoDecimalString,
|
decimalString,
|
||||||
chartMargin,
|
chartMargin,
|
||||||
} from '@/lib/utils'
|
} from '@/lib/utils'
|
||||||
// import Spinner from '../spinner'
|
// import Spinner from '../spinner'
|
||||||
@@ -116,7 +116,7 @@ export default function ContainerMemChart({
|
|||||||
content={
|
content={
|
||||||
<ChartTooltipContent
|
<ChartTooltipContent
|
||||||
filter={filter}
|
filter={filter}
|
||||||
contentFormatter={(item) => twoDecimalString(item.value) + ' MB'}
|
contentFormatter={(item) => decimalString(item.value) + ' MB'}
|
||||||
indicator="line"
|
indicator="line"
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
|
@@ -12,7 +12,7 @@ import {
|
|||||||
cn,
|
cn,
|
||||||
formatShortDate,
|
formatShortDate,
|
||||||
toFixedWithoutTrailingZeros,
|
toFixedWithoutTrailingZeros,
|
||||||
twoDecimalString,
|
decimalString,
|
||||||
chartMargin,
|
chartMargin,
|
||||||
} from '@/lib/utils'
|
} from '@/lib/utils'
|
||||||
// import Spinner from '../spinner'
|
// import Spinner from '../spinner'
|
||||||
@@ -119,10 +119,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">
|
||||||
{twoDecimalString(received)} MB/s
|
{decimalString(received)} 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" />
|
||||||
{twoDecimalString(sent)} MB/s<span className="opacity-70 ml-0.5"> tx</span>
|
{decimalString(sent)} MB/s<span className="opacity-70 ml-0.5"> tx</span>
|
||||||
</span>
|
</span>
|
||||||
)
|
)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@@ -6,7 +6,7 @@ import {
|
|||||||
chartTimeData,
|
chartTimeData,
|
||||||
cn,
|
cn,
|
||||||
formatShortDate,
|
formatShortDate,
|
||||||
twoDecimalString,
|
decimalString,
|
||||||
chartMargin,
|
chartMargin,
|
||||||
} from '@/lib/utils'
|
} from '@/lib/utils'
|
||||||
// import Spinner from '../spinner'
|
// import Spinner from '../spinner'
|
||||||
@@ -70,7 +70,7 @@ export default function CpuChart({
|
|||||||
content={
|
content={
|
||||||
<ChartTooltipContent
|
<ChartTooltipContent
|
||||||
labelFormatter={(_, data) => formatShortDate(data[0].payload.created)}
|
labelFormatter={(_, data) => formatShortDate(data[0].payload.created)}
|
||||||
contentFormatter={(item) => twoDecimalString(item.value) + '%'}
|
contentFormatter={(item) => decimalString(item.value) + '%'}
|
||||||
indicator="line"
|
indicator="line"
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
|
@@ -6,7 +6,7 @@ import {
|
|||||||
chartTimeData,
|
chartTimeData,
|
||||||
cn,
|
cn,
|
||||||
formatShortDate,
|
formatShortDate,
|
||||||
twoDecimalString,
|
decimalString,
|
||||||
toFixedFloat,
|
toFixedFloat,
|
||||||
getSizeVal,
|
getSizeVal,
|
||||||
getSizeUnit,
|
getSizeUnit,
|
||||||
@@ -72,7 +72,7 @@ export default function DiskChart({
|
|||||||
<ChartTooltipContent
|
<ChartTooltipContent
|
||||||
labelFormatter={(_, data) => formatShortDate(data[0].payload.created)}
|
labelFormatter={(_, data) => formatShortDate(data[0].payload.created)}
|
||||||
contentFormatter={({ value }) =>
|
contentFormatter={({ value }) =>
|
||||||
twoDecimalString(getSizeVal(value)) + getSizeUnit(value)
|
decimalString(getSizeVal(value)) + getSizeUnit(value)
|
||||||
}
|
}
|
||||||
indicator="line"
|
indicator="line"
|
||||||
/>
|
/>
|
||||||
|
@@ -6,7 +6,7 @@ import {
|
|||||||
chartTimeData,
|
chartTimeData,
|
||||||
cn,
|
cn,
|
||||||
toFixedFloat,
|
toFixedFloat,
|
||||||
twoDecimalString,
|
decimalString,
|
||||||
formatShortDate,
|
formatShortDate,
|
||||||
chartMargin,
|
chartMargin,
|
||||||
} from '@/lib/utils'
|
} from '@/lib/utils'
|
||||||
@@ -74,7 +74,7 @@ export default function MemChart({
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
itemSorter={(a, b) => a.order - b.order}
|
itemSorter={(a, b) => a.order - b.order}
|
||||||
labelFormatter={(_, data) => formatShortDate(data[0].payload.created)}
|
labelFormatter={(_, data) => formatShortDate(data[0].payload.created)}
|
||||||
contentFormatter={(item) => twoDecimalString(item.value) + ' GB'}
|
contentFormatter={(item) => decimalString(item.value) + ' GB'}
|
||||||
indicator="line"
|
indicator="line"
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
|
@@ -7,7 +7,7 @@ import {
|
|||||||
cn,
|
cn,
|
||||||
formatShortDate,
|
formatShortDate,
|
||||||
toFixedWithoutTrailingZeros,
|
toFixedWithoutTrailingZeros,
|
||||||
twoDecimalString,
|
decimalString,
|
||||||
chartMargin,
|
chartMargin,
|
||||||
} from '@/lib/utils'
|
} from '@/lib/utils'
|
||||||
import { useStore } from '@nanostores/react'
|
import { useStore } from '@nanostores/react'
|
||||||
@@ -58,7 +58,7 @@ export default function SwapChart({
|
|||||||
content={
|
content={
|
||||||
<ChartTooltipContent
|
<ChartTooltipContent
|
||||||
labelFormatter={(_, data) => formatShortDate(data[0].payload.created)}
|
labelFormatter={(_, data) => formatShortDate(data[0].payload.created)}
|
||||||
contentFormatter={(item) => twoDecimalString(item.value) + ' GB'}
|
contentFormatter={(item) => decimalString(item.value) + ' GB'}
|
||||||
indicator="line"
|
indicator="line"
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
|
@@ -13,7 +13,7 @@ import {
|
|||||||
cn,
|
cn,
|
||||||
formatShortDate,
|
formatShortDate,
|
||||||
toFixedWithoutTrailingZeros,
|
toFixedWithoutTrailingZeros,
|
||||||
twoDecimalString,
|
decimalString,
|
||||||
chartMargin,
|
chartMargin,
|
||||||
} from '@/lib/utils'
|
} from '@/lib/utils'
|
||||||
import { useStore } from '@nanostores/react'
|
import { useStore } from '@nanostores/react'
|
||||||
@@ -97,7 +97,7 @@ export default function TemperatureChart({
|
|||||||
content={
|
content={
|
||||||
<ChartTooltipContent
|
<ChartTooltipContent
|
||||||
labelFormatter={(_, data) => formatShortDate(data[0].payload.created)}
|
labelFormatter={(_, data) => formatShortDate(data[0].payload.created)}
|
||||||
contentFormatter={(item) => twoDecimalString(item.value) + ' °C'}
|
contentFormatter={(item) => decimalString(item.value) + ' °C'}
|
||||||
indicator="line"
|
indicator="line"
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
|
@@ -224,17 +224,18 @@ export function toFixedFloat(num: number, digits: number) {
|
|||||||
return parseFloat(num.toFixed(digits))
|
return parseFloat(num.toFixed(digits))
|
||||||
}
|
}
|
||||||
|
|
||||||
let twoDecimalFormatter: Intl.NumberFormat
|
let decimalFormatters: Map<number, Intl.NumberFormat> = new Map()
|
||||||
/** Format number to two decimal places */
|
/** Format number to x decimal places */
|
||||||
export function twoDecimalString(num: number) {
|
export function decimalString(num: number, digits = 2) {
|
||||||
if (!twoDecimalFormatter) {
|
let formatter = decimalFormatters.get(digits)
|
||||||
twoDecimalFormatter = new Intl.NumberFormat(undefined, {
|
if (!formatter) {
|
||||||
minimumFractionDigits: 2,
|
formatter = new Intl.NumberFormat(undefined, {
|
||||||
maximumFractionDigits: 2,
|
minimumFractionDigits: digits,
|
||||||
|
maximumFractionDigits: digits,
|
||||||
})
|
})
|
||||||
|
decimalFormatters.set(digits, formatter)
|
||||||
}
|
}
|
||||||
// Return a function that formats numbers using the saved formatter
|
return formatter.format(num)
|
||||||
return twoDecimalFormatter.format(num)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get value from local storage */
|
/** Get value from local storage */
|
||||||
|
Reference in New Issue
Block a user