Real-Time Line Chart for React
A real-time React line chart: each new sample glides in from the right while the time axis slides under it. Pass data as readonly ChartRow[]: samples so far, oldest first: { time, …values }.
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/streaming-chart.jsonweb-01
StreamingChartStreamingTableuseLiveDatauseStreamingDatadefaultStreamingChartLabels- +3 more
Two series, one sample a second
useStreamingData calls your function every second and keeps a rolling buffer; the chart and its table both read it. Call it in the small component that renders the card, so each sample re-renders only that card. Hover, tap or arrow through the line to hold the view and read values. Pause stops the view; data keeps arriving and Resume glides to the present.
web-01
us-east-1 · last 60 seconds
import { useEffect, useRef, useState } from "react";
import { ChartCard } from "@/components/sheenui/charts/chart-parts/chart-card";
import { type ChartSeries } from "@/components/sheenui/charts/chart-utils";
import { StreamingChart } from "@/components/sheenui/charts/streaming-chart";
import { useStreamingData } from "@/components/sheenui/charts/streaming-chart/model";
import { StreamingTable } from "@/components/sheenui/charts/streaming-chart/parts";
// Package build (after the first npm release, or a packed tarball): import { useStreamingData, ChartCard, StreamingTable, StreamingChart, type ChartSeries } from "@sheenui/theme";
const series: ChartSeries[] = [
{ key: "cpu", label: "CPU", color: 1 },
{ key: "memory", label: "Memory", color: 3 },
];
const percent = (n: number) => Math.round(n) + "%";
// Stand-ins for your metrics source: replace them with a real reading.
const readCpu = () => 35 + Math.random() * 30;
const readMemory = () => 55 + Math.random() * 10;
// The hook lives in the card's own component: a sample re-renders this card, not the page.
// Off-screen the feed rests (idle) and the missed samples are filled in when it's back.
export function Example() {
const ref = useRef<HTMLDivElement>(null);
const [near, setNear] = useState(true);
useEffect(() => {
const el = ref.current;
if (!el) return;
const observer = new IntersectionObserver(([entry]) => setNear(entry?.isIntersecting ?? true), { rootMargin: "200px" });
observer.observe(el);
return () => observer.disconnect();
}, []);
const data = useStreamingData(
() => ({ cpu: readCpu(), memory: readMemory() }),
{ rate: 1000, history: 60, idle: !near },
);
return (
<div ref={ref}>
<ChartCard title="web-01" table={<StreamingTable data={data} series={series} format={percent} />}>
<StreamingChart
data={data}
series={series}
rate={1000}
window={60}
format={percent}
aria-label="CPU and memory of web-01, last minute"
/>
</ChartCard>
</div>
);
}Files to copy
17 folders · 34 files · 4 packages
- From
theme/src/ components/charts/{streaming-chart.tsx, category-bar.tsx, chart-export-menu.tsx, chart-export.ts, chart-figure.tsx, chart-fit.tsx, chart-icons.tsx, chart-parts.tsx, chart-patterns.tsx, chart-pointer.tsx, chart-tip.tsx, chart-utils.ts, heatmap.tsx, streamgraph.tsx}components/button/components/charts/category-bar/components/charts/chart-parts/components/charts/heatmap/components/charts/streamgraph/components/charts/streaming-chart/components/drawer/parts/sheet.tsxcomponents/kbd/components/menu/components/skeleton/components/spinner/components/status-icons/hooks/{use-controllable.ts, use-direction.ts, use-intersection.ts, use-latest.ts, use-magnetic.ts, use-media-query.ts, use-preferences.ts}motion/{gate.ts, tokens.ts}lib/{cn.ts, csv.ts, form.ts, glide-highlight.tsx, heading.ts, math.ts, popup-a11y.ts, popup-motion.ts, state-light.tsx, warn-once.ts}styles/
- shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/streaming-chart.jsonRegistry item: streaming-chart. The styles need the Tailwind v4 glass UI setup.- Packages
pnpm add @base-ui/react clsx motion tailwind-merge
Next.js: samples here pass functions (formatters, seriesLegend, a dashboard's load), so put them in a file that starts with "use client".
Fast feed with a zero baseline
Two samples a second, 40 seconds in view, the axis anchored at zero. Push your own rows (from a socket, say) and the chart animates them the same way.
Edge requests
All regions, requests per second
import { useEffect, useState } from "react";
import { type ChartRow } from "@/components/sheenui/charts/chart-utils";
import { StreamingChart } from "@/components/sheenui/charts/streaming-chart";
// Package build (after the first npm release, or a packed tarball): import { StreamingChart, type ChartRow } from "@sheenui/theme";
export function Example() {
const [rows, setRows] = useState<ChartRow[]>([]);
useEffect(() => {
// Your feed: one message per sample, e.g. { "rps": 812 }
const socket = new WebSocket("wss://example.com/metrics");
socket.onmessage = (event) => {
const rps = Number(JSON.parse(String(event.data)).rps);
setRows((r) => [...r.slice(-200), { time: Date.now(), rps }]);
};
return () => socket.close();
}, []);
return (
<StreamingChart
data={rows}
series={[{ key: "rps", label: "Requests / s", color: 7 }]}
rate={500}
window={80}
zero
aria-label="Requests per second"
/>
);
}Props
StreamingChartLabels keys and English defaults (8)
useStreamingData(source, options)
StreamingTable props
Keyboard
Accessibility
- Anything that moves on its own can be paused (WCAG 2.2.2): the Pause button is built in, and Space or K toggles it from the chart.
- New samples are never announced on their own (that would be a flood); a sample read by keyboard is announced once, with every series' value.
- The status pill says Live, Holding or Paused in words, and the latest values in the header double as the legend.
- Reduced motion: no sliding, no pulse and no rolling numbers; the chart redraws in place once per sample.
- Off-screen or in a background tab the chart rests (no frame loop, no redraw, no pulse); useStreamingData stops its timer in a background tab (and off-screen with `idle`), then fills in the gap so the chart glides to the present.
- The table view lists the latest samples, newest first.
All exports on this page
StreamingChart— A live line chart: new samples glide in from the right as the time axis slides smoothly under them, like a heart-rate monitor.StreamingTable— The latest samples, newest first: time and each series' value.defaultStreamingChartLabels— The built-in English strings of StreamingChart; spread it into your own labels and change what you need.useLiveData— Live (streaming) rows for any chart: appends next() every interval ms into a sliding window.useStreamingData— A rolling buffer of samples: calls source(time, index) every rate ms and keeps the last capacity rows, { time, ...values }.
When to use the real-time chart
- Use it as a live chart in React for an ops wall or a status page, such as requests per second and error rate over the last minute, where samples keep arriving and the line should glide along instead of redrawing.
- It fits a monitoring panel fed by a WebSocket, because you can append your own rows to data and the chart animates them the same way as generated ones.
- It also works as a fast feed with the axis anchored at zero, such as two samples a second with 40 seconds in view.
- Skip it when data arrives rarely or the history is the point; LineChart handles a static range better, and Tracker fits status-by-day history.
- Pair it with ChartCard for the title, useStreamingData to generate and buffer samples, and StreamingTable for the latest rows as text; the chart brings its own Live status and Pause button.
Frequently asked questions
Related pages
48 charts pages in all: browse them on Charts.
By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .