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//! - [`Funnel`], [`Bullet`], [`PairedBars`], [`BubbleChart`], [`LollipopChart`], [`SlopeChart`], [`CalendarHeatmap`], [`ViolinChart`], [`VoronoiChart`], [`VoronoiTreemap`], [`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 funnel;
107mod gantt;
108mod gauge;
109mod gauge_kit;
110mod heatmap;
111mod histogram;
112mod indicator;
113mod instruments;
114mod interact;
115mod layout;
116mod line;
117mod lollipop;
118mod paired_bars;
119#[cfg(feature = "piechart")]
120mod pie;
121mod radar;
122mod radial;
123mod renko;
124mod rose;
125mod sankey;
126mod scale;
127mod slope;
128mod sparkline;
129mod stack;
130mod stacked_area;
131mod stream;
132mod text;
133mod theme;
134mod timeaxis;
135mod treemap;
136mod violin;
137mod voronoi;
138mod voronoi_treemap;
139mod waterfall;
140
141pub mod util;
142
143pub use accessibility::{Monochrome, no_color_requested, strip_color};
144pub use annotation::{Annotation, AnnotationKind, draw_annotations};
145pub use axis::{Axis, GridStyle};
146pub use bar::{BarChart, BarGroup, BarMode, BarOrientation, LabelPosition};
147pub use boxplot::{BoxOrientation, BoxPlot, BoxStats};
148pub use bubble::{BubbleChart, BubbleSeries};
149pub use bullet::{Bullet, BulletItem};
150pub use calendar::CalendarHeatmap;
151pub use candle::{
152    BandOverlay, Candle, CandleStyle, CandlestickChart, HLine, IndicatorPane, Overlay, TradeMarker,
153    TradeSide, ema, heikin_ashi, sma,
154};
155pub use canvas::{Canvas, Marker, OverlapPolicy};
156pub use crosshair::{Crosshair, NearestPoint, nearest_point};
157pub use depth::DepthChart;
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 voronoi::{VoronoiChart, VoronoiMetric, VoronoiSite, voronoi_stipple};
193pub use voronoi_treemap::{VoronoiOutline, VoronoiTreemap};
194pub use waterfall::Waterfall;
195
196use ratatui::style::Color;
197
198/// Default series color cycle.
199pub const PALETTE: [Color; 8] = [
200    Color::Cyan,
201    Color::Yellow,
202    Color::Magenta,
203    Color::Green,
204    Color::LightRed,
205    Color::LightBlue,
206    Color::LightGreen,
207    Color::White,
208];
209
210#[cfg(test)]
211mod tests {
212    use super::*;
213
214    #[test]
215    fn palette_not_empty() {
216        assert_eq!(PALETTE.len(), 8);
217    }
218}