Add domain animation

This commit is contained in:
2024-05-06 15:56:06 +01:00
parent f6e933f784
commit e2476c7988
5 changed files with 28 additions and 12 deletions

View File

@@ -9,12 +9,13 @@ type Props = {
legend?: Omit<LegendProps, 'values'> & Partial<Pick<LegendProps, 'values'>>; legend?: Omit<LegendProps, 'values'> & Partial<Pick<LegendProps, 'values'>>;
formatOptions?: FormatOptions; formatOptions?: FormatOptions;
domain?: [number, number]; domain?: [number, number];
hardDomain?: boolean;
hueOffset?: number; hueOffset?: number;
data: number[]; data: number[];
total: number; total: number;
}; };
export const ChartCard = ({ data, domain, legend, hueOffset = 0, title, subtitle, formatOptions, total }: Props) => { export const ChartCard = ({ data, legend, hueOffset = 0, title, subtitle, formatOptions, ...rest }: Props) => {
return ( return (
<div className='chart-card'> <div className='chart-card'>
<h2>{title}</h2> <h2>{title}</h2>
@@ -23,7 +24,7 @@ export const ChartCard = ({ data, domain, legend, hueOffset = 0, title, subtitle
{legend && <Legend hueOffset={hueOffset} formatOptions={formatOptions} values={data} {...legend} />} {legend && <Legend hueOffset={hueOffset} formatOptions={formatOptions} values={data} {...legend} />}
<CanvasChart data={data} hueOffset={hueOffset} domain={domain} total={total} formatOptions={formatOptions} /> <CanvasChart data={data} hueOffset={hueOffset} formatOptions={formatOptions} {...rest} />
</div> </div>
); );
}; };

View File

@@ -16,6 +16,7 @@ type Props = {
total: number; total: number;
data: number[]; data: number[];
domain?: [number, number]; domain?: [number, number];
hardDomain?: boolean;
formatOptions?: FormatOptions; formatOptions?: FormatOptions;
hueOffset?: number; hueOffset?: number;
}; };
@@ -60,18 +61,25 @@ const CartesianGrid = () => (
</svg> </svg>
); );
export const CanvasChart = ({ total, hueOffset = 0, domain, data, formatOptions }: Props) => { export const CanvasChart = ({ total, hueOffset = 0, domain, hardDomain, data, formatOptions }: Props) => {
const canvasRef = useRef<HTMLCanvasElement>(null!); const canvasRef = useRef<HTMLCanvasElement>(null!);
const ignored = useRef(0); const ignored = useRef(0);
const now = useMemo(() => Date.now(), []); const now = useMemo(() => Date.now(), []);
const [history, setHistory] = useState<[number, number[]][]>([[now, []]]); const [history, setHistory] = useState<[number, number[]][]>([[now, []]]);
const max = useMemo(() => { const targetMax = useMemo(() => {
if (domain) { if (domain && hardDomain) {
return domain[1]; return domain[1];
} }
return 1.25 * history.reduce((max, [_, values]) => Math.max(max, ...values), 0); const historyMax = history.reduce((max, [_, values]) => Math.max(max, ...values), 0);
}, [history, domain]);
if (!domain || historyMax > domain[1]) {
return 1.25 * historyMax;
}
return domain[1];
}, [history, domain, hardDomain]);
const max = useRef(targetMax);
useEffect(() => { useEffect(() => {
if (data) { if (data) {
@@ -102,11 +110,16 @@ export const CanvasChart = ({ total, hueOffset = 0, domain, data, formatOptions
return; return;
} }
ignored.current = 0; ignored.current = 0;
const ctx = canvasRef.current.getContext('2d'); const ctx = canvasRef.current.getContext('2d');
if (!ctx) { if (!ctx) {
return; return;
} }
if (!hardDomain) {
max.current = (max.current + targetMax) / 2;
}
ctx.clearRect(0, 0, width, height); ctx.clearRect(0, 0, width, height);
for (let i = 0; i < total; i++) { for (let i = 0; i < total; i++) {
@@ -114,14 +127,14 @@ export const CanvasChart = ({ total, hueOffset = 0, domain, data, formatOptions
ctx.strokeStyle = getStrokeColor(hueOffset + (360 * Number(i)) / total); ctx.strokeStyle = getStrokeColor(hueOffset + (360 * Number(i)) / total);
ctx.beginPath(); ctx.beginPath();
ctx.moveTo(-xMargin, height); ctx.moveTo(-xMargin, height);
ctx.lineTo(-xMargin, height - (height * (history[0][1][i] ?? 0)) / max); ctx.lineTo(-xMargin, height - (height * (history[0][1][i] ?? 0)) / max.current);
for (const [timestamp, values] of history) { for (const [timestamp, values] of history) {
const x = xFromTimestamp(timestamp); const x = xFromTimestamp(timestamp);
const y = height - (height * (values[i] ?? 0)) / max; const y = height - (height * (values[i] ?? 0)) / max.current;
ctx.lineTo(x, y); ctx.lineTo(x, y);
} }
ctx.lineTo(width + xMargin, height - (height * (history[0][1][i] ?? 0)) / max); ctx.lineTo(width + xMargin, height - (height * (history[0][1][i] ?? 0)) / max.current);
ctx.lineTo(width + xMargin, height); ctx.lineTo(width + xMargin, height);
ctx.stroke(); ctx.stroke();
ctx.fill(); ctx.fill();
@@ -131,7 +144,7 @@ export const CanvasChart = ({ total, hueOffset = 0, domain, data, formatOptions
return ( return (
<div className='chart'> <div className='chart'>
<YAxis max={max} formatOptions={formatOptions} /> <YAxis max={targetMax} formatOptions={formatOptions} />
<div className='canvas-wrapper'> <div className='canvas-wrapper'>
<CartesianGrid /> <CartesianGrid />

View File

@@ -22,6 +22,7 @@ export const Cpu = () => {
</h3> </h3>
} }
domain={[0, 100]} domain={[0, 100]}
hardDomain
formatOptions={{ prefix: false, units: '%' }} formatOptions={{ prefix: false, units: '%' }}
data={dynamicData.cpu_usage} data={dynamicData.cpu_usage}
total={total_cpus} total={total_cpus}

View File

@@ -31,6 +31,7 @@ export const Memory = () => {
labels: ['Memory', 'Swap'], labels: ['Memory', 'Swap'],
}} }}
domain={[0, Math.max(staticData.total_memory, staticData.total_swap)]} domain={[0, Math.max(staticData.total_memory, staticData.total_swap)]}
hardDomain
formatOptions={formatOptions} formatOptions={formatOptions}
data={[dynamicData.mem_usage, dynamicData.swap_usage]} data={[dynamicData.mem_usage, dynamicData.swap_usage]}
total={2} total={2}

View File

@@ -15,7 +15,7 @@ export const Temps = () => {
legend={{ legend={{
labels: staticData.components, labels: staticData.components,
}} }}
domain={[0, 100]} //(max: number) => Math.max(100, Math.ceil(1.25 * max))]} domain={[0, 100]}
formatOptions={{ si: true, prefix: false, units: 'ºC' }} formatOptions={{ si: true, prefix: false, units: 'ºC' }}
data={dynamicData.temps} data={dynamicData.temps}
total={staticData.components.length} total={staticData.components.length}