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 layout;
114mod line;
115mod lollipop;
116mod paired_bars;
117mod radar;
118mod radial;
119mod renko;
120mod rose;
121mod sankey;
122mod scale;
123#[cfg(feature = "shader")]
124mod shader;
125mod slope;
126mod sparkline;
127mod stack;
128mod stacked_area;
129mod stream;
130mod text;
131mod theme;
132mod timeaxis;
133mod treemap;
134mod violin;
135mod waterfall;
136
137pub mod util;
138
139pub use accessibility::{no_color_requested, strip_color, Monochrome};
140pub use annotation::{draw_annotations, Annotation, AnnotationKind};
141pub use axis::{Axis, GridStyle};
142pub use bar::{BarChart, BarGroup, BarMode, BarOrientation, LabelPosition};
143pub use boxplot::{BoxOrientation, BoxPlot, BoxStats};
144pub use bubble::{BubbleChart, BubbleSeries};
145pub use bullet::{Bullet, BulletItem};
146pub use calendar::CalendarHeatmap;
147pub use candle::{
148 ema, heikin_ashi, sma, BandOverlay, Candle, CandleStyle, CandlestickChart, HLine,
149 IndicatorPane, Overlay, TradeMarker, TradeSide,
150};
151pub use canvas::{Canvas, Marker, OverlapPolicy};
152pub use crosshair::{nearest_point, Crosshair, NearestPoint};
153pub use depth::DepthChart;
154pub use effects::{bracket_block, chamfer_block, glow, scanlines, Fx};
155pub use funnel::{Funnel, FunnelStage};
156pub use gantt::{Gantt, Task};
157pub use gauge::{Gauge, GaugeOrientation, GaugeStyle};
158pub use heatmap::{Gradient, Heatmap};
159pub use histogram::{Bin, Histogram};
160pub use indicator::{atr, bollinger, macd, rsi, stochastic, vwap};
161pub use instruments::{
162 blip_alpha, ecg_beat, hex_center, hex_extent, jitter, Blip, DotMatrix, HexMap, Oscilloscope,
163 RadarScope, Spectrum,
164};
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};
173#[cfg(feature = "shader")]
174pub use shader::{ShaderBackdrop, ShaderPreset};
175pub use slope::{SlopeChart, SlopeItem};
176pub use sparkline::{Sparkline, SparklineStyle};
177pub use stack::{shared_gutter, ChartStack, StackDirection};
178pub use stacked_area::{AreaLayer, StackedAreaChart};
179pub use stream::{RingBuffer, Stream};
180#[cfg(feature = "theme")]
181pub use theme::{ChartThemeExt, Theme};
182pub use timeaxis::{
183 civil_from_days, date_md, date_ymd, datetime, days_from_civil, time_hm, time_hms,
184 time_nice_step, time_ticks,
185};
186pub use treemap::{Treemap, TreemapItem};
187#[cfg(feature = "theme")]
188pub use tui_nuances::{Status, ThemeProvider, ThemeSwitcher, Themed};
189pub use violin::{ViolinChart, ViolinGroup};
190pub use waterfall::Waterfall;
191
192use ratatui::style::Color;
193
194/// Default series color cycle.
195pub const PALETTE: [Color; 8] = [
196 Color::Cyan,
197 Color::Yellow,
198 Color::Magenta,
199 Color::Green,
200 Color::LightRed,
201 Color::LightBlue,
202 Color::LightGreen,
203 Color::White,
204];
205
206#[cfg(test)]
207mod tests {
208 use super::*;
209
210 #[test]
211 fn palette_not_empty() {
212 assert_eq!(PALETTE.len(), 8);
213 }
214}