Skip to main content

weirdnet_chart/
lib.rs

1//! Real-time charts for [Weird Brains](https://weirdbrains.com) apps: a renderer-independent
2//! core with optional [GPUI](https://crates.io/crates/gpui) and
3//! [Ratatui](https://crates.io/crates/ratatui) backends.
4//!
5//! The core has no GUI dependencies. It holds time-ordered series in ring buffers
6//! ([`Series`]), arranges them in panes over one time axis ([`Chart`]), tracks the visible
7//! window with pan, zoom and live following ([`Viewport`]), and lays the visible part out as
8//! backend-neutral primitives ([`Scene`]) with "nice" value ticks and calendar-aware time
9//! ticks. [`FrameThrottle`] turns a stream of live updates into redraws at the frame rate.
10//!
11//! ```
12//! use weirdnet_chart::{Candle, Chart, Metrics, Pane, Size, YRange, time::MINUTE};
13//!
14//! let mut chart = Chart::new();
15//! let candles = chart.add_candles(Chart::MAIN, "BTC-USD").unwrap();
16//! let volume = chart.add_pane(Pane { weight: 1.0, y_range: YRange::AutoFromZero, title: None });
17//! chart.add_volume(volume, candles).unwrap();
18//! chart.show_latest(60 * MINUTE);
19//!
20//! // History, then live trades folded into one-minute candles.
21//! chart.push_candle(candles, Candle::new(0, 100.0, 101.0, 99.5, 100.5, 12.0)).unwrap();
22//! chart.apply_trade(candles, 61_000, 100.7, 0.5, MINUTE).unwrap();
23//!
24//! let scene = chart.layout(Size::new(800.0, 400.0), &Metrics::PIXELS);
25//! assert_eq!(scene.panes.len(), 2);
26//! ```
27//!
28//! # Features
29//!
30//! - `ratatui`: [`ratatui::ChartWidget`], a Ratatui widget drawn with braille dots.
31//! - `gpui`: [`gpui::ChartView`], a GPUI view drawn with paths and quads, with pan and zoom.
32#![warn(missing_docs)]
33
34pub mod chart;
35pub mod color;
36pub mod scale;
37pub mod scene;
38pub mod series;
39pub mod throttle;
40pub mod time;
41pub mod viewport;
42
43#[cfg(feature = "gpui")]
44#[path = "backend/gpui.rs"]
45pub mod gpui;
46#[cfg(feature = "ratatui")]
47#[path = "backend/ratatui.rs"]
48pub mod ratatui;
49
50pub use chart::{Chart, ChartError, Mark, Pane, PaneId, SeriesId, SeriesStyle, Theme, YRange};
51pub use color::Rgba;
52pub use scale::{LinearScale, TimeTick};
53pub use scene::{Align, Layer, Metrics, PaneLayout, Pos, Primitive, Rect, Scene, Size};
54pub use series::{Candle, Change, OutOfOrder, Point, Sample, Series};
55pub use throttle::{FrameThrottle, Redraw};
56pub use time::{TimeZone, Timestamp};
57pub use viewport::Viewport;