pandrs 0.4.1

A high-performance DataFrame library for Rust, providing pandas-like API with advanced features including SIMD optimization, parallel processing, and distributed computing capabilities
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
//! High-quality visualization using Plotters
//!
//! This module provides Plotters-based visualization functionality for DataFrame and Series.
//! It can generate high-quality graphs and visualizations in various formats.

#[cfg(feature = "visualization")]
use plotters::prelude::*;
#[cfg(feature = "visualization")]
use std::collections::HashMap;
#[cfg(feature = "visualization")]
use std::path::Path;

use crate::error::{PandRSError, Result};
#[cfg(feature = "visualization")]
use crate::vis::config::{PlotKind, PlotSettings};

/// Statistics for one Tukey-style box plot, computed once and shared by
/// every box-plot renderer in this crate (PNG/SVG file output, the web
/// canvas backend, and the backward-compatible `plotters_ext` module) so
/// they all agree on quartiles, whiskers, and outliers instead of each
/// carrying its own divergent — and in one case outlier-hiding — copy.
#[cfg(feature = "visualization")]
#[derive(Debug, Clone)]
pub struct BoxPlotStats {
    pub min: f64,
    pub q1: f64,
    pub median: f64,
    pub q3: f64,
    pub max: f64,
    /// Lower whisker end: the smallest data point >= Q1 - 1.5*IQR.
    pub whisker_low: f64,
    /// Upper whisker end: the largest data point <= Q3 + 1.5*IQR.
    pub whisker_high: f64,
    /// Points beyond the whiskers (Tukey's outlier rule), reported
    /// rather than silently clipped out of the whisker range.
    pub outliers: Vec<f64>,
}

/// Linear-interpolation quantile: the value at fractional rank
/// `q * (n-1)` of `sorted`, interpolating between the two nearest ranks.
/// This is the convention NumPy/pandas use by default (`interpolation="linear"`).
#[cfg(feature = "visualization")]
fn quantile_linear(sorted: &[f64], q: f64) -> f64 {
    match sorted.len() {
        0 => f64::NAN,
        1 => sorted[0],
        n => {
            let pos = q * (n - 1) as f64;
            let lo = pos.floor() as usize;
            let hi = pos.ceil() as usize;
            if lo == hi {
                sorted[lo]
            } else {
                let frac = pos - lo as f64;
                sorted[lo] + (sorted[hi] - sorted[lo]) * frac
            }
        }
    }
}

/// Compute Tukey box-plot statistics for one category's values.
///
/// Returns `None` if there is no finite data. Quartiles use linear
/// interpolation; whiskers extend to the most extreme finite data point
/// within 1.5*IQR of the corresponding quartile, and anything further
/// out is returned as an outlier instead of being clipped away.
#[cfg(feature = "visualization")]
pub fn boxplot_stats(values: &[f64]) -> Option<BoxPlotStats> {
    let mut sorted: Vec<f64> = values.iter().copied().filter(|v| v.is_finite()).collect();
    if sorted.is_empty() {
        return None;
    }
    sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

    let min = sorted[0];
    let max = sorted[sorted.len() - 1];
    let q1 = quantile_linear(&sorted, 0.25);
    let median = quantile_linear(&sorted, 0.5);
    let q3 = quantile_linear(&sorted, 0.75);
    let iqr = q3 - q1;
    let lower_fence = q1 - 1.5 * iqr;
    let upper_fence = q3 + 1.5 * iqr;

    let whisker_low = sorted
        .iter()
        .copied()
        .find(|&v| v >= lower_fence)
        .unwrap_or(min);
    let whisker_high = sorted
        .iter()
        .rev()
        .copied()
        .find(|&v| v <= upper_fence)
        .unwrap_or(max);

    let outliers: Vec<f64> = sorted
        .iter()
        .copied()
        .filter(|&v| v < whisker_low || v > whisker_high)
        .collect();

    Some(BoxPlotStats {
        min,
        q1,
        median,
        q3,
        max,
        whisker_low,
        whisker_high,
        outliers,
    })
}

/// Draw one box plot per category onto an already-configured cartesian
/// chart with a continuous `x in [0, categories.len())` axis (category
/// `i` centered at `x = i`).
///
/// Shared by every box-plot backend (PNG/SVG files, the web canvas, and
/// the backward-compatible `plotters_ext` module) so a real box width,
/// a real (non-zero-length) median line, and correctly centered whisker
/// caps only have to be implemented once.
#[cfg(feature = "visualization")]
pub fn draw_boxplot_series<DB: plotters::prelude::DrawingBackend>(
    chart: &mut plotters::chart::ChartContext<
        DB,
        plotters::coord::cartesian::Cartesian2d<
            plotters::coord::types::RangedCoordf64,
            plotters::coord::types::RangedCoordf64,
        >,
    >,
    categories: &[String],
    category_map: &std::collections::HashMap<String, Vec<f64>>,
    color_palette: &[(u8, u8, u8)],
) -> Result<()>
where
    DB::ErrorType: std::error::Error + Send + Sync + 'static,
{
    use plotters::prelude::*;

    const BOX_WIDTH: f64 = 0.6;
    let cap_half = BOX_WIDTH / 4.0;

    for (i, category) in categories.iter().enumerate() {
        let values = match category_map.get(category) {
            Some(v) => v,
            None => continue,
        };
        let stats = match boxplot_stats(values) {
            Some(s) => s,
            None => continue,
        };
        let x = i as f64;
        let palette_len = color_palette.len().max(1);
        let (r, g, b) = color_palette
            .get(i % palette_len)
            .copied()
            .unwrap_or((70, 130, 180));
        let color = RGBColor(r, g, b);

        // Box: a real rectangle spanning [Q1, Q3], not a zero-width sliver.
        chart.draw_series(std::iter::once(Rectangle::new(
            [
                (x - BOX_WIDTH / 2.0, stats.q1),
                (x + BOX_WIDTH / 2.0, stats.q3),
            ],
            color.mix(0.25).filled(),
        )))?;

        // Median: two distinct endpoints spanning the box width, not a
        // zero-length segment collapsed onto a single point.
        chart.draw_series(std::iter::once(PathElement::new(
            vec![
                (x - BOX_WIDTH / 2.0, stats.median),
                (x + BOX_WIDTH / 2.0, stats.median),
            ],
            color.stroke_width(2),
        )))?;

        // Whiskers
        chart.draw_series(std::iter::once(PathElement::new(
            vec![(x, stats.q3), (x, stats.whisker_high)],
            color.stroke_width(1),
        )))?;
        chart.draw_series(std::iter::once(PathElement::new(
            vec![(x, stats.q1), (x, stats.whisker_low)],
            color.stroke_width(1),
        )))?;

        // Whisker caps, centered on this category's own column (not
        // shifted into the neighboring category by a truncating
        // float-to-usize cast).
        chart.draw_series(std::iter::once(PathElement::new(
            vec![
                (x - cap_half, stats.whisker_low),
                (x + cap_half, stats.whisker_low),
            ],
            color.stroke_width(1),
        )))?;
        chart.draw_series(std::iter::once(PathElement::new(
            vec![
                (x - cap_half, stats.whisker_high),
                (x + cap_half, stats.whisker_high),
            ],
            color.stroke_width(1),
        )))?;

        // Outliers beyond the whiskers, shown instead of silently
        // vanishing when the whiskers are drawn IQR-bounded.
        if !stats.outliers.is_empty() {
            chart.draw_series(
                stats
                    .outliers
                    .iter()
                    .map(|&v| Circle::new((x, v), 3, color.filled())),
            )?;
        }
    }
    Ok(())
}

#[cfg(feature = "visualization")]
pub use self::backend::plot_boxplot_png;
#[cfg(feature = "visualization")]
pub use self::backend::plot_boxplot_svg;
#[cfg(feature = "visualization")]
pub use self::backend::plot_histogram_png;
#[cfg(feature = "visualization")]
pub use self::backend::plot_histogram_svg;
#[cfg(feature = "visualization")]
pub use self::backend::plot_multi_series_png;
#[cfg(feature = "visualization")]
pub use self::backend::plot_multi_series_svg;
#[cfg(feature = "visualization")]
pub use self::backend::plot_series_xy_png;
#[cfg(feature = "visualization")]
pub use self::backend::plot_series_xy_svg;

/// Backend module for implementing plotters-based visualization
#[cfg(feature = "visualization")]
pub mod backend {
    use super::*;

    /// Plot XY data to PNG using Plotters
    pub fn plot_series_xy_png<P: AsRef<Path>>(
        x: &[f64],
        y: &[f64],
        path: P,
        settings: &PlotSettings,
        series_name: &str,
    ) -> Result<()> {
        // Implementation using Plotters backend
        let root = BitMapBackend::new(path.as_ref(), (settings.width, settings.height))
            .into_drawing_area();

        root.fill(&WHITE)?;

        // Determine min/max values for both axes
        let x_min = x.iter().fold(f64::INFINITY, |a, &b| a.min(b));
        let x_max = x.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));
        let y_min = y.iter().fold(f64::INFINITY, |a, &b| a.min(b));
        let y_max = y.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));

        // Add margins
        let x_range = x_max - x_min;
        let y_range = y_max - y_min;
        let x_min = x_min - x_range * 0.05;
        let x_max = x_max + x_range * 0.05;
        let y_min = y_min - y_range * 0.05;
        let y_max = y_max + y_range * 0.05;

        // Create chart context
        let mut chart = ChartBuilder::on(&root)
            .caption(&settings.title, ("sans-serif", 30).into_font())
            .margin(10)
            .x_label_area_size(30)
            .y_label_area_size(30)
            .build_cartesian_2d(x_min..x_max, y_min..y_max)?;

        // Add grid if specified
        if settings.show_grid {
            chart
                .configure_mesh()
                .x_desc(&settings.x_label)
                .y_desc(&settings.y_label)
                .draw()?;
        } else {
            chart
                .configure_mesh()
                .x_desc(&settings.x_label)
                .y_desc(&settings.y_label)
                .disable_mesh()
                .draw()?;
        }

        // Define color
        let color = RGBColor(
            settings.color_palette[0].0,
            settings.color_palette[0].1,
            settings.color_palette[0].2,
        );

        // Draw series based on plot type
        match settings.plot_kind {
            PlotKind::Line => {
                let line_series =
                    LineSeries::new(x.iter().zip(y.iter()).map(|(&x, &y)| (x, y)), color);

                if settings.show_legend {
                    chart
                        .draw_series(line_series)?
                        .label(series_name)
                        .legend(move |(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], color));
                } else {
                    chart.draw_series(line_series)?;
                }
            }
            PlotKind::Scatter => {
                let scatter_series = x
                    .iter()
                    .zip(y.iter())
                    .map(|(&x, &y)| Circle::new((x, y), 3, color.filled()));

                if settings.show_legend {
                    chart
                        .draw_series(scatter_series)?
                        .label(series_name)
                        .legend(move |(x, y)| Circle::new((x, y), 3, color.filled()));
                } else {
                    chart.draw_series(scatter_series)?;
                }
            }
            PlotKind::Bar => {
                // For a bar chart, we use indices as x-values
                let num_bars = x.len() as f64;
                let bars = x
                    .iter()
                    .zip(y.iter())
                    .enumerate()
                    .map(|(_i, (&x_val, &y))| {
                        let bar_width = x_range / num_bars * 0.8;
                        let x0 = x_val - bar_width / 2.0;
                        let x1 = x_val + bar_width / 2.0;

                        Rectangle::new([(x0, 0.0), (x1, y)], color.filled())
                    });

                if settings.show_legend {
                    chart
                        .draw_series(bars)?
                        .label(series_name)
                        .legend(move |(x, y)| {
                            Rectangle::new([(x, y - 5), (x + 20, y + 5)], color.filled())
                        });
                } else {
                    chart.draw_series(bars)?;
                }
            }
            PlotKind::Area => {
                let area_series = AreaSeries::new(
                    x.iter().zip(y.iter()).map(|(&x, &y)| (x, y)),
                    0.0,
                    color.mix(0.2),
                )
                .border_style(color);

                if settings.show_legend {
                    chart
                        .draw_series(area_series)?
                        .label(series_name)
                        .legend(move |(x, y)| {
                            Rectangle::new([(x, y - 5), (x + 20, y + 5)], color.mix(0.2).filled())
                        });
                } else {
                    chart.draw_series(area_series)?;
                }
            }
            _ => {
                return Err(PandRSError::NotImplemented(format!(
                    "Plot kind {:?} not supported for this function",
                    settings.plot_kind
                )));
            }
        }

        // Add legend if specified
        if settings.show_legend {
            chart
                .configure_series_labels()
                .background_style(&WHITE.mix(0.8))
                .border_style(&BLACK)
                .draw()?;
        }

        root.present()?;

        Ok(())
    }

    /// Plot XY data to SVG using Plotters
    pub fn plot_series_xy_svg<P: AsRef<Path>>(
        x: &[f64],
        y: &[f64],
        path: P,
        settings: &PlotSettings,
        series_name: &str,
    ) -> Result<()> {
        // SVG implementation (very similar to PNG but with different backend)
        let root =
            SVGBackend::new(path.as_ref(), (settings.width, settings.height)).into_drawing_area();

        root.fill(&WHITE)?;

        // Determine min/max values for both axes
        let x_min = x.iter().fold(f64::INFINITY, |a, &b| a.min(b));
        let x_max = x.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));
        let y_min = y.iter().fold(f64::INFINITY, |a, &b| a.min(b));
        let y_max = y.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));

        // Add margins
        let x_range = x_max - x_min;
        let y_range = y_max - y_min;
        let x_min = x_min - x_range * 0.05;
        let x_max = x_max + x_range * 0.05;
        let y_min = y_min - y_range * 0.05;
        let y_max = y_max + y_range * 0.05;

        // Similar implementation as PNG but using SVG backend
        // Create chart context
        let mut chart = ChartBuilder::on(&root)
            .caption(&settings.title, ("sans-serif", 30).into_font())
            .margin(10)
            .x_label_area_size(30)
            .y_label_area_size(30)
            .build_cartesian_2d(x_min..x_max, y_min..y_max)?;

        // Add grid if specified
        if settings.show_grid {
            chart
                .configure_mesh()
                .x_desc(&settings.x_label)
                .y_desc(&settings.y_label)
                .draw()?;
        } else {
            chart
                .configure_mesh()
                .x_desc(&settings.x_label)
                .y_desc(&settings.y_label)
                .disable_mesh()
                .draw()?;
        }

        // Define color
        let color = RGBColor(
            settings.color_palette[0].0,
            settings.color_palette[0].1,
            settings.color_palette[0].2,
        );

        // Draw series based on plot type
        match settings.plot_kind {
            PlotKind::Line => {
                let line_series =
                    LineSeries::new(x.iter().zip(y.iter()).map(|(&x, &y)| (x, y)), color);

                if settings.show_legend {
                    chart
                        .draw_series(line_series)?
                        .label(series_name)
                        .legend(move |(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], color));
                } else {
                    chart.draw_series(line_series)?;
                }
            }
            PlotKind::Scatter => {
                let scatter_series = x
                    .iter()
                    .zip(y.iter())
                    .map(|(&x, &y)| Circle::new((x, y), 3, color.filled()));

                if settings.show_legend {
                    chart
                        .draw_series(scatter_series)?
                        .label(series_name)
                        .legend(move |(x, y)| Circle::new((x, y), 3, color.filled()));
                } else {
                    chart.draw_series(scatter_series)?;
                }
            }
            // Other plot types would follow the same pattern
            _ => {
                return Err(PandRSError::NotImplemented(format!(
                    "Plot kind {:?} not supported for this function in SVG format",
                    settings.plot_kind
                )));
            }
        }

        // Add legend if specified
        if settings.show_legend {
            chart
                .configure_series_labels()
                .background_style(&WHITE.mix(0.8))
                .border_style(&BLACK)
                .draw()?;
        }

        root.present()?;

        Ok(())
    }

    /// Plot multiple series to PNG using Plotters
    pub fn plot_multi_series_png<P: AsRef<Path>>(
        series_data: Vec<(String, Vec<f64>, Vec<f64>, (u8, u8, u8))>,
        path: P,
        settings: &PlotSettings,
    ) -> Result<()> {
        // Implementation for multiple series
        // Similar to plot_series_xy_png but handles multiple series
        let root = BitMapBackend::new(path.as_ref(), (settings.width, settings.height))
            .into_drawing_area();

        root.fill(&WHITE)?;

        // Determine global min/max values across all series
        let mut x_min = f64::INFINITY;
        let mut x_max = f64::NEG_INFINITY;
        let mut y_min = f64::INFINITY;
        let mut y_max = f64::NEG_INFINITY;

        for (_, x, y, _) in &series_data {
            let x_min_local = x.iter().fold(f64::INFINITY, |a, &b| a.min(b));
            let x_max_local = x.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));
            let y_min_local = y.iter().fold(f64::INFINITY, |a, &b| a.min(b));
            let y_max_local = y.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));

            x_min = x_min.min(x_min_local);
            x_max = x_max.max(x_max_local);
            y_min = y_min.min(y_min_local);
            y_max = y_max.max(y_max_local);
        }

        // Add margins
        let x_range = x_max - x_min;
        let y_range = y_max - y_min;
        let x_min = x_min - x_range * 0.05;
        let x_max = x_max + x_range * 0.05;
        let y_min = y_min - y_range * 0.05;
        let y_max = y_max + y_range * 0.05;

        // Create chart context
        let mut chart = ChartBuilder::on(&root)
            .caption(&settings.title, ("sans-serif", 30).into_font())
            .margin(10)
            .x_label_area_size(30)
            .y_label_area_size(30)
            .build_cartesian_2d(x_min..x_max, y_min..y_max)?;

        // Add grid if specified
        if settings.show_grid {
            chart
                .configure_mesh()
                .x_desc(&settings.x_label)
                .y_desc(&settings.y_label)
                .draw()?;
        } else {
            chart
                .configure_mesh()
                .x_desc(&settings.x_label)
                .y_desc(&settings.y_label)
                .disable_mesh()
                .draw()?;
        }

        // Draw each series
        for (name, x, y, rgb) in series_data {
            let color = RGBColor(rgb.0, rgb.1, rgb.2);

            match settings.plot_kind {
                PlotKind::Line => {
                    let line_series =
                        LineSeries::new(x.iter().zip(y.iter()).map(|(&x, &y)| (x, y)), color);

                    if settings.show_legend {
                        chart
                            .draw_series(line_series)?
                            .label(&name)
                            .legend(move |(x, y)| {
                                PathElement::new(vec![(x, y), (x + 20, y)], color)
                            });
                    } else {
                        chart.draw_series(line_series)?;
                    }
                }
                // Add support for other plot types as needed
                _ => {
                    return Err(PandRSError::NotImplemented(format!(
                        "Plot kind {:?} not supported for multiple series",
                        settings.plot_kind
                    )));
                }
            }
        }

        // Add legend if specified
        if settings.show_legend {
            chart
                .configure_series_labels()
                .background_style(&WHITE.mix(0.8))
                .border_style(&BLACK)
                .draw()?;
        }

        root.present()?;

        Ok(())
    }

    /// Plot multiple series to SVG using Plotters
    pub fn plot_multi_series_svg<P: AsRef<Path>>(
        _series_data: Vec<(String, Vec<f64>, Vec<f64>, (u8, u8, u8))>,
        path: P,
        settings: &PlotSettings,
    ) -> Result<()> {
        // Similar to plot_multi_series_png but with SVG backend
        let _root =
            SVGBackend::new(path.as_ref(), (settings.width, settings.height)).into_drawing_area();

        // Rest of implementation would be similar to plot_multi_series_png
        // with SVG-specific adaptations

        // For brevity, we'll just return a not implemented error for now
        Err(PandRSError::NotImplemented(
            "SVG multi-series plotting not fully implemented yet".to_string(),
        ))
    }

    /// Plot histogram to PNG using Plotters
    pub fn plot_histogram_png<P: AsRef<Path>>(
        values: &[f64],
        bins: usize,
        path: P,
        settings: &PlotSettings,
        series_name: &str,
    ) -> Result<()> {
        // Histogram implementation
        // We need to bin the data first
        if values.is_empty() {
            return Err(PandRSError::Empty("No data to plot".to_string()));
        }
        if bins == 0 {
            return Err(PandRSError::InvalidInput(
                "Histogram: bins must be greater than 0".to_string(),
            ));
        }

        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));

        // A constant series has max_val == min_val; fall back to a
        // single unit-width bin instead of dividing by zero.
        let bin_width = if (max_val - min_val).abs() < f64::EPSILON {
            1.0
        } else {
            (max_val - min_val) / bins as f64
        };
        let mut histogram = vec![0; bins];

        for &value in values {
            let bin = ((value - min_val) / bin_width).floor() as usize;
            let bin = bin.min(bins - 1); // Ensure within bounds
            histogram[bin] += 1;
        }

        // Now plot the histogram
        let root = BitMapBackend::new(path.as_ref(), (settings.width, settings.height))
            .into_drawing_area();

        root.fill(&WHITE)?;

        // Find max bin height for y-axis scaling
        let max_height = *histogram.iter().max().unwrap_or(&1) as f64;

        // Create chart context
        let mut chart = ChartBuilder::on(&root)
            .caption(&settings.title, ("sans-serif", 30).into_font())
            .margin(10)
            .x_label_area_size(30)
            .y_label_area_size(30)
            .build_cartesian_2d(
                (min_val - bin_width * 0.1)..(max_val + bin_width * 0.1),
                0.0..(max_height * 1.1),
            )?;

        // Add grid if specified
        if settings.show_grid {
            chart
                .configure_mesh()
                .x_desc(&settings.x_label)
                .y_desc("Frequency")
                .draw()?;
        } else {
            chart
                .configure_mesh()
                .x_desc(&settings.x_label)
                .y_desc("Frequency")
                .disable_mesh()
                .draw()?;
        }

        // Define color
        let color = RGBColor(
            settings.color_palette[0].0,
            settings.color_palette[0].1,
            settings.color_palette[0].2,
        );

        // Draw histogram bars
        let bars = histogram.iter().enumerate().map(|(i, &count)| {
            let x0 = min_val + i as f64 * bin_width;
            let x1 = x0 + bin_width * 0.8; // Slight space between bars
            let y = count as f64;

            Rectangle::new([(x0, 0.0), (x1, y)], color.filled())
        });

        if settings.show_legend {
            chart
                .draw_series(bars)?
                .label(series_name)
                .legend(move |(x, y)| {
                    Rectangle::new([(x, y - 5), (x + 20, y + 5)], color.filled())
                });
        } else {
            chart.draw_series(bars)?;
        }

        // Add legend if specified
        if settings.show_legend {
            chart
                .configure_series_labels()
                .background_style(&WHITE.mix(0.8))
                .border_style(&BLACK)
                .draw()?;
        }

        root.present()?;

        Ok(())
    }

    /// Plot histogram to SVG using Plotters
    pub fn plot_histogram_svg<P: AsRef<Path>>(
        _values: &[f64],
        _bins: usize,
        _path: P,
        _settings: &PlotSettings,
        _series_name: &str,
    ) -> Result<()> {
        // Similar to PNG implementation but with SVG backend
        Err(PandRSError::NotImplemented(
            "SVG histogram plotting not fully implemented yet".to_string(),
        ))
    }

    /// Plot box plot to PNG using Plotters
    pub fn plot_boxplot_png<P: AsRef<Path>>(
        category_map: &HashMap<String, Vec<f64>>,
        path: P,
        settings: &PlotSettings,
    ) -> Result<()> {
        let root = BitMapBackend::new(path.as_ref(), (settings.width, settings.height))
            .into_drawing_area();
        root.fill(&WHITE)?;
        draw_boxplot_chart(&root, category_map, settings)
    }

    /// Plot box plot to SVG using Plotters
    pub fn plot_boxplot_svg<P: AsRef<Path>>(
        category_map: &HashMap<String, Vec<f64>>,
        path: P,
        settings: &PlotSettings,
    ) -> Result<()> {
        let root =
            SVGBackend::new(path.as_ref(), (settings.width, settings.height)).into_drawing_area();
        root.fill(&WHITE)?;
        draw_boxplot_chart(&root, category_map, settings)
    }

    /// Shared box-plot chart setup (axis, mesh, category labels) for any
    /// drawing backend; the per-category box/whisker/median/outlier
    /// drawing itself is delegated to [`super::draw_boxplot_series`] so
    /// PNG, SVG, and the web canvas backend all render identically.
    fn draw_boxplot_chart<DB: DrawingBackend>(
        root: &DrawingArea<DB, plotters::coord::Shift>,
        category_map: &HashMap<String, Vec<f64>>,
        settings: &PlotSettings,
    ) -> Result<()>
    where
        DB::ErrorType: std::error::Error + Send + Sync + 'static,
    {
        if category_map.is_empty() {
            return Err(PandRSError::Empty("No data to plot".to_string()));
        }

        let mut categories: Vec<String> = category_map
            .iter()
            .filter(|(_, v)| !v.is_empty())
            .map(|(k, _)| k.clone())
            .collect();
        categories.sort();
        if categories.is_empty() {
            return Err(PandRSError::Empty("No data to plot".to_string()));
        }

        let all_stats: Vec<super::BoxPlotStats> = categories
            .iter()
            .filter_map(|c| category_map.get(c))
            .filter_map(|v| super::boxplot_stats(v))
            .collect();
        if all_stats.is_empty() {
            return Err(PandRSError::Empty("No finite data to plot".to_string()));
        }

        let y_min = all_stats
            .iter()
            .map(|s| s.min.min(s.whisker_low))
            .fold(f64::INFINITY, f64::min);
        let y_max = all_stats
            .iter()
            .map(|s| s.max.max(s.whisker_high))
            .fold(f64::NEG_INFINITY, f64::max);
        let y_range = if (y_max - y_min).abs() < f64::EPSILON {
            1.0
        } else {
            y_max - y_min
        };
        let y_min = y_min - y_range * 0.1;
        let y_max = y_max + y_range * 0.1;

        let mut chart = ChartBuilder::on(root)
            .caption(&settings.title, ("sans-serif", 30).into_font())
            .margin(10)
            .x_label_area_size(30)
            .y_label_area_size(50)
            .build_cartesian_2d(-0.5f64..(categories.len() as f64 - 0.5), y_min..y_max)?;

        let label_formatter = |x: &f64| {
            let i = x.round() as isize;
            if i >= 0 && (i as usize) < categories.len() {
                categories[i as usize].clone()
            } else {
                String::new()
            }
        };
        let mut mesh = chart.configure_mesh();
        mesh.x_labels(categories.len())
            .x_label_formatter(&label_formatter)
            .y_desc(&settings.y_label);
        if !settings.show_grid {
            mesh.disable_mesh();
        }
        mesh.draw()?;

        super::draw_boxplot_series(
            &mut chart,
            &categories,
            category_map,
            &settings.color_palette,
        )
    }
}

/// Fallback implementations when visualization is not enabled
#[cfg(not(feature = "visualization"))]
pub mod backend {
    use super::*;
    use std::collections::HashMap;
    use std::path::Path;

    macro_rules! visualization_not_enabled {
        ($name:ident, $($arg:ident: $type:ty),*) => {
            pub fn $name<P: AsRef<Path>>($($arg: $type,)* _path: P) -> Result<()> {
                Err(PandRSError::FeatureNotAvailable("Visualization feature is not enabled. Recompile with --feature visualization".to_string()))
            }
        };
    }

    visualization_not_enabled!(plot_series_xy_png, _x: &[f64], _y: &[f64], _settings: &crate::vis::config::PlotSettings, _series_name: &str);
    visualization_not_enabled!(plot_series_xy_svg, _x: &[f64], _y: &[f64], _settings: &crate::vis::config::PlotSettings, _series_name: &str);
    visualization_not_enabled!(plot_multi_series_png, _series_data: Vec<(String, Vec<f64>, Vec<f64>, (u8, u8, u8))>, _settings: &crate::vis::config::PlotSettings);
    visualization_not_enabled!(plot_multi_series_svg, _series_data: Vec<(String, Vec<f64>, Vec<f64>, (u8, u8, u8))>, _settings: &crate::vis::config::PlotSettings);
    visualization_not_enabled!(plot_histogram_png, _values: &[f64], _bins: usize, _settings: &crate::vis::config::PlotSettings, _series_name: &str);
    visualization_not_enabled!(plot_histogram_svg, _values: &[f64], _bins: usize, _settings: &crate::vis::config::PlotSettings, _series_name: &str);
    visualization_not_enabled!(plot_boxplot_png, _category_map: &HashMap<String, Vec<f64>>, _settings: &crate::vis::config::PlotSettings);
    visualization_not_enabled!(plot_boxplot_svg, _category_map: &HashMap<String, Vec<f64>>, _settings: &crate::vis::config::PlotSettings);
}