Demo
Live data and empty state
Documented in Chart.
Live data and empty state
Stream server load into one chart through reactive data and into another through api.appendPoints, clear the data to see the empty state, with series descriptions for screen readers.
Line chart: Server load (reactive data), 2 series, x from 0 to 50. CPU: 60 points, minimum 11, maximum 80, trending up. Memory: 60 points, minimum 52, maximum 68, trending down. CPU: CPU use of the web server in percent, sampled four times a second.. Memory: Memory use in percent..
| x | CPU | Memory |
|---|---|---|
| 0 | 45% | 60% |
| 1 | 58% | 61% |
| 2 | 57% | 61% |
| 3 | 49% | 62% |
| 4 | 50% | 63% |
| 5 | 64% | 64% |
| 6 | 74% | 64% |
| 7 | 69% | 65% |
| 8 | 59% | 65% |
| 9 | 61% | 66% |
| 10 | 74% | 66% |
| 11 | 80% | 67% |
| 12 | 71% | 67% |
| 13 | 60% | 67% |
| 14 | 62% | 68% |
| 15 | 72% | 68% |
| 16 | 73% | 68% |
| 17 | 60% | 68% |
| 18 | 49% | 68% |
| 19 | 51% | 68% |
| 20 | 60% | 68% |
| 21 | 57% | 68% |
| 22 | 42% | 67% |
| 23 | 31% | 67% |
| 24 | 36% | 67% |
| 25 | 43% | 66% |
| 26 | 38% | 66% |
| 27 | 23% | 65% |
| 28 | 16% | 64% |
| 29 | 24% | 64% |
| 30 | 32% | 63% |
| 31 | 26% | 63% |
| 32 | 13% | 62% |
| 33 | 11% | 61% |
| 34 | 22% | 60% |
| 35 | 31% | 60% |
| 36 | 25% | 59% |
| 37 | 16% | 58% |
| 38 | 19% | 58% |
| 39 | 33% | 57% |
| 40 | 41% | 56% |
| 41 | 36% | 56% |
| 42 | 29% | 55% |
| 43 | 35% | 54% |
| 44 | 51% | 54% |
| 45 | 58% | 53% |
| 46 | 51% | 53% |
| 47 | 46% | 53% |
| 48 | 54% | 52% |
| 49 | 69% | 52% |
| 50 | 72% | 52% |
| 51 | 63% | 52% |
| 52 | 58% | 52% |
| 53 | 67% | 52% |
| 54 | 79% | 52% |
| 55 | 77% | 52% |
| 56 | 65% | 53% |
| 57 | 59% | 53% |
| 58 | 68% | 53% |
| 59 | 76% | 54% |
Line chart: CPU (streamed with appendPoints), 1 series, x from 0 to 50. CPU: 60 points, minimum 11, maximum 80, trending up. CPU: The same CPU samples, streamed into the chart store..
| x | CPU |
|---|---|
| 0 | 45 |
| 1 | 58 |
| 2 | 57 |
| 3 | 49 |
| 4 | 50 |
| 5 | 64 |
| 6 | 74 |
| 7 | 69 |
| 8 | 59 |
| 9 | 61 |
| 10 | 74 |
| 11 | 80 |
| 12 | 71 |
| 13 | 60 |
| 14 | 62 |
| 15 | 72 |
| 16 | 73 |
| 17 | 60 |
| 18 | 49 |
| 19 | 51 |
| 20 | 60 |
| 21 | 57 |
| 22 | 42 |
| 23 | 31 |
| 24 | 36 |
| 25 | 43 |
| 26 | 38 |
| 27 | 23 |
| 28 | 16 |
| 29 | 24 |
| 30 | 32 |
| 31 | 26 |
| 32 | 13 |
| 33 | 11 |
| 34 | 22 |
| 35 | 31 |
| 36 | 25 |
| 37 | 16 |
| 38 | 19 |
| 39 | 33 |
| 40 | 41 |
| 41 | 36 |
| 42 | 29 |
| 43 | 35 |
| 44 | 51 |
| 45 | 58 |
| 46 | 51 |
| 47 | 46 |
| 48 | 54 |
| 49 | 69 |
| 50 | 72 |
| 51 | 63 |
| 52 | 58 |
| 53 | 67 |
| 54 | 79 |
| 55 | 77 |
| 56 | 65 |
| 57 | 59 |
| 58 | 68 |
| 59 | 76 |
Demo.tsx
'use client';
import { useEffect, useRef, useState } from 'react';
import { Chart } from '@salmonui/charts/chart';
import type { ChartApi } from '@salmonui/charts/chart';
import { Button } from '@salmonui/buttons/button';
const WINDOW = 60;
const sample = (t: number) => ({ t, cpu: Math.round(45 + 25 * Math.sin(t / 7) + 10 * Math.sin(t * 1.3)), memory: Math.round(60 + 8 * Math.sin(t / 11)) });
const initial = Array.from({ length: WINDOW }, (_, i) => sample(i));
export default function ChartLiveDemo() {
const [running, setRunning] = useState(false);
const [rows, setRows] = useState(initial);
const streamed = useRef<ChartApi>(null);
const tick = useRef(WINDOW);
useEffect(() => {
if (!running) return undefined;
const timer = window.setInterval(() => {
const point = sample(tick.current++);
// Reactive data: a new array (the oldest point dropped) lays the first chart out again.
setRows((r) => [...r.slice(r.length >= WINDOW ? 1 : 0), point]);
// Streaming: the second chart takes the point through its store, without a React render.
streamed.current?.appendPoints('cpu', [point], { maxPoints: WINDOW });
}, 250);
return () => window.clearInterval(timer);
}, [running]);
return (
<div className="demo-stack">
<div className="demo-toolbar">
<Button variant="outline" type="button" onClick={() => setRunning((r) => !r)} aria-pressed={running}>
{running ? 'Pause' : 'Start'} stream
</Button>
<Button variant="outline" type="button" onClick={() => setRows([])}>
Clear data
</Button>
<Button variant="outline" type="button" onClick={() => setRows(initial)}>
Restore data
</Button>
</div>
<Chart
title="Server load (reactive data)"
data={rows}
height={240}
yAxis={{ min: 0, max: 100, format: '{value}%' }}
renderEmpty={() => <span>No samples yet: press Restore data</span>}
series={[
{ id: 'cpu', name: 'CPU', xField: 't', yField: 'cpu', description: 'CPU use of the web server in percent, sampled four times a second.' },
{ id: 'memory', name: 'Memory', xField: 't', yField: 'memory', description: 'Memory use in percent.' },
]}
/>
<Chart
ref={streamed}
title="CPU (streamed with appendPoints)"
data={initial}
height={200}
yAxis={{ min: 0, max: 100 }}
series={[{ id: 'cpu', name: 'CPU', xField: 't', yField: 'cpu', description: 'The same CPU samples, streamed into the chart store.' }]}
/>
</div>
);
}
Start the stream: every 250 ms the first chart gets a new data array
(oldest point dropped) and the second receives the same point through
ref.current.appendPoints('cpu', [point], { maxPoints: 60 }), which
writes the chart store without rendering the demo. "Clear data" passes an
empty array: the axes stay and renderEmpty() replaces the localized
"No data" text. Each series has a description, appended to the chart
summary and the accessible table caption.