Live, interactive and animated
Interactive window and live data
Figure::show opens a window: mouse wheel zooms around the cursor, left-drag pans, right-drag zooms to a box, the range slider below the plot is dragged to pan and at its edges to zoom, Shift+drag measures (Δx, Δy, change in %), Ctrl+drag selects a box (Figure::selection()), clicking a legend row hides or shows its series, R resets, C toggles the crosshair (cursor values on the axes and the series values at that x), H toggles hover tooltips, S saves a PNG, Shift+S an SVG, Ctrl+C copies to the clipboard, V records a GIF, Esc closes. Plots showing the same x range get a linked cursor and are zoomed and panned together. Figure::show_live then streams data into a rolling window.
The animation was recorded with cargo run --release -- --demo: wheel zoom and drag through the same calls a window or GUI makes (zoom_at, pan_by, reset_view), the range slider following the view.
Code of this example · cargo run --release -p plot-interactive
Live streaming data
A live dashboard for data that keeps arriving: a producer thread sends
samples over a channel, the window takes everything that arrived since the
last frame and redraws (Figure::show_live, 30 frames per second).
- top: the last 30 seconds of every channel on a time axis (UTC), latest
values in the title (
Line::push+Line::keep_since), - bottom left: distribution of the first channel in that window
(
Histogram::set_values), - bottom right: incoming samples per second.
Data sources
| Source | Data |
|---|---|
sim (default) | Simulated sensors at 100 Hz: temperature, humidity, vibration with bursts |
cpu | This machine’s real CPU and memory use, 10 Hz (Linux, /proc) |
stdin | Any numbers piped in, one sample per line (spaces, commas or tabs); a non-numeric first line names the channels |
Code of this example · cargo run --release -p plot-streaming
Animations
A travelling wave packet and the phase portrait of a damped oscillator,
recorded with Figure::record as a GIF, a lossless animated PNG and a
lossless animated WebP (the format follows the extension).
Only the part of each frame that changed is stored. For 90 frames at 820×520:
| Format | Size | Time | Colors |
|---|---|---|---|
| GIF | 815 KB | 0.23 s | at most 256 per frame |
| APNG | 2169 KB | 0.14 s | full color, transparency |
| WebP | 1858 KB | 0.15 s | full color, transparency |
The same call writes .mp4 / .webm when | |||
ffmpeg is installed. For data arriving over time, use Recorder::push_at | |||
(see plot-streaming); in a window V records a GIF. |
Code of this example · cargo run --release -p plot-animation
Live Binance market dashboard
Live BTC/USDT (or any spot symbol) from Binance’s public market-data API,
redrawn about once a second (Figure::show_live, dark theme):
- 1-minute candlesticks with MA(7) and MA(25) and the last price,
- volume per minute (total and taker buys),
- every trade of the last two minutes (buys green, sells red, marker size by amount),
- order-book depth (cumulative bids and asks, 500 levels),
- taker buy minus sell volume per 10 seconds,
- 24 h change, high, low and volume in the title.
A background thread polls https://data-api.binance.vision/api/v3
(trades, depth, klines, ticker/24hr) with the system’s curl, so
there is no HTTP or JSON dependency and no API key; the window only redraws
when new data arrived. Times are shown in the local time zone.
Code of this example · cargo run --release -p plot-binance
Interactive HTML
fig.save("weather.html") writes one self-contained file: the chart as
SVG with a small script, no server, no internet access and no external
scripts needed. In the browser:
- a crosshair with the cursor’s x and y values on the axes,
- a tooltip with every series’ value at the cursor’s x (and a linked cursor in plots sharing the x range),
- clicking a legend row hides or shows its series,
- a button saves the chart as PNG.
Code of this example · cargo run --release -p plot-html
dataviz in the browser (WebAssembly)
The chart is rendered by Rust compiled to WebAssembly into an RGBA buffer
that a few lines of JavaScript put on a <canvas>. Two sensors stream in
every frame; the wheel zooms, dragging pans, a double-click follows the
data again, hovering shows the nearest value and clicking a legend row
hides or shows a series — all handled by dataviz in Rust.
No wasm-bindgen or other JavaScript tooling: the module exports plain
functions (init, step, render, wheel, drag, reset, click,
hover) and imports nothing. Release build: 3.1 MB (1.2 MB gzipped, the
embedded font and text shaping included); a frame (new data + render at
960×520 with 2 × 3000 points) takes about 1.7 ms in V8.
The interaction calls (Figure::zoom_at, pan_by, zoom_to_box,
reset_view, hover, click) are the same for any GUI toolkit that
shows the figure’s pixels (Figure::render_into).
Code of this example · cargo run --release -p plot-wasm
dataviz inside an egui app
A live chart in an egui application: each
frame the figure is rendered into a texture at the panel’s size
(Figure::render_into, sharp on HiDPI screens) and the mouse is passed on
with dataviz’s window-free interaction API — wheel to zoom (zoom_at),
drag to pan (pan_by), hover readouts (hover), legend clicks (click),
double-click to reset (reset_view). The side panel changes the data
speed, noise and theme while it streams.
The same approach works with any toolkit that can show an RGBA image (iced, Slint, GTK, Qt, …).
Code of this example · cargo run --release -p plot-egui
Raster backends
The same real-time chart (1920×1080, rolling 100 000 points) drawn on one CPU
thread, on all cores and, with --features gpu, on the GPU (Vulkan, Metal,
DirectX 12 or OpenGL through wgpu). Prints the time and CPU time per frame.
Built with the dataviz::plot API
(Figure::backend, Figure::render_0rgb).
Code of this example · cargo run --release -p plot-backends
Terminal output
Charts printed in the terminal with 24-bit colored half blocks or braille dots (Figure::to_terminal, Figure::print), e.g. over SSH or in CI logs.
Braille output (TerminalMode::BrailleMono) is saved to screenshots/terminal.txt.
Code of this example · cargo run --release -p plot-terminal
dataviz from C and C++
The dataviz renderer as a C library with a header-only C++17 wrapper
(dataviz.h, dataviz.hpp). It runs on Linux, macOS and Windows, and
iOS and Android get static libraries. CMake downloads dataviz and builds
the library with cargo, so you need CMake 3.16+, a C/C++ compiler and
Rust.
dashboard.cpp builds the dashboard above and saves it
as PNG and SVG in about 85 ms. It uses three ways in:
- Direct calls:
line,scatter,hist,heatmap,density,scatter3d,surface,candlestick. - Any chart type through a JSON spec: large arrays stay binary. A
field
"@a"takes the array nameda, copied once and never turned into text. For 1 M points this takes 4 ms; the same data as JSON numbers takes 166 ms. - The grammar of graphics: data, a mark and encodings.
#include "dataviz.hpp"
dataviz::Figure fig(1200, 800);
auto ax = fig.axes(0, 0);
ax.line(t, signal, dataviz::Style().label("signal").width(1.0));
ax.title("Signal").xlabel("time (s)");
fig.axes(1, 0).plot(R"({"type": "violin", "groups": {"auth": "@a", "search": "@b"}})",
{{"a", auth_ms}, {"b", search_ms}});
fig.save("dashboard.png"); // .svg .pdf .html .jpg .webp
live.c is a live window in plain C: a sensor value arrives
every frame, the last 600 points are kept, and zoom, pan and hover work.
static int update(dv_figure *fig, void *user) {
state *s = user;
dv_series_push(s->raw, s->t, read_sensor(), 600);
return 1; /* redraw */
}
dv_figure_show_live(fig, "sensor", 60, update, &s);
In your own UI (Qt, GTK, Dear ImGui, SDL, a game engine),
dv_figure_render returns RGBA pixels to upload as a texture.
dv_figure_zoom, dv_figure_pan, dv_figure_click and
dv_figure_hover handle the user’s input.
Code of this example · cargo run --release -p plot-c-cpp
Grammar of graphics
A chart described by data, a mark and encodings, the way Vega-Lite, ggplot2 and Altair describe charts. From the encodings dataviz derives:
- color groups with a legend;
- aggregation (
count,sum,mean,median,min,max); - histograms, when x is binned;
- stacked, grouped or percent bars;
- heatmaps (
rect) and box plots; - small multiples (
facet), with the same colors in every plot and a single legend.
#![allow(unused)]
fn main() {
use dataviz::plot::grammar::{Aggregate, Chart, Data, Field, Mark};
let data = Data::new()
.text("species", species)
.text("island", island)
.num("body mass (g)", mass);
Chart::new(data, Mark::Bar)
.x("island")
.y(Field::new("body mass (g)").aggregate(Aggregate::Mean))
.color("species")
.stack(BarMode::Grouped)
.draw(&mut fig)?;
}
The same chart as a JSON spec works from Python, JavaScript, C/C++ and the MCP server:
{"data": {"island": [...], "species": [...], "body mass (g)": [...]},
"mark": "bar",
"encoding": {"x": "island", "y": {"field": "body mass (g)", "aggregate": "mean"}, "color": "species"},
"stack": "grouped"}
In Python, with pandas or Polars DataFrames and Altair shorthand:
dv.plot(df, "bar", x="island", y="mean(body mass (g))", color="species", stack="grouped")
Speed: 1 000 000 rows in 3 color groups build in about 30 ms; with the PNG saved, about 120 ms.
Code of this example · cargo run --release -p plot-grammar