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