tui-chart 0.7.1

Terminal chart widgets for ratatui: line, bar, candlestick, histogram, heatmap, gauge, radar, treemap, pie (via tui-piechart) and more, with themes, annotations and streaming
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! Shared helpers for the matrix-style integration tests.
#![allow(dead_code)] // Shared by several test binaries; each uses only part of it.

use ratatui::{buffer::Buffer, layout::Rect, style::Color, widgets::Widget};
use tui_chart::*;

/// Kind of input data fed to every widget.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Data {
    Empty,
    Single,
    Nasty,
    Normal,
}

pub const DATAS: [Data; 4] = [Data::Empty, Data::Single, Data::Nasty, Data::Normal];

/// Areas used by the robustness matrix.
pub const AREAS: [(u16, u16); 8] = [
    (0, 0),
    (1, 1),
    (2, 2),
    (3, 1),
    (1, 40),
    (10, 3),
    (80, 24),
    (300, 80),
];

pub fn vals(d: Data) -> Vec<f64> {
    match d {
        Data::Empty => vec![],
        Data::Single => vec![5.0],
        Data::Nasty => vec![
            f64::NAN,
            f64::INFINITY,
            -1.0,
            1e300,
            f64::NEG_INFINITY,
            -1e300,
            0.0,
            f64::MAX,
            f64::MIN,
            3.0,
        ],
        Data::Normal => (0..30)
            .map(|i| ((i as f64) * 0.4).sin() * 10.0 + 12.0)
            .collect(),
    }
}

pub fn candles(d: Data) -> Vec<Candle> {
    match d {
        Data::Empty => vec![],
        Data::Single => vec![Candle::new(10.0, 12.0, 9.0, 11.0).volume(5.0)],
        Data::Nasty => vec![
            Candle::new(f64::NAN, f64::INFINITY, -1e300, 1e300),
            Candle::new(1.0, 1.0, 1.0, 1.0).volume(f64::NAN),
            Candle::new(-5.0, -2.0, -9.0, -3.0).volume(-4.0),
            Candle::new(f64::MAX, f64::MAX, f64::MIN, 0.0).volume(f64::INFINITY),
        ],
        Data::Normal => (0..40)
            .map(|i| {
                let b = 50.0 + (i as f64 * 0.3).sin() * 8.0;
                Candle::new(b, b + 4.0, b - 3.0, b + if i % 2 == 0 { 2.0 } else { -2.0 })
                    .volume(10.0 * (i + 1) as f64)
            })
            .collect(),
    }
}

fn pairs(d: Data) -> Vec<(f64, f64)> {
    vals(d)
        .into_iter()
        .enumerate()
        .map(|(i, v)| (i as f64, v))
        .collect()
}

/// A render closure that can be invoked repeatedly.
pub type Draw = Box<dyn Fn(Rect, &mut Buffer)>;

fn boxed<W>(w: W) -> Draw
where
    W: Fn() -> Box<dyn FnOnce(Rect, &mut Buffer)> + 'static,
{
    Box::new(move |a, b| w()(a, b))
}

fn mk<W: Widget + 'static>(f: impl Fn() -> W + 'static) -> Draw {
    boxed(move || {
        let w = f();
        Box::new(move |a, b| w.render(a, b))
    })
}

/// Theme type accepted by [`all_widgets`]; uninhabited without the `theme` feature.
#[cfg(feature = "theme")]
pub type Th = Theme;
#[cfg(not(feature = "theme"))]
#[derive(Clone)]
pub enum Th {}

/// Every public widget, built from `d`, optionally themed. Names are stable.
pub fn all_widgets(d: Data, theme: Option<Th>) -> Vec<(&'static str, Draw)> {
    let t = move || theme.clone();
    macro_rules! th {
        ($w:expr, $t:expr) => {{
            let w = $w;
            #[cfg(feature = "theme")]
            let w = match $t {
                Some(t) => w.theme(t),
                None => w,
            };
            #[cfg(not(feature = "theme"))]
            let _ = $t;
            w
        }};
    }
    let mut v: Vec<(&'static str, Draw)> = Vec::new();
    let c = t();
    v.push((
        "LineChart",
        mk(move || {
            th!(
                LineChart::new(vec![
                    Series::new("a").data(pairs(d)),
                    Series::new("b").values(vals(d)),
                ])
                .block(ratatui::widgets::Block::bordered().title("t")),
                c.clone()
            )
        }),
    ));
    let c = t();
    v.push((
        "BarChart",
        mk(move || {
            let groups = if d == Data::Empty {
                vec![]
            } else {
                vec![BarGroup::new("g1", vals(d)), BarGroup::new("g2", vals(d))]
            };
            th!(BarChart::new(groups), c.clone())
        }),
    ));
    let c = t();
    v.push((
        "CandlestickChart",
        mk(move || {
            let cs = candles(d);
            th!(
                CandlestickChart::new(cs.clone())
                    .show_volume(true)
                    .overlay(Overlay::new("SMA", sma(&cs, 3), Color::Yellow))
                    .pane(IndicatorPane::Rsi { period: 5 }),
                c.clone()
            )
        }),
    ));
    let c = t();
    v.push((
        "Histogram",
        mk(move || th!(Histogram::new(vals(d)), c.clone())),
    ));
    let c = t();
    v.push((
        "Heatmap",
        mk(move || {
            let rows = match d {
                Data::Empty => vec![],
                _ => (0..4).map(|_| vals(d)).collect(),
            };
            th!(Heatmap::new(rows), c.clone())
        }),
    ));
    let c = t();
    v.push((
        "Gauge",
        mk(move || {
            th!(
                Gauge::new(vals(d).first().copied().unwrap_or(f64::NAN)),
                c.clone()
            )
        }),
    ));
    let c = t();
    v.push((
        "RadarChart",
        mk(move || {
            let vs = vals(d);
            let axes: Vec<String> = (0..vs.len().max(3)).map(|i| format!("a{i}")).collect();
            let r = RadarChart::new(axes).series(RadarSeries::new("s", vs));
            th!(r, c.clone())
        }),
    ));
    let c = t();
    v.push((
        "Funnel",
        mk(move || {
            let st: Vec<FunnelStage> = vals(d)
                .into_iter()
                .enumerate()
                .map(|(i, x)| FunnelStage::new(format!("s{i}"), x))
                .collect();
            th!(Funnel::new(st), c.clone())
        }),
    ));
    let c = t();
    v.push((
        "Bullet",
        mk(move || {
            let it: Vec<BulletItem> = vals(d)
                .into_iter()
                .enumerate()
                .map(|(i, x)| {
                    BulletItem::new(format!("b{i}"), x)
                        .target(x * 1.1)
                        .ranges([x, x * 2.0])
                })
                .collect();
            th!(Bullet::new(it), c.clone())
        }),
    ));
    let c = t();
    v.push((
        "PairedBars",
        mk(move || {
            let it: Vec<PairedBarsItem> = vals(d)
                .into_iter()
                .enumerate()
                .map(|(i, x)| PairedBarsItem::new(format!("c{i}"), x, x * 0.5).unit("%"))
                .collect();
            th!(PairedBars::new(it).legend("a", "b"), c.clone())
        }),
    ));
    let c = t();
    v.push((
        "BubbleChart",
        mk(move || {
            let pts: Vec<(f64, f64, f64)> = vals(d).into_iter().map(|x| (x, x, x)).collect();
            th!(BubbleChart::new([BubbleSeries::new("a", pts)]), c.clone())
        }),
    ));
    let c = t();
    v.push((
        "LollipopChart",
        mk(move || {
            let it: Vec<LollipopItem> = vals(d)
                .into_iter()
                .enumerate()
                .map(|(i, x)| LollipopItem::new(format!("l{i}"), x).from(x * 0.5))
                .collect();
            th!(LollipopChart::new(it), c.clone())
        }),
    ));
    let c = t();
    v.push((
        "SlopeChart",
        mk(move || {
            let it: Vec<SlopeItem> = vals(d)
                .into_iter()
                .enumerate()
                .map(|(i, x)| SlopeItem::new(format!("s{i}"), x, x * 1.5 - 2.0))
                .collect();
            th!(SlopeChart::new(it).titles("a", "b"), c.clone())
        }),
    ));
    let c = t();
    v.push((
        "CalendarHeatmap",
        mk(move || th!(CalendarHeatmap::new(19_000, vals(d)), c.clone())),
    ));
    let c = t();
    v.push((
        "ViolinChart",
        mk(move || {
            th!(
                ViolinChart::new([
                    ViolinGroup::new("a", vals(d)),
                    ViolinGroup::new("b", vals(d))
                ]),
                c.clone()
            )
        }),
    ));
    let c = t();
    v.push((
        "RoseChart",
        mk(move || {
            let it: Vec<RoseItem> = vals(d)
                .into_iter()
                .enumerate()
                .map(|(i, x)| RoseItem::new(format!("r{i}"), x))
                .collect();
            th!(RoseChart::new(it), c.clone())
        }),
    ));
    let c = t();
    v.push((
        "StackedAreaChart",
        mk(move || {
            th!(
                StackedAreaChart::new([
                    AreaLayer::new("a", vals(d)),
                    AreaLayer::new("b", vals(d).into_iter().rev())
                ]),
                c.clone()
            )
        }),
    ));
    let c = t();
    v.push((
        "Sankey",
        mk(move || {
            let l: Vec<SankeyLink> = vals(d)
                .into_iter()
                .enumerate()
                .map(|(i, x)| {
                    SankeyLink::new(format!("n{}", i % 4), format!("n{}", (i + 1) % 5), x)
                })
                .collect();
            th!(Sankey::new(l), c.clone())
        }),
    ));
    let c = t();
    v.push((
        "Treemap",
        mk(move || {
            let items: Vec<TreemapItem> = vals(d)
                .into_iter()
                .enumerate()
                .map(|(i, x)| TreemapItem::new(format!("i{i}"), x))
                .collect();
            th!(Treemap::new(items), c.clone())
        }),
    ));
    let c = t();
    v.push((
        "VoronoiChart",
        mk(move || {
            let sites: Vec<VoronoiSite> = vals(d)
                .into_iter()
                .enumerate()
                .map(|(i, x)| {
                    VoronoiSite::new(format!("s{i}"), (i * 7 % 11) as f64, x)
                        .value(x)
                        .weight(x)
                })
                .collect();
            th!(
                VoronoiChart::new(sites)
                    .gradient(Gradient::Viridis)
                    .weighted(true)
                    .show_labels(true)
                    .delaunay(true),
                c.clone()
            )
        }),
    ));
    let c = t();
    v.push((
        "VoronoiTreemap",
        mk(move || {
            let items: Vec<TreemapItem> = vals(d)
                .into_iter()
                .enumerate()
                .map(|(i, x)| TreemapItem::new(format!("i{i}"), x))
                .collect();
            th!(VoronoiTreemap::new(items), c.clone())
        }),
    ));
    let c = t();
    v.push((
        "BoxPlot",
        mk(move || {
            let mut b = BoxPlot::new();
            if let Some(s) = BoxStats::from_samples(&vals(d), 1.5) {
                b = b.stats("x", s);
            }
            if d == Data::Nasty {
                b = b.stats(
                    "n",
                    BoxStats::new(f64::NAN, 1.0, f64::INFINITY, -1.0, 1e300),
                );
            }
            th!(b, c.clone())
        }),
    ));
    let c = t();
    v.push((
        "Waterfall",
        mk(move || {
            let mut w = Waterfall::new();
            for (i, x) in vals(d).into_iter().enumerate() {
                w = w.delta(format!("s{i}"), x);
            }
            if d != Data::Empty {
                w = w.total("T");
            }
            th!(w, c.clone())
        }),
    ));
    let c = t();
    v.push((
        "Gantt",
        mk(move || {
            let tasks: Vec<Task> = vals(d)
                .into_iter()
                .enumerate()
                .map(|(i, x)| Task::new(format!("t{i}"), x, x + 3.0).progress(0.5))
                .collect();
            th!(Gantt::new(tasks), c.clone())
        }),
    ));
    let c = t();
    v.push((
        "DepthChart",
        mk(move || {
            let vs = vals(d);
            let bids: Vec<(f64, f64)> = vs.iter().map(|x| (*x, x.abs() + 1.0)).collect();
            let asks: Vec<(f64, f64)> = vs.iter().map(|x| (x + 20.0, x.abs() + 2.0)).collect();
            th!(DepthChart::new(bids, asks), c.clone())
        }),
    ));
    let c = t();
    v.push((
        "RenkoChart",
        mk(move || th!(RenkoChart::new(&vals(d), 1.0), c.clone())),
    ));
    let c = t();
    v.push((
        "RenkoChart(zero brick)",
        mk(move || th!(RenkoChart::new(&vals(d), 0.0), c.clone())),
    ));
    let c = t();
    v.push((
        "Sparkline",
        mk(move || th!(Sparkline::new(vals(d)), c.clone())),
    ));
    v
}

/// Render `draw` into a buffer that is larger than `area`, with a sentinel
/// symbol everywhere; return the buffer.
pub fn render_padded(draw: &Draw, area: Rect) -> (Buffer, Rect) {
    let full = Rect::new(0, 0, area.x + area.width + 3, area.y + area.height + 3);
    let mut buf = Buffer::empty(full);
    for c in buf.content.iter_mut() {
        c.set_symbol("§");
    }
    draw(area, &mut buf);
    (buf, full)
}

/// Assert every cell outside `area` still holds the sentinel.
pub fn assert_no_outside_writes(name: &str, buf: &Buffer, full: Rect, area: Rect) {
    for y in full.y..full.bottom() {
        for x in full.x..full.right() {
            let inside = x >= area.x && x < area.right() && y >= area.y && y < area.bottom();
            if !inside {
                assert_eq!(
                    buf[(x, y)].symbol(),
                    "§",
                    "{name}: write outside area {area:?} at ({x},{y})"
                );
            }
        }
    }
}

pub fn rows(buf: &Buffer, area: Rect) -> Vec<String> {
    (area.y..area.bottom())
        .map(|y| {
            (area.x..area.right())
                .map(|x| buf[(x, y)].symbol())
                .collect()
        })
        .collect()
}