runmat-plot 0.4.0

GPU-accelerated and static plotting for RunMat with WGPU and Plotters
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
//! Bar chart implementation
//!
//! High-performance bar charts with GPU acceleration and MATLAB-compatible styling.

use crate::core::{
    BoundingBox, DrawCall, GpuVertexBuffer, Material, PipelineType, RenderData, Vertex,
};
use glam::{Vec3, Vec4};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Orientation {
    Vertical,
    Horizontal,
}

/// High-performance GPU-accelerated bar chart
#[derive(Debug, Clone)]
pub struct BarChart {
    /// Category labels and values
    pub labels: Vec<String>,
    values: Option<Vec<f64>>,
    value_count: usize,

    /// Visual styling
    pub color: Vec4,
    pub bar_width: f32,
    pub outline_color: Option<Vec4>,
    pub outline_width: f32,
    per_bar_colors: Option<Vec<Vec4>>,

    /// Orientation (vertical = default bar, horizontal = barh)
    pub orientation: Orientation,

    /// Grouped bar configuration: index within group and total group count
    /// Example: for 3-series grouped bars, group_count=3 and each series has group_index 0,1,2
    pub group_index: usize,
    pub group_count: usize,

    /// Stacked bar offsets per category (bottom/base for each bar)
    /// When provided, bars are drawn starting at offset[i] and extending by values[i]
    pub stack_offsets: Option<Vec<f64>>,

    /// Metadata
    pub label: Option<String>,
    pub visible: bool,
    histogram_bin_edges: Option<Vec<f64>>,

    /// Generated rendering data (cached)
    vertices: Option<Vec<Vertex>>,
    indices: Option<Vec<u32>>,
    bounds: Option<BoundingBox>,
    dirty: bool,
    gpu_vertices: Option<GpuVertexBuffer>,
    gpu_vertex_count: Option<usize>,
    gpu_bounds: Option<BoundingBox>,
}

impl BarChart {
    fn histogram_slot_geometry(&self, index: usize) -> Option<(f32, f32)> {
        let edges = self.histogram_bin_edges.as_ref()?;
        let left = *edges.get(index)? as f32;
        let right = *edges.get(index + 1)? as f32;
        if !(left.is_finite() && right.is_finite()) {
            return None;
        }
        let bin_width = (right - left).abs().max(f32::EPSILON);
        let direction = if right >= left { 1.0 } else { -1.0 };
        let available_width = bin_width * self.bar_width.clamp(0.1, 1.0);
        let per_group_width = (available_width / self.group_count.max(1) as f32).max(0.0);
        let start = left + direction * ((bin_width - available_width) * 0.5);
        let bar_start = start + direction * (per_group_width * self.group_index as f32);
        let bar_end = bar_start + direction * per_group_width;
        Some(if direction >= 0.0 {
            (bar_start.min(bar_end), bar_start.max(bar_end))
        } else {
            (bar_end.min(bar_start), bar_end.max(bar_start))
        })
    }

    /// Create a new bar chart with labels and values
    pub fn new(labels: Vec<String>, values: Vec<f64>) -> Result<Self, String> {
        if labels.len() != values.len() {
            return Err(format!(
                "Data length mismatch: {} labels, {} values",
                labels.len(),
                values.len()
            ));
        }

        if labels.is_empty() {
            return Err("Cannot create bar chart with empty data".to_string());
        }

        let count = values.len();
        Ok(Self {
            labels,
            values: Some(values),
            value_count: count,
            color: Vec4::new(0.0, 0.5, 1.0, 1.0), // Default blue
            bar_width: 0.8,                       // 80% of available space
            outline_color: None,
            outline_width: 1.0,
            orientation: Orientation::Vertical,
            group_index: 0,
            group_count: 1,
            stack_offsets: None,
            label: None,
            visible: true,
            histogram_bin_edges: None,
            vertices: None,
            indices: None,
            bounds: None,
            dirty: true,
            gpu_vertices: None,
            gpu_vertex_count: None,
            gpu_bounds: None,
            per_bar_colors: None,
        })
    }

    /// Build a bar chart backed by a GPU vertex buffer.
    pub fn from_gpu_buffer(
        labels: Vec<String>,
        value_count: usize,
        buffer: GpuVertexBuffer,
        vertex_count: usize,
        bounds: BoundingBox,
        color: Vec4,
        bar_width: f32,
    ) -> Self {
        Self {
            labels,
            values: None,
            value_count,
            color,
            bar_width,
            outline_color: None,
            outline_width: 1.0,
            orientation: Orientation::Vertical,
            group_index: 0,
            group_count: 1,
            stack_offsets: None,
            label: None,
            visible: true,
            histogram_bin_edges: None,
            vertices: None,
            indices: None,
            bounds: Some(bounds),
            dirty: false,
            gpu_vertices: Some(buffer),
            gpu_vertex_count: Some(vertex_count),
            gpu_bounds: Some(bounds),
            per_bar_colors: None,
        }
    }

    pub fn set_data(&mut self, labels: Vec<String>, values: Vec<f64>) -> Result<(), String> {
        if labels.len() != values.len() || labels.is_empty() {
            return Err(
                "Bar data must be non-empty and label/value lengths must match".to_string(),
            );
        }
        self.labels = labels;
        self.value_count = values.len();
        self.values = Some(values);
        self.vertices = None;
        self.indices = None;
        self.bounds = None;
        self.gpu_vertices = None;
        self.gpu_vertex_count = None;
        self.gpu_bounds = None;
        self.dirty = true;
        Ok(())
    }

    fn invalidate_gpu_data(&mut self) {
        self.gpu_vertices = None;
        self.gpu_vertex_count = None;
        self.gpu_bounds = None;
    }

    /// Create a bar chart with custom styling
    pub fn with_style(mut self, color: Vec4, bar_width: f32) -> Self {
        self.color = color;
        self.bar_width = bar_width.clamp(0.1, 1.0);
        self.dirty = true;
        self
    }

    /// Add outline to bars
    pub fn with_outline(mut self, outline_color: Vec4, outline_width: f32) -> Self {
        self.outline_color = Some(outline_color);
        self.outline_width = outline_width.max(0.1);
        self.dirty = true;
        self
    }

    /// Set orientation (vertical/horizontal)
    pub fn with_orientation(mut self, orientation: Orientation) -> Self {
        self.orientation = orientation;
        self.dirty = true;
        self
    }

    pub fn bar_count(&self) -> usize {
        self.value_count
    }

    pub fn values(&self) -> Option<&[f64]> {
        self.values.as_deref()
    }

    pub fn stack_offsets(&self) -> Option<&[f64]> {
        self.stack_offsets.as_deref()
    }

    pub fn histogram_bin_edges(&self) -> Option<&[f64]> {
        self.histogram_bin_edges.as_deref()
    }

    pub fn set_histogram_bin_edges(&mut self, edges: Vec<f64>) {
        if edges.len() == self.value_count + 1 {
            self.histogram_bin_edges = Some(edges);
        }
    }

    pub fn set_per_bar_colors(&mut self, colors: Vec<Vec4>) {
        if colors.is_empty() {
            self.per_bar_colors = None;
        } else {
            self.per_bar_colors = Some(colors);
        }
        self.dirty = true;
        self.invalidate_gpu_data();
    }

    pub fn clear_per_bar_colors(&mut self) {
        if self.per_bar_colors.is_some() {
            self.per_bar_colors = None;
            self.dirty = true;
        }
    }

    /// Configure grouped bars (index within group and total group count)
    pub fn with_group(mut self, group_index: usize, group_count: usize) -> Self {
        self.group_index = group_index.min(group_count.saturating_sub(1));
        self.group_count = group_count.max(1);
        self.dirty = true;
        self
    }

    /// Configure stacked bars with per-category offsets (base values)
    pub fn with_stack_offsets(mut self, offsets: Vec<f64>) -> Self {
        if self
            .values
            .as_ref()
            .is_some_and(|v| offsets.len() == v.len())
            || offsets.len() == self.value_count
        {
            self.stack_offsets = Some(offsets);
            self.dirty = true;
            self.invalidate_gpu_data();
        }
        self
    }

    /// Set the chart label for legends
    pub fn with_label<S: Into<String>>(mut self, label: S) -> Self {
        self.label = Some(label.into());
        self
    }

    /// Update the data
    pub fn update_data(&mut self, labels: Vec<String>, values: Vec<f64>) -> Result<(), String> {
        if labels.len() != values.len() {
            return Err(format!(
                "Data length mismatch: {} labels, {} values",
                labels.len(),
                values.len()
            ));
        }

        if labels.is_empty() {
            return Err("Cannot update with empty data".to_string());
        }

        self.labels = labels;
        self.value_count = values.len();
        self.values = Some(values);
        self.dirty = true;
        self.vertices = None;
        self.indices = None;
        self.bounds = None;
        self.invalidate_gpu_data();
        Ok(())
    }

    /// Set the bar color
    pub fn set_color(&mut self, color: Vec4) {
        self.color = color;
        self.per_bar_colors = None;
        self.dirty = true;
    }

    /// Set the bar width (0.1 to 1.0)
    pub fn set_bar_width(&mut self, width: f32) {
        self.bar_width = width.clamp(0.1, 1.0);
        self.dirty = true;
    }

    /// Override the face color and width without invalidating GPU data.
    pub fn apply_face_style(&mut self, color: Vec4, width: f32) {
        if self.gpu_vertices.is_some() {
            self.color = color;
            self.bar_width = width.clamp(0.1, 1.0);
        } else {
            self.set_color(color);
            self.set_bar_width(width);
        }
    }

    /// Set the outline color (enables outline if not present)
    pub fn set_outline_color(&mut self, color: Vec4) {
        if self.outline_color.is_none() {
            self.outline_width = self.outline_width.max(1.0);
        }
        self.outline_color = Some(color);
        self.dirty = true;
    }

    /// Set the outline width (enables outline if not present)
    pub fn set_outline_width(&mut self, width: f32) {
        self.outline_width = width.max(0.1);
        if self.outline_color.is_none() {
            // Default to black if no color set
            self.outline_color = Some(Vec4::new(0.0, 0.0, 0.0, 1.0));
        }
        self.dirty = true;
        self.invalidate_gpu_data();
    }

    /// Override outline styling while preserving GPU geometry when possible.
    pub fn apply_outline_style(&mut self, color: Option<Vec4>, width: f32) {
        match color {
            Some(color) => {
                if self.gpu_vertices.is_some() {
                    self.outline_color = Some(color);
                    self.outline_width = width.max(0.1);
                } else {
                    self.set_outline_color(color);
                    self.set_outline_width(width);
                }
            }
            None => {
                self.outline_color = None;
            }
        }
    }

    /// Show or hide the chart
    pub fn set_visible(&mut self, visible: bool) {
        self.visible = visible;
    }

    /// Get the number of bars
    pub fn len(&self) -> usize {
        self.value_count
    }

    /// Check if the chart has no data
    pub fn is_empty(&self) -> bool {
        self.value_count == 0
    }

    /// Generate vertices for GPU rendering
    pub fn generate_vertices(&mut self) -> (&Vec<Vertex>, &Vec<u32>) {
        if self.gpu_vertices.is_some() {
            if self.vertices.is_none() {
                self.vertices = Some(Vec::new());
            }
            if self.indices.is_none() {
                self.indices = Some(Vec::new());
            }
            return (
                self.vertices.as_ref().unwrap(),
                self.indices.as_ref().unwrap(),
            );
        }

        if self.dirty || self.vertices.is_none() {
            let (vertices, indices) = self.create_bar_geometry();
            self.vertices = Some(vertices);
            self.indices = Some(indices);
            self.dirty = false;
        }
        (
            self.vertices.as_ref().unwrap(),
            self.indices.as_ref().unwrap(),
        )
    }

    /// Create the geometry for all bars
    fn create_bar_geometry(&self) -> (Vec<Vertex>, Vec<u32>) {
        let values = self
            .values
            .as_ref()
            .expect("CPU bar geometry requested without host values");
        let mut vertices = Vec::new();
        let mut indices = Vec::new();

        let group_count = self.group_count.max(1) as f32;
        let per_group_width = (self.bar_width / group_count).max(0.01);
        let group_offset_start = -self.bar_width * 0.5;
        let local_offset = group_offset_start
            + per_group_width * (self.group_index as f32)
            + per_group_width * 0.5;

        match self.orientation {
            Orientation::Vertical => {
                for (i, &value) in values.iter().enumerate() {
                    if !value.is_finite() {
                        continue;
                    }
                    let color = self.color_for_bar(i);
                    let (left, right) = if self.histogram_bin_edges.is_some() {
                        self.histogram_slot_geometry(i)
                            .unwrap_or(((i as f32) + 0.6, (i as f32) + 1.4))
                    } else {
                        let x_center = (i as f32) + 1.0;
                        let center = x_center + local_offset;
                        let half = per_group_width * 0.5;
                        (center - half, center + half)
                    };
                    let base = self
                        .stack_offsets
                        .as_ref()
                        .map(|v| v[i] as f32)
                        .unwrap_or(0.0);
                    let bottom = base;
                    let top = base + value as f32;

                    let vertex_offset = vertices.len() as u32;
                    vertices.push(Vertex::new(Vec3::new(left, bottom, 0.0), color));
                    vertices.push(Vertex::new(Vec3::new(right, bottom, 0.0), color));
                    vertices.push(Vertex::new(Vec3::new(right, top, 0.0), color));
                    vertices.push(Vertex::new(Vec3::new(left, top, 0.0), color));
                    indices.push(vertex_offset);
                    indices.push(vertex_offset + 1);
                    indices.push(vertex_offset + 2);
                    indices.push(vertex_offset);
                    indices.push(vertex_offset + 2);
                    indices.push(vertex_offset + 3);
                }
            }
            Orientation::Horizontal => {
                for (i, &value) in values.iter().enumerate() {
                    if !value.is_finite() {
                        continue;
                    }
                    let color = self.color_for_bar(i);
                    let (bottom, top) = if self.histogram_bin_edges.is_some() {
                        self.histogram_slot_geometry(i)
                            .unwrap_or(((i as f32) + 0.6, (i as f32) + 1.4))
                    } else {
                        let y_center = (i as f32) + 1.0;
                        let center = y_center + local_offset;
                        let half = per_group_width * 0.5;
                        (center - half, center + half)
                    };
                    let base = self
                        .stack_offsets
                        .as_ref()
                        .map(|v| v[i] as f32)
                        .unwrap_or(0.0);
                    let left = base;
                    let right = base + value as f32;

                    let vertex_offset = vertices.len() as u32;
                    vertices.push(Vertex::new(Vec3::new(left, bottom, 0.0), color));
                    vertices.push(Vertex::new(Vec3::new(right, bottom, 0.0), color));
                    vertices.push(Vertex::new(Vec3::new(right, top, 0.0), color));
                    vertices.push(Vertex::new(Vec3::new(left, top, 0.0), color));
                    indices.push(vertex_offset);
                    indices.push(vertex_offset + 1);
                    indices.push(vertex_offset + 2);
                    indices.push(vertex_offset);
                    indices.push(vertex_offset + 2);
                    indices.push(vertex_offset + 3);
                }
            }
        }

        (vertices, indices)
    }

    fn color_for_bar(&self, index: usize) -> Vec4 {
        if let Some(colors) = &self.per_bar_colors {
            if let Some(color) = colors.get(index) {
                return *color;
            }
        }
        self.color
    }

    /// Get the bounding box of the chart
    pub fn bounds(&mut self) -> BoundingBox {
        if let Some(bounds) = self.gpu_bounds {
            self.bounds = Some(bounds);
            return bounds;
        }

        if self.dirty || self.bounds.is_none() {
            let values = self
                .values
                .as_ref()
                .expect("CPU bar bounds requested without host values");
            let num_bars = values.len();
            if num_bars == 0 {
                self.bounds = Some(BoundingBox::default());
                return self.bounds.unwrap();
            }

            match self.orientation {
                Orientation::Vertical => {
                    let (min_x, max_x) = if self.histogram_bin_edges.is_some() {
                        let mut min_x = f32::INFINITY;
                        let mut max_x = f32::NEG_INFINITY;
                        for i in 0..num_bars {
                            if let Some((left, right)) = self.histogram_slot_geometry(i) {
                                min_x = min_x.min(left);
                                max_x = max_x.max(right);
                            }
                        }
                        if !min_x.is_finite() || !max_x.is_finite() {
                            (
                                1.0 - self.bar_width * 0.5,
                                num_bars as f32 + self.bar_width * 0.5,
                            )
                        } else {
                            (min_x, max_x)
                        }
                    } else {
                        (
                            1.0 - self.bar_width * 0.5,
                            num_bars as f32 + self.bar_width * 0.5,
                        )
                    };
                    // Y spans min/max of values and optional stack offsets
                    let (mut min_y, mut max_y) = (0.0f32, 0.0f32);
                    if let Some(offsets) = &self.stack_offsets {
                        for i in 0..num_bars {
                            let base = offsets[i] as f32;
                            let v = values[i];
                            if !v.is_finite() {
                                continue;
                            }
                            let top = base + v as f32;
                            min_y = min_y.min(base.min(top));
                            max_y = max_y.max(base.max(top));
                        }
                    } else {
                        for &v in values {
                            if !v.is_finite() {
                                continue;
                            }
                            min_y = min_y.min(v as f32);
                            max_y = max_y.max(v as f32);
                        }
                    }
                    self.bounds = Some(BoundingBox::new(
                        Vec3::new(min_x, min_y, 0.0),
                        Vec3::new(max_x, max_y, 0.0),
                    ));
                }
                Orientation::Horizontal => {
                    let (min_y, max_y) = if self.histogram_bin_edges.is_some() {
                        let mut min_y = f32::INFINITY;
                        let mut max_y = f32::NEG_INFINITY;
                        for i in 0..num_bars {
                            if let Some((bottom, top)) = self.histogram_slot_geometry(i) {
                                min_y = min_y.min(bottom);
                                max_y = max_y.max(top);
                            }
                        }
                        if !min_y.is_finite() || !max_y.is_finite() {
                            (
                                1.0 - self.bar_width * 0.5,
                                num_bars as f32 + self.bar_width * 0.5,
                            )
                        } else {
                            (min_y, max_y)
                        }
                    } else {
                        (
                            1.0 - self.bar_width * 0.5,
                            num_bars as f32 + self.bar_width * 0.5,
                        )
                    };
                    // X spans min/max of values and optional stack offsets
                    let (mut min_x, mut max_x) = (0.0f32, 0.0f32);
                    if let Some(offsets) = &self.stack_offsets {
                        for i in 0..num_bars {
                            let base = offsets[i] as f32;
                            let v = values[i];
                            if !v.is_finite() {
                                continue;
                            }
                            let right = base + v as f32;
                            min_x = min_x.min(base.min(right));
                            max_x = max_x.max(base.max(right));
                        }
                    } else {
                        for &v in values {
                            if !v.is_finite() {
                                continue;
                            }
                            min_x = min_x.min(v as f32);
                            max_x = max_x.max(v as f32);
                        }
                    }
                    self.bounds = Some(BoundingBox::new(
                        Vec3::new(min_x, min_y, 0.0),
                        Vec3::new(max_x, max_y, 0.0),
                    ));
                }
            }
        }
        self.bounds.unwrap()
    }

    /// Generate complete render data for the graphics pipeline
    pub fn render_data(&mut self) -> RenderData {
        let using_gpu = self.gpu_vertices.is_some();
        let gpu_vertices = self.gpu_vertices.clone();
        let bounds = self.bounds();
        let (vertices, indices, vertex_count) = if using_gpu {
            let count = self
                .gpu_vertex_count
                .or_else(|| gpu_vertices.as_ref().map(|buf| buf.vertex_count))
                .unwrap_or(0);
            (Vec::new(), None, count)
        } else {
            let (verts, inds) = self.generate_vertices();
            (verts.clone(), Some(inds.clone()), verts.len())
        };

        let material = Material {
            albedo: self.color,
            ..Default::default()
        };

        let draw_call = DrawCall {
            vertex_offset: 0,
            vertex_count,
            index_offset: indices.as_ref().map(|_| 0),
            index_count: indices.as_ref().map(|ind| ind.len()),
            instance_count: 1,
        };

        RenderData {
            pipeline_type: PipelineType::Triangles,
            vertices,
            indices,
            gpu_vertices,
            bounds: Some(bounds),
            material,
            draw_calls: vec![draw_call],
            image: None,
        }
    }

    /// Get chart statistics for debugging
    pub fn statistics(&self) -> BarChartStatistics {
        let (bar_count, value_range) = if let Some(values) = &self.values {
            if values.is_empty() {
                (0, (0.0, 0.0))
            } else {
                let min_val = values.iter().fold(f64::INFINITY, |a, &b| a.min(b));
                let max_val = values.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));
                (values.len(), (min_val, max_val))
            }
        } else if let Some(bounds) = self.gpu_bounds.or(self.bounds) {
            (self.value_count, (bounds.min.y as f64, bounds.max.y as f64))
        } else {
            (self.value_count, (0.0, 0.0))
        };

        BarChartStatistics {
            bar_count,
            value_range,
            memory_usage: self.estimated_memory_usage(),
        }
    }

    /// Estimate memory usage in bytes
    pub fn estimated_memory_usage(&self) -> usize {
        let labels_size: usize = self.labels.iter().map(|s| s.len()).sum();
        let values_size = self
            .values
            .as_ref()
            .map_or(0, |v| v.len() * std::mem::size_of::<f64>());
        let vertices_size = self
            .vertices
            .as_ref()
            .map_or(0, |v| v.len() * std::mem::size_of::<Vertex>());
        let indices_size = self
            .indices
            .as_ref()
            .map_or(0, |i| i.len() * std::mem::size_of::<u32>());

        labels_size + values_size + vertices_size + indices_size
    }
}

/// Bar chart statistics
#[derive(Debug, Clone)]
pub struct BarChartStatistics {
    pub bar_count: usize,
    pub value_range: (f64, f64),
    pub memory_usage: usize,
}

/// MATLAB-compatible bar chart creation utilities
pub mod matlab_compat {
    use super::*;

    /// Create a simple bar chart (equivalent to MATLAB's `bar(values)`)
    pub fn bar(values: Vec<f64>) -> Result<BarChart, String> {
        let labels: Vec<String> = (1..=values.len()).map(|i| i.to_string()).collect();
        BarChart::new(labels, values)
    }

    /// Create a bar chart with custom labels (`bar(labels, values)`)
    pub fn bar_with_labels(labels: Vec<String>, values: Vec<f64>) -> Result<BarChart, String> {
        BarChart::new(labels, values)
    }

    /// Create a bar chart with specified color
    pub fn bar_with_color(values: Vec<f64>, color: &str) -> Result<BarChart, String> {
        let color_vec = parse_matlab_color(color)?;
        let labels: Vec<String> = (1..=values.len()).map(|i| i.to_string()).collect();
        Ok(BarChart::new(labels, values)?.with_style(color_vec, 0.8))
    }

    /// Parse MATLAB color specifications
    fn parse_matlab_color(color: &str) -> Result<Vec4, String> {
        match color {
            "r" | "red" => Ok(Vec4::new(1.0, 0.0, 0.0, 1.0)),
            "g" | "green" => Ok(Vec4::new(0.0, 1.0, 0.0, 1.0)),
            "b" | "blue" => Ok(Vec4::new(0.0, 0.0, 1.0, 1.0)),
            "c" | "cyan" => Ok(Vec4::new(0.0, 1.0, 1.0, 1.0)),
            "m" | "magenta" => Ok(Vec4::new(1.0, 0.0, 1.0, 1.0)),
            "y" | "yellow" => Ok(Vec4::new(1.0, 1.0, 0.0, 1.0)),
            "k" | "black" => Ok(Vec4::new(0.0, 0.0, 0.0, 1.0)),
            "w" | "white" => Ok(Vec4::new(1.0, 1.0, 1.0, 1.0)),
            _ => Err(format!("Unknown color: {color}")),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_bar_chart_creation() {
        let labels = vec!["A".to_string(), "B".to_string(), "C".to_string()];
        let values = vec![10.0, 25.0, 15.0];

        let chart = BarChart::new(labels.clone(), values.clone()).unwrap();

        assert_eq!(chart.labels, labels);
        assert_eq!(chart.values.as_ref(), Some(&values));
        assert_eq!(chart.len(), 3);
        assert!(!chart.is_empty());
        assert!(chart.visible);
    }

    #[test]
    fn test_bar_chart_data_validation() {
        // Mismatched lengths should fail
        let labels = vec!["A".to_string(), "B".to_string()];
        let values = vec![10.0, 25.0, 15.0];
        assert!(BarChart::new(labels, values).is_err());

        // Empty data should fail
        let empty_labels: Vec<String> = vec![];
        let empty_values: Vec<f64> = vec![];
        assert!(BarChart::new(empty_labels, empty_values).is_err());
    }

    #[test]
    fn test_bar_chart_styling() {
        let labels = vec!["X".to_string(), "Y".to_string()];
        let values = vec![5.0, 10.0];
        let color = Vec4::new(1.0, 0.0, 0.0, 1.0);

        let chart = BarChart::new(labels, values)
            .unwrap()
            .with_style(color, 0.6)
            .with_outline(Vec4::new(0.0, 0.0, 0.0, 1.0), 2.0)
            .with_label("Test Chart");

        assert_eq!(chart.color, color);
        assert_eq!(chart.bar_width, 0.6);
        assert_eq!(chart.outline_color, Some(Vec4::new(0.0, 0.0, 0.0, 1.0)));
        assert_eq!(chart.outline_width, 2.0);
        assert_eq!(chart.label, Some("Test Chart".to_string()));
    }

    #[test]
    fn test_bar_chart_bounds() {
        let labels = vec!["A".to_string(), "B".to_string(), "C".to_string()];
        let values = vec![5.0, -2.0, 8.0];

        let mut chart = BarChart::new(labels, values).unwrap();
        let bounds = chart.bounds();

        // X bounds should span all bars (centers are 1-based)
        assert!(bounds.min.x < 1.0);
        assert!(bounds.max.x > 3.0);

        // Y bounds should include negative and positive values
        assert_eq!(bounds.min.y, -2.0);
        assert_eq!(bounds.max.y, 8.0);
    }

    #[test]
    fn test_bar_chart_vertex_generation() {
        let labels = vec!["A".to_string(), "B".to_string()];
        let values = vec![3.0, 5.0];

        let mut chart = BarChart::new(labels, values).unwrap();
        let (vertices, indices) = chart.generate_vertices();

        // Should have 4 vertices per bar (rectangle)
        assert_eq!(vertices.len(), 8);

        // Should have 6 indices per bar (2 triangles)
        assert_eq!(indices.len(), 12);

        // Check first bar vertices are reasonable
        assert_eq!(vertices[0].position[1], 0.0); // Bottom
        assert_eq!(vertices[2].position[1], 3.0); // Top of first bar
    }

    #[test]
    fn test_bar_chart_render_data() {
        let labels = vec!["Test".to_string()];
        let values = vec![10.0];

        let mut chart = BarChart::new(labels, values).unwrap();
        let render_data = chart.render_data();

        assert_eq!(render_data.pipeline_type, PipelineType::Triangles);
        assert_eq!(render_data.vertices.len(), 4); // One rectangle
        assert!(render_data.indices.is_some());
        assert_eq!(render_data.indices.as_ref().unwrap().len(), 6); // Two triangles
        let bounds = render_data.bounds.expect("bar render data bounds");
        assert_eq!(bounds.min.x, 0.6);
        assert_eq!(bounds.max.x, 1.4);
        assert_eq!(bounds.min.y, 0.0);
        assert_eq!(bounds.max.y, 10.0);
    }

    #[test]
    fn histogram_edges_drive_bar_geometry_and_bounds() {
        let labels = vec!["bin1".to_string(), "bin2".to_string()];
        let values = vec![2.0, 3.0];

        let mut chart = BarChart::new(labels, values).unwrap();
        chart.set_histogram_bin_edges(vec![0.0, 0.5, 1.0]);
        chart.set_bar_width(1.0);

        let bounds = chart.bounds();
        assert_eq!(bounds.min.x, 0.0);
        assert_eq!(bounds.max.x, 1.0);

        let (vertices, _) = chart.generate_vertices();
        assert_eq!(vertices[0].position[0], 0.0);
        assert_eq!(vertices[1].position[0], 0.5);
        assert_eq!(vertices[4].position[0], 0.5);
        assert_eq!(vertices[5].position[0], 1.0);
    }

    #[test]
    fn test_bar_chart_statistics() {
        let labels = vec!["A".to_string(), "B".to_string(), "C".to_string()];
        let values = vec![1.0, 5.0, 3.0];

        let chart = BarChart::new(labels, values).unwrap();
        let stats = chart.statistics();

        assert_eq!(stats.bar_count, 3);
        assert_eq!(stats.value_range, (1.0, 5.0));
        assert!(stats.memory_usage > 0);
    }

    #[test]
    fn test_matlab_compat_bar() {
        use super::matlab_compat::*;

        let values = vec![1.0, 3.0, 2.0];

        let chart1 = bar(values.clone()).unwrap();
        assert_eq!(chart1.len(), 3);
        assert_eq!(chart1.labels, vec!["1", "2", "3"]);

        let labels = vec!["X".to_string(), "Y".to_string(), "Z".to_string()];
        let chart2 = bar_with_labels(labels.clone(), values.clone()).unwrap();
        assert_eq!(chart2.labels, labels);

        let chart3 = bar_with_color(values, "g").unwrap();
        assert_eq!(chart3.color, Vec4::new(0.0, 1.0, 0.0, 1.0));
    }
}