revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
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
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
//! Box Plot widget for statistical distribution visualization
//!
//! Displays median, quartiles, and outliers for comparing distributions across categories.

use super::chart_common::{Axis, ChartGrid, ChartOrientation, ColorScheme, Legend};
use super::chart_render::{fill_background, render_title};
use crate::layout::Rect;
use crate::style::Color;
use crate::widget::traits::{RenderContext, View, WidgetProps};
use crate::{impl_props_builders, impl_styled_view};

mod group;
mod render;
mod types;

// Public API tests extracted to tests/widget/data/chart_boxplot.rs
// KEEP HERE - Render tests require access to private RenderContext
#[cfg(test)]
mod tests {
    //! Tests for boxplot module

    use super::super::chart_stats::percentile;
    use super::*;

    #[test]
    fn test_percentile() {
        let sorted = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        assert_eq!(percentile(&sorted, 0.0), 1.0);
        assert_eq!(percentile(&sorted, 50.0), 3.0);
        assert_eq!(percentile(&sorted, 100.0), 5.0);
    }

    // ========== Render Tests - KEEP HERE (access private RenderContext) ==========

    #[test]
    fn test_boxplot_render_basic() {
        use crate::layout::Rect;
        use crate::render::Buffer;
        use crate::widget::traits::RenderContext;

        let data: Vec<f64> = (0..50).map(|x| x as f64).collect();
        let mut buffer = Buffer::new(40, 20);
        let area = Rect::new(0, 0, 40, 20);
        let mut ctx = RenderContext::new(&mut buffer, area);

        let bp = BoxPlot::new().group("Data", &data);
        bp.render(&mut ctx);

        // Verify box elements are rendered
        let mut has_box = false;
        for y in 0..20 {
            for x in 0..40 {
                if let Some(cell) = buffer.get(x, y) {
                    if cell.symbol == ''
                        || cell.symbol == ''
                        || cell.symbol == ''
                        || cell.symbol == ''
                        || cell.symbol == ''
                        || cell.symbol == ''
                    {
                        has_box = true;
                        break;
                    }
                }
            }
        }
        assert!(has_box);
    }

    #[test]
    fn test_boxplot_render_multiple_groups() {
        use crate::layout::Rect;
        use crate::render::Buffer;
        use crate::widget::traits::RenderContext;

        let mut buffer = Buffer::new(60, 25);
        let area = Rect::new(0, 0, 60, 25);
        let mut ctx = RenderContext::new(&mut buffer, area);

        let bp = BoxPlot::new()
            .group("Group A", &[1.0, 2.0, 3.0, 4.0, 5.0])
            .group("Group B", &[3.0, 4.0, 5.0, 6.0, 7.0])
            .group("Group C", &[5.0, 6.0, 7.0, 8.0, 9.0]);

        bp.render(&mut ctx);

        // Should render without panic
        let mut has_content = false;
        for y in 0..25 {
            for x in 0..60 {
                if let Some(cell) = buffer.get(x, y) {
                    if cell.symbol != ' ' {
                        has_content = true;
                        break;
                    }
                }
            }
        }
        assert!(has_content);
    }

    #[test]
    fn test_boxplot_render_with_title() {
        use crate::layout::Rect;
        use crate::render::Buffer;
        use crate::widget::traits::RenderContext;

        let mut buffer = Buffer::new(40, 20);
        let area = Rect::new(0, 0, 40, 20);
        let mut ctx = RenderContext::new(&mut buffer, area);

        let bp = BoxPlot::new()
            .title("Test BoxPlot")
            .group("D", &[1.0, 2.0, 3.0, 4.0, 5.0]);

        bp.render(&mut ctx);

        // Title should be rendered
        let mut title_found = false;
        for x in 0..40 {
            if let Some(cell) = buffer.get(x, 0) {
                if cell.symbol == 'T' {
                    title_found = true;
                    break;
                }
            }
        }
        assert!(title_found);
    }

    #[test]
    fn test_boxplot_render_with_outliers() {
        use crate::layout::Rect;
        use crate::render::Buffer;
        use crate::widget::traits::RenderContext;

        let mut buffer = Buffer::new(40, 20);
        let area = Rect::new(0, 0, 40, 20);
        let mut ctx = RenderContext::new(&mut buffer, area);

        // Data with outliers
        let mut data: Vec<f64> = (0..20).map(|x| x as f64).collect();
        data.push(100.0); // Outlier
        data.push(-50.0); // Outlier

        let bp = BoxPlot::new()
            .group("Data", &data)
            .show_outliers(true)
            .whisker_style(WhiskerStyle::IQR);

        bp.render(&mut ctx);

        // Should render without panic and show outliers
        let mut has_outliers = false;
        for y in 0..20 {
            for x in 0..40 {
                if let Some(cell) = buffer.get(x, y) {
                    if cell.symbol == '' {
                        has_outliers = true;
                        break;
                    }
                }
            }
        }
        assert!(has_outliers);
    }

    #[test]
    fn test_boxplot_render_horizontal() {
        use crate::layout::Rect;
        use crate::render::Buffer;
        use crate::widget::traits::RenderContext;

        let mut buffer = Buffer::new(40, 20);
        let area = Rect::new(0, 0, 40, 20);
        let mut ctx = RenderContext::new(&mut buffer, area);

        let bp = BoxPlot::new()
            .group("Data", &[1.0, 2.0, 3.0, 4.0, 5.0])
            .horizontal();

        bp.render(&mut ctx);

        // Should render without panic
        let mut has_content = false;
        for y in 0..20 {
            for x in 0..40 {
                if let Some(cell) = buffer.get(x, y) {
                    if cell.symbol != ' ' {
                        has_content = true;
                        break;
                    }
                }
            }
        }
        assert!(has_content);
    }

    #[test]
    fn test_boxplot_render_small_area() {
        use crate::layout::Rect;
        use crate::render::Buffer;
        use crate::widget::traits::RenderContext;

        let mut buffer = Buffer::new(10, 5);
        let area = Rect::new(0, 0, 10, 5);
        let mut ctx = RenderContext::new(&mut buffer, area);

        let bp = BoxPlot::new().group("D", &[1.0, 2.0, 3.0]);

        // Should not panic on small area
        bp.render(&mut ctx);
    }

    #[test]
    fn test_boxplot_render_empty() {
        use crate::layout::Rect;
        use crate::render::Buffer;
        use crate::widget::traits::RenderContext;

        let mut buffer = Buffer::new(30, 15);
        let area = Rect::new(0, 0, 30, 15);
        let mut ctx = RenderContext::new(&mut buffer, area);

        // Empty boxplot
        let bp = BoxPlot::new();
        bp.render(&mut ctx);
    }

    #[test]
    fn test_boxplot_render_notched() {
        use crate::layout::Rect;
        use crate::render::Buffer;
        use crate::widget::traits::RenderContext;

        let mut buffer = Buffer::new(40, 20);
        let area = Rect::new(0, 0, 40, 20);
        let mut ctx = RenderContext::new(&mut buffer, area);

        let data: Vec<f64> = (0..30).map(|x| x as f64).collect();
        let bp = BoxPlot::new().group("Data", &data).notched(true);

        bp.render(&mut ctx);

        // Should render without panic
        let mut has_content = false;
        for y in 0..20 {
            for x in 0..40 {
                if let Some(cell) = buffer.get(x, y) {
                    if cell.symbol != ' ' {
                        has_content = true;
                        break;
                    }
                }
            }
        }
        assert!(has_content);
    }

    #[test]
    fn test_boxplot_render_minmax_whiskers() {
        use crate::layout::Rect;
        use crate::render::Buffer;
        use crate::widget::traits::RenderContext;

        let mut buffer = Buffer::new(40, 20);
        let area = Rect::new(0, 0, 40, 20);
        let mut ctx = RenderContext::new(&mut buffer, area);

        let data: Vec<f64> = (0..20).map(|x| x as f64).collect();
        let bp = BoxPlot::new()
            .group("Data", &data)
            .whisker_style(WhiskerStyle::MinMax);

        bp.render(&mut ctx);

        // Should render without panic
        let mut has_content = false;
        for y in 0..20 {
            for x in 0..40 {
                if let Some(cell) = buffer.get(x, y) {
                    if cell.symbol != ' ' {
                        has_content = true;
                        break;
                    }
                }
            }
        }
        assert!(has_content);
    }
}

pub use group::BoxGroup;
pub use types::{BoxStats, WhiskerStyle};

/// Box plot widget
pub struct BoxPlot {
    /// Box groups
    groups: Vec<BoxGroup>,
    /// Orientation
    orientation: ChartOrientation,
    /// Value axis configuration
    value_axis: Axis,
    /// Category axis configuration
    category_axis: Axis,
    /// Legend configuration
    legend: Legend,
    /// Grid configuration
    grid: ChartGrid,
    /// Color palette
    colors: ColorScheme,
    /// Show outliers
    show_outliers: bool,
    /// Notched box plot
    notched: bool,
    /// Whisker calculation style
    whisker_style: WhiskerStyle,
    /// Box width (0.0-1.0)
    box_width: f64,
    /// Chart title
    title: Option<String>,
    /// Background color
    bg_color: Option<Color>,
    /// Widget properties
    props: WidgetProps,
}

impl Default for BoxPlot {
    fn default() -> Self {
        Self::new()
    }
}

impl BoxPlot {
    /// Create a new box plot
    pub fn new() -> Self {
        Self {
            groups: Vec::new(),
            orientation: ChartOrientation::Vertical,
            value_axis: Axis::default(),
            category_axis: Axis::default(),
            legend: Legend::none(),
            grid: ChartGrid::new().y(true),
            colors: ColorScheme::default_palette(),
            show_outliers: true,
            notched: false,
            whisker_style: WhiskerStyle::IQR,
            box_width: 0.6,
            title: None,
            bg_color: None,
            props: WidgetProps::new(),
        }
    }

    /// Add a group from raw data
    pub fn group(mut self, label: impl Into<String>, data: &[f64]) -> Self {
        self.groups.push(BoxGroup::new(label, data));
        self
    }

    /// Add a group with pre-computed stats
    pub fn group_stats(mut self, label: impl Into<String>, stats: BoxStats) -> Self {
        self.groups.push(BoxGroup::from_stats(label, stats));
        self
    }

    /// Add a box group
    pub fn add_group(mut self, group: BoxGroup) -> Self {
        self.groups.push(group);
        self
    }

    /// Set orientation
    pub fn orientation(mut self, orientation: ChartOrientation) -> Self {
        self.orientation = orientation;
        self
    }

    /// Set horizontal orientation
    pub fn horizontal(mut self) -> Self {
        self.orientation = ChartOrientation::Horizontal;
        self
    }

    /// Set vertical orientation
    pub fn vertical(mut self) -> Self {
        self.orientation = ChartOrientation::Vertical;
        self
    }

    /// Set value axis configuration
    pub fn value_axis(mut self, axis: Axis) -> Self {
        self.value_axis = axis;
        self
    }

    /// Set category axis configuration
    pub fn category_axis(mut self, axis: Axis) -> Self {
        self.category_axis = axis;
        self
    }

    /// Set legend configuration
    pub fn legend(mut self, legend: Legend) -> Self {
        self.legend = legend;
        self
    }

    /// Set grid configuration
    pub fn grid(mut self, grid: ChartGrid) -> Self {
        self.grid = grid;
        self
    }

    /// Set color palette
    pub fn colors(mut self, colors: ColorScheme) -> Self {
        self.colors = colors;
        self
    }

    /// Show/hide outliers
    pub fn show_outliers(mut self, show: bool) -> Self {
        self.show_outliers = show;
        self
    }

    /// Enable notched box plot
    pub fn notched(mut self, enabled: bool) -> Self {
        self.notched = enabled;
        self
    }

    /// Set whisker style
    pub fn whisker_style(mut self, style: WhiskerStyle) -> Self {
        self.whisker_style = style;
        self
    }

    /// Set box width (0.0-1.0)
    pub fn box_width(mut self, width: f64) -> Self {
        self.box_width = width.clamp(0.1, 1.0);
        self
    }

    /// Set chart title
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Set background color
    pub fn bg(mut self, color: Color) -> Self {
        self.bg_color = Some(color);
        self
    }

    /// Compute value bounds
    fn compute_bounds(&self) -> (f64, f64) {
        let mut min = f64::INFINITY;
        let mut max = f64::NEG_INFINITY;

        for group in &self.groups {
            if let Some(stats) = group.get_stats(self.whisker_style) {
                min = min.min(stats.whisker_low);
                max = max.max(stats.whisker_high);
                for &outlier in &stats.outliers {
                    min = min.min(outlier);
                    max = max.max(outlier);
                }
            }
        }

        // Apply axis overrides
        let min = self.value_axis.min.unwrap_or(min);
        let max = self.value_axis.max.unwrap_or(max);

        // Add padding
        let range = (max - min).max(1.0);
        let padding = range * 0.1;

        (min - padding, max + padding)
    }
}

impl View for BoxPlot {
    crate::impl_view_meta!("BoxPlot");

    fn render(&self, ctx: &mut RenderContext) {
        let area = ctx.area;

        if area.width < 10 || area.height < 8 {
            return;
        }

        // Use relative area (0,0 origin) for shared functions that use ctx.set()
        let rel_area = Rect::new(0, 0, area.width, area.height);

        // Fill background using shared function
        if let Some(bg) = self.bg_color {
            fill_background(ctx, rel_area, bg);
        }

        // Draw title using shared function
        let title_offset = render_title(ctx, rel_area, self.title.as_deref(), Color::WHITE);

        // Calculate chart area (relative coordinates)
        let y_label_width = 6u16;
        let x_label_height = 1u16;

        let chart_area = Rect {
            x: y_label_width,
            y: title_offset,
            width: area.width.saturating_sub(y_label_width + 1),
            height: area
                .height
                .saturating_sub(title_offset + x_label_height + 1),
        };

        if chart_area.width < 5 || chart_area.height < 5 {
            return;
        }

        let bounds = self.compute_bounds();

        // Render using render module
        let render_state = render::BoxPlotRender::new(
            &self.groups,
            bounds,
            chart_area,
            self.box_width,
            self.whisker_style,
            self.show_outliers,
        );

        render_state.render_boxes(ctx, &self.colors);
        render_state.render_axes(ctx, rel_area, &self.value_axis, &self.category_axis);
    }
}

impl_styled_view!(BoxPlot);
impl_props_builders!(BoxPlot);

/// Create a new box plot
pub fn boxplot() -> BoxPlot {
    BoxPlot::new()
}