Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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.

Interactive window and live data The interactive chart with its range slider

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.
Live streaming dashboard

Data sources

SourceData
sim (default)Simulated sensors at 100 Hz: temperature, humidity, vibration with bursts
cpuThis machine’s real CPU and memory use, 10 Hz (Linux, /proc)
stdinAny 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).

Animation

Only the part of each frame that changed is stored. For 90 frames at 820×520:

FormatSizeTimeColors
GIF815 KB0.23 sat most 256 per frame
APNG2169 KB0.14 sfull color, transparency
WebP1858 KB0.15 sfull 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.
Live Binance dashboard

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.
Interactive HTML

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.

dataviz in the browser

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.

dataviz in egui

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).

Raster backends

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.

Chart in a terminal

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.

A C++ dashboard: signal with moving average, 200 000 colored points, violins and a grammar-of-graphics line chart

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 named a, 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.
One table drawn as a scatter with size, grouped bars of means, histograms and box plots Facets: one plot per species
#![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