Guide

Live data

A live chart is an ordinary chart with a feed behind it. Your app owns the feed: a WebSocket, an EventSource, a poll. The chart draws what it is given and never makes data up. This page covers the four ways a chart takes a feed, and what the feed owes the reader.

A window that follows the newest readings

Append readings to the arrays you bind, and the chart shows the newest ones. On the line chart (Pro), [live]="true" with liveWindow does this. The draw-on animation is off, and a reader's brush zoom wins until it is reset. On the candlestick, liveWindow draws the newest sessions and fits the price axis to them.

import { svAppendSample } from '@styloviz/core';

socket.onmessage = (e) => {
  const r = JSON.parse(e.data);
  // Keep history bounded: the chart shows liveWindow of it, memory keeps 500.
  this.times.update((t) => svAppendSample(t, r.time, { retain: 500 }).samples);
  this.cpu.update((v) => svAppendSample(v, r.cpu, { retain: 500 }).samples);
};

<sv-line-chart-pro [live]="true" [liveWindow]="30"
  [categories]="times()" [series]="[{ label: 'CPU', data: cpu() }]" />

A stream that scrolls

sv-live-stream-chart takes readings through push() and slides the line left as each one arrives. Pushes that land mid-slide are combined, and the latest one wins. It keeps its own window, so you don't bind an array. It is documented with the line chart.

Values that change in place

On the bar chart (Pro), sort orders the categories by their totals and slides the bars when the values change, which makes a live leaderboard. The data table, the keyboard order and (barSelect) keep your input order. sv-live-update-chart animates between whole data sources, such as this year and last year.

What the feed owes the reader

  • Its state. A feed that dropped looks just like a quiet one. Pass the socket's state as connection (open, reconnecting, closed) on the stream and the candlestick, and set reconnectable to offer a button that emits (reconnect).
  • Bounded history. The window limits what is drawn, not what is kept in memory. Cap your arrays with svAppendSample(…, { retain }).
  • The gaps. If your page generates readings on a timer, use createLiveTicker from @styloviz/core. It stops while the tab is hidden and gives you a sentence that says how long it was away. Don't resume silently: a line joined across that stretch claims readings nobody took.
  • Something to read. Show the latest value as text near the chart, for readers who can't follow motion.