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//! - [`Funnel`], [`Bullet`], [`PairedBars`], [`BubbleChart`], [`LollipopChart`], [`SlopeChart`], [`CalendarHeatmap`], [`ViolinChart`], [`RoseChart`], [`StackedAreaChart`], [`Sankey`]
13//! - [`RadialBar`], [`RingStack`] – concentric progress rings
14//! - [`Oscilloscope`], [`Spectrum`], [`DotMatrix`], [`RadarScope`], [`HexMap`] – instrument panels
15//! - [`ChartStack`] – stack charts with a shared gutter
16//! - [`PieChart`] / [`PieSlice`] – re-exported from the
17//! [`tui-piechart`](https://crates.io/crates/tui-piechart) crate (default `piechart` feature;
18//! the whole crate is also available as [`piechart`])
19//!
20//! ## Accessibility
21//!
22//! Wrap any chart in [`Monochrome`] (or [`Monochrome::auto`], which honours
23//! `NO_COLOR`) to render without colour: solid fills turn into `░▒▓█` shades.
24//!
25//! ## Features
26//!
27//! - [`Annotation`]s (lines, regions, labels) and an interactive [`Crosshair`]
28//! - log scale (`y_log`), spline smoothing, error bars and bands
29//! - indicators: [`sma`], [`ema`], [`bollinger`], [`rsi`], [`macd`], [`atr`],
30//! [`stochastic`], [`vwap`]
31//! - streaming with [`RingBuffer`] / [`Stream`]
32//! - time axis helpers ([`time_ticks`], [`time_hm`], [`date_ymd`], ...)
33//! - optional `theme` cargo feature: `Theme` (built-in themes via `Theme::all`,
34//! `Theme::next`, `Theme::prev`); every widget then has `.theme(impl Into<Theme>)`
35//! - high resolution: [`Marker`] (`Braille` 2×4 default, `HalfBlock`, `Block`,
36//! `Dot`, opt-in `Sextant` 2×3 which needs a font with the Unicode
37//! "Symbols for Legacy Computing" block), [`CandleStyle::Braille`],
38//! `.marker(..)` on line/bar/sparkline/radar/gauge/histogram/depth/waterfall
39//! charts and [`OverlapPolicy`] for [`Canvas`] colour conflicts
40//! - optional `serde` cargo feature: `Serialize`/`Deserialize` on data types and
41//! (with `theme`) `Theme`
42//!
43//! All widgets implement [`ratatui::widgets::Widget`] (both owned and by reference)
44//! and are drawn straight into the buffer.
45//!
46//! ```
47//! # #[cfg(feature = "theme")] {
48//! use ratatui::{buffer::Buffer, layout::Rect, widgets::Widget};
49//! use tui_chart::{Axis, LineChart, Series, Theme};
50//!
51//! let chart = LineChart::new(vec![Series::new("sin").data(
52//! (0..50).map(|i| (i as f64, (i as f64 / 5.0).sin())),
53//! )])
54//! .theme(Theme::nord())
55//! .x_axis(Axis::new().title("t"))
56//! .y_axis(Axis::new().grid(true));
57//!
58//! let area = Rect::new(0, 0, 60, 15);
59//! let mut buf = Buffer::empty(area);
60//! chart.render(area, &mut buf);
61//! # }
62//! ```
63//!
64//! ```
65//! use ratatui::{buffer::Buffer, layout::Rect, widgets::Widget};
66//! use tui_chart::{Candle, CandlestickChart, sma, Overlay};
67//! use ratatui::style::Color;
68//!
69//! let candles = vec![
70//! Candle::new(10.0, 12.0, 9.0, 11.0).volume(1200.0).label("Mon"),
71//! Candle::new(11.0, 13.0, 10.5, 10.8).volume(900.0).label("Tue"),
72//! ];
73//! let avg = sma(&candles, 2);
74//! let chart = CandlestickChart::new(candles)
75//! .show_volume(true)
76//! .overlay(Overlay::new("SMA2", avg, Color::Yellow));
77//! let area = Rect::new(0, 0, 40, 16);
78//! let mut buf = Buffer::empty(area);
79//! chart.render(area, &mut buf);
80//! ```
81#![warn(missing_docs)]
82
83#[macro_use]
84mod macros;
85/// The full [`tui-piechart`](https://docs.rs/tui-piechart) crate, re-exported.
86#[cfg(feature = "piechart")]
87pub use tui_piechart as piechart;
88#[cfg(feature = "piechart")]
89pub use tui_piechart::{PieChart, PieSlice};
90
91mod accessibility;
92mod annotation;
93mod axis;
94mod bar;
95mod boxplot;
96mod braille;
97mod bubble;
98mod bullet;
99mod calendar;
100mod candle;
101mod canvas;
102mod crosshair;
103mod depth;
104mod effects;
105mod funnel;
106mod gantt;
107mod gauge;
108mod gauge_kit;
109mod heatmap;
110mod histogram;
111mod indicator;
112mod instruments;
113mod interact;
114mod layout;
115mod line;
116mod lollipop;
117mod paired_bars;
118mod radar;
119mod radial;
120mod renko;
121mod rose;
122mod sankey;
123mod scale;
124mod slope;
125mod sparkline;
126mod stack;
127mod stacked_area;
128mod stream;
129mod text;
130mod theme;
131mod timeaxis;
132mod treemap;
133mod violin;
134mod waterfall;
135
136pub mod util;
137
138pub use accessibility::{no_color_requested, strip_color, Monochrome};
139pub use annotation::{draw_annotations, Annotation, AnnotationKind};
140pub use axis::{Axis, GridStyle};
141pub use bar::{BarChart, BarGroup, BarMode, BarOrientation, LabelPosition};
142pub use boxplot::{BoxOrientation, BoxPlot, BoxStats};
143pub use bubble::{BubbleChart, BubbleSeries};
144pub use bullet::{Bullet, BulletItem};
145pub use calendar::CalendarHeatmap;
146pub use candle::{
147 ema, heikin_ashi, sma, BandOverlay, Candle, CandleStyle, CandlestickChart, HLine,
148 IndicatorPane, Overlay, TradeMarker, TradeSide,
149};
150pub use canvas::{Canvas, Marker, OverlapPolicy};
151pub use crosshair::{nearest_point, Crosshair, NearestPoint};
152pub use depth::DepthChart;
153pub use effects::{bracket_block, chamfer_block, glow, scanlines, Fx};
154pub use funnel::{Funnel, FunnelStage};
155pub use gantt::{Gantt, Task};
156pub use gauge::{Gauge, GaugeOrientation, GaugeStyle};
157pub use heatmap::{Gradient, Heatmap};
158pub use histogram::{Bin, Histogram};
159pub use indicator::{atr, bollinger, macd, rsi, stochastic, vwap};
160pub use instruments::{
161 blip_alpha, ecg_beat, hex_center, hex_extent, jitter, Blip, DotMatrix, HexMap, Oscilloscope,
162 RadarScope, Spectrum,
163};
164pub use interact::{Click, Hit, HitKind, HitMap, Interaction};
165pub use line::{LegendPosition, LineChart, LineMode, Series};
166pub use lollipop::{LollipopChart, LollipopItem};
167pub use paired_bars::{PairedBars, PairedBarsItem, PairedBarsLayout};
168pub use radar::{RadarChart, RadarSeries};
169pub use radial::{RadialBar, RadialRing, RadialSweep, RingStack};
170pub use renko::{renko_bricks, RenkoBrick, RenkoChart, RenkoStyle};
171pub use rose::{RoseChart, RoseItem};
172pub use sankey::{Sankey, SankeyLink};
173pub use slope::{SlopeChart, SlopeItem};
174pub use sparkline::{Sparkline, SparklineStyle};
175pub use stack::{shared_gutter, ChartStack, StackDirection};
176pub use stacked_area::{AreaLayer, StackedAreaChart};
177pub use stream::{RingBuffer, Stream};
178#[cfg(feature = "theme")]
179pub use theme::{ChartThemeExt, Theme};
180pub use timeaxis::{
181 civil_from_days, date_md, date_ymd, datetime, days_from_civil, time_hm, time_hms,
182 time_nice_step, time_ticks,
183};
184pub use treemap::{Treemap, TreemapItem};
185#[cfg(feature = "theme")]
186pub use tui_nuances::{Status, ThemeProvider, ThemeSwitcher, Themed};
187pub use violin::{ViolinChart, ViolinGroup};
188pub use waterfall::Waterfall;
189
190use ratatui::style::Color;
191
192/// Default series color cycle.
193pub const PALETTE: [Color; 8] = [
194 Color::Cyan,
195 Color::Yellow,
196 Color::Magenta,
197 Color::Green,
198 Color::LightRed,
199 Color::LightBlue,
200 Color::LightGreen,
201 Color::White,
202];
203
204#[cfg(test)]
205mod tests {
206 use super::*;
207
208 #[test]
209 fn palette_not_empty() {
210 assert_eq!(PALETTE.len(), 8);
211 }
212}