Live data
live feeds a chart from a source. It retries with growing waits after an error, stops while the tab is hidden, and draws at most ten times a second.
Market price
ts
import { append, live, poll } from 'nervure/live';
const spec = { type: 'xy', categories: [], series: [{ name: 'Price', data: [], dots: false }] };
const chart = createChart(host, spec);
const feed = live(chart, spec, poll('/api/price', { every: 1000 }), {
apply: (data, previous) => append(previous, data.time, { Price: data.price }, 60),
onStatus: (status) => console.log(status),
});The figures on this page are made up in your browser.
Processor and memory
Sources
ts
poll('/api/cpu', { every: 2000, init: { headers: { Authorization: `Bearer ${token}` } } }); // an API, every 2 s
poll(async (signal) => (await client.read({ signal })).value, { every: 5000 }); // your own function
events('/api/cpu/stream'); // server-sent events
socket('wss://example.com/prices', { hello: { subscribe: ['EURUSD'] } }); // WebSocket| Source | Use it when |
|---|---|
poll | you have a plain API; every few seconds is enough |
events | your server pushes; one way, over plain HTTP, see server side |
socket | many messages a second, or an existing WebSocket feed |
poll waits for an answer before it counts the next interval, so a slow API is never called twice at once.
Control
ts
feed.pause();
feed.resume();
feed.stop(); // call it when the chart goes away
feed.status; // connecting, live, paused, retrying or stopped| Option | Default | Does |
|---|---|---|
apply | turns a message into the next spec; null ignores it | |
throttle | 100 | at most one drawing in this time (ms) |
retry | 1 s to 30 s | waits after errors; false gives up |
pauseWhenHidden | true | stops while the tab is hidden |
onStatus, onError | follow what happens |
Long series
A year of minutes is more points than pixels. thin keeps the lowest and the highest point of each stretch, so peaks and dips stay visible, and the chart stays quick to draw. The data is untouched: the table and the CSV export keep every point, and zooming in brings the detail back.
js
createChart(el, { type: 'xy', categories, series, thin: true, zoom: true });
// or the most points kept
createChart(el, { type: 'xy', categories, series, thin: 500 });In React
tsx
<Chart spec={spec} style={{ height: 300 }} onReady={(chart) => {
if (chart) feed.current = live(chart, spec, events('/api/cpu/stream'), { apply });
else feed.current?.stop();
}} />Keep keys on your server
A key used in the browser can be read by everyone. Call paid or private APIs from your server, and let the chart read yours.