Skip to main content

tui_chart/
lib.rs

1//! # tui-chart
2//!
3//! Chart widgets for [ratatui](https://ratatui.rs).
4//!
5//! ## Widgets
6//!
7//! - [`LineChart`] – line / step / area / scatter, multi-series, secondary y axis
8//! - [`CandlestickChart`] – candles, Heikin-Ashi, OHLC, volume, indicator panes
9//! - [`BarChart`] – vertical/horizontal, grouped/stacked
10//! - [`Histogram`], [`Heatmap`], [`Sparkline`], [`Gauge`], [`RadarChart`]
11//! - [`BoxPlot`], [`Waterfall`], [`Gantt`], [`DepthChart`], [`RenkoChart`], [`Treemap`]
12//! - [`ChartStack`] – stack charts with a shared gutter
13//!
14//! ## Features
15//!
16//! - [`Annotation`]s (lines, regions, labels) and an interactive [`Crosshair`]
17//! - log scale (`y_log`), spline smoothing, error bars and bands
18//! - indicators: [`sma`], [`ema`], [`bollinger`], [`rsi`], [`macd`], [`atr`],
19//!   [`stochastic`], [`vwap`]
20//! - streaming with [`RingBuffer`] / [`Stream`]
21//! - time axis helpers ([`time_ticks`], [`time_hm`], [`date_ymd`], ...)
22//! - optional `theme` cargo feature: `Theme` (built-in themes via `Theme::all`,
23//!   `Theme::next`, `Theme::prev`); every widget then has `.theme(impl Into<Theme>)`
24//! - high resolution: [`Marker`] (`Braille` 2×4 default, `HalfBlock`, `Block`,
25//!   `Dot`, opt-in `Sextant` 2×3 which needs a font with the Unicode
26//!   "Symbols for Legacy Computing" block), [`CandleStyle::Braille`],
27//!   `.marker(..)` on line/bar/sparkline/radar/gauge/histogram/depth/waterfall
28//!   charts and [`OverlapPolicy`] for [`Canvas`] colour conflicts
29//! - optional `serde` cargo feature: `Serialize`/`Deserialize` on data types and
30//!   (with `theme`) `Theme`
31//!
32//! All widgets implement [`ratatui::widgets::Widget`] (both owned and by reference)
33//! and are drawn straight into the buffer.
34//!
35//! ```
36//! # #[cfg(feature = "theme")] {
37//! use ratatui::{buffer::Buffer, layout::Rect, widgets::Widget};
38//! use tui_chart::{Axis, LineChart, Series, Theme};
39//!
40//! let chart = LineChart::new(vec![Series::new("sin").data(
41//!     (0..50).map(|i| (i as f64, (i as f64 / 5.0).sin())),
42//! )])
43//! .theme(Theme::nord())
44//! .x_axis(Axis::new().title("t"))
45//! .y_axis(Axis::new().grid(true));
46//!
47//! let area = Rect::new(0, 0, 60, 15);
48//! let mut buf = Buffer::empty(area);
49//! chart.render(area, &mut buf);
50//! # }
51//! ```
52//!
53//! ```
54//! use ratatui::{buffer::Buffer, layout::Rect, widgets::Widget};
55//! use tui_chart::{Candle, CandlestickChart, sma, Overlay};
56//! use ratatui::style::Color;
57//!
58//! let candles = vec![
59//!     Candle::new(10.0, 12.0, 9.0, 11.0).volume(1200.0).label("Mon"),
60//!     Candle::new(11.0, 13.0, 10.5, 10.8).volume(900.0).label("Tue"),
61//! ];
62//! let avg = sma(&candles, 2);
63//! let chart = CandlestickChart::new(candles)
64//!     .show_volume(true)
65//!     .overlay(Overlay::new("SMA2", avg, Color::Yellow));
66//! let area = Rect::new(0, 0, 40, 16);
67//! let mut buf = Buffer::empty(area);
68//! chart.render(area, &mut buf);
69//! ```
70#![warn(missing_docs)]
71
72#[macro_use]
73mod macros;
74mod annotation;
75mod axis;
76mod bar;
77mod boxplot;
78mod candle;
79mod canvas;
80mod crosshair;
81mod depth;
82mod gantt;
83mod gauge;
84mod heatmap;
85mod histogram;
86mod indicator;
87mod layout;
88mod line;
89mod radar;
90mod renko;
91mod scale;
92mod sparkline;
93mod stack;
94mod stream;
95mod text;
96mod theme;
97mod timeaxis;
98mod treemap;
99mod waterfall;
100
101pub mod util;
102
103pub use annotation::{draw_annotations, Annotation, AnnotationKind};
104pub use axis::{Axis, GridStyle};
105pub use bar::{BarChart, BarGroup, BarMode, BarOrientation, LabelPosition};
106pub use boxplot::{BoxOrientation, BoxPlot, BoxStats};
107pub use candle::{
108    ema, heikin_ashi, sma, BandOverlay, Candle, CandleStyle, CandlestickChart, HLine,
109    IndicatorPane, Overlay, TradeMarker, TradeSide,
110};
111pub use canvas::{Canvas, Marker, OverlapPolicy};
112pub use crosshair::{nearest_point, Crosshair, NearestPoint};
113pub use depth::DepthChart;
114pub use gantt::{Gantt, Task};
115pub use gauge::{Gauge, GaugeOrientation, GaugeStyle};
116pub use heatmap::{Gradient, Heatmap};
117pub use histogram::{Bin, Histogram};
118pub use indicator::{atr, bollinger, macd, rsi, stochastic, vwap};
119pub use line::{LegendPosition, LineChart, LineMode, Series};
120pub use radar::{RadarChart, RadarSeries};
121pub use renko::{renko_bricks, RenkoBrick, RenkoChart, RenkoStyle};
122pub use sparkline::{Sparkline, SparklineStyle};
123pub use stack::{shared_gutter, ChartStack, StackDirection};
124pub use stream::{RingBuffer, Stream};
125#[cfg(feature = "theme")]
126pub use theme::{ChartThemeExt, Theme};
127pub use timeaxis::{
128    civil_from_days, date_md, date_ymd, datetime, days_from_civil, time_hm, time_hms,
129    time_nice_step, time_ticks,
130};
131pub use treemap::{Treemap, TreemapItem};
132#[cfg(feature = "theme")]
133pub use tui_nuances::{Status, ThemeProvider, ThemeSwitcher, Themed};
134pub use waterfall::Waterfall;
135
136use ratatui::style::Color;
137
138/// Default series color cycle.
139pub const PALETTE: [Color; 8] = [
140    Color::Cyan,
141    Color::Yellow,
142    Color::Magenta,
143    Color::Green,
144    Color::LightRed,
145    Color::LightBlue,
146    Color::LightGreen,
147    Color::White,
148];
149
150#[cfg(test)]
151mod tests {
152    use super::*;
153
154    #[test]
155    fn palette_not_empty() {
156        assert_eq!(PALETTE.len(), 8);
157    }
158}