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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
//! WebAssembly support for interactive browser visualization
//!
//! This module provides WebAssembly (wasm) integration for PandRS, allowing for
//! interactive data visualization in web browsers.

use plotters::drawing::IntoDrawingArea;
use plotters_canvas::CanvasBackend;
use std::cell::RefCell;
use std::rc::Rc;
use wasm_bindgen::prelude::*;
use web_sys::{CanvasRenderingContext2d, HtmlCanvasElement};

use crate::error::{Error, Result};
use crate::vis::plotters_ext::{PlotKind, PlotSettings};
use crate::DataFrame;

// Type alias for web-compatible results
type WebResult<T> = std::result::Result<T, JsValue>;

// Helper to convert our errors to JsValue
fn to_js_error<E: std::fmt::Display>(err: E) -> JsValue {
    JsValue::from_str(&err.to_string())
}

/// Possible color themes for web visualizations
#[wasm_bindgen]
#[derive(Debug, Clone, Copy)]
pub enum ColorTheme {
    Default,
    Dark,
    Light,
    Pastel,
    Vibrant,
}

/// Types of interactive visualizations
#[wasm_bindgen]
#[derive(Debug, Clone, Copy)]
pub enum VisualizationType {
    Line,
    Bar,
    Scatter,
    Area,
    Pie,
    Histogram,
    BoxPlot,
    HeatMap,
}

/// Configuration for web visualizations
#[wasm_bindgen]
#[derive(Debug, Clone)]
pub struct WebVisualizationConfig {
    /// Canvas element ID
    canvas_id: String,
    /// Chart title
    title: String,
    /// Visualization type
    viz_type: VisualizationType,
    /// Color theme
    theme: ColorTheme,
    /// Width of the visualization
    width: u32,
    /// Height of the visualization
    height: u32,
    /// Show interactive legend
    show_legend: bool,
    /// Show interactive tooltips
    show_tooltips: bool,
    /// Enable animation
    animate: bool,
}

#[wasm_bindgen]
impl WebVisualizationConfig {
    /// Create a new web visualization configuration
    #[wasm_bindgen(constructor)]
    pub fn new(canvas_id: &str) -> Self {
        WebVisualizationConfig {
            canvas_id: canvas_id.to_string(),
            title: "Chart".to_string(),
            viz_type: VisualizationType::Line,
            theme: ColorTheme::Default,
            width: 800,
            height: 600,
            show_legend: true,
            show_tooltips: true,
            animate: true,
        }
    }

    /// Set the title
    #[wasm_bindgen]
    pub fn set_title(&mut self, title: &str) -> Self {
        self.title = title.to_string();
        self.clone()
    }

    /// Set the visualization type
    #[wasm_bindgen]
    pub fn set_type(&mut self, viz_type: VisualizationType) -> Self {
        self.viz_type = viz_type;
        self.clone()
    }

    /// Set the color theme
    #[wasm_bindgen]
    pub fn set_theme(&mut self, theme: ColorTheme) -> Self {
        self.theme = theme;
        self.clone()
    }

    /// Set the dimensions
    #[wasm_bindgen]
    pub fn set_dimensions(&mut self, width: u32, height: u32) -> Self {
        self.width = width;
        self.height = height;
        self.clone()
    }

    /// Set whether to show legend
    #[wasm_bindgen]
    pub fn show_legend(&mut self, show: bool) -> Self {
        self.show_legend = show;
        self.clone()
    }

    /// Set whether to show tooltips
    #[wasm_bindgen]
    pub fn show_tooltips(&mut self, show: bool) -> Self {
        self.show_tooltips = show;
        self.clone()
    }

    /// Set whether to animate
    #[wasm_bindgen]
    pub fn animate(&mut self, animate: bool) -> Self {
        self.animate = animate;
        self.clone()
    }
}

/// Interactive visualization for the web
#[wasm_bindgen]
pub struct WebVisualization {
    config: WebVisualizationConfig,
    canvas: HtmlCanvasElement,
    context: CanvasRenderingContext2d,
    /// A sibling canvas stacked exactly on top of `canvas`, used only for
    /// drawing the tooltip. Tooltip drawing must never touch `context`
    /// (the chart canvas): the previous implementation called
    /// `context.clear_rect` on every `mousemove`, which erased the whole
    /// chart and left only a floating "Tooltip" box behind. Since the
    /// overlay starts fully transparent and pointer-events are disabled
    /// on it, clearing *it* on each move just removes the previous
    /// tooltip without ever touching the chart underneath.
    overlay_canvas: HtmlCanvasElement,
    overlay_context: CanvasRenderingContext2d,
    data: Option<Rc<RefCell<DataFrame>>>,
    event_listeners: Vec<(String, Closure<dyn FnMut(web_sys::MouseEvent)>)>,
}

#[wasm_bindgen]
impl WebVisualization {
    /// Create a new web visualization
    #[wasm_bindgen(constructor)]
    pub fn new(config: WebVisualizationConfig) -> WebResult<WebVisualization> {
        // Get the canvas element
        let window = web_sys::window().ok_or_else(|| to_js_error("No window object available"))?;

        let document = window
            .document()
            .ok_or_else(|| to_js_error("No document object available"))?;

        let canvas = document
            .get_element_by_id(&config.canvas_id)
            .ok_or_else(|| {
                to_js_error(format!(
                    "Canvas element with ID '{}' not found",
                    config.canvas_id
                ))
            })?;

        let canvas = canvas
            .dyn_into::<HtmlCanvasElement>()
            .map_err(|_| to_js_error("Element is not a canvas"))?;

        // Set canvas dimensions
        canvas.set_width(config.width);
        canvas.set_height(config.height);

        // Get drawing context
        let context = canvas
            .get_context("2d")
            .map_err(|_| to_js_error("Failed to get canvas context"))?
            .ok_or_else(|| to_js_error("Canvas context is null"))?
            .dyn_into::<CanvasRenderingContext2d>()
            .map_err(|_| to_js_error("Failed to convert to CanvasRenderingContext2d"))?;

        // A same-sized sibling canvas, absolutely positioned directly on
        // top of the chart canvas, used only for tooltip drawing (see
        // the `overlay_canvas`/`overlay_context` doc comment on
        // `WebVisualization`). `pointer-events: none` keeps it from
        // intercepting the mouse events the chart canvas needs.
        let overlay_canvas = document
            .create_element("canvas")
            .map_err(|_| to_js_error("Failed to create tooltip overlay canvas"))?
            .dyn_into::<HtmlCanvasElement>()
            .map_err(|_| to_js_error("Failed to cast overlay element to canvas"))?;
        overlay_canvas.set_width(config.width);
        overlay_canvas.set_height(config.height);
        {
            let style = overlay_canvas.style();
            let _ = style.set_property("position", "absolute");
            let _ = style.set_property("pointer-events", "none");
            let _ = style.set_property("left", &format!("{}px", canvas.offset_left()));
            let _ = style.set_property("top", &format!("{}px", canvas.offset_top()));
        }
        if let Some(parent) = canvas.parent_node() {
            let _ = parent.append_child(&overlay_canvas);
        }
        let overlay_context = overlay_canvas
            .get_context("2d")
            .map_err(|_| to_js_error("Failed to get overlay canvas context"))?
            .ok_or_else(|| to_js_error("Overlay canvas context is null"))?
            .dyn_into::<CanvasRenderingContext2d>()
            .map_err(|_| to_js_error("Failed to convert overlay context"))?;

        Ok(WebVisualization {
            config,
            canvas,
            context,
            overlay_canvas,
            overlay_context,
            data: None,
            event_listeners: Vec::new(),
        })
    }

    /// Set the DataFrame to visualize
    #[wasm_bindgen]
    pub fn set_data(&mut self, data_json: &str) -> WebResult<()> {
        // Parse JSON to DataFrame
        let df = DataFrame::from_json(data_json).map_err(|e| to_js_error(e))?;
        self.data = Some(Rc::new(RefCell::new(df)));
        Ok(())
    }

    /// Re-align the tooltip overlay canvas with the chart canvas.
    ///
    /// The overlay is positioned with `left`/`top` computed once in
    /// [`WebVisualization::new`]; if the page reflows the chart canvas
    /// afterward (a resize, a layout change elsewhere on the page, ...)
    /// the overlay would drift out of alignment and tooltips would be
    /// drawn in the wrong place relative to the chart. Called at the
    /// start of every [`WebVisualization::render`] so the overlay tracks
    /// the chart canvas's current position.
    fn sync_overlay_position(&self) {
        let style = self.overlay_canvas.style();
        let _ = style.set_property("left", &format!("{}px", self.canvas.offset_left()));
        let _ = style.set_property("top", &format!("{}px", self.canvas.offset_top()));
    }

    /// Render the visualization
    #[wasm_bindgen]
    pub fn render(&mut self) -> WebResult<()> {
        self.sync_overlay_position();
        {
            let df = match &self.data {
                Some(df) => df.borrow(),
                None => return Err(to_js_error("No data available to visualize")),
            };

            // Clear canvas
            self.context.clear_rect(
                0.0,
                0.0,
                self.config.width as f64,
                self.config.height as f64,
            );

            // Get column names
            let columns = df.column_names();
            if columns.is_empty() {
                return Err(to_js_error("DataFrame has no columns"));
            }

            // Create a plotters backend with the canvas
            let backend = CanvasBackend::with_canvas_object(self.canvas.clone())
                .ok_or_else(|| to_js_error("Failed to create canvas backend"))?;

            // Convert WebVisualizationConfig to PlotSettings
            let settings = self.create_plot_settings();

            // Render based on visualization type
            match self.config.viz_type {
                VisualizationType::Line => self.render_line_chart(&df, backend, &settings)?,
                VisualizationType::Bar => self.render_bar_chart(&df, backend, &settings)?,
                VisualizationType::Scatter => self.render_scatter_chart(&df, backend, &settings)?,
                VisualizationType::Area => self.render_area_chart(&df, backend, &settings)?,
                VisualizationType::Histogram => self.render_histogram(&df, backend, &settings)?,
                VisualizationType::BoxPlot => self.render_boxplot(&df, backend, &settings)?,
                VisualizationType::Pie => self.render_pie_chart(&df, backend, &settings)?,
                VisualizationType::HeatMap => self.render_heatmap(&df, backend, &settings)?,
            }
        }

        // Set up event listeners for interactivity if tooltips are enabled
        if self.config.show_tooltips {
            self.setup_tooltip_listeners()?;
        }

        Ok(())
    }

    // Convert WebVisualizationConfig to PlotSettings
    fn create_plot_settings(&self) -> PlotSettings {
        let mut settings = PlotSettings::default();

        settings.title = self.config.title.clone();
        settings.width = self.config.width;
        settings.height = self.config.height;
        settings.show_legend = self.config.show_legend;

        // Convert visualization type
        settings.plot_kind = match self.config.viz_type {
            VisualizationType::Line => PlotKind::Line,
            VisualizationType::Bar => PlotKind::Bar,
            VisualizationType::Scatter => PlotKind::Scatter,
            VisualizationType::Area => PlotKind::Area,
            VisualizationType::Histogram => PlotKind::Histogram,
            VisualizationType::BoxPlot => PlotKind::BoxPlot,
            // Default to Line for types not supported in PlotKind
            _ => PlotKind::Line,
        };

        // Set color palette based on theme
        settings.color_palette = match self.config.theme {
            ColorTheme::Default => vec![
                (0, 123, 255),  // Blue
                (255, 99, 71),  // Red
                (46, 204, 113), // Green
                (255, 193, 7),  // Yellow
                (142, 68, 173), // Purple
            ],
            ColorTheme::Dark => vec![
                (41, 98, 255), // Blue
                (221, 65, 36), // Red
                (11, 156, 49), // Green
                (255, 147, 0), // Orange
                (116, 0, 184), // Purple
            ],
            ColorTheme::Light => vec![
                (99, 179, 237),  // Light blue
                (255, 161, 145), // Light red
                (134, 226, 173), // Light green
                (255, 230, 140), // Light yellow
                (198, 163, 229), // Light purple
            ],
            ColorTheme::Pastel => vec![
                (174, 198, 242), // Pastel blue
                (255, 179, 186), // Pastel red
                (186, 241, 191), // Pastel green
                (255, 239, 186), // Pastel yellow
                (220, 198, 239), // Pastel purple
            ],
            ColorTheme::Vibrant => vec![
                (0, 116, 217),  // Vibrant blue
                (255, 65, 54),  // Vibrant red
                (46, 204, 64),  // Vibrant green
                (255, 220, 0),  // Vibrant yellow
                (177, 13, 201), // Vibrant purple
            ],
        };

        settings
    }

    // Helper methods for rendering different chart types
    fn render_line_chart(
        &self,
        df: &DataFrame,
        backend: CanvasBackend,
        settings: &PlotSettings,
    ) -> Result<()> {
        // Get numeric columns
        let mut numeric_columns: Vec<String> = Vec::new();

        for col_name in df.column_names() {
            if df.is_numeric_column(col_name.as_str()) {
                numeric_columns.push(col_name.to_string());
            }
        }

        if numeric_columns.is_empty() {
            return Err(Error::InvalidInput(
                "No numeric columns available for line chart".to_string(),
            ));
        }

        // Select columns to plot (up to 5)
        let columns_to_plot = if numeric_columns.len() > 5 {
            numeric_columns[0..5].to_vec()
        } else {
            numeric_columns
        };

        // Convert to &[&str] for plotters
        let columns_str: Vec<&str> = columns_to_plot.iter().map(|s| s.as_str()).collect();

        // Use existing plotters implementation
        let drawing_area = backend.into_drawing_area();
        drawing_area
            .fill(&plotters::style::colors::WHITE)
            .map_err(|e| Error::Visualization(format!("Failed to fill drawing area: {}", e)))?;

        // Create a temporary plot to set up the chart
        let mut plot_settings = settings.clone();
        plot_settings.plot_kind = PlotKind::Line;

        // Use existing plotting functionality from plotters_ext module
        plotters_ext_web::plot_multi_series_for_web(
            df,
            &columns_str,
            drawing_area,
            &plot_settings,
        )?;

        Ok(())
    }

    fn render_bar_chart(
        &self,
        df: &DataFrame,
        backend: CanvasBackend,
        settings: &PlotSettings,
    ) -> Result<()> {
        // Similar to line chart but with bar plot
        let mut numeric_columns: Vec<String> = Vec::new();

        for col_name in df.column_names() {
            if df.is_numeric_column(col_name.as_str()) {
                numeric_columns.push(col_name.to_string());
            }
        }

        if numeric_columns.is_empty() {
            return Err(Error::InvalidInput(
                "No numeric columns available for bar chart".to_string(),
            ));
        }

        // For bar chart, let's just use the first numeric column
        let column = &numeric_columns[0];

        // Use existing plotters implementation
        let drawing_area = backend.into_drawing_area();
        drawing_area
            .fill(&plotters::style::colors::WHITE)
            .map_err(|e| Error::Visualization(format!("Failed to fill drawing area: {}", e)))?;

        // Create a temporary plot to set up the chart
        let mut plot_settings = settings.clone();
        plot_settings.plot_kind = PlotKind::Bar;

        // Use existing plotting functionality
        plotters_ext_web::plot_column_for_web(df, column, drawing_area, &plot_settings)?;

        Ok(())
    }

    fn render_scatter_chart(
        &self,
        df: &DataFrame,
        backend: CanvasBackend,
        settings: &PlotSettings,
    ) -> Result<()> {
        // Need at least two numeric columns for scatter plot
        let mut numeric_columns: Vec<String> = Vec::new();

        for col_name in df.column_names() {
            if df.is_numeric_column(col_name.as_str()) {
                numeric_columns.push(col_name.to_string());
            }
        }

        if numeric_columns.len() < 2 {
            return Err(Error::InvalidInput(
                "Need at least two numeric columns for scatter plot".to_string(),
            ));
        }

        let x_column = &numeric_columns[0];
        let y_column = &numeric_columns[1];

        // Use existing plotters implementation
        let drawing_area = backend.into_drawing_area();
        drawing_area
            .fill(&plotters::style::colors::WHITE)
            .map_err(|e| Error::Visualization(format!("Failed to fill drawing area: {}", e)))?;

        // Create a temporary plot to set up the chart
        let mut plot_settings = settings.clone();
        plot_settings.plot_kind = PlotKind::Scatter;

        // Use existing scatter plot functionality
        plotters_ext_web::plot_scatter_for_web(
            df,
            x_column,
            y_column,
            drawing_area,
            &plot_settings,
        )?;

        Ok(())
    }

    fn render_area_chart(
        &self,
        df: &DataFrame,
        backend: CanvasBackend,
        settings: &PlotSettings,
    ) -> Result<()> {
        // Similar to line chart but with area plot
        let mut numeric_columns: Vec<String> = Vec::new();

        for col_name in df.column_names() {
            if df.is_numeric_column(col_name.as_str()) {
                numeric_columns.push(col_name.to_string());
            }
        }

        if numeric_columns.is_empty() {
            return Err(Error::InvalidInput(
                "No numeric columns available for area chart".to_string(),
            ));
        }

        // For area chart, just use the first numeric column
        let column = &numeric_columns[0];

        // Use existing plotters implementation
        let drawing_area = backend.into_drawing_area();
        drawing_area
            .fill(&plotters::style::colors::WHITE)
            .map_err(|e| Error::Visualization(format!("Failed to fill drawing area: {}", e)))?;

        // Create a temporary plot to set up the chart
        let mut plot_settings = settings.clone();
        plot_settings.plot_kind = PlotKind::Area;

        // Use existing plotting functionality
        plotters_ext_web::plot_column_for_web(df, column, drawing_area, &plot_settings)?;

        Ok(())
    }

    fn render_histogram(
        &self,
        df: &DataFrame,
        backend: CanvasBackend,
        settings: &PlotSettings,
    ) -> Result<()> {
        // Find a numeric column for histogram
        let mut numeric_columns = Vec::new();

        for col_name in df.column_names() {
            if df.is_numeric_column(&col_name) {
                numeric_columns.push(col_name);
                break; // Only need one numeric column
            }
        }

        if numeric_columns.is_empty() {
            return Err(Error::InvalidInput(
                "No numeric columns available for histogram".to_string(),
            ));
        }

        let column = &numeric_columns[0];

        // Use existing plotters implementation
        let drawing_area = backend.into_drawing_area();
        drawing_area
            .fill(&plotters::style::colors::WHITE)
            .map_err(|e| Error::Visualization(format!("Failed to fill drawing area: {}", e)))?;

        // Create a temporary plot to set up the chart
        let mut plot_settings = settings.clone();
        plot_settings.plot_kind = PlotKind::Histogram;

        // Use existing histogram functionality
        plotters_ext_web::plot_histogram_for_web(
            df,
            column,
            10, // Default 10 bins
            drawing_area,
            &plot_settings,
        )?;

        Ok(())
    }

    fn render_boxplot(
        &self,
        df: &DataFrame,
        backend: CanvasBackend,
        settings: &PlotSettings,
    ) -> Result<()> {
        // Need a numeric column and a categorical column
        let mut numeric_columns = Vec::new();
        let mut categorical_columns = Vec::new();

        for col_name in df.column_names() {
            if df.is_numeric_column(&col_name) {
                numeric_columns.push(col_name);
            } else if df.is_categorical(&col_name) || !df.is_numeric_column(&col_name) {
                categorical_columns.push(col_name);
            }
        }

        if numeric_columns.is_empty() || categorical_columns.is_empty() {
            return Err(Error::InvalidInput(
                "Need at least one numeric and one categorical column for boxplot".to_string(),
            ));
        }

        let value_column = &numeric_columns[0];
        let category_column = &categorical_columns[0];

        // Use existing plotters implementation
        let drawing_area = backend.into_drawing_area();
        drawing_area
            .fill(&plotters::style::colors::WHITE)
            .map_err(|e| Error::Visualization(format!("Failed to fill drawing area: {}", e)))?;

        // Create a temporary plot to set up the chart
        let mut plot_settings = settings.clone();
        plot_settings.plot_kind = PlotKind::BoxPlot;

        // Use existing boxplot functionality
        plotters_ext_web::plot_boxplot_for_web(
            df,
            category_column,
            value_column,
            drawing_area,
            &plot_settings,
        )?;

        Ok(())
    }

    fn render_pie_chart(
        &self,
        df: &DataFrame,
        backend: CanvasBackend,
        settings: &PlotSettings,
    ) -> Result<()> {
        // Need a numeric column and a categorical column for pie chart
        let mut numeric_columns = Vec::new();
        let mut categorical_columns = Vec::new();

        for col_name in df.column_names() {
            if df.is_numeric_column(&col_name) {
                numeric_columns.push(col_name);
            } else if df.is_categorical(&col_name) || !df.is_numeric_column(&col_name) {
                categorical_columns.push(col_name);
            }
        }

        if numeric_columns.is_empty() || categorical_columns.is_empty() {
            return Err(Error::InvalidInput(
                "Need at least one numeric and one categorical column for pie chart".to_string(),
            ));
        }

        let value_column = &numeric_columns[0];
        let category_column = &categorical_columns[0];

        // Use existing plotters implementation
        let drawing_area = backend.into_drawing_area();
        drawing_area
            .fill(&plotters::style::colors::WHITE)
            .map_err(|e| Error::Visualization(format!("Failed to fill drawing area: {}", e)))?;

        // Create a temporary plot to set up the chart
        let plot_settings = settings.clone();

        // Use custom pie chart implementation for web
        self.draw_pie_chart(df, category_column, value_column, &plot_settings)?;

        Ok(())
    }

    fn render_heatmap(
        &self,
        df: &DataFrame,
        backend: CanvasBackend,
        settings: &PlotSettings,
    ) -> Result<()> {
        // Need at least two numeric columns for heatmap
        let mut numeric_columns: Vec<String> = Vec::new();

        for col_name in df.column_names() {
            if df.is_numeric_column(col_name.as_str()) {
                numeric_columns.push(col_name.to_string());
            }
        }

        if numeric_columns.len() < 2 {
            return Err(Error::InvalidInput(
                "Need at least two numeric columns for heatmap".to_string(),
            ));
        }

        // Use existing plotters implementation
        let drawing_area = backend.into_drawing_area();
        drawing_area
            .fill(&plotters::style::colors::WHITE)
            .map_err(|e| Error::Visualization(format!("Failed to fill drawing area: {}", e)))?;

        // Create a temporary plot to set up the chart
        let plot_settings = settings.clone();

        // Use custom heatmap implementation for web
        self.draw_heatmap(df, &numeric_columns, &plot_settings)?;

        Ok(())
    }

    // Custom implementations for pie charts and heatmaps
    fn draw_pie_chart(
        &self,
        df: &DataFrame,
        category_col: &str,
        value_col: &str,
        settings: &PlotSettings,
    ) -> Result<()> {
        // Get values and labels
        let categories = df.get_column_string_values(category_col)?;
        let values = df.get_column_numeric_values(value_col)?;

        if categories.len() != values.len() {
            return Err(Error::DimensionMismatch(
                "Category and value columns must have the same length".to_string(),
            ));
        }

        // Aggregate values by category
        let mut category_values = std::collections::HashMap::new();
        for (cat, val) in categories.iter().zip(values.iter()) {
            *category_values.entry(cat.clone()).or_insert(0.0) += *val as f64;
        }

        // Calculate total
        let total: f64 = category_values.values().sum();
        if total <= 0.0 {
            // A zero/negative total makes `value / total` NaN or
            // sign-flipped for every slice (all category_values.sum()
            // to <= 0, e.g. every value is 0), which cannot be drawn as
            // a meaningful pie; fail honestly instead of feeding NaN
            // angles into the canvas arc calls. Mirrors the equivalent
            // guard in `vis::svg::charts::PieChart::render`.
            return Err(Error::InvalidInput(
                "PieChart: total must be positive".to_string(),
            ));
        }

        // Draw pie chart
        let cx = (self.config.width as f64) / 2.0;
        let cy = (self.config.height as f64) / 2.0;
        let radius = (self.config.width.min(self.config.height) as f64) * 0.4;

        // Draw title. A failed text draw is not worth aborting the whole
        // visualization over, so failures are ignored rather than
        // `.expect()`-ed (which would panic and abort the WASM module).
        self.context.set_font("16px sans-serif");
        self.context.set_text_align("center");
        self.context.set_fill_style_str("black");
        let _ = self.context.fill_text(&settings.title, cx, 30.0);

        // Draw pie slices
        let mut start_angle = 0.0;
        let mut i = 0;
        let colors = settings.color_palette.clone();
        // An empty `color_palette` would otherwise make `i % colors.len()`
        // a division-by-zero panic; fall back to a single reasonable
        // default color in that case instead.
        let palette_len = colors.len().max(1);
        let color_at = |idx: usize| -> (u8, u8, u8) {
            colors
                .get(idx % palette_len)
                .copied()
                .unwrap_or((70, 130, 180))
        };

        for (_category, value) in &category_values {
            let slice_angle = 2.0 * std::f64::consts::PI * (value / total);

            let (r, g, b) = color_at(i);
            let color = format!("rgb({}, {}, {})", r, g, b);

            // Draw slice
            self.context.begin_path();
            self.context.move_to(cx, cy);
            let _ = self
                .context
                .arc(cx, cy, radius, start_angle, start_angle + slice_angle);
            self.context.close_path();

            self.context.set_fill_style_str(&color);
            self.context.fill();

            // Draw slice outline
            self.context.set_stroke_style_str("white");
            self.context.set_line_width(1.0);
            self.context.stroke();

            // Calculate label position
            let label_angle = start_angle + slice_angle / 2.0;
            let label_x = cx + radius * 0.7 * label_angle.cos();
            let label_y = cy + radius * 0.7 * label_angle.sin();

            // Draw percentage label
            let percentage = (value / total * 100.0).round() / 10.0 * 10.0;
            self.context.set_font("12px sans-serif");
            self.context.set_fill_style_str("white");
            self.context.set_text_align("center");

            if percentage >= 5.0 {
                // Only show label if slice is big enough
                let _ = self
                    .context
                    .fill_text(&format!("{}%", percentage), label_x, label_y);
            }

            start_angle += slice_angle;
            i += 1;
        }

        // Draw legend
        if settings.show_legend {
            let legend_x = self.config.width as f64 - 150.0;
            let legend_y = 50.0;
            let mut y_offset = 0.0;

            i = 0;
            for (category, _) in &category_values {
                let (r, g, b) = color_at(i);
                let color = format!("rgb({}, {}, {})", r, g, b);

                // Draw color square
                self.context.set_fill_style_str(&color);
                self.context
                    .fill_rect(legend_x, legend_y + y_offset, 15.0, 15.0);

                // Draw category name
                self.context.set_font("12px sans-serif");
                self.context.set_fill_style_str("black");
                self.context.set_text_align("left");

                // Truncate long category names. Byte-slicing
                // (`&category[0..12]`) panics as soon as a category name
                // contains any multi-byte UTF-8 character (e.g. Japanese
                // text) whose encoding straddles byte offset 12; slicing
                // by `char` instead is always a valid boundary.
                let display_cat = if category.chars().count() > 15 {
                    let truncated: String = category.chars().take(12).collect();
                    format!("{}...", truncated)
                } else {
                    category.clone()
                };

                let _ = self.context.fill_text(
                    &display_cat,
                    legend_x + 20.0,
                    legend_y + y_offset + 12.0,
                );

                y_offset += 20.0;
                i += 1;
            }
        }

        Ok(())
    }

    fn draw_heatmap(
        &self,
        df: &DataFrame,
        columns: &[String],
        settings: &PlotSettings,
    ) -> Result<()> {
        // Get matrix of values
        let mut data_matrix = Vec::new();

        for col in columns {
            let values = df.get_column_numeric_values(col)?;
            data_matrix.push(values);
        }

        // Transpose matrix if needed
        let rows = data_matrix.len();
        if rows == 0 {
            return Err(Error::InvalidInput("No data for heatmap".to_string()));
        }

        // The shortest column's length, not just the first column's: if
        // columns are ragged (one shorter than the rest — e.g. from
        // upstream NA-dropping that only touched some columns),
        // indexing every row up to `data_matrix[0].len()` would run past
        // the end of any shorter column and panic. Bounding by the
        // minimum keeps every `data_matrix[i][j]` access below in
        // bounds for every row `i`; any extra trailing values in longer
        // columns are simply not drawn.
        let cols = data_matrix.iter().map(|c| c.len()).min().unwrap_or(0);
        if cols == 0 {
            return Err(Error::InvalidInput("Empty columns for heatmap".to_string()));
        }

        // Find min and max values
        let mut min_val = f64::MAX;
        let mut max_val = f64::MIN;

        for row in &data_matrix {
            for &val in row {
                let val = val as f64;
                if val < min_val {
                    min_val = val;
                }
                if val > max_val {
                    max_val = val;
                }
            }
        }

        // Draw title. Text-draw failures are ignored (not `.expect()`-ed
        // into a panic that would abort the whole WASM module) since a
        // missing label is not worth losing the rest of the chart over.
        self.context.set_font("16px sans-serif");
        self.context.set_text_align("center");
        self.context.set_fill_style_str("black");
        let _ = self
            .context
            .fill_text(&settings.title, (self.config.width as f64) / 2.0, 30.0);

        // Calculate dimensions
        let margin = 70.0;
        let chart_width = self.config.width as f64 - 2.0 * margin;
        let chart_height = self.config.height as f64 - 2.0 * margin;

        let cell_width = chart_width / cols as f64;
        let cell_height = chart_height / rows as f64;

        // Draw heatmap cells
        for i in 0..rows {
            for j in 0..cols {
                let x = margin + j as f64 * cell_width;
                let y = margin + i as f64 * cell_height;

                let val = data_matrix[i][j] as f64;

                // Normalize value between 0 and 1
                let normalized = if max_val > min_val {
                    (val - min_val) / (max_val - min_val)
                } else {
                    0.5 // Avoid division by zero
                };

                // Use a color gradient from blue to red
                let r = (normalized * 255.0) as u8;
                let b = (255.0 - normalized * 255.0) as u8;
                let color = format!("rgb({}, 0, {})", r, b);

                // Draw cell
                self.context.set_fill_style_str(&color);
                self.context.fill_rect(x, y, cell_width, cell_height);

                // Draw cell outline
                self.context.set_stroke_style_str("rgba(255,255,255,0.2)");
                self.context.set_line_width(0.5);
                self.context.stroke_rect(x, y, cell_width, cell_height);

                // Draw value text if cells are large enough
                if cell_width > 30.0 && cell_height > 20.0 {
                    self.context.set_font("10px sans-serif");
                    self.context.set_fill_style_str("white");
                    self.context.set_text_align("center");

                    let _ = self.context.fill_text(
                        &format!("{:.1}", val),
                        x + cell_width / 2.0,
                        y + cell_height / 2.0 + 3.0,
                    );
                }
            }
        }

        // Draw column labels
        self.context.set_font("12px sans-serif");
        self.context.set_fill_style_str("black");
        self.context.set_text_align("center");

        for (j, col) in columns.iter().enumerate().take(cols) {
            let x = margin + j as f64 * cell_width + cell_width / 2.0;
            let y = margin - 10.0;

            // Truncate long column names. Byte-slicing (`&col[0..7]`)
            // panics as soon as a name contains a multi-byte UTF-8
            // character whose encoding straddles byte offset 7 (e.g. a
            // Japanese column name); slicing by `char` is always valid.
            let display_name = if col.chars().count() > 10 {
                let truncated: String = col.chars().take(7).collect();
                format!("{}...", truncated)
            } else {
                col.clone()
            };

            let _ = self.context.fill_text(&display_name, x, y);
        }

        // Draw row labels (use row indices)
        self.context.set_text_align("right");

        for i in 0..rows {
            let x = margin - 10.0;
            let y = margin + i as f64 * cell_height + cell_height / 2.0 + 5.0;

            let _ = self.context.fill_text(&format!("Row {}", i + 1), x, y);
        }

        // Draw color scale
        let scale_width = 20.0;
        let scale_height = chart_height * 0.7;
        let scale_x = self.config.width as f64 - margin / 2.0;
        let scale_y = margin + (chart_height - scale_height) / 2.0;

        for i in 0..100 {
            let normalized = i as f64 / 100.0;
            let r = (normalized * 255.0) as u8;
            let b = (255.0 - normalized * 255.0) as u8;
            let color = format!("rgb({}, 0, {})", r, b);

            self.context.set_fill_style_str(&color);
            self.context.fill_rect(
                scale_x - scale_width / 2.0,
                scale_y + (1.0 - normalized) * scale_height,
                scale_width,
                scale_height / 100.0,
            );
        }

        // Draw scale labels
        self.context.set_font("10px sans-serif");
        self.context.set_fill_style_str("black");
        self.context.set_text_align("center");

        let _ = self
            .context
            .fill_text(&format!("{:.1}", max_val), scale_x, scale_y - 5.0);

        let _ = self.context.fill_text(
            &format!("{:.1}", min_val),
            scale_x,
            scale_y + scale_height + 15.0,
        );

        Ok(())
    }

    // Set up interactive tooltips
    fn setup_tooltip_listeners(&mut self) -> Result<()> {
        // Clean up any existing event listeners
        for (event, listener) in self.event_listeners.drain(..) {
            self.canvas
                .remove_event_listener_with_callback(&event, listener.as_ref().unchecked_ref())
                .map_err(|_| Error::InvalidInput("Failed to remove event listener".to_string()))?;
        }

        // Add mousemove listener for tooltips. All drawing happens on
        // `overlay_context` — a separate canvas stacked on top of the
        // chart (see the field doc comment on `WebVisualization`) — so
        // hovering the mouse never clears or otherwise touches the
        // chart canvas itself, unlike the previous implementation which
        // called `clear_rect` on the chart's own context every move.
        let canvas_clone = self.canvas.clone();
        let overlay_context_clone = self.overlay_context.clone();
        let config_clone = self.config.clone();
        let data_clone = self.data.clone();

        let mousemove_callback = Closure::wrap(Box::new(move |event: web_sys::MouseEvent| {
            let rect = canvas_clone.get_bounding_client_rect();
            let x = event.client_x() as f64 - rect.left();
            let y = event.client_y() as f64 - rect.top();
            let width = config_clone.width as f64;
            let height = config_clone.height as f64;

            overlay_context_clone.clear_rect(0.0, 0.0, width, height);

            if x < 0.0 || y < 0.0 || x > width || y > height {
                return;
            }
            let Some(data) = &data_clone else {
                return;
            };
            let df = data.borrow();
            let row_count = df.row_count();
            if row_count == 0 {
                return;
            }

            // Map the cursor's horizontal position proportionally across
            // the canvas to a row index and read that row's real values
            // out of the DataFrame. This does not reproduce plotters'
            // exact per-chart margin/label-area pixel geometry — that
            // transform is internal to each `render_*_chart` call and
            // is not retained afterward — but it is a real, monotonic
            // lookup into the actual plotted data, not a fixed string.
            let frac = (x / width.max(1.0)).clamp(0.0, 1.0);
            let row = ((frac * row_count as f64) as usize).min(row_count - 1);

            let mut lines: Vec<String> = vec![format!("row {}", row)];
            for col in df.column_names() {
                if lines.len() > 5 {
                    lines.push("...".to_string());
                    break;
                }
                if let Ok(values) = df.get_column_string_values(col) {
                    if let Some(v) = values.get(row) {
                        lines.push(format!("{}: {}", col, v));
                    }
                }
            }

            let line_height = 14.0;
            let box_h = line_height * lines.len() as f64 + 10.0;
            let box_w =
                lines.iter().map(|l| l.chars().count()).max().unwrap_or(4) as f64 * 6.5 + 16.0;
            let box_x = (x + 12.0).min((width - box_w).max(0.0));
            let box_y = (y - box_h - 8.0).max(0.0);

            overlay_context_clone.set_fill_style_str("rgba(0,0,0,0.75)");
            overlay_context_clone.fill_rect(box_x, box_y, box_w, box_h);

            overlay_context_clone.set_font("11px sans-serif");
            overlay_context_clone.set_fill_style_str("white");
            overlay_context_clone.set_text_align("left");
            for (i, line) in lines.iter().enumerate() {
                // A failed tooltip draw is not worth aborting the whole
                // WASM module over: ignore it and move on.
                let _ = overlay_context_clone.fill_text(
                    line,
                    box_x + 8.0,
                    box_y + 14.0 + i as f64 * line_height,
                );
            }
        }) as Box<dyn FnMut(_)>);

        self.canvas
            .add_event_listener_with_callback(
                "mousemove",
                mousemove_callback.as_ref().unchecked_ref(),
            )
            .map_err(|_| Error::InvalidInput("Failed to add event listener".to_string()))?;
        self.event_listeners
            .push(("mousemove".to_string(), mousemove_callback));

        // Clear any lingering tooltip once the cursor leaves the chart.
        let overlay_context_leave = self.overlay_context.clone();
        let config_leave = self.config.clone();
        let mouseleave_callback = Closure::wrap(Box::new(move |_event: web_sys::MouseEvent| {
            overlay_context_leave.clear_rect(
                0.0,
                0.0,
                config_leave.width as f64,
                config_leave.height as f64,
            );
        }) as Box<dyn FnMut(_)>);
        self.canvas
            .add_event_listener_with_callback(
                "mouseleave",
                mouseleave_callback.as_ref().unchecked_ref(),
            )
            .map_err(|_| Error::InvalidInput("Failed to add event listener".to_string()))?;
        self.event_listeners
            .push(("mouseleave".to_string(), mouseleave_callback));

        Ok(())
    }

    /// Export the visualization as an image
    #[wasm_bindgen]
    pub fn export_image(&self) -> WebResult<String> {
        self.canvas
            .to_data_url()
            .map_err(|_| to_js_error("Failed to export image"))
    }

    /// Update title
    #[wasm_bindgen]
    pub fn update_title(&mut self, title: &str) -> WebResult<()> {
        self.config.title = title.to_string();
        self.render()
    }

    /// Change visualization type
    #[wasm_bindgen]
    pub fn change_type(&mut self, viz_type: VisualizationType) -> WebResult<()> {
        self.config.viz_type = viz_type;
        self.render()
    }

    /// Update theme
    #[wasm_bindgen]
    pub fn update_theme(&mut self, theme: ColorTheme) -> WebResult<()> {
        self.config.theme = theme;
        self.render()
    }
}

// Real chart rendering for the web canvas backend.
//
// Every drawing routine here is generic over `DB: DrawingBackend`
// instead of being written directly against `CanvasBackend`. This is
// what makes these renderers testable at all under `cargo nextest`:
// `CanvasBackend` can only be constructed from a real
// `web_sys::HtmlCanvasElement`, which requires an actual browser/DOM and
// does not exist when running natively, so a native test exercises the
// exact same drawing code against `SVGBackend`/`BitMapBackend` instead.
// The public `plot_*_for_web` entry points stay concrete over
// `CanvasBackend` because that is what `WebVisualization::render` calls
// them with.
mod plotters_ext_web {
    use super::*;
    use crate::vis::plotters_ext::{PlotKind, PlotSettings};
    use plotters::coord::Shift;
    use plotters::prelude::*;

    /// Draw one or more (name, x, y, color) series as a line, scatter,
    /// bar, or area chart onto an already-created drawing area.
    fn draw_xy_chart<DB: DrawingBackend>(
        drawing_area: &DrawingArea<DB, Shift>,
        settings: &PlotSettings,
        series: &[(String, Vec<f64>, Vec<f64>, (u8, u8, u8))],
    ) -> Result<()>
    where
        DB::ErrorType: std::error::Error + Send + Sync + 'static,
    {
        if series.is_empty() || series.iter().all(|(_, x, _, _)| x.is_empty()) {
            return Err(Error::EmptyData("No data to plot".to_string()));
        }

        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 (_, xs, ys, _) in series {
            for &v in xs.iter().filter(|v| v.is_finite()) {
                x_min = x_min.min(v);
                x_max = x_max.max(v);
            }
            for &v in ys.iter().filter(|v| v.is_finite()) {
                y_min = y_min.min(v);
                y_max = y_max.max(v);
            }
        }
        if !x_min.is_finite() || !y_min.is_finite() {
            return Err(Error::EmptyData("No finite data to plot".to_string()));
        }
        // Keep the zero baseline visible for bar/area charts, the same
        // way the SVG bar chart does, instead of letting an all-positive
        // or all-negative series push it off the drawn range.
        if matches!(settings.plot_kind, PlotKind::Bar | PlotKind::Area) {
            y_min = y_min.min(0.0);
            y_max = y_max.max(0.0);
        }

        let x_range = if (x_max - x_min).abs() < f64::EPSILON {
            1.0
        } else {
            x_max - x_min
        };
        let y_range = if (y_max - y_min).abs() < f64::EPSILON {
            1.0
        } else {
            y_max - y_min
        };
        let x_margin = x_range * 0.05;
        let y_margin = y_range * 0.05;

        let mut chart = ChartBuilder::on(drawing_area)
            .caption(&settings.title, ("sans-serif", 24).into_font())
            .margin(10)
            .x_label_area_size(30)
            .y_label_area_size(40)
            .build_cartesian_2d(
                (x_min - x_margin)..(x_max + x_margin),
                (y_min - y_margin)..(y_max + y_margin),
            )?;

        let mut mesh = chart.configure_mesh();
        mesh.x_desc(&settings.x_label).y_desc(&settings.y_label);
        if !settings.show_grid {
            mesh.disable_mesh();
        }
        mesh.draw()?;

        for (name, xs, ys, rgb) in series {
            let color = RGBColor(rgb.0, rgb.1, rgb.2);
            let points: Vec<(f64, f64)> = xs
                .iter()
                .zip(ys.iter())
                .map(|(&x, &y)| (x, y))
                .filter(|(x, y)| x.is_finite() && y.is_finite())
                .collect();
            if points.is_empty() {
                continue;
            }

            match settings.plot_kind {
                PlotKind::Line => {
                    let handle =
                        chart.draw_series(LineSeries::new(points.iter().copied(), color))?;
                    if settings.show_legend {
                        handle.label(name.clone()).legend(move |(x, y)| {
                            PathElement::new(vec![(x, y), (x + 20, y)], color)
                        });
                    }
                }
                PlotKind::Scatter => {
                    let handle = chart.draw_series(
                        points
                            .iter()
                            .map(|&(x, y)| Circle::new((x, y), 3, color.filled())),
                    )?;
                    if settings.show_legend {
                        handle
                            .label(name.clone())
                            .legend(move |(x, y)| Circle::new((x + 10, y), 3, color.filled()));
                    }
                }
                PlotKind::Bar => {
                    let mut xs_sorted: Vec<f64> = points.iter().map(|&(x, _)| x).collect();
                    xs_sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
                    let bar_width = if xs_sorted.len() <= 1 {
                        0.5
                    } else {
                        let mut min_diff = f64::INFINITY;
                        for w in xs_sorted.windows(2) {
                            let diff = (w[1] - w[0]).abs();
                            if diff > 0.0 && diff < min_diff {
                                min_diff = diff;
                            }
                        }
                        if min_diff.is_finite() {
                            min_diff * 0.8
                        } else {
                            0.5
                        }
                    };
                    let handle = chart.draw_series(points.iter().map(|&(x, y)| {
                        Rectangle::new(
                            [(x - bar_width / 2.0, 0.0), (x + bar_width / 2.0, y)],
                            color.filled(),
                        )
                    }))?;
                    if settings.show_legend {
                        handle.label(name.clone()).legend(move |(x, y)| {
                            Rectangle::new([(x, y - 5), (x + 20, y + 5)], color.filled())
                        });
                    }
                }
                PlotKind::Area => {
                    let handle = chart.draw_series(AreaSeries::new(
                        points.iter().copied(),
                        0.0,
                        color.mix(0.2),
                    ))?;
                    if settings.show_legend {
                        handle.label(name.clone()).legend(move |(x, y)| {
                            PathElement::new(vec![(x, y), (x + 20, y)], color)
                        });
                    }
                }
                PlotKind::Histogram | PlotKind::BoxPlot => {
                    return Err(Error::NotImplemented(
                        "use plot_histogram_for_web/plot_boxplot_for_web for this plot kind"
                            .to_string(),
                    ));
                }
            }
        }

        if settings.show_legend {
            chart
                .configure_series_labels()
                .background_style(WHITE.mix(0.8))
                .border_style(BLACK)
                .draw()?;
        }

        Ok(())
    }

    fn numeric_series_for_columns(
        df: &DataFrame,
        columns: &[&str],
        settings: &PlotSettings,
    ) -> Result<Vec<(String, Vec<f64>, Vec<f64>, (u8, u8, u8))>> {
        let mut out = Vec::with_capacity(columns.len());
        for (i, &col) in columns.iter().enumerate() {
            let values = df.get_column_numeric_values(col)?;
            let indices: Vec<f64> = (0..values.len()).map(|i| i as f64).collect();
            let palette_len = settings.color_palette.len().max(1);
            let color = settings
                .color_palette
                .get(i % palette_len)
                .copied()
                .unwrap_or((70, 130, 180));
            out.push((col.to_string(), indices, values, color));
        }
        Ok(out)
    }

    /// Multi-series line chart (one line per column, sharing a row-index
    /// x-axis) — used for [`VisualizationType::Line`].
    pub fn plot_multi_series_for_web<DB: DrawingBackend>(
        df: &DataFrame,
        columns: &[&str],
        drawing_area: DrawingArea<DB, Shift>,
        settings: &PlotSettings,
    ) -> Result<()>
    where
        DB::ErrorType: std::error::Error + Send + Sync + 'static,
    {
        let series = numeric_series_for_columns(df, columns, settings)?;
        draw_xy_chart(&drawing_area, settings, &series)
    }

    /// Single-column chart (bar or area, per `settings.plot_kind`)
    /// plotted against a row-index x-axis — used for
    /// [`VisualizationType::Bar`] and [`VisualizationType::Area`].
    pub fn plot_column_for_web<DB: DrawingBackend>(
        df: &DataFrame,
        column: &str,
        drawing_area: DrawingArea<DB, Shift>,
        settings: &PlotSettings,
    ) -> Result<()>
    where
        DB::ErrorType: std::error::Error + Send + Sync + 'static,
    {
        let series = numeric_series_for_columns(df, &[column], settings)?;
        draw_xy_chart(&drawing_area, settings, &series)
    }

    /// Scatter plot of two numeric columns — used for
    /// [`VisualizationType::Scatter`].
    pub fn plot_scatter_for_web<DB: DrawingBackend>(
        df: &DataFrame,
        x_column: &str,
        y_column: &str,
        drawing_area: DrawingArea<DB, Shift>,
        settings: &PlotSettings,
    ) -> Result<()>
    where
        DB::ErrorType: std::error::Error + Send + Sync + 'static,
    {
        let x_values = df.get_column_numeric_values(x_column)?;
        let y_values = df.get_column_numeric_values(y_column)?;
        if x_values.len() != y_values.len() {
            return Err(Error::DimensionMismatch(
                "x and y columns must have the same length".to_string(),
            ));
        }
        let color = settings
            .color_palette
            .first()
            .copied()
            .unwrap_or((70, 130, 180));
        let series = vec![(
            format!("{} vs {}", y_column, x_column),
            x_values,
            y_values,
            color,
        )];
        draw_xy_chart(&drawing_area, settings, &series)
    }

    /// Histogram of one numeric column — used for
    /// [`VisualizationType::Histogram`].
    pub fn plot_histogram_for_web<DB: DrawingBackend>(
        df: &DataFrame,
        column: &str,
        bins: usize,
        drawing_area: DrawingArea<DB, Shift>,
        settings: &PlotSettings,
    ) -> Result<()>
    where
        DB::ErrorType: std::error::Error + Send + Sync + 'static,
    {
        let values = df.get_column_numeric_values(column)?;
        let finite: Vec<f64> = values.into_iter().filter(|v| v.is_finite()).collect();
        if finite.is_empty() {
            return Err(Error::EmptyData("No data to plot".to_string()));
        }
        if bins == 0 {
            return Err(Error::InvalidInput(
                "Histogram: bins must be greater than 0".to_string(),
            ));
        }

        let min_val = finite.iter().cloned().fold(f64::INFINITY, f64::min);
        let max_val = finite.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
        // A constant column 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 counts = vec![0usize; bins];
        for &v in &finite {
            let idx = ((v - min_val) / bin_width).floor() as usize;
            counts[idx.min(bins - 1)] += 1;
        }
        let max_freq = *counts.iter().max().unwrap_or(&1) as f64;

        let mut chart = ChartBuilder::on(&drawing_area)
            .caption(&settings.title, ("sans-serif", 24).into_font())
            .margin(10)
            .x_label_area_size(30)
            .y_label_area_size(40)
            .build_cartesian_2d(
                min_val..(max_val + bin_width * 0.1).max(min_val + f64::EPSILON),
                0.0..(max_freq * 1.1).max(1.0),
            )?;

        let mut mesh = chart.configure_mesh();
        mesh.x_desc(&settings.x_label).y_desc("Frequency");
        if !settings.show_grid {
            mesh.disable_mesh();
        }
        mesh.draw()?;

        let color_rgb = settings
            .color_palette
            .first()
            .copied()
            .unwrap_or((70, 130, 180));
        let color = RGBColor(color_rgb.0, color_rgb.1, color_rgb.2);
        chart.draw_series(counts.iter().enumerate().map(|(i, &count)| {
            let x0 = min_val + i as f64 * bin_width;
            let x1 = x0 + bin_width;
            Rectangle::new([(x0, 0.0), (x1, count as f64)], color.mix(0.7).filled())
        }))?;

        Ok(())
    }

    /// Box plot of a numeric column grouped by a category column — used
    /// for [`VisualizationType::BoxPlot`].
    ///
    /// Delegates the statistics and box/whisker/median/outlier drawing
    /// to [`crate::vis::plotters::boxplot_stats`] and
    /// [`crate::vis::plotters::draw_boxplot_series`], the same shared
    /// implementation the PNG/SVG box plot backends use, so a real box
    /// width and real median line are not yet another divergent copy.
    pub fn plot_boxplot_for_web<DB: DrawingBackend>(
        df: &DataFrame,
        category_column: &str,
        value_column: &str,
        drawing_area: DrawingArea<DB, Shift>,
        settings: &PlotSettings,
    ) -> Result<()>
    where
        DB::ErrorType: std::error::Error + Send + Sync + 'static,
    {
        let categories_raw = df.get_column_string_values(category_column)?;
        let values = df.get_column_numeric_values(value_column)?;
        if categories_raw.len() != values.len() {
            return Err(Error::DimensionMismatch(
                "category and value columns must have the same length".to_string(),
            ));
        }

        let mut category_map: std::collections::HashMap<String, Vec<f64>> =
            std::collections::HashMap::new();
        for (cat, val) in categories_raw.into_iter().zip(values.into_iter()) {
            category_map.entry(cat).or_default().push(val);
        }
        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(Error::EmptyData("No data to plot".to_string()));
        }

        let all_stats: Vec<crate::vis::plotters::BoxPlotStats> = categories
            .iter()
            .filter_map(|c| category_map.get(c))
            .filter_map(|v| crate::vis::plotters::boxplot_stats(v))
            .collect();
        if all_stats.is_empty() {
            return Err(Error::EmptyData("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_margin = y_range * 0.1;

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

        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)
            .x_desc(&settings.x_label)
            .y_desc(&settings.y_label);
        if !settings.show_grid {
            mesh.disable_mesh();
        }
        mesh.draw()?;

        crate::vis::plotters::draw_boxplot_series(
            &mut chart,
            &categories,
            &category_map,
            &settings.color_palette,
        )
    }
}