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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
//! Helper functions for chart widget

use super::chart_common::{Axis, AxisFormat, LegendPosition, Marker};
use super::types::{ChartType, LineStyle, Series};
use crate::layout::Rect;
use crate::render::Cell;
use crate::style::Color;
use crate::utils::{char_width, display_width};
use crate::widget::theme::DARK_GRAY;
use crate::widget::traits::{RenderContext, View, WidgetProps};
use crate::{impl_props_builders, impl_styled_view};

/// Line segment for drawing
pub(super) struct LineSegment {
    x0: u16,
    y0: u16,
    x1: u16,
    y1: u16,
    color: Color,
    style: LineStyle,
}

/// Chart widget
#[derive(Debug)]
pub struct Chart {
    /// Chart title
    title: Option<String>,
    /// Data series
    series: Vec<Series>,
    /// X axis
    x_axis: Axis,
    /// Y axis
    y_axis: Axis,
    /// Legend position
    legend: LegendPosition,
    /// Background color
    bg_color: Option<Color>,
    /// Border color
    border_color: Option<Color>,
    /// Use Braille for higher resolution
    braille_mode: bool,
    /// Tooltip configuration
    tooltip: Option<super::chart_common::ChartTooltip>,
    /// Widget properties
    props: WidgetProps,
}

impl Chart {
    /// Create a new chart
    pub fn new() -> Self {
        Self {
            title: None,
            series: Vec::new(),
            x_axis: Axis::default(),
            y_axis: Axis::default(),
            legend: LegendPosition::TopRight,
            bg_color: None,
            border_color: None,
            braille_mode: false,
            tooltip: None,
            props: WidgetProps::new(),
        }
    }

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

    /// Add a series
    pub fn series(mut self, series: Series) -> Self {
        self.series.push(series);
        self
    }

    /// Add multiple series
    pub fn series_vec(mut self, series: Vec<Series>) -> Self {
        self.series.extend(series);
        self
    }

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

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

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

    /// Hide legend
    pub fn no_legend(mut self) -> Self {
        self.legend = LegendPosition::None;
        self
    }

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

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

    /// Enable Braille mode for higher resolution
    pub fn braille(mut self) -> Self {
        self.braille_mode = true;
        self
    }

    /// Set tooltip configuration
    pub fn tooltip(mut self, tooltip: super::chart_common::ChartTooltip) -> Self {
        self.tooltip = Some(tooltip);
        self
    }

    /// Enable tooltips with default settings
    pub fn with_tooltip(mut self) -> Self {
        self.tooltip = Some(super::chart_common::ChartTooltip::enabled());
        self
    }

    /// Compute data bounds
    ///
    /// Returns (x_min, x_max, y_min, y_max) with safe defaults for edge cases.
    fn compute_bounds(&self) -> (f64, f64, f64, f64) {
        let mut x_min = f64::MAX;
        let mut x_max = f64::MIN;
        let mut y_min = f64::MAX;
        let mut y_max = f64::MIN;
        let mut has_data = false;

        for series in &self.series {
            for &(x, y) in &series.data {
                // Skip NaN and infinite values
                if !x.is_finite() || !y.is_finite() {
                    continue;
                }
                has_data = true;
                x_min = x_min.min(x);
                x_max = x_max.max(x);
                y_min = y_min.min(y);
                y_max = y_max.max(y);
            }
        }

        // Default bounds for empty data
        if !has_data {
            x_min = self.x_axis.min.unwrap_or(0.0);
            x_max = self.x_axis.max.unwrap_or(1.0);
            y_min = self.y_axis.min.unwrap_or(0.0);
            y_max = self.y_axis.max.unwrap_or(1.0);
        } else {
            // Use axis bounds if specified
            x_min = self.x_axis.min.unwrap_or(x_min);
            x_max = self.x_axis.max.unwrap_or(x_max);
            y_min = self.y_axis.min.unwrap_or(y_min);
            y_max = self.y_axis.max.unwrap_or(y_max);
        }

        // Ensure non-zero ranges (avoid division by zero)
        const EPSILON: f64 = 1e-10;
        if (x_max - x_min).abs() < EPSILON {
            let center = (x_max + x_min) / 2.0;
            x_min = center - 0.5;
            x_max = center + 0.5;
        }
        if (y_max - y_min).abs() < EPSILON {
            let center = (y_max + y_min) / 2.0;
            y_min = center - 0.5;
            y_max = center + 0.5;
        }

        // Add padding for auto bounds
        let y_range = y_max - y_min;
        let y_min = if self.y_axis.min.is_none() {
            y_min - y_range * 0.05
        } else {
            y_min
        };
        let y_max = if self.y_axis.max.is_none() {
            y_max + y_range * 0.05
        } else {
            y_max
        };

        (x_min, x_max, y_min, y_max)
    }

    /// Format axis label
    fn format_label(&self, value: f64, format: &AxisFormat) -> String {
        match format {
            AxisFormat::Auto => {
                if value.abs() >= 1000.0 || (value != 0.0 && value.abs() < 0.01) {
                    format!("{:.1e}", value)
                } else if value.fract() == 0.0 {
                    format!("{:.0}", value)
                } else {
                    format!("{:.2}", value)
                }
            }
            AxisFormat::Integer => format!("{:.0}", value),
            AxisFormat::Fixed(decimals) => format!("{:.1$}", value, *decimals),
            AxisFormat::Percent => format!("{:.0}%", value * 100.0),
            AxisFormat::Custom(fmt) => fmt.replace("{}", &value.to_string()),
        }
    }

    /// Map data coordinates to screen coordinates
    fn map_point(
        &self,
        x: f64,
        y: f64,
        bounds: (f64, f64, f64, f64),
        chart_area: (u16, u16, u16, u16),
    ) -> (u16, u16) {
        let (x_min, x_max, y_min, y_max) = bounds;
        let (cx, cy, cw, ch) = chart_area;

        let x_range = x_max - x_min;
        let y_range = y_max - y_min;

        let px = if x_range > 0.0 {
            cx + ((x - x_min) / x_range * (cw as f64 - 1.0)) as u16
        } else {
            cx + cw / 2
        };

        let py = if y_range > 0.0 {
            cy + ch - 1 - ((y - y_min) / y_range * (ch as f64 - 1.0)) as u16
        } else {
            cy + ch / 2
        };

        (px, py)
    }

    /// Get line character based on direction
    fn get_line_char(&self, dx: i32, dy: i32) -> char {
        match (dx.signum(), dy.signum()) {
            (1, 0) | (-1, 0) => '',  // Horizontal
            (0, 1) | (0, -1) => '',  // Vertical
            (1, -1) | (-1, 1) => '', // Up-right or down-left
            (1, 1) | (-1, -1) => '', // Down-right or up-left
            _ => '·',
        }
    }

    /// Draw line between two points using Bresenham's algorithm
    fn draw_line(&self, ctx: &mut RenderContext, seg: &LineSegment, bounds: &Rect) {
        let LineSegment {
            x0,
            y0,
            x1,
            y1,
            color,
            style,
        } = *seg;

        let dx = (x1 as i32 - x0 as i32).abs();
        let dy = (y1 as i32 - y0 as i32).abs();
        let sx = if x0 < x1 { 1i32 } else { -1i32 };
        let sy = if y0 < y1 { 1i32 } else { -1i32 };
        let mut err = dx - dy;

        let mut x = x0 as i32;
        let mut y = y0 as i32;
        let mut step = 0;

        loop {
            // Check bounds
            if x >= bounds.x as i32
                && x < (bounds.x + bounds.width) as i32
                && y >= bounds.y as i32
                && y < (bounds.y + bounds.height) as i32
            {
                let draw = match style {
                    LineStyle::Solid => true,
                    LineStyle::Dashed => (step / 3) % 2 == 0,
                    LineStyle::Dotted => step % 2 == 0,
                    LineStyle::None => false,
                };

                if draw {
                    let ch = self.get_line_char(x1 as i32 - x0 as i32, y1 as i32 - y0 as i32);
                    let mut cell = Cell::new(ch);
                    cell.fg = Some(color);
                    ctx.set(x as u16, y as u16, cell);
                }
            }

            if x == x1 as i32 && y == y1 as i32 {
                break;
            }

            let e2 = 2 * err;
            if e2 > -dy {
                err -= dy;
                x += sx;
            }
            if e2 < dx {
                err += dx;
                y += sy;
            }
            step += 1;
        }
    }

    /// Draw area fill
    fn draw_area_fill(
        &self,
        ctx: &mut RenderContext,
        points: &[(u16, u16)],
        fill_color: Color,
        chart_area: (u16, u16, u16, u16),
        y_bottom: u16,
    ) {
        let (cx, cy, cw, ch) = chart_area;

        for window in points.windows(2) {
            let (x0, y0) = window[0];
            let (x1, y1) = window[1];

            // Fill vertical columns between the two points
            for x in x0..=x1 {
                if x < cx || x >= cx + cw {
                    continue;
                }

                // Interpolate y value
                let t = if x1 != x0 {
                    (x - x0) as f64 / (x1 - x0) as f64
                } else {
                    0.0
                };
                let y_line = y0 as f64 + t * (y1 as f64 - y0 as f64);

                let y_start = y_line.ceil() as u16;
                let y_end = y_bottom.min(cy + ch - 1);

                for y in y_start..=y_end {
                    if y >= cy && y < cy + ch {
                        // Use a transparent fill character
                        let gradient = (y - y_start) as f64 / (y_end - y_start).max(1) as f64;
                        let ch = if gradient < 0.33 {
                            ''
                        } else if gradient < 0.66 {
                            ''
                        } else {
                            ''
                        };
                        let mut cell = Cell::new(ch);
                        cell.fg = Some(fill_color);
                        ctx.set(x, y, cell);
                    }
                }
            }
        }
    }
}

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

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

    fn render(&self, ctx: &mut RenderContext) {
        let area = ctx.area;
        if area.width < 10 || area.height < 5 {
            return;
        }

        // Fill background
        if let Some(bg) = self.bg_color {
            for y in 0..area.height {
                for x in 0..area.width {
                    let mut cell = Cell::new(' ');
                    cell.bg = Some(bg);
                    ctx.set(x, y, cell);
                }
            }
        }

        // Draw border
        if let Some(border_color) = self.border_color {
            // Top and bottom
            for x in 0..area.width {
                let mut top = Cell::new('');
                top.fg = Some(border_color);
                ctx.set(x, 0, top);

                let mut bottom = Cell::new('');
                bottom.fg = Some(border_color);
                ctx.set(x, area.height - 1, bottom);
            }
            // Left and right
            for y in 0..area.height {
                let mut left = Cell::new('');
                left.fg = Some(border_color);
                ctx.set(0, y, left);

                let mut right = Cell::new('');
                right.fg = Some(border_color);
                ctx.set(area.width - 1, y, right);
            }
            // Corners
            let corners = [
                (0u16, 0u16, ''),
                (area.width - 1, 0, ''),
                (0, area.height - 1, ''),
                (area.width - 1, area.height - 1, ''),
            ];
            for (x, y, ch) in corners {
                let mut cell = Cell::new(ch);
                cell.fg = Some(border_color);
                ctx.set(x, y, cell);
            }
        }

        // Calculate layout (relative coordinates)
        let has_border = self.border_color.is_some();
        let has_title = self.title.is_some();
        let y_label_width = 8u16; // Space for Y axis labels
        let x_label_height = 2u16; // Space for X axis labels

        let inner_x = if has_border { 1 } else { 0 } + y_label_width;
        let inner_y = if has_border { 1 } else { 0 } + if has_title { 1u16 } else { 0 };
        let inner_w = area
            .width
            .saturating_sub(y_label_width + if has_border { 2 } else { 0 });
        let inner_h = area.height.saturating_sub(
            x_label_height + if has_border { 2 } else { 0 } + if has_title { 1 } else { 0 },
        );

        if inner_w < 5 || inner_h < 3 {
            return;
        }

        let chart_bounds = Rect::new(inner_x, inner_y, inner_w, inner_h);

        // Draw title
        if let Some(ref title) = self.title {
            let title_x = (area.width.saturating_sub(display_width(title) as u16)) / 2;
            let title_y = if has_border { 1u16 } else { 0 };
            let mut dx: u16 = 0;
            for ch in title.chars() {
                let mut cell = Cell::new(ch);
                cell.fg = Some(Color::WHITE);
                cell.modifier |= crate::render::Modifier::BOLD;
                ctx.set(title_x + dx, title_y, cell);
                dx += char_width(ch) as u16;
            }
        }

        // Compute bounds
        let bounds = self.compute_bounds();
        let (x_min, x_max, y_min, y_max) = bounds;

        // Draw Y axis labels
        let y_label_x = if has_border { 1u16 } else { 0 };
        for i in 0..=self.y_axis.ticks {
            let t = i as f64 / self.y_axis.ticks as f64;
            let value = y_min + t * (y_max - y_min);
            let label = self.format_label(value, &self.y_axis.format);
            let y = inner_y + inner_h - 1 - ((t * (inner_h as f64 - 1.0)) as u16);

            // Right-align label
            let label_start =
                y_label_x + y_label_width.saturating_sub(display_width(&label) as u16 + 1);
            let mut dx: u16 = 0;
            for ch in label.chars() {
                let mut cell = Cell::new(ch);
                cell.fg = Some(self.y_axis.color);
                ctx.set(label_start + dx, y, cell);
                dx += char_width(ch) as u16;
            }

            // Draw grid line
            if self.y_axis.grid && i > 0 && i < self.y_axis.ticks {
                for x in inner_x..inner_x + inner_w {
                    let mut cell = Cell::new('');
                    cell.fg = Some(Color::rgb(50, 50, 50));
                    ctx.set(x, y, cell);
                }
            }
        }

        // Draw X axis labels
        let x_label_y = inner_y + inner_h;
        for i in 0..=self.x_axis.ticks {
            let t = i as f64 / self.x_axis.ticks as f64;
            let value = x_min + t * (x_max - x_min);
            let label = self.format_label(value, &self.x_axis.format);
            let x = inner_x + (t * (inner_w as f64 - 1.0)) as u16;

            // Center label
            let label_start = x.saturating_sub(display_width(&label) as u16 / 2);
            let mut dx: u16 = 0;
            for ch in label.chars() {
                let px = label_start + dx;
                if px < area.width {
                    let mut cell = Cell::new(ch);
                    cell.fg = Some(self.x_axis.color);
                    ctx.set(px, x_label_y, cell);
                }
                dx += char_width(ch) as u16;
            }

            // Draw grid line
            if self.x_axis.grid && i > 0 && i < self.x_axis.ticks {
                for y in inner_y..inner_y + inner_h {
                    let mut cell = Cell::new('');
                    cell.fg = Some(Color::rgb(50, 50, 50));
                    ctx.set(x, y, cell);
                }
            }
        }

        // Draw axis titles
        if let Some(ref title) = self.y_axis.title {
            // Draw vertically on the left (one char per row, so char count governs height)
            let char_count = title.chars().count() as u16;
            let y_start = inner_y + (inner_h.saturating_sub(char_count)) / 2;
            for (i, ch) in title.chars().enumerate() {
                let mut cell = Cell::new(ch);
                cell.fg = Some(self.y_axis.color);
                ctx.set(y_label_x, y_start + i as u16, cell);
            }
        }

        if let Some(ref title) = self.x_axis.title {
            let x_start = inner_x + (inner_w.saturating_sub(display_width(title) as u16)) / 2;
            let y = x_label_y + 1;
            if y < area.height {
                let mut dx: u16 = 0;
                for ch in title.chars() {
                    let mut cell = Cell::new(ch);
                    cell.fg = Some(self.x_axis.color);
                    ctx.set(x_start + dx, y, cell);
                    dx += char_width(ch) as u16;
                }
            }
        }

        // Draw each series
        let y_bottom = inner_y + inner_h - 1;

        for series in &self.series {
            if series.data.is_empty() {
                continue;
            }

            // Map all points to screen coordinates
            let chart_area = (
                chart_bounds.x,
                chart_bounds.y,
                chart_bounds.width,
                chart_bounds.height,
            );
            let screen_points: Vec<(u16, u16)> = series
                .data
                .iter()
                .map(|&(x, y)| self.map_point(x, y, bounds, chart_area))
                .collect();

            // Draw area fill first (if applicable)
            if matches!(series.chart_type, ChartType::Area) {
                if let Some(fill_color) = series.fill_color {
                    self.draw_area_fill(ctx, &screen_points, fill_color, chart_area, y_bottom);
                }
            }

            // Draw lines
            if !matches!(series.line_style, LineStyle::None) {
                match series.chart_type {
                    ChartType::Line | ChartType::Area => {
                        for window in screen_points.windows(2) {
                            let (x0, y0) = window[0];
                            let (x1, y1) = window[1];
                            let seg = LineSegment {
                                x0,
                                y0,
                                x1,
                                y1,
                                color: series.color,
                                style: series.line_style,
                            };
                            self.draw_line(ctx, &seg, &chart_bounds);
                        }
                    }
                    ChartType::StepAfter => {
                        for window in screen_points.windows(2) {
                            let (x0, y0) = window[0];
                            let (x1, y1) = window[1];
                            // Horizontal then vertical
                            let horiz = LineSegment {
                                x0,
                                y0,
                                x1,
                                y1: y0,
                                color: series.color,
                                style: series.line_style,
                            };
                            self.draw_line(ctx, &horiz, &chart_bounds);
                            let vert = LineSegment {
                                x0: x1,
                                y0,
                                x1,
                                y1,
                                color: series.color,
                                style: series.line_style,
                            };
                            self.draw_line(ctx, &vert, &chart_bounds);
                        }
                    }
                    ChartType::StepBefore => {
                        for window in screen_points.windows(2) {
                            let (x0, y0) = window[0];
                            let (x1, y1) = window[1];
                            // Vertical then horizontal
                            let vert = LineSegment {
                                x0,
                                y0,
                                x1: x0,
                                y1,
                                color: series.color,
                                style: series.line_style,
                            };
                            self.draw_line(ctx, &vert, &chart_bounds);
                            let horiz = LineSegment {
                                x0,
                                y0: y1,
                                x1,
                                y1,
                                color: series.color,
                                style: series.line_style,
                            };
                            self.draw_line(ctx, &horiz, &chart_bounds);
                        }
                    }
                    ChartType::Scatter => {}
                }
            }

            // Draw markers
            if !matches!(series.marker, Marker::None) {
                let marker_char = series.marker.char();
                for &(x, y) in &screen_points {
                    if x >= inner_x
                        && x < inner_x + inner_w
                        && y >= inner_y
                        && y < inner_y + inner_h
                    {
                        let mut cell = Cell::new(marker_char);
                        cell.fg = Some(series.color);
                        ctx.set(x, y, cell);
                    }
                }
            }
        }

        // Draw legend
        if !matches!(self.legend, LegendPosition::None) && !self.series.is_empty() {
            let legend_width = self
                .series
                .iter()
                .map(|s| s.name.len() + 4)
                .max()
                .unwrap_or(10) as u16;
            let legend_height = self.series.len() as u16 + 2;

            let (legend_x, legend_y) = match self.legend {
                LegendPosition::TopLeft => (inner_x + 1, inner_y + 1),
                LegendPosition::TopCenter => (inner_x + (inner_w - legend_width) / 2, inner_y + 1),
                LegendPosition::TopRight => (inner_x + inner_w - legend_width - 1, inner_y + 1),
                LegendPosition::BottomLeft => (inner_x + 1, inner_y + inner_h - legend_height - 1),
                LegendPosition::BottomCenter => (
                    inner_x + (inner_w - legend_width) / 2,
                    inner_y + inner_h - legend_height - 1,
                ),
                LegendPosition::BottomRight => (
                    inner_x + inner_w - legend_width - 1,
                    inner_y + inner_h - legend_height - 1,
                ),
                LegendPosition::Left => (inner_x + 1, inner_y + (inner_h - legend_height) / 2),
                LegendPosition::Right => (
                    inner_x + inner_w - legend_width - 1,
                    inner_y + (inner_h - legend_height) / 2,
                ),
                // None is filtered out by the if condition above, but provide fallback
                LegendPosition::None => (inner_x + 1, inner_y + 1),
            };

            // Draw legend background
            for dy in 0..legend_height {
                for dx in 0..legend_width {
                    let x = legend_x + dx;
                    let y = legend_y + dy;
                    if x < inner_x + inner_w && y < inner_y + inner_h {
                        let ch = if dy == 0 && dx == 0 {
                            ''
                        } else if dy == 0 && dx == legend_width - 1 {
                            ''
                        } else if dy == legend_height - 1 && dx == 0 {
                            ''
                        } else if dy == legend_height - 1 && dx == legend_width - 1 {
                            ''
                        } else if dy == 0 || dy == legend_height - 1 {
                            ''
                        } else if dx == 0 || dx == legend_width - 1 {
                            ''
                        } else {
                            ' '
                        };
                        let mut cell = Cell::new(ch);
                        cell.fg = Some(DARK_GRAY);
                        cell.bg = self.bg_color.or(Some(Color::rgb(20, 20, 20)));
                        ctx.set(x, y, cell);
                    }
                }
            }

            // Draw legend entries
            for (i, series) in self.series.iter().enumerate() {
                let y = legend_y + 1 + i as u16;
                if y >= inner_y + inner_h - 1 {
                    break;
                }

                // Color indicator
                let mut indicator = Cell::new('');
                indicator.fg = Some(series.color);
                ctx.set(legend_x + 1, y, indicator);

                // Series name
                let mut dx: u16 = 0;
                for ch in series.name.chars() {
                    let x = legend_x + 3 + dx;
                    if x < legend_x + legend_width - 1 {
                        let mut cell = Cell::new(ch);
                        cell.fg = Some(Color::WHITE);
                        cell.bg = self.bg_color.or(Some(Color::rgb(20, 20, 20)));
                        ctx.set(x, y, cell);
                    }
                    dx += char_width(ch) as u16;
                }
            }
        }
    }
}

impl_styled_view!(Chart);
impl_props_builders!(Chart);

/// Helper function to create a chart
pub fn chart() -> Chart {
    Chart::new()
}

/// Quick line chart from data
pub fn line_chart(data: &[f64]) -> Chart {
    Chart::new().series(Series::new("Data").data_y(data).line())
}

/// Quick scatter plot from data
pub fn scatter_plot(data: &[(f64, f64)]) -> Chart {
    Chart::new().series(Series::new("Data").data(data.to_vec()).scatter())
}