What it brings
Use weirdnet-chart for live charts in a desktop or terminal app: prices with volume below, indicators, anything time-ordered that keeps arriving. One chart model drives both a GPUI view and a Ratatui widget, so a GUI and a TUI show the same chart. The core has no GUI dependencies.
What it is
Real-time charts for Weird Brains apps on Weirdnet. The core is renderer-independent and has no GUI dependencies; GPUI and Ratatui backends are optional features.
How it works
Series
A Series is a time-ordered ring buffer of points or candles. Appending is O(1), the oldest samples drop off
at capacity, and a push with the newest timestamp replaces it in place, which is how a live candle updates.
Chart::apply_trade(series, time, price, size, interval) folds trades into fixed-interval candles: it updates the
open candle or starts the next one. Timestamps are milliseconds since the Unix epoch.
Panes and marks
A Chart stacks any number of panes over one time axis, each with a weight and its own y axis
(YRange: auto, auto from zero, or fixed), for example price above volume. Each series has a Mark
that says how it is drawn:
Line: a polyline through points.Area: a polyline with the area under it filled.Bars: a bar from zero per point.Candles: candlesticks.Ohlc: OHLC bars, a high-low line with open and close ticks.Volume: volume bars taken from another candle series, coloured by direction (add_volume(pane, candles)).
Value axes use "nice" ticks with compact labels. Time ticks are calendar-aware from milliseconds to decades and land on
whole local minutes, hours, days, months and years for a fixed UTC offset
(set_time_zone(TimeZone::Fixed(seconds))); charts carry no time-zone database.
Viewport
The Viewport is the visible time window. It pans (by time, a fraction or pixels), zooms around a point, and
keeps the span within limits (set_span_limits). While it is following, new data scrolls the view so the newest
sample stays at the right edge with a small margin; panning away stops following, and panning or zooming back to the live
edge resumes it. Chart::show_latest(span) shows the last span milliseconds and follows.
Layout to a Scene
Chart::layout turns the chart into a Scene for a given size: the plot area, the time scale, each
pane's layout, and backend-neutral primitives (rectangles, lines, polylines, areas and text) in clip layers, back to front.
Only the visible range is laid out. Lines are reduced to the first, lowest, highest and last point per pixel column, so
spikes and gaps survive, and candles merge into wider bars when they would be thinner than three pixels. A new backend only
has to draw those five primitives.
FrameThrottle
FrameThrottle turns a burst of updates into one frame now and at most one more after the frame interval, so
live data redraws at the frame rate instead of on every tick. In a terminal event loop, pair FrameThrottle::poll
with FrameThrottle::timeout.
Ratatui
With the ratatui feature, ratatui::ChartWidget draws the scene with braille dots (two across and
four down per cell) on ratatui-core, so it works with ratatui 0.30. pan_columns and zoom_columns
pan and zoom by terminal columns, and render_scene draws a laid-out scene into a buffer.
GPUI
With the gpui feature, gpui::ChartView is a view that owns the chart, draws it with paths, quads
and shaped text, pans on drag and horizontal scroll, and zooms on vertical scroll. Feed it from a task with
update_chart, which coalesces redraws to 60 frames per second (set_fps changes that);
gpui::paint_scene draws a scene in your own element or canvas.
The backend builds against gpui 0.2.2 from crates.io. An app that pins GPUI to a git revision can point this crate at the
same revision with [patch.crates-io], as long as that revision's gpui is semver-compatible. On Linux, GPUI needs
the X11, Wayland, xkbcommon, Vulkan, fontconfig and freetype development packages. gpui 0.2.2 also pulls in
xattr 0.2.3, which needs libc 0.2.189 or older on Linux; the
weirdnet-chart repository's Cargo.lock pins it
there.
Key types and modules
Series: time-ordered ring buffers of points or candles, with live-candle replacement and trades folded into fixed-interval candles.Panes stacked over one time axis, each with its own y axis, and marks:Line,Area,Bars,Candles,OhlcandVolume.- A viewport with pan, zoom, span limits and following the newest data.
- Per-pixel reduction of lines and merging of thin candles, so only the visible range is laid out.
FrameThrottlefor frame-rate redraws, and backend-neutral scenes fromChart::layout.
Feature flags
- No feature is on by default: the core has no GUI dependencies.
ratatui:ratatui::ChartWidget(braille, ratatui 0.30 through ratatui-core),render_scene, column-based pan and zoom.gpui:gpui::ChartViewandpaint_scene, against gpui 0.2.2 from crates.io.gpui-test-support: GPUI's headless test platform, for the crate's own tests.
Usage
[dependencies]
weirdnet-chart = "0.1.0"Where it fits
- License: BSD-3-Clause.