tui_realm_stdlib/components/
chart.rs

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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//! ## Chart
//!
//! A component to plot one or more dataset in a cartesian coordinate system

use tuirealm::command::{Cmd, CmdResult, Direction, Position};
use tuirealm::props::{
    Alignment, AttrValue, Attribute, Borders, Color, Dataset, PropPayload, PropValue, Props, Style,
};
use tuirealm::ratatui::text::Line;
use tuirealm::ratatui::{
    layout::Rect,
    text::Span,
    widgets::{Axis, Chart as TuiChart, Dataset as TuiDataset},
};
use tuirealm::{Frame, MockComponent, State};

// -- Props
use super::props::{
    CHART_X_BOUNDS, CHART_X_LABELS, CHART_X_STYLE, CHART_X_TITLE, CHART_Y_BOUNDS, CHART_Y_LABELS,
    CHART_Y_STYLE, CHART_Y_TITLE,
};

/// ### ChartStates
///
/// chart states
#[derive(Default)]
pub struct ChartStates {
    pub cursor: usize,
    pub data: Vec<Dataset>,
}

impl ChartStates {
    /// ### move_cursor_left
    ///
    /// Move cursor to the left
    pub fn move_cursor_left(&mut self) {
        if self.cursor > 0 {
            self.cursor -= 1;
        }
    }

    /// ### move_cursor_right
    ///
    /// Move cursor to the right
    pub fn move_cursor_right(&mut self, data_len: usize) {
        if data_len > 0 && self.cursor + 1 < data_len {
            self.cursor += 1;
        }
    }

    /// ### reset_cursor
    ///
    /// Reset cursor to 0
    pub fn reset_cursor(&mut self) {
        self.cursor = 0;
    }

    /// ### cursor_at_end
    ///
    /// Move cursor to the end of the chart
    pub fn cursor_at_end(&mut self, data_len: usize) {
        if data_len > 0 {
            self.cursor = data_len - 1;
        } else {
            self.cursor = 0;
        }
    }
}

// -- component

/// ### Chart
///
/// A component to display a chart on a cartesian coordinate system.
/// The chart can work both in "active" and "disabled" mode.
///
/// #### Disabled mode
///
/// When in disabled mode, the chart won't be interactive, so you won't be able to move through data using keys.
/// If you have more data than the maximum amount of bars that can be displayed, you'll have to update data to display the remaining entries
///
/// #### Active mode
///
/// While in active mode (default) you can put as many entries as you wish. You can move with arrows and END/HOME keys
#[derive(Default)]
pub struct Chart {
    props: Props,
    pub states: ChartStates,
}

impl Chart {
    pub fn foreground(mut self, fg: Color) -> Self {
        self.props.set(Attribute::Foreground, AttrValue::Color(fg));
        self
    }

    pub fn background(mut self, bg: Color) -> Self {
        self.props.set(Attribute::Background, AttrValue::Color(bg));
        self
    }

    pub fn borders(mut self, b: Borders) -> Self {
        self.props.set(Attribute::Borders, AttrValue::Borders(b));
        self
    }

    pub fn title<S: AsRef<str>>(mut self, t: S, a: Alignment) -> Self {
        self.props.set(
            Attribute::Title,
            AttrValue::Title((t.as_ref().to_string(), a)),
        );
        self
    }

    pub fn disabled(mut self, disabled: bool) -> Self {
        self.attr(Attribute::Disabled, AttrValue::Flag(disabled));
        self
    }

    pub fn inactive(mut self, s: Style) -> Self {
        self.props.set(Attribute::FocusStyle, AttrValue::Style(s));
        self
    }

    pub fn data(mut self, data: &[Dataset]) -> Self {
        self.props.set(
            Attribute::Dataset,
            AttrValue::Payload(PropPayload::Vec(
                data.iter().cloned().map(PropValue::Dataset).collect(),
            )),
        );
        self
    }

    pub fn x_bounds(mut self, bounds: (f64, f64)) -> Self {
        self.props.set(
            Attribute::Custom(CHART_X_BOUNDS),
            AttrValue::Payload(PropPayload::Tup2((
                PropValue::F64(bounds.0),
                PropValue::F64(bounds.1),
            ))),
        );
        self
    }

    pub fn y_bounds(mut self, bounds: (f64, f64)) -> Self {
        self.props.set(
            Attribute::Custom(CHART_Y_BOUNDS),
            AttrValue::Payload(PropPayload::Tup2((
                PropValue::F64(bounds.0),
                PropValue::F64(bounds.1),
            ))),
        );
        self
    }

    pub fn x_labels(mut self, labels: &[&str]) -> Self {
        self.attr(
            Attribute::Custom(CHART_X_LABELS),
            AttrValue::Payload(PropPayload::Vec(
                labels
                    .iter()
                    .map(|x| PropValue::Str(x.to_string()))
                    .collect(),
            )),
        );
        self
    }

    pub fn y_labels(mut self, labels: &[&str]) -> Self {
        self.attr(
            Attribute::Custom(CHART_Y_LABELS),
            AttrValue::Payload(PropPayload::Vec(
                labels
                    .iter()
                    .map(|x| PropValue::Str(x.to_string()))
                    .collect(),
            )),
        );
        self
    }

    pub fn x_style(mut self, s: Style) -> Self {
        self.attr(Attribute::Custom(CHART_X_STYLE), AttrValue::Style(s));
        self
    }

    pub fn y_style(mut self, s: Style) -> Self {
        self.attr(Attribute::Custom(CHART_Y_STYLE), AttrValue::Style(s));
        self
    }

    pub fn x_title<S: AsRef<str>>(mut self, t: S) -> Self {
        self.props.set(
            Attribute::Custom(CHART_X_TITLE),
            AttrValue::String(t.as_ref().to_string()),
        );
        self
    }

    pub fn y_title<S: AsRef<str>>(mut self, t: S) -> Self {
        self.props.set(
            Attribute::Custom(CHART_Y_TITLE),
            AttrValue::String(t.as_ref().to_string()),
        );
        self
    }

    fn is_disabled(&self) -> bool {
        self.props
            .get_or(Attribute::Disabled, AttrValue::Flag(false))
            .unwrap_flag()
    }

    /// ### max_dataset_len
    ///
    /// Get the maximum len among the datasets
    fn max_dataset_len(&self) -> usize {
        self.props
            .get(Attribute::Dataset)
            .map(|x| {
                x.unwrap_payload()
                    .unwrap_vec()
                    .iter()
                    .cloned()
                    .map(|x| x.unwrap_dataset().get_data().len())
                    .max()
            })
            .unwrap_or(None)
            .unwrap_or(0)
    }

    /// ### data
    ///
    /// Get data to be displayed, starting from provided index at `start` with a max length of `len`
    fn get_data(&mut self, start: usize, len: usize) -> Vec<TuiDataset> {
        self.states.data = self
            .props
            .get(Attribute::Dataset)
            .map(|x| {
                x.unwrap_payload()
                    .unwrap_vec()
                    .into_iter()
                    .map(|x| x.unwrap_dataset())
                    .collect()
            })
            .unwrap_or_default();
        self.states
            .data
            .iter()
            .map(|x| Self::get_tui_dataset(x, start, len))
            .collect()
    }
}

impl<'a> Chart {
    /// ### get_tui_dataset
    ///
    /// Create tui_dataset from dataset
    /// Only elements from `start` to `len` are preserved from dataset
    fn get_tui_dataset(dataset: &'a Dataset, start: usize, len: usize) -> TuiDataset<'a> {
        // Recalc len
        let points = dataset.get_data();
        let end: usize = match points.len() > start {
            true => std::cmp::min(len, points.len() - start),
            false => 0,
        };

        // Prepare data storage
        TuiDataset::default()
            .name(dataset.name.clone())
            .marker(dataset.marker)
            .graph_type(dataset.graph_type)
            .style(dataset.style)
            .data(&points[start..end])
    }
}

impl MockComponent for Chart {
    fn view(&mut self, render: &mut Frame, area: Rect) {
        if self.props.get_or(Attribute::Display, AttrValue::Flag(true)) == AttrValue::Flag(true) {
            let foreground = self
                .props
                .get_or(Attribute::Foreground, AttrValue::Color(Color::Reset))
                .unwrap_color();
            let background = self
                .props
                .get_or(Attribute::Background, AttrValue::Color(Color::Reset))
                .unwrap_color();
            let borders = self
                .props
                .get_or(Attribute::Borders, AttrValue::Borders(Borders::default()))
                .unwrap_borders();
            let title = self.props.get(Attribute::Title).map(|x| x.unwrap_title());
            let focus = self
                .props
                .get_or(Attribute::Focus, AttrValue::Flag(false))
                .unwrap_flag();
            let inactive_style = self
                .props
                .get(Attribute::FocusStyle)
                .map(|x| x.unwrap_style());
            let active: bool = match self.is_disabled() {
                true => true,
                false => focus,
            };
            let div = crate::utils::get_block(borders, title, active, inactive_style);
            // Create widget
            // -- x axis
            let mut x_axis: Axis = Axis::default();
            if let Some((PropValue::F64(floor), PropValue::F64(ceil))) = self
                .props
                .get(Attribute::Custom(CHART_X_BOUNDS))
                .map(|x| x.unwrap_payload().unwrap_tup2())
            {
                let why_using_vecs_when_you_can_use_useless_arrays: [f64; 2] = [floor, ceil];
                x_axis = x_axis.bounds(why_using_vecs_when_you_can_use_useless_arrays);
            }
            if let Some(PropPayload::Vec(labels)) = self
                .props
                .get(Attribute::Custom(CHART_X_LABELS))
                .map(|x| x.unwrap_payload())
            {
                x_axis = x_axis.labels(labels.iter().cloned().map(|x| Line::from(x.unwrap_str())));
            }
            if let Some(s) = self
                .props
                .get(Attribute::Custom(CHART_X_STYLE))
                .map(|x| x.unwrap_style())
            {
                x_axis = x_axis.style(s);
            }
            if let Some(title) = self
                .props
                .get(Attribute::Custom(CHART_X_TITLE))
                .map(|x| x.unwrap_string())
            {
                x_axis = x_axis.title(Span::styled(
                    title,
                    Style::default().fg(foreground).bg(background),
                ));
            }
            // -- y axis
            let mut y_axis: Axis = Axis::default();
            if let Some((PropValue::F64(floor), PropValue::F64(ceil))) = self
                .props
                .get(Attribute::Custom(CHART_Y_BOUNDS))
                .map(|x| x.unwrap_payload().unwrap_tup2())
            {
                let why_using_vecs_when_you_can_use_useless_arrays: [f64; 2] = [floor, ceil];
                y_axis = y_axis.bounds(why_using_vecs_when_you_can_use_useless_arrays);
            }
            if let Some(PropPayload::Vec(labels)) = self
                .props
                .get(Attribute::Custom(CHART_Y_LABELS))
                .map(|x| x.unwrap_payload())
            {
                y_axis = y_axis.labels(labels.iter().cloned().map(|x| Line::from(x.unwrap_str())));
            }
            if let Some(s) = self
                .props
                .get(Attribute::Custom(CHART_Y_STYLE))
                .map(|x| x.unwrap_style())
            {
                y_axis = y_axis.style(s);
            }
            if let Some(title) = self
                .props
                .get(Attribute::Custom(CHART_Y_TITLE))
                .map(|x| x.unwrap_string())
            {
                y_axis = y_axis.title(Span::styled(
                    title,
                    Style::default().fg(foreground).bg(background),
                ));
            }
            // Get data
            let data: Vec<TuiDataset> = self.get_data(self.states.cursor, area.width as usize);
            // Build widget
            let widget: TuiChart = TuiChart::new(data).block(div).x_axis(x_axis).y_axis(y_axis);
            // Render
            render.render_widget(widget, area);
        }
    }

    fn query(&self, attr: Attribute) -> Option<AttrValue> {
        self.props.get(attr)
    }

    fn attr(&mut self, attr: Attribute, value: AttrValue) {
        self.props.set(attr, value);
        self.states.reset_cursor();
    }

    fn perform(&mut self, cmd: Cmd) -> CmdResult {
        if !self.is_disabled() {
            match cmd {
                Cmd::Move(Direction::Left) => {
                    self.states.move_cursor_left();
                }
                Cmd::Move(Direction::Right) => {
                    self.states.move_cursor_right(self.max_dataset_len());
                }
                Cmd::GoTo(Position::Begin) => {
                    self.states.reset_cursor();
                }
                Cmd::GoTo(Position::End) => {
                    self.states.cursor_at_end(self.max_dataset_len());
                }
                _ => {}
            }
        }
        CmdResult::None
    }

    fn state(&self) -> State {
        State::None
    }
}

#[cfg(test)]
mod test {

    use super::*;

    use pretty_assertions::assert_eq;
    use tuirealm::ratatui::{symbols::Marker, widgets::GraphType};

    #[test]
    fn test_components_chart_states() {
        let mut states: ChartStates = ChartStates::default();
        assert_eq!(states.cursor, 0);
        // Incr
        states.move_cursor_right(2);
        assert_eq!(states.cursor, 1);
        // At end
        states.move_cursor_right(2);
        assert_eq!(states.cursor, 1);
        // Decr
        states.move_cursor_left();
        assert_eq!(states.cursor, 0);
        // At begin
        states.move_cursor_left();
        assert_eq!(states.cursor, 0);
        // Move at end
        states.cursor_at_end(3);
        assert_eq!(states.cursor, 2);
        states.reset_cursor();
        assert_eq!(states.cursor, 0);
    }

    #[test]
    fn test_components_chart() {
        let mut component: Chart = Chart::default()
            .disabled(false)
            .background(Color::Reset)
            .foreground(Color::Reset)
            .borders(Borders::default())
            .title("average temperatures in Udine", Alignment::Center)
            .x_bounds((0.0, 11.0))
            .x_labels(&[
                "january",
                "february",
                "march",
                "april",
                "may",
                "june",
                "july",
                "august",
                "september",
                "october",
                "november",
                "december",
            ])
            .x_style(Style::default().fg(Color::LightBlue))
            .x_title("Temperature (°C)")
            .y_bounds((-5.0, 35.0))
            .y_labels(&["-5", "0", "5", "10", "15", "20", "25", "30", "35"])
            .y_style(Style::default().fg(Color::LightYellow))
            .y_title("Month")
            .data(&[
                Dataset::default()
                    .name("Minimum")
                    .graph_type(GraphType::Scatter)
                    .marker(Marker::Braille)
                    .style(Style::default().fg(Color::Cyan))
                    .data(vec![
                        (0.0, -1.0),
                        (1.0, 1.0),
                        (2.0, 3.0),
                        (3.0, 7.0),
                        (4.0, 11.0),
                        (5.0, 15.0),
                        (6.0, 17.0),
                        (7.0, 17.0),
                        (8.0, 13.0),
                        (9.0, 9.0),
                        (10.0, 4.0),
                        (11.0, 0.0),
                    ]),
                Dataset::default()
                    .name("Maximum")
                    .graph_type(GraphType::Line)
                    .marker(Marker::Dot)
                    .style(Style::default().fg(Color::LightRed))
                    .data(vec![
                        (0.0, 7.0),
                        (1.0, 9.0),
                        (2.0, 13.0),
                        (3.0, 17.0),
                        (4.0, 22.0),
                        (5.0, 25.0),
                        (6.0, 28.0),
                        (7.0, 28.0),
                        (8.0, 24.0),
                        (9.0, 19.0),
                        (10.0, 13.0),
                        (11.0, 8.0),
                    ]),
            ]);
        // Commands
        assert_eq!(component.state(), State::None);
        // -> Right
        assert_eq!(
            component.perform(Cmd::Move(Direction::Right)),
            CmdResult::None
        );
        assert_eq!(component.states.cursor, 1);
        // <- Left
        assert_eq!(
            component.perform(Cmd::Move(Direction::Left)),
            CmdResult::None
        );
        assert_eq!(component.states.cursor, 0);
        // End
        assert_eq!(component.perform(Cmd::GoTo(Position::End)), CmdResult::None);
        assert_eq!(component.states.cursor, 11);
        // Home
        assert_eq!(
            component.perform(Cmd::GoTo(Position::Begin)),
            CmdResult::None
        );
        assert_eq!(component.states.cursor, 0);
        // component funcs
        assert_eq!(component.max_dataset_len(), 12);
        assert_eq!(component.is_disabled(), false);
        assert_eq!(component.get_data(2, 4).len(), 2);

        let mut comp = Chart::default().data(&[Dataset::default()
            .name("Maximum")
            .graph_type(GraphType::Line)
            .marker(Marker::Dot)
            .style(Style::default().fg(Color::LightRed))
            .data(vec![(0.0, 7.0)])]);
        assert!(comp.get_data(0, 1).len() > 0);

        // Update and test empty data
        component.states.cursor_at_end(12);
        component.attr(
            Attribute::Dataset,
            AttrValue::Payload(PropPayload::Vec(vec![])),
        );
        assert_eq!(component.max_dataset_len(), 0);
        // Cursor is reset
        assert_eq!(component.states.cursor, 0);
    }
}