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#[cfg(feature = "piechart")]
86pub use pie::Pie;
87/// The full [`tui-piechart`](https://docs.rs/tui-piechart) crate, re-exported.
88#[cfg(feature = "piechart")]
89pub use tui_piechart as piechart;
90#[cfg(feature = "piechart")]
91pub use tui_piechart::{PieChart, PieSlice};
92
93mod accessibility;
94mod annotation;
95mod axis;
96mod bar;
97mod boxplot;
98mod braille;
99mod bubble;
100mod bullet;
101mod calendar;
102mod candle;
103mod canvas;
104mod crosshair;
105mod depth;
106mod effects;
107mod funnel;
108mod gantt;
109mod gauge;
110mod gauge_kit;
111mod heatmap;
112mod histogram;
113mod indicator;
114mod instruments;
115mod interact;
116mod layout;
117mod line;
118mod lollipop;
119mod paired_bars;
120#[cfg(feature = "piechart")]
121mod pie;
122mod radar;
123mod radial;
124mod renko;
125mod rose;
126mod sankey;
127mod scale;
128mod slope;
129mod sparkline;
130mod stack;
131mod stacked_area;
132mod stream;
133mod text;
134mod theme;
135mod timeaxis;
136mod treemap;
137mod violin;
138mod waterfall;
139
140pub mod util;
141
142pub use accessibility::{Monochrome, no_color_requested, strip_color};
143pub use annotation::{Annotation, AnnotationKind, draw_annotations};
144pub use axis::{Axis, GridStyle};
145pub use bar::{BarChart, BarGroup, BarMode, BarOrientation, LabelPosition};
146pub use boxplot::{BoxOrientation, BoxPlot, BoxStats};
147pub use bubble::{BubbleChart, BubbleSeries};
148pub use bullet::{Bullet, BulletItem};
149pub use calendar::CalendarHeatmap;
150pub use candle::{
151 BandOverlay, Candle, CandleStyle, CandlestickChart, HLine, IndicatorPane, Overlay, TradeMarker,
152 TradeSide, ema, heikin_ashi, sma,
153};
154pub use canvas::{Canvas, Marker, OverlapPolicy};
155pub use crosshair::{Crosshair, NearestPoint, nearest_point};
156pub use depth::DepthChart;
157pub use effects::{Fx, FxExt, bracket_block, chamfer_block, glow, scanlines};
158pub use funnel::{Funnel, FunnelStage};
159pub use gantt::{Gantt, Task};
160pub use gauge::{Gauge, GaugeOrientation, GaugeStyle};
161pub use heatmap::{Gradient, Heatmap};
162pub use histogram::{Bin, Histogram};
163pub use indicator::{atr, bollinger, macd, rsi, stochastic, vwap};
164pub use instruments::{
165 Blip, DotMatrix, HexMap, Oscilloscope, RadarScope, Spectrum, blip_alpha, ecg_beat, hex_center,
166 hex_extent, jitter,
167};
168pub use interact::{Click, Hit, HitKind, HitMap, Interaction, View};
169pub use line::{LegendPosition, LineChart, LineMode, Series};
170pub use lollipop::{LollipopChart, LollipopItem};
171pub use paired_bars::{PairedBars, PairedBarsItem, PairedBarsLayout};
172pub use radar::{RadarChart, RadarSeries};
173pub use radial::{RadialBar, RadialRing, RadialSweep, RingStack};
174pub use renko::{RenkoBrick, RenkoChart, RenkoStyle, renko_bricks};
175pub use rose::{RoseChart, RoseItem};
176pub use sankey::{Sankey, SankeyLink};
177pub use slope::{SlopeChart, SlopeItem};
178pub use sparkline::{Sparkline, SparklineStyle};
179pub use stack::{ChartStack, StackDirection, shared_gutter};
180pub use stacked_area::{AreaLayer, StackedAreaChart};
181pub use stream::{RingBuffer, Stream};
182#[cfg(feature = "theme")]
183pub use theme::{ChartThemeExt, Theme};
184pub use timeaxis::{
185 civil_from_days, date_md, date_ymd, datetime, days_from_civil, time_hm, time_hms,
186 time_nice_step, time_ticks,
187};
188pub use treemap::{Treemap, TreemapItem};
189#[cfg(feature = "theme")]
190pub use tui_nuances::{Status, ThemeProvider, ThemeSwitcher, Themed};
191pub use violin::{ViolinChart, ViolinGroup};
192pub use waterfall::Waterfall;
193
194use ratatui::style::Color;
195
196/// Default series color cycle.
197pub const PALETTE: [Color; 8] = [
198 Color::Cyan,
199 Color::Yellow,
200 Color::Magenta,
201 Color::Green,
202 Color::LightRed,
203 Color::LightBlue,
204 Color::LightGreen,
205 Color::White,
206];
207
208#[cfg(test)]
209mod tests {
210 use super::*;
211
212 #[test]
213 fn palette_not_empty() {
214 assert_eq!(PALETTE.len(), 8);
215 }
216}