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