Add chart tick width estimation

This commit is contained in:
2024-05-03 00:52:31 +01:00
parent cb5fb93c97
commit 4c2cb0ecec
5 changed files with 11 additions and 5 deletions

View File

@@ -1,7 +1,7 @@
import { Legend, type LegendProps } from '@/components/legend'; import { Legend, type LegendProps } from '@/components/legend';
import { getFillColor, getStrokeColor } from '@/utils/colors'; import { getFillColor, getStrokeColor } from '@/utils/colors';
import { type FormatOptions, formatValue } from '@/utils/format'; import { type FormatOptions, formatValue } from '@/utils/format';
import type { ReactNode } from 'react'; import { type ReactNode, useMemo } from 'react';
import { Area, AreaChart, CartesianGrid, ResponsiveContainer, XAxis, YAxis } from 'recharts'; import { Area, AreaChart, CartesianGrid, ResponsiveContainer, XAxis, YAxis } from 'recharts';
import type { AxisDomain } from 'recharts/types/util/types'; import type { AxisDomain } from 'recharts/types/util/types';
@@ -17,6 +17,11 @@ type Props = {
}; };
export const ChartCard = ({ data, domain, legend, hueOffset = 0, title, subtitle, formatOptions, total }: Props) => { export const ChartCard = ({ data, domain, legend, hueOffset = 0, title, subtitle, formatOptions, total }: Props) => {
const estimateTickWidth = useMemo(
() => ((formatOptions?.units?.length ?? 0) + (formatOptions?.prefix === false ? 0 : 2)) * 8,
[formatOptions],
);
return ( return (
<div className='chart-card'> <div className='chart-card'>
<h2>{title}</h2> <h2>{title}</h2>
@@ -32,7 +37,7 @@ export const ChartCard = ({ data, domain, legend, hueOffset = 0, title, subtitle
data={data} data={data}
margin={{ margin={{
bottom: -16, bottom: -16,
left: 40, left: estimateTickWidth,
}} }}
> >
<CartesianGrid vertical={false} stroke='var(--color-neutral1)' /> <CartesianGrid vertical={false} stroke='var(--color-neutral1)' />

View File

@@ -29,6 +29,7 @@ export const Memory = () => {
values: [staticData.total_memory, staticData.total_swap], values: [staticData.total_memory, staticData.total_swap],
labels: ['Total memory', 'Total swap'], labels: ['Total memory', 'Total swap'],
}} }}
domain={[0, Math.max(staticData.total_memory, staticData.total_swap)]}
formatOptions={{ units: 'B' }} formatOptions={{ units: 'B' }}
data={history} data={history}
total={2} total={2}

View File

@@ -31,7 +31,7 @@ export const Temps = () => {
values: dynamicData.temps, values: dynamicData.temps,
labels: staticData.components, labels: staticData.components,
}} }}
formatOptions={{ units: 'ºC' }} formatOptions={{ si: true, prefix: false, units: 'ºC' }}
data={history} data={history}
total={total_sensors} total={total_sensors}
/> />

View File

@@ -8,7 +8,7 @@ export const formatValue = (value: number, { decimals = 1, prefix = true, si, un
} }
if (!prefix) { if (!prefix) {
return `${value}\u2009${units}`; return `${Number(value.toFixed(Math.max(0, decimals)))}\u2009${units}`;
} }
const k = si ? 1000 : 1024; const k = si ? 1000 : 1024;