plotkit-core 0.5.0

Core types and logic for the plotkit plotting library
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
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
//! Artist types -- data + styling for each visual chart element.
//!
//! Artists are the data-carrying objects stored in [`Axes`]. Each artist type
//! holds the data-space geometry and styling for one visual element. When the
//! figure is rendered, the renderer iterates over the artist list and draws
//! each one according to its variant.
//!
//! [`Axes`]: crate::axes::Axes
//!
//! # Variants
//!
//! | Variant          | Description                                     |
//! |------------------|-------------------------------------------------|
//! | [`Line`]         | A polyline connecting (x, y) points.             |
//! | [`Scatter`]      | Individual markers at (x, y) positions.          |
//! | [`Bar`]          | Vertical or horizontal bars over categories.     |
//! | [`Histogram`]    | Binned frequency distribution of a single series.|
//! | [`FillBetween`]  | Shaded region between two y-series.              |
//! | [`Pie`]          | A pie chart showing proportional wedge slices.   |
//! | [`Violin`]       | A violin plot showing kernel density estimates.  |
//! | [`Polar`]        | A polar line or filled radar chart.               |
//! | [`Hexbin`]       | Hexagonal binning plot showing point density.    |
//! | [`Waterfall`]    | Cumulative positive/negative change bars.        |
//!
//! [`Line`]: Artist::Line
//! [`Scatter`]: Artist::Scatter
//! [`Bar`]: Artist::Bar
//! [`Histogram`]: Artist::Histogram
//! [`FillBetween`]: Artist::FillBetween
//! [`Pie`]: Artist::Pie
//! [`Violin`]: Artist::Violin
//! [`Polar`]: Artist::Polar
//! [`Hexbin`]: Artist::Hexbin
//! [`Waterfall`]: Artist::Waterfall

use crate::charts::boxplot::BoxStats;
use crate::colormap::Colormap;
use crate::decimate::DecimateMethod;
use crate::primitives::Color;
use crate::series::{Categories, Series};
use crate::theme::{LineStyle, Marker};

// ---------------------------------------------------------------------------
// Artist enum
// ---------------------------------------------------------------------------

/// A visual element drawn on an axes.
///
/// `Artist` is the primary unit of chart content. Each variant wraps a
/// concrete artist struct that stores the data, colors, and styling needed
/// to render one visual element. The enum provides convenience accessors
/// ([`label`](Artist::label), [`color`](Artist::color),
/// [`data_bounds`](Artist::data_bounds)) that dispatch to the inner type.
#[derive(Debug, Clone)]
pub enum Artist {
    /// A line chart connecting (x, y) points.
    Line(LineArtist),
    /// A scatter plot of individual points.
    Scatter(ScatterArtist),
    /// A bar chart (vertical or horizontal).
    Bar(BarArtist),
    /// A histogram (binned frequency distribution).
    Histogram(HistArtist),
    /// A filled region between two y-series sharing a common x-series.
    FillBetween(FillBetweenArtist),
    /// A step (staircase) chart connecting data points.
    Step(StepArtist),
    /// A stem (lollipop) chart from data points.
    Stem(StemArtist),
    /// A box-and-whisker plot showing distribution summaries.
    BoxPlot(BoxPlotArtist),
    /// An error bar plot showing data points with uncertainty bars.
    ErrorBar(ErrorBarArtist),
    /// A heatmap showing a 2D grid of values mapped to colors.
    Heatmap(HeatmapArtist),
    /// A pie chart showing proportional wedge slices.
    Pie(PieArtist),
    /// A violin plot showing kernel density estimates of distributions.
    Violin(ViolinArtist),
    /// A contour or filled contour plot over a 2D grid.
    Contour(ContourArtist),
    /// A polar line or filled radar chart in polar coordinates.
    Polar(PolarArtist),
    /// A hexagonal binning plot showing point density as colored hexagons.
    Hexbin(HexbinArtist),
    /// A waterfall chart showing cumulative positive and negative changes.
    Waterfall(WaterfallArtist),
}



impl Artist {
    /// Returns the legend label for this artist, if one has been set.
    ///
    /// The legend renderer uses this to decide which artists appear in the
    /// legend. Artists without a label are silently skipped.
    pub fn label(&self) -> Option<&str> {
        match self {
            Artist::Line(a) => a.label.as_deref(),
            Artist::Scatter(a) => a.label.as_deref(),
            Artist::Bar(a) => a.label.as_deref(),
            Artist::Histogram(a) => a.label.as_deref(),
            Artist::FillBetween(a) => a.label.as_deref(),
            Artist::Step(a) => a.label.as_deref(),
            Artist::Stem(a) => a.label.as_deref(),
            Artist::BoxPlot(a) => a.label.as_deref(),
            Artist::ErrorBar(a) => a.label.as_deref(),
            Artist::Heatmap(a) => a.label.as_deref(),
            Artist::Pie(a) => a.label.as_deref(),
            Artist::Violin(a) => a.label.as_deref(),
            Artist::Contour(a) => a.label.as_deref(),
            Artist::Polar(a) => a.label.as_deref(),
            Artist::Hexbin(a) => a.label.as_deref(),
            Artist::Waterfall(a) => a.label.as_deref(),
        }
    }

    /// Returns the primary color of this artist.
    ///
    /// Used by the legend to draw a color swatch next to the label, and by
    /// any other component that needs to identify an artist's color (e.g.
    /// tooltip rendering).
    pub fn color(&self) -> Color {
        match self {
            Artist::Line(a) => a.color,
            Artist::Scatter(a) => a.color,
            Artist::Bar(a) => a.color,
            Artist::Histogram(a) => a.color,
            Artist::FillBetween(a) => a.color,
            Artist::Step(a) => a.color,
            Artist::Stem(a) => a.color,
            Artist::BoxPlot(a) => a.color,
            Artist::ErrorBar(a) => a.color,
            Artist::Heatmap(a) => a.color,
            Artist::Pie(a) => a.color,
            Artist::Violin(a) => a.color,
            Artist::Contour(a) => a.color,
            Artist::Polar(a) => a.color,
            Artist::Hexbin(a) => a.color,
            Artist::Waterfall(a) => a.color,
        }
    }

    /// Returns the data-space bounding box as `(xmin, xmax, ymin, ymax)`.
    ///
    /// The axes autoscaling logic calls this on every artist to compute the
    /// tightest axis limits that contain all visible data. If a series is
    /// empty or contains no finite values, the corresponding min/max pair
    /// falls back to `(0.0, 1.0)` so that the axes always have a non-zero
    /// extent.
    pub fn data_bounds(&self) -> (f64, f64, f64, f64) {
        match self {
            Artist::Line(a) => a.data_bounds(),
            Artist::Scatter(a) => a.data_bounds(),
            Artist::Bar(a) => a.data_bounds(),
            Artist::Histogram(a) => a.data_bounds(),
            Artist::FillBetween(a) => a.data_bounds(),
            Artist::Step(a) => a.data_bounds(),
            Artist::Stem(a) => a.data_bounds(),
            Artist::BoxPlot(a) => a.data_bounds(),
            Artist::ErrorBar(a) => a.data_bounds(),
            Artist::Heatmap(a) => a.data_bounds(),
            Artist::Pie(a) => a.data_bounds(),
            Artist::Violin(a) => a.data_bounds(),
            Artist::Contour(a) => a.data_bounds(),
            Artist::Polar(a) => a.data_bounds(),
            Artist::Hexbin(a) => a.data_bounds(),
            Artist::Waterfall(a) => a.data_bounds(),
        }
    }
}

// ---------------------------------------------------------------------------
// Helper: safe bounds with fallback
// ---------------------------------------------------------------------------

/// Returns `(min, max)` of the finite values in `series`, falling back to
/// `(fallback_min, fallback_max)` when the series is empty or entirely
/// non-finite.
fn series_bounds_or(series: &Series, fallback_min: f64, fallback_max: f64) -> (f64, f64) {
    match series.bounds() {
        Some((lo, hi)) => (lo, hi),
        None => (fallback_min, fallback_max),
    }
}

// ---------------------------------------------------------------------------
// LineArtist
// ---------------------------------------------------------------------------

/// A line chart connecting a sequence of (x, y) data points.
///
/// The `x` and `y` series must have the same length. Points are drawn in
/// order, producing a single connected polyline with the configured stroke
/// style.
#[derive(Debug, Clone)]
pub struct LineArtist {
    /// X-coordinates of the data points.
    pub x: Series,
    /// Y-coordinates of the data points.
    pub y: Series,
    /// Stroke color of the line.
    pub color: Color,
    /// Stroke width in pixels.
    pub width: f64,
    /// Stroke pattern (solid, dashed, dotted, dash-dot).
    pub style: LineStyle,
    /// Optional legend label. When `Some`, the line appears in the legend.
    pub label: Option<String>,
    /// Opacity from 0.0 (fully transparent) to 1.0 (fully opaque).
    pub alpha: f64,
    /// Optional decimation: `(threshold, method)`. When set and data length
    /// exceeds `threshold`, the rendering pipeline downsamples the data
    /// before drawing.
    pub decimate: Option<(usize, DecimateMethod)>,
}

impl LineArtist {
    /// Computes the data-space bounding box `(xmin, xmax, ymin, ymax)`.
    ///
    /// Falls back to `(0.0, 1.0)` on each axis when the corresponding
    /// series contains no finite values.
    pub fn data_bounds(&self) -> (f64, f64, f64, f64) {
        let (xmin, xmax) = series_bounds_or(&self.x, 0.0, 1.0);
        let (ymin, ymax) = series_bounds_or(&self.y, 0.0, 1.0);
        (xmin, xmax, ymin, ymax)
    }
}

// ---------------------------------------------------------------------------
// ScatterArtist
// ---------------------------------------------------------------------------

/// A scatter plot rendering individual markers at (x, y) positions.
///
/// Each data point is drawn as a marker whose shape, size, and color can be
/// configured. An optional per-point `colors` vector overrides the uniform
/// `color` field, enabling colormap-based visualizations.
#[derive(Debug, Clone)]
pub struct ScatterArtist {
    /// X-coordinates of the data points.
    pub x: Series,
    /// Y-coordinates of the data points.
    pub y: Series,
    /// Default marker color (used when `colors` is `None`).
    pub color: Color,
    /// Marker shape.
    pub marker: Marker,
    /// Marker diameter in pixels.
    pub size: f64,
    /// Optional legend label. When `Some`, the scatter appears in the legend.
    pub label: Option<String>,
    /// Opacity from 0.0 (fully transparent) to 1.0 (fully opaque).
    pub alpha: f64,
    /// Optional per-point colors for colormap-driven scatter plots.
    ///
    /// When set, `colors.len()` must equal `x.len()` (and `y.len()`). Each
    /// entry overrides `color` for the corresponding data point.
    pub colors: Option<Vec<Color>>,
    /// Optional per-point scalar values for colormap-driven coloring.
    ///
    /// When set together with `cmap`, each value is mapped through the
    /// colormap to produce per-point colors. Takes precedence over `colors`.
    pub c: Option<Vec<f64>>,
    /// Optional colormap used to map `c` values to colors.
    pub cmap: Option<Colormap>,
}

impl ScatterArtist {
    /// Computes the data-space bounding box `(xmin, xmax, ymin, ymax)`.
    ///
    /// Falls back to `(0.0, 1.0)` on each axis when the corresponding
    /// series contains no finite values.
    pub fn data_bounds(&self) -> (f64, f64, f64, f64) {
        let (xmin, xmax) = series_bounds_or(&self.x, 0.0, 1.0);
        let (ymin, ymax) = series_bounds_or(&self.y, 0.0, 1.0);
        (xmin, xmax, ymin, ymax)
    }
}

// ---------------------------------------------------------------------------
// BarArtist
// ---------------------------------------------------------------------------

/// A bar chart rendering vertical or horizontal bars over categorical data.
///
/// Categories are placed at integer positions `0, 1, 2, ...` on the
/// category axis, with each bar centered on its position. The `bar_width`
/// field controls the fraction of the inter-category spacing that the bar
/// occupies (1.0 = bars touching, 0.5 = half-width with gaps).
///
/// For stacked bars, set `bottom` to offset each bar from a baseline other
/// than zero. For grouped (side-by-side) bars, adjust category positions and
/// `bar_width` for each series.
#[derive(Debug, Clone)]
pub struct BarArtist {
    /// Category labels for the bar axis.
    pub categories: Categories,
    /// Bar heights (or lengths, for horizontal bars).
    pub heights: Series,
    /// Fill color of the bars.
    pub color: Color,
    /// Optional legend label. When `Some`, the bar series appears in the legend.
    pub label: Option<String>,
    /// Opacity from 0.0 (fully transparent) to 1.0 (fully opaque).
    pub alpha: f64,
    /// When `true`, bars extend horizontally (categories on the y-axis).
    pub horizontal: bool,
    /// Bar width as a fraction of the category spacing (0.0, 1.0].
    pub bar_width: f64,
    /// Optional per-bar base offset for stacking.
    ///
    /// When `Some`, each bar starts at `bottom[i]` instead of `0.0` and extends
    /// to `bottom[i] + heights[i]`. The length must equal `heights.len()`.
    pub bottom: Option<Vec<f64>>,
    /// Optional per-bar x-position offset for grouped (side-by-side) bars.
    ///
    /// When `Some`, each bar's category center is shifted by `offset[i]` data
    /// units. The length must equal `heights.len()`.
    pub offset: Option<Vec<f64>>,
}

impl BarArtist {
    /// Computes the data-space bounding box `(xmin, xmax, ymin, ymax)`.
    ///
    /// For vertical bars, the x-axis spans from `-0.5` to `n - 0.5` (where
    /// `n` is the number of categories) so that bars are centered on integer
    /// positions. The y-axis spans from `0.0` to the tallest bar, with a
    /// fallback of `(0.0, 1.0)` when the heights series is empty.
    ///
    /// When `bottom` is set, the value axis includes both the bottom offsets
    /// and `bottom + height` values. When `offset` is set, the category axis
    /// is expanded to accommodate shifted bar positions.
    ///
    /// For horizontal bars the axes are transposed: the y-axis holds the
    /// category positions and the x-axis holds the bar lengths.
    pub fn data_bounds(&self) -> (f64, f64, f64, f64) {
        let n = self.categories.len() as f64;

        // Determine the extent along the value axis (heights / lengths),
        // accounting for an optional bottom offset.
        let (height_min, height_max) = if let Some(ref bot) = self.bottom {
            let mut vmin = f64::INFINITY;
            let mut vmax = f64::NEG_INFINITY;
            for i in 0..self.heights.len() {
                let b = if i < bot.len() { bot[i] } else { 0.0 };
                let h = self.heights.data[i];
                let top = b + h;
                vmin = vmin.min(b).min(top);
                vmax = vmax.max(b).max(top);
            }
            if !vmin.is_finite() {
                vmin = 0.0;
            }
            if !vmax.is_finite() {
                vmax = 1.0;
            }
            // Ensure 0.0 is included when all values are positive or negative.
            (vmin.min(0.0), vmax)
        } else {
            let hmin = self.heights.min().unwrap_or(0.0).min(0.0);
            let hmax = self.heights.max().unwrap_or(1.0);
            (hmin, hmax)
        };

        // Category axis runs from -0.5 to n-0.5 so bars are centered on 0..n-1.
        // Expand if offsets push bars outside this range.
        let mut cat_min: f64 = -0.5;
        let mut cat_max: f64 = if n > 0.0 { n - 0.5 } else { 0.5 };
        if let Some(ref off) = self.offset {
            let half_bar = self.bar_width * 0.5;
            for i in 0..self.heights.len() {
                let o = if i < off.len() { off[i] } else { 0.0 };
                let center = i as f64 + o;
                cat_min = cat_min.min(center - half_bar);
                cat_max = cat_max.max(center + half_bar);
            }
        }

        if self.horizontal {
            // Horizontal bars: x = value axis, y = category axis.
            (height_min, height_max, cat_min, cat_max)
        } else {
            // Vertical bars: x = category axis, y = value axis.
            (cat_min, cat_max, height_min, height_max)
        }
    }
}

// ---------------------------------------------------------------------------
// HistArtist
// ---------------------------------------------------------------------------

/// A histogram showing the frequency distribution of a single data series.
///
/// The raw data is retained in `data`, but the binning results (`bin_edges`
/// and `counts`) are expected to be pre-computed when the artist is created
/// (typically by the histogram chart builder). This avoids re-binning during
/// every render pass.
///
/// When `density` is `true`, the `counts` vector stores probability density
/// values (each count divided by `n * bin_width`) rather than raw counts, so
/// that the total area under the histogram integrates to 1.0.
#[derive(Debug, Clone)]
pub struct HistArtist {
    /// The original (un-binned) data values.
    pub data: Series,
    /// The requested number of bins (used for display/debugging; the actual
    /// bin count is `bin_edges.len() - 1`).
    pub bins: usize,
    /// Sorted bin edges of length `bins + 1`. The i-th bin spans
    /// `[bin_edges[i], bin_edges[i+1])`.
    pub bin_edges: Vec<f64>,
    /// The count (or density) for each bin. Length equals `bin_edges.len() - 1`.
    pub counts: Vec<f64>,
    /// Fill color of the histogram bars.
    pub color: Color,
    /// Optional legend label. When `Some`, the histogram appears in the legend.
    pub label: Option<String>,
    /// Opacity from 0.0 (fully transparent) to 1.0 (fully opaque).
    pub alpha: f64,
    /// When `true`, `counts` stores probability density instead of raw counts.
    pub density: bool,
}

impl HistArtist {
    /// Computes the data-space bounding box `(xmin, xmax, ymin, ymax)`.
    ///
    /// The x-axis spans from the first bin edge to the last bin edge. The
    /// y-axis spans from `0.0` to the tallest bin count (or density value).
    /// Returns `(0.0, 1.0, 0.0, 1.0)` when there are no bin edges.
    pub fn data_bounds(&self) -> (f64, f64, f64, f64) {
        if self.bin_edges.len() < 2 {
            return (0.0, 1.0, 0.0, 1.0);
        }

        // x-axis: first edge to last edge.
        let xmin = self.bin_edges[0];
        let xmax = self.bin_edges[self.bin_edges.len() - 1];

        // y-axis: 0 to tallest bin.
        let ymax = self
            .counts
            .iter()
            .copied()
            .filter(|v| v.is_finite())
            .fold(0.0_f64, f64::max);

        // Guarantee a non-zero y extent so the axes are always drawable.
        let ymax = if ymax <= 0.0 { 1.0 } else { ymax };

        (xmin, xmax, 0.0, ymax)
    }
}

// ---------------------------------------------------------------------------
// FillBetweenArtist
// ---------------------------------------------------------------------------

/// A filled region between two y-series that share a common x-series.
///
/// The renderer draws a closed polygon connecting `(x, y1)` forward and
/// `(x, y2)` backward, then fills it with the configured color and opacity.
/// This is commonly used for confidence bands, area charts, and shaded
/// difference regions.
#[derive(Debug, Clone)]
pub struct FillBetweenArtist {
    /// X-coordinates shared by both y-series.
    pub x: Series,
    /// Y-coordinates of the first boundary curve.
    pub y1: Series,
    /// Y-coordinates of the second boundary curve.
    pub y2: Series,
    /// Fill color of the shaded region.
    pub color: Color,
    /// Optional legend label. When `Some`, the fill region appears in the legend.
    pub label: Option<String>,
    /// Opacity from 0.0 (fully transparent) to 1.0 (fully opaque).
    pub alpha: f64,
}

impl FillBetweenArtist {
    /// Computes the data-space bounding box `(xmin, xmax, ymin, ymax)`.
    ///
    /// The x-bounds come from the shared `x` series. The y-bounds are the
    /// union of `y1` and `y2` (i.e. the overall min and max across both
    /// boundary curves). Falls back to `(0.0, 1.0)` on any axis that has
    /// no finite values.
    pub fn data_bounds(&self) -> (f64, f64, f64, f64) {
        let (xmin, xmax) = series_bounds_or(&self.x, 0.0, 1.0);

        // Union the y-bounds of both boundary series.
        let y1_min = self.y1.min();
        let y2_min = self.y2.min();
        let y1_max = self.y1.max();
        let y2_max = self.y2.max();

        let ymin = match (y1_min, y2_min) {
            (Some(a), Some(b)) => a.min(b),
            (Some(a), None) => a,
            (None, Some(b)) => b,
            (None, None) => 0.0,
        };

        let ymax = match (y1_max, y2_max) {
            (Some(a), Some(b)) => a.max(b),
            (Some(a), None) => a,
            (None, Some(b)) => b,
            (None, None) => 1.0,
        };

        (xmin, xmax, ymin, ymax)
    }
}

// ---------------------------------------------------------------------------
// BoxPlotArtist
// ---------------------------------------------------------------------------

/// A box-and-whisker plot showing distribution summaries for one or more
/// groups of data.
///
/// Each group produces a box spanning Q1 to Q3 with a median line, whiskers
/// extending to the most extreme data points within the configured fence, and
/// optional outlier dots beyond the whiskers.
#[derive(Debug, Clone)]
pub struct BoxPlotArtist {
    /// Pre-computed summary statistics for each group.
    pub stats: Vec<BoxStats>,
    /// Category labels for the x-axis (one per group).
    pub labels: Vec<String>,
    /// Fill color of the boxes.
    pub color: Color,
    /// Optional legend label.
    pub label: Option<String>,
    /// Opacity from 0.0 (fully transparent) to 1.0 (fully opaque).
    pub alpha: f64,
    /// Box width as a fraction of the category spacing.
    pub box_width: f64,
    /// Whether to draw outlier dots.
    pub show_outliers: bool,
    /// Whisker extent as a multiple of IQR.
    pub whisker_iq_factor: f64,
    /// Raw data retained for re-computing stats when parameters change.
    pub raw_data: Vec<Vec<f64>>,
}

impl BoxPlotArtist {
    /// Computes the data-space bounding box `(xmin, xmax, ymin, ymax)`.
    ///
    /// The x-axis spans from `-0.5` to `n - 0.5` (where `n` is the number
    /// of groups), centering each box on an integer position. The y-axis
    /// spans from the lowest whisker (or outlier) to the highest.
    pub fn data_bounds(&self) -> (f64, f64, f64, f64) {
        let n = self.stats.len();
        if n == 0 {
            return (0.0, 1.0, 0.0, 1.0);
        }
        let xmin = -0.5;
        let xmax = n as f64 - 0.5;
        let mut ymin = f64::INFINITY;
        let mut ymax = f64::NEG_INFINITY;
        for s in &self.stats {
            ymin = ymin.min(s.whisker_low);
            ymax = ymax.max(s.whisker_high);
            for &o in &s.outliers {
                ymin = ymin.min(o);
                ymax = ymax.max(o);
            }
        }
        if !ymin.is_finite() {
            ymin = 0.0;
        }
        if !ymax.is_finite() {
            ymax = 1.0;
        }
        (xmin, xmax, ymin, ymax)
    }
}


// ---------------------------------------------------------------------------
// ErrorBarData
// ---------------------------------------------------------------------------

/// The error data for one axis of an error bar plot.
///
/// Symmetric errors apply the same magnitude on both sides of the data point.
/// Asymmetric errors allow separate low and high magnitudes.
#[derive(Debug, Clone)]
pub enum ErrorBarData {
    /// Equal error on both sides: `y - e` to `y + e`.
    Symmetric(Vec<f64>),
    /// Separate low and high errors: `y - low[i]` to `y + high[i]`.
    Asymmetric {
        /// Error magnitudes below each data point.
        low: Vec<f64>,
        /// Error magnitudes above each data point.
        high: Vec<f64>,
    },
}

// ---------------------------------------------------------------------------
// ErrorBarArtist
// ---------------------------------------------------------------------------

/// An error bar plot showing data points with uncertainty bars.
///
/// Each data point `(x, y)` can have optional horizontal (`xerr`) and/or
/// vertical (`yerr`) error bars. The error bars are drawn as lines with
/// optional caps at the ends.
#[derive(Debug, Clone)]
pub struct ErrorBarArtist {
    /// X-coordinates of the data points.
    pub x: Series,
    /// Y-coordinates of the data points.
    pub y: Series,
    /// Optional x-axis error data.
    pub xerr: Option<ErrorBarData>,
    /// Optional y-axis error data.
    pub yerr: Option<ErrorBarData>,
    /// Color for the center line, error bars, and caps.
    pub color: Color,
    /// Optional legend label.
    pub label: Option<String>,
    /// Cap size in pixels for the error bar ends.
    pub cap_size: f64,
    /// Stroke width of the error bar lines and caps.
    pub line_width: f64,
}

impl ErrorBarArtist {
    /// Computes the data-space bounding box `(xmin, xmax, ymin, ymax)`.
    ///
    /// Includes the extent of error bars when present, so that auto-scaling
    /// shows the full error range.
    pub fn data_bounds(&self) -> (f64, f64, f64, f64) {
        let (mut xmin, mut xmax) = series_bounds_or(&self.x, 0.0, 1.0);
        let (mut ymin, mut ymax) = series_bounds_or(&self.y, 0.0, 1.0);

        // Expand x-bounds by xerr.
        if let Some(ref xerr) = self.xerr {
            for i in 0..self.x.len() {
                let xv = self.x.data[i];
                let (lo, hi) = match xerr {
                    ErrorBarData::Symmetric(e) => (xv - e[i], xv + e[i]),
                    ErrorBarData::Asymmetric { low, high } => (xv - low[i], xv + high[i]),
                };
                xmin = xmin.min(lo);
                xmax = xmax.max(hi);
            }
        }

        // Expand y-bounds by yerr.
        if let Some(ref yerr) = self.yerr {
            for i in 0..self.y.len() {
                let yv = self.y.data[i];
                let (lo, hi) = match yerr {
                    ErrorBarData::Symmetric(e) => (yv - e[i], yv + e[i]),
                    ErrorBarData::Asymmetric { low, high } => (yv - low[i], yv + high[i]),
                };
                ymin = ymin.min(lo);
                ymax = ymax.max(hi);
            }
        }

        (xmin, xmax, ymin, ymax)
    }
}

// ---------------------------------------------------------------------------
// HeatmapArtist
// ---------------------------------------------------------------------------

/// A heatmap showing a 2D grid of values mapped to colors via a colormap.
///
/// Each cell in the grid is filled with a color determined by mapping its
/// value through the configured [`Colormap`]. Optional text annotations
/// can display the numeric value inside each cell.
#[derive(Debug, Clone)]
pub struct HeatmapArtist {
    /// Row-major grid of values. `data[row][col]`.
    pub data: Vec<Vec<f64>>,
    /// Optional column labels for the x-axis.
    pub x_labels: Option<Vec<String>>,
    /// Optional row labels for the y-axis.
    pub y_labels: Option<Vec<String>>,
    /// Colormap used to map cell values to colors.
    pub cmap: Colormap,
    /// Minimum value for colormap normalisation. `None` means auto.
    pub vmin: Option<f64>,
    /// Maximum value for colormap normalisation. `None` means auto.
    pub vmax: Option<f64>,
    /// Whether to draw cell values as text.
    pub show_values: bool,
    /// Primary color (used for legend swatch).
    pub color: Color,
    /// Optional legend label.
    pub label: Option<String>,
    /// Whether to auto-attach a colorbar when this heatmap is drawn.
    pub show_colorbar: bool,
}

impl HeatmapArtist {
    /// Computes the data-space bounding box `(xmin, xmax, ymin, ymax)`.
    ///
    /// The grid spans from `(0, 0)` to `(ncols, nrows)`. Returns
    /// `(0.0, 1.0, 0.0, 1.0)` when the data is empty.
    pub fn data_bounds(&self) -> (f64, f64, f64, f64) {
        let nrows = self.data.len();
        if nrows == 0 {
            return (0.0, 1.0, 0.0, 1.0);
        }
        let ncols = self.data[0].len();
        if ncols == 0 {
            return (0.0, 1.0, 0.0, 1.0);
        }
        (0.0, ncols as f64, 0.0, nrows as f64)
    }
}

// ---------------------------------------------------------------------------
// PieArtist
// ---------------------------------------------------------------------------

/// A pie chart rendering proportional wedge slices from a set of sizes.
///
/// Each entry in `sizes` is automatically normalised so that the wedges
/// sum to a full circle. The starting angle, explode offsets, and colors
/// can be customised through the builder API.
#[derive(Debug, Clone)]
pub struct PieArtist {
    /// Wedge sizes (auto-normalised to sum to 1.0 during rendering).
    pub sizes: Vec<f64>,
    /// Optional labels for each wedge, drawn outside the wedge arc.
    pub labels: Option<Vec<String>>,
    /// Optional custom colors for each wedge. When `None`, the theme
    /// color cycle is used.
    pub colors: Option<Vec<Color>>,
    /// Optional explode offset for each wedge, as a fraction of the
    /// radius. A value of `0.0` means no offset.
    pub explode: Option<Vec<f64>>,
    /// When `true`, percentage labels are drawn at the midpoint of each
    /// wedge arc.
    pub autopct: bool,
    /// Starting angle in degrees, counter-clockwise from the positive
    /// x-axis. Default is `90.0` (top of the circle).
    pub start_angle: f64,
    /// Radius of the pie in data-space units. Default is `1.0`.
    pub radius: f64,
    /// Optional legend label. When `Some`, the pie appears in the legend.
    pub label: Option<String>,
    /// Primary color (used for legend swatch when no custom colors are set).
    pub color: Color,
}

impl PieArtist {
    /// Computes the data-space bounding box `(xmin, xmax, ymin, ymax)`.
    ///
    /// Returns a fixed square region that accommodates the pie radius plus
    /// any explode offsets, with a small margin so that labels do not get
    /// clipped.
    pub fn data_bounds(&self) -> (f64, f64, f64, f64) {
        let max_explode = self
            .explode
            .as_ref()
            .map(|e| e.iter().copied().fold(0.0_f64, f64::max))
            .unwrap_or(0.0);
        let extent = self.radius * (1.0 + max_explode) + 0.1 * self.radius;
        (-extent, extent, -extent, extent)
    }
}

// ---------------------------------------------------------------------------
// ViolinArtist
// ---------------------------------------------------------------------------

/// A violin plot showing the probability density of data distributions.
///
/// Each dataset produces a mirrored kernel density estimate (KDE) shape,
/// similar to a boxplot but showing the full distribution. Optional median
/// and quartile lines can be drawn inside the violin.
#[derive(Debug, Clone)]
pub struct ViolinArtist {
    /// One dataset per violin. Each inner `Vec<f64>` contains the raw values.
    pub datasets: Vec<Vec<f64>>,
    /// Optional x-positions for each violin. Defaults to 1, 2, 3, etc.
    pub positions: Option<Vec<f64>>,
    /// Maximum width of each violin shape.
    pub widths: f64,
    /// Whether to draw a median line inside each violin.
    pub show_median: bool,
    /// Whether to draw Q1/Q3 quartile lines inside each violin.
    pub show_quartiles: bool,
    /// Fill color of the violin shapes.
    pub color: Color,
    /// Opacity from 0.0 (fully transparent) to 1.0 (fully opaque).
    pub alpha: f64,
    /// Optional legend label.
    pub label: Option<String>,
    /// KDE bandwidth override. When <= 0.0, Silverman's rule is used.
    pub bw_method: f64,
}

// ---------------------------------------------------------------------------
// ContourArtist
// ---------------------------------------------------------------------------

/// A contour or filled contour plot over a 2D grid of z = f(x, y) values.
///
/// In unfilled mode (`filled = false`), iso-lines are drawn at each contour
/// level using the marching squares algorithm. In filled mode (`filled = true`),
/// the regions between contour levels are filled with colors from a colormap.
#[derive(Debug, Clone)]
pub struct ContourArtist {
    /// X grid coordinates (length `nx`).
    pub x: Vec<f64>,
    /// Y grid coordinates (length `ny`).
    pub y: Vec<f64>,
    /// Z values on the grid, shape `[ny][nx]` (row-major).
    pub z: Vec<Vec<f64>>,
    /// Explicit contour levels. When `None`, levels are auto-computed.
    pub levels: Option<Vec<f64>>,
    /// Whether to fill regions between levels (`true` for contourf).
    pub filled: bool,
    /// Colormap used to map contour levels to colors.
    pub cmap: Colormap,
    /// Optional explicit colors for each contour level, overriding the colormap.
    pub colors: Option<Vec<Color>>,
    /// Stroke width for contour lines (unfilled mode). Default `1.0`.
    pub linewidths: f64,
    /// Optional legend label.
    pub label: Option<String>,
    /// Primary color (used for legend swatch).
    pub color: Color,
    /// Number of auto-computed levels when `levels` is `None`. Default `10`.
    pub num_levels: usize,
}

impl ContourArtist {
    /// Computes the data-space bounding box `(xmin, xmax, ymin, ymax)`.
    ///
    /// Returns the extent of the x and y grid coordinates. Falls back to
    /// `(0.0, 1.0, 0.0, 1.0)` when either coordinate vector is empty.
    pub fn data_bounds(&self) -> (f64, f64, f64, f64) {
        if self.x.is_empty() || self.y.is_empty() {
            return (0.0, 1.0, 0.0, 1.0);
        }
        let xmin = self.x.iter().copied().fold(f64::INFINITY, f64::min);
        let xmax = self.x.iter().copied().fold(f64::NEG_INFINITY, f64::max);
        let ymin = self.y.iter().copied().fold(f64::INFINITY, f64::min);
        let ymax = self.y.iter().copied().fold(f64::NEG_INFINITY, f64::max);

        let (xmin, xmax) = if xmin.is_finite() && xmax.is_finite() {
            (xmin, xmax)
        } else {
            (0.0, 1.0)
        };
        let (ymin, ymax) = if ymin.is_finite() && ymax.is_finite() {
            (ymin, ymax)
        } else {
            (0.0, 1.0)
        };
        (xmin, xmax, ymin, ymax)
    }
}

// ---------------------------------------------------------------------------
// StepWhere
// ---------------------------------------------------------------------------

/// Controls where the horizontal segment of a step chart is placed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StepWhere {
    /// The y-value changes *before* the x-value (vertical then horizontal).
    Pre,
    /// The y-value changes *after* the x-value (horizontal then vertical).
    Post,
    /// The y-value changes at the midpoint between consecutive x-values.
    Mid,
}

// ---------------------------------------------------------------------------
// StepArtist
// ---------------------------------------------------------------------------

/// A step (staircase) chart.
#[derive(Debug, Clone)]
pub struct StepArtist {
    /// X-coordinates of the data points.
    pub x: Series,
    /// Y-coordinates of the data points.
    pub y: Series,
    /// Stroke color of the step line.
    pub color: Color,
    /// Stroke width in pixels.
    pub width: f64,
    /// Step alignment mode.
    pub where_step: StepWhere,
    /// Optional legend label.
    pub label: Option<String>,
    /// Opacity from 0.0 (fully transparent) to 1.0 (fully opaque).
    pub alpha: f64,
}

impl StepArtist {
    /// Computes the data-space bounding box `(xmin, xmax, ymin, ymax)`.
    pub fn data_bounds(&self) -> (f64, f64, f64, f64) {
        let (xmin, xmax) = series_bounds_or(&self.x, 0.0, 1.0);
        let (ymin, ymax) = series_bounds_or(&self.y, 0.0, 1.0);
        (xmin, xmax, ymin, ymax)
    }
}

// ---------------------------------------------------------------------------
// StemArtist
// ---------------------------------------------------------------------------

/// A stem (lollipop) chart.
#[derive(Debug, Clone)]
pub struct StemArtist {
    /// X-coordinates of the data points.
    pub x: Series,
    /// Y-coordinates of the data points.
    pub y: Series,
    /// Color of the stem lines and markers.
    pub color: Color,
    /// Stroke width of the stem lines in pixels.
    pub line_width: f64,
    /// Diameter of the marker circle in pixels.
    pub marker_size: f64,
    /// The y-value from which stems originate.
    pub baseline: f64,
    /// Optional legend label.
    pub label: Option<String>,
    /// Opacity from 0.0 (fully transparent) to 1.0 (fully opaque).
    pub alpha: f64,
}

impl StemArtist {
    /// Computes the data-space bounding box `(xmin, xmax, ymin, ymax)`.
    ///
    /// The y-bounds include the baseline.
    pub fn data_bounds(&self) -> (f64, f64, f64, f64) {
        let (xmin, xmax) = series_bounds_or(&self.x, 0.0, 1.0);
        let (ymin, ymax) = series_bounds_or(&self.y, 0.0, 1.0);
        (xmin, xmax, ymin.min(self.baseline), ymax.max(self.baseline))
    }
}

// ---------------------------------------------------------------------------
// PolarArtist
// ---------------------------------------------------------------------------

/// A polar line or filled radar chart in polar coordinates.
///
/// Each data point is defined by an angle `theta` (in radians) and a radial
/// distance `r`. In line mode, a polyline connects the data points. In filled
/// mode, the path is closed and the interior is filled, producing a radar or
/// area chart.
///
/// The rendering pipeline converts polar coordinates to Cartesian pixel
/// coordinates using `x = cx + r*cos(theta)`, `y = cy - r*sin(theta)`, draws
/// concentric circles for the r-grid, and radial lines for the theta-grid.
#[derive(Debug, Clone)]
pub struct PolarArtist {
    /// Angles in radians, measured counter-clockwise from the positive x-axis.
    pub theta: Vec<f64>,
    /// Radial distances from the origin. Must have the same length as `theta`.
    pub r: Vec<f64>,
    /// Stroke/fill color.
    pub color: Color,
    /// Optional legend label.
    pub label: Option<String>,
    /// Opacity from 0.0 (fully transparent) to 1.0 (fully opaque).
    pub alpha: f64,
    /// Stroke width in pixels for the polar line.
    pub linewidth: f64,
    /// When `true`, the polar path is closed and filled (radar/area chart).
    pub filled: bool,
    /// Optional marker shape drawn at each data point.
    pub marker: Option<Marker>,
}

// ---------------------------------------------------------------------------
// HexbinArtist
// ---------------------------------------------------------------------------

/// A hexagonal binning (hexbin) plot that visualises point density on a 2D
/// plane using a grid of flat-top hexagons.
///
/// Each hexagon is coloured according to the number of data points that fall
/// within its boundaries, mapped through the configured [`Colormap`]. This
/// is especially useful for large datasets where individual scatter points
/// would overlap heavily.
#[derive(Debug, Clone)]
pub struct HexbinArtist {
    /// X-coordinates of the raw data points.
    pub x: Vec<f64>,
    /// Y-coordinates of the raw data points.
    pub y: Vec<f64>,
    /// Number of hexagons across the x-axis. Default `20`.
    pub gridsize: usize,
    /// Colormap used to map bin counts to colors.
    pub cmap: Colormap,
    /// Minimum point count for a hex to be drawn. Default `1`.
    pub mincnt: usize,
    /// Opacity from 0.0 (fully transparent) to 1.0 (fully opaque).
    pub alpha: f64,
    /// Primary color (used for legend swatch).
    pub color: Color,
    /// Optional legend label.
    pub label: Option<String>,
    /// Optional edge (stroke) color for hexagons.
    pub edgecolor: Option<Color>,
    /// Whether to auto-attach a colorbar when this hexbin is drawn.
    pub show_colorbar: bool,
}

impl HexbinArtist {
    /// Computes the data-space bounding box `(xmin, xmax, ymin, ymax)`.
    ///
    /// Returns the extent of the finite x and y values. Falls back to
    /// `(0.0, 1.0, 0.0, 1.0)` when data is empty or entirely non-finite.
    pub fn data_bounds(&self) -> (f64, f64, f64, f64) {
        if self.x.is_empty() || self.y.is_empty() {
            return (0.0, 1.0, 0.0, 1.0);
        }

        let mut xmin = f64::INFINITY;
        let mut xmax = f64::NEG_INFINITY;
        let mut ymin = f64::INFINITY;
        let mut ymax = f64::NEG_INFINITY;

        for &v in &self.x {
            if v.is_finite() {
                if v < xmin { xmin = v; }
                if v > xmax { xmax = v; }
            }
        }
        for &v in &self.y {
            if v.is_finite() {
                if v < ymin { ymin = v; }
                if v > ymax { ymax = v; }
            }
        }

        let (xmin, xmax) = if xmin.is_finite() && xmax.is_finite() {
            (xmin, xmax)
        } else {
            (0.0, 1.0)
        };
        let (ymin, ymax) = if ymin.is_finite() && ymax.is_finite() {
            (ymin, ymax)
        } else {
            (0.0, 1.0)
        };
        (xmin, xmax, ymin, ymax)
    }
}

// ---------------------------------------------------------------------------
// WaterfallArtist
// ---------------------------------------------------------------------------

/// A waterfall chart showing how an initial value is affected by a series of
/// positive and negative changes.
///
/// Each bar represents an incremental change from the previous cumulative
/// total. Bars that increase the total are colored with `increase_color`,
/// bars that decrease it use `decrease_color`, and bars explicitly marked
/// as totals (via `total_indices`) are drawn from zero using `total_color`.
#[derive(Debug, Clone)]
pub struct WaterfallArtist {
    /// Category labels for each bar.
    pub categories: Categories,
    /// Change values: positive values increase the running total, negative
    /// values decrease it. For total bars, the value is the absolute total.
    pub values: Series,
    /// Indices of bars that represent totals (drawn from zero).
    pub total_indices: Vec<usize>,
    /// Fill color for bars showing positive changes.
    pub increase_color: Color,
    /// Fill color for bars showing negative changes.
    pub decrease_color: Color,
    /// Fill color for total bars.
    pub total_color: Color,
    /// When `true`, thin horizontal connector lines are drawn from each bar's
    /// top to the next bar's base.
    pub connector_lines: bool,
    /// When `true`, value labels are rendered on each bar.
    pub show_values: bool,
    /// Bar width as a fraction of the category spacing (0.0, 1.0].
    pub bar_width: f64,
    /// Optional legend label.
    pub label: Option<String>,
    /// Primary color used for legend swatch rendering.
    pub color: Color,
    /// Opacity from 0.0 (fully transparent) to 1.0 (fully opaque).
    pub alpha: f64,
}

impl WaterfallArtist {
    /// Computes the data-space bounding box `(xmin, xmax, ymin, ymax)`.
    ///
    /// The x-axis spans from `-0.5` to `n - 0.5` so that bars are centered
    /// on integer positions. The y-axis covers the full range of the running
    /// cumulative sum (including zero) so that all bars are visible.
    pub fn data_bounds(&self) -> (f64, f64, f64, f64) {
        let n = self.categories.len() as f64;
        if n == 0.0 {
            return (0.0, 1.0, 0.0, 1.0);
        }

        let cat_min = -0.5;
        let cat_max = n - 0.5;

        // Compute running cumulative sum to find y-extent.
        let mut running = 0.0;
        let mut y_min = 0.0_f64;
        let mut y_max = 0.0_f64;

        for i in 0..self.values.len() {
            let prev = running;
            if self.total_indices.contains(&i) {
                running = self.values.data[i];
            } else {
                running += self.values.data[i];
            }
            // For non-total bars, the bar spans from prev to running.
            // For total bars, the bar spans from 0 to running.
            if self.total_indices.contains(&i) {
                y_min = y_min.min(0.0).min(running);
                y_max = y_max.max(0.0).max(running);
            } else {
                y_min = y_min.min(prev).min(running);
                y_max = y_max.max(prev).max(running);
            }
        }

        // Ensure we always include zero.
        y_min = y_min.min(0.0);
        y_max = y_max.max(0.0);

        // Ensure non-zero extent.
        if (y_max - y_min).abs() < f64::EPSILON {
            y_max = y_min + 1.0;
        }

        (cat_min, cat_max, y_min, y_max)
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    /// Helper: build a simple `LineArtist` for testing.
    fn sample_line() -> LineArtist {
        LineArtist {
            x: Series::new(vec![1.0, 2.0, 3.0]),
            y: Series::new(vec![10.0, 20.0, 30.0]),
            color: Color::TAB_BLUE,
            width: 1.5,
            style: LineStyle::Solid,
            label: Some("line".to_string()),
            alpha: 1.0,
            decimate: None,
        }
    }

    /// Helper: build a simple `ScatterArtist` for testing.
    fn sample_scatter() -> ScatterArtist {
        ScatterArtist {
            x: Series::new(vec![0.0, 5.0, 10.0]),
            y: Series::new(vec![-1.0, 0.0, 1.0]),
            color: Color::TAB_ORANGE,
            marker: Marker::Circle,
            size: 6.0,
            label: None,
            alpha: 0.8,
            colors: None,
            c: None,
            cmap: None,
        }
    }

    /// Helper: build a simple `BarArtist` for testing.
    fn sample_bar() -> BarArtist {
        BarArtist {
            categories: Categories::new(vec!["A".into(), "B".into(), "C".into()]),
            heights: Series::new(vec![4.0, 7.0, 2.0]),
            color: Color::TAB_GREEN,
            label: Some("bars".to_string()),
            alpha: 1.0,
            horizontal: false,
            bar_width: 0.8,
           bottom: None,
            offset: None,
        }
    }

    /// Helper: build a simple `HistArtist` for testing.
    fn sample_hist() -> HistArtist {
        HistArtist {
            data: Series::new(vec![1.0, 2.0, 2.5, 3.0, 3.5, 4.0]),
            bins: 3,
            bin_edges: vec![1.0, 2.0, 3.0, 4.0],
            counts: vec![1.0, 2.0, 3.0],
            color: Color::TAB_RED,
            label: Some("hist".to_string()),
            alpha: 0.7,
            density: false,
        }
    }

    /// Helper: build a simple `FillBetweenArtist` for testing.
    fn sample_fill_between() -> FillBetweenArtist {
        FillBetweenArtist {
            x: Series::new(vec![0.0, 1.0, 2.0]),
            y1: Series::new(vec![1.0, 3.0, 2.0]),
            y2: Series::new(vec![0.0, 1.0, 0.5]),
            color: Color::TAB_PURPLE,
            label: Some("fill".to_string()),
            alpha: 0.3,
        }
    }

    // -- Artist enum dispatch -----------------------------------------------

    #[test]
    fn artist_label_returns_inner_label() {
        let a = Artist::Line(sample_line());
        assert_eq!(a.label(), Some("line"));

        let a = Artist::Scatter(sample_scatter());
        assert_eq!(a.label(), None);

        let a = Artist::Bar(sample_bar());
        assert_eq!(a.label(), Some("bars"));

        let a = Artist::Histogram(sample_hist());
        assert_eq!(a.label(), Some("hist"));

        let a = Artist::FillBetween(sample_fill_between());
        assert_eq!(a.label(), Some("fill"));
    }

    #[test]
    fn artist_color_returns_inner_color() {
        assert_eq!(Artist::Line(sample_line()).color(), Color::TAB_BLUE);
        assert_eq!(Artist::Scatter(sample_scatter()).color(), Color::TAB_ORANGE);
        assert_eq!(Artist::Bar(sample_bar()).color(), Color::TAB_GREEN);
        assert_eq!(Artist::Histogram(sample_hist()).color(), Color::TAB_RED);
        assert_eq!(
            Artist::FillBetween(sample_fill_between()).color(),
            Color::TAB_PURPLE
        );
    }

    #[test]
    fn artist_data_bounds_dispatches_correctly() {
        let a = Artist::Line(sample_line());
        assert_eq!(a.data_bounds(), (1.0, 3.0, 10.0, 30.0));
    }

    // -- LineArtist ---------------------------------------------------------

    #[test]
    fn line_data_bounds_basic() {
        let a = sample_line();
        assert_eq!(a.data_bounds(), (1.0, 3.0, 10.0, 30.0));
    }

    #[test]
    fn line_data_bounds_empty_series() {
        let a = LineArtist {
            x: Series::new(vec![]),
            y: Series::new(vec![]),
            color: Color::BLACK,
            width: 1.0,
            style: LineStyle::Solid,
            label: None,
            alpha: 1.0,
            decimate: None,
        };
        assert_eq!(a.data_bounds(), (0.0, 1.0, 0.0, 1.0));
    }

    #[test]
    fn line_data_bounds_with_nan() {
        let a = LineArtist {
            x: Series::new(vec![f64::NAN, 2.0, 5.0]),
            y: Series::new(vec![1.0, f64::NAN, 3.0]),
            color: Color::BLACK,
            width: 1.0,
            style: LineStyle::Solid,
            label: None,
            alpha: 1.0,
            decimate: None,
        };
        assert_eq!(a.data_bounds(), (2.0, 5.0, 1.0, 3.0));
    }

    // -- ScatterArtist ------------------------------------------------------

    #[test]
    fn scatter_data_bounds_basic() {
        let a = sample_scatter();
        assert_eq!(a.data_bounds(), (0.0, 10.0, -1.0, 1.0));
    }

    #[test]
    fn scatter_data_bounds_empty() {
        let a = ScatterArtist {
            x: Series::new(vec![]),
            y: Series::new(vec![]),
            color: Color::BLACK,
            marker: Marker::Circle,
            size: 6.0,
            label: None,
            alpha: 1.0,
            colors: None,
            c: None,
            cmap: None,
        };
        assert_eq!(a.data_bounds(), (0.0, 1.0, 0.0, 1.0));
    }

    // -- BarArtist ----------------------------------------------------------

    #[test]
    fn bar_data_bounds_vertical() {
        let a = sample_bar();
        let (xmin, xmax, ymin, ymax) = a.data_bounds();
        assert!((xmin - (-0.5)).abs() < f64::EPSILON);
        assert!((xmax - 2.5).abs() < f64::EPSILON);
        assert!((ymin - 0.0).abs() < f64::EPSILON);
        assert!((ymax - 7.0).abs() < f64::EPSILON);
    }

    #[test]
    fn bar_data_bounds_horizontal() {
        let mut a = sample_bar();
        a.horizontal = true;
        let (xmin, xmax, ymin, ymax) = a.data_bounds();
        // Horizontal: x = value axis, y = category axis.
        assert!((xmin - 0.0).abs() < f64::EPSILON);
        assert!((xmax - 7.0).abs() < f64::EPSILON);
        assert!((ymin - (-0.5)).abs() < f64::EPSILON);
        assert!((ymax - 2.5).abs() < f64::EPSILON);
    }

    #[test]
    fn bar_data_bounds_negative_heights() {
        let a = BarArtist {
            categories: Categories::new(vec!["A".into(), "B".into()]),
            heights: Series::new(vec![-3.0, 5.0]),
            color: Color::BLACK,
            label: None,
            alpha: 1.0,
            horizontal: false,
            bar_width: 0.8,
           bottom: None,
            offset: None,
        };
        let (_, _, ymin, ymax) = a.data_bounds();
        assert!((ymin - (-3.0)).abs() < f64::EPSILON);
        assert!((ymax - 5.0).abs() < f64::EPSILON);
    }

    #[test]
    fn bar_data_bounds_empty() {
        let a = BarArtist {
            categories: Categories::new(vec![]),
            heights: Series::new(vec![]),
            color: Color::BLACK,
            label: None,
            alpha: 1.0,
            horizontal: false,
            bar_width: 0.8,
           bottom: None,
            offset: None,
        };
        let (xmin, xmax, ymin, ymax) = a.data_bounds();
        assert!((xmin - (-0.5)).abs() < f64::EPSILON);
        assert!((xmax - 0.5).abs() < f64::EPSILON);
        assert!((ymin - 0.0).abs() < f64::EPSILON);
        assert!((ymax - 1.0).abs() < f64::EPSILON);
    }

    // -- BarArtist with bottom (stacking) -----------------------------------

    #[test]
    fn bar_data_bounds_with_bottom() {
        let a = BarArtist {
            categories: Categories::new(vec!["A".into(), "B".into(), "C".into()]),
            heights: Series::new(vec![3.0, 4.0, 2.0]),
            color: Color::BLACK,
            label: None,
            alpha: 1.0,
            horizontal: false,
            bar_width: 0.8,
            bottom: Some(vec![1.0, 2.0, 3.0]),
            offset: None,
        };
        let (_, _, ymin, ymax) = a.data_bounds();
        // bottom[0]=1, top[0]=4; bottom[1]=2, top[1]=6; bottom[2]=3, top[2]=5
        // min includes 0.0 (ensured), max = 6.0
        assert!((ymin - 0.0).abs() < f64::EPSILON);
        assert!((ymax - 6.0).abs() < f64::EPSILON);
    }

    #[test]
    fn bar_data_bounds_with_bottom_negative_base() {
        let a = BarArtist {
            categories: Categories::new(vec!["A".into(), "B".into()]),
            heights: Series::new(vec![5.0, 3.0]),
            color: Color::BLACK,
            label: None,
            alpha: 1.0,
            horizontal: false,
            bar_width: 0.8,
            bottom: Some(vec![-2.0, 1.0]),
            offset: None,
        };
        let (_, _, ymin, ymax) = a.data_bounds();
        assert!((ymin - (-2.0)).abs() < f64::EPSILON);
        assert!((ymax - 4.0).abs() < f64::EPSILON);
    }

    #[test]
    fn bar_data_bounds_with_bottom_horizontal() {
        let a = BarArtist {
            categories: Categories::new(vec!["X".into(), "Y".into()]),
            heights: Series::new(vec![4.0, 6.0]),
            color: Color::BLACK,
            label: None,
            alpha: 1.0,
            horizontal: true,
            bar_width: 0.8,
            bottom: Some(vec![1.0, 2.0]),
            offset: None,
        };
        let (xmin, xmax, ymin, ymax) = a.data_bounds();
        // Horizontal: x = value axis, y = category axis.
        assert!((xmin - 0.0).abs() < f64::EPSILON);
        assert!((xmax - 8.0).abs() < f64::EPSILON);
        assert!((ymin - (-0.5)).abs() < f64::EPSILON);
        assert!((ymax - 1.5).abs() < f64::EPSILON);
    }

    #[test]
    fn bar_data_bounds_with_offset() {
        let a = BarArtist {
            categories: Categories::new(vec!["A".into(), "B".into()]),
            heights: Series::new(vec![5.0, 3.0]),
            color: Color::BLACK,
            label: None,
            alpha: 1.0,
            horizontal: false,
            bar_width: 0.4,
            bottom: None,
            offset: Some(vec![-0.2, -0.2]),
        };
        let (xmin, _xmax, _, _) = a.data_bounds();
        // center for bar 0 = 0 + (-0.2) = -0.2, left edge = -0.2 - 0.2 = -0.4
        assert!(xmin <= -0.4);
    }

    #[test]
    fn bar_data_bounds_bottom_and_offset_combined() {
        let a = BarArtist {
            categories: Categories::new(vec!["A".into(), "B".into()]),
            heights: Series::new(vec![3.0, 4.0]),
            color: Color::BLACK,
            label: None,
            alpha: 1.0,
            horizontal: false,
            bar_width: 0.4,
            bottom: Some(vec![2.0, 1.0]),
            offset: Some(vec![0.2, 0.2]),
        };
        let (_, _, ymin, ymax) = a.data_bounds();
        // bottoms: 2,1; tops: 5,5; min(all,0)=0; max=5
        assert!((ymin - 0.0).abs() < f64::EPSILON);
        assert!((ymax - 5.0).abs() < f64::EPSILON);
    }

    #[test]
    fn bar_data_bounds_single_bar_with_bottom() {
        let a = BarArtist {
            categories: Categories::new(vec!["Solo".into()]),
            heights: Series::new(vec![10.0]),
            color: Color::BLACK,
            label: None,
            alpha: 1.0,
            horizontal: false,
            bar_width: 0.8,
            bottom: Some(vec![5.0]),
            offset: None,
        };
        let (_, _, ymin, ymax) = a.data_bounds();
        assert!((ymin - 0.0).abs() < f64::EPSILON);
        assert!((ymax - 15.0).abs() < f64::EPSILON);
    }

    #[test]
    fn bar_data_bounds_zero_bottom() {
        // Setting bottom to all zeros should behave identically to no bottom.
        let a = BarArtist {
            categories: Categories::new(vec!["A".into(), "B".into()]),
            heights: Series::new(vec![3.0, 5.0]),
            color: Color::BLACK,
            label: None,
            alpha: 1.0,
            horizontal: false,
            bar_width: 0.8,
            bottom: Some(vec![0.0, 0.0]),
            offset: None,
        };
        let (_, _, ymin, ymax) = a.data_bounds();
        assert!((ymin - 0.0).abs() < f64::EPSILON);
        assert!((ymax - 5.0).abs() < f64::EPSILON);
    }

    #[test]
    fn bar_data_bounds_empty_with_bottom() {
        let a = BarArtist {
            categories: Categories::new(vec![]),
            heights: Series::new(vec![]),
            color: Color::BLACK,
            label: None,
            alpha: 1.0,
            horizontal: false,
            bar_width: 0.8,
            bottom: Some(vec![]),
            offset: None,
        };
        let (xmin, xmax, ymin, ymax) = a.data_bounds();
        assert!((xmin - (-0.5)).abs() < f64::EPSILON);
        assert!((xmax - 0.5).abs() < f64::EPSILON);
        assert!((ymin - 0.0).abs() < f64::EPSILON);
        assert!((ymax - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn bar_data_bounds_stacked_three_layers() {
        // Simulates top layer of a 3-layer stack: bottom=5, height=1 => top=6.
        let a = BarArtist {
            categories: Categories::new(vec!["A".into()]),
            heights: Series::new(vec![1.0]),
            color: Color::BLACK,
            label: None,
            alpha: 1.0,
            horizontal: false,
            bar_width: 0.8,
            bottom: Some(vec![5.0]),
            offset: None,
        };
        let (_, _, ymin, ymax) = a.data_bounds();
        assert!((ymin - 0.0).abs() < f64::EPSILON);
        assert!((ymax - 6.0).abs() < f64::EPSILON);
    }

    #[test]
    fn bar_builder_bottom_sets_field() {
        let mut a = sample_bar();
        a.bottom(vec![1.0, 2.0, 3.0]);
        assert_eq!(a.bottom.as_ref().unwrap(), &vec![1.0, 2.0, 3.0]);
    }

    #[test]
    fn bar_builder_offset_sets_field() {
        let mut a = sample_bar();
        a.offset(vec![0.1, 0.2, 0.3]);
        assert_eq!(a.offset.as_ref().unwrap(), &vec![0.1, 0.2, 0.3]);
    }

    // -- HistArtist ---------------------------------------------------------

    #[test]
    fn hist_data_bounds_basic() {
        let a = sample_hist();
        let (xmin, xmax, ymin, ymax) = a.data_bounds();
        assert!((xmin - 1.0).abs() < f64::EPSILON);
        assert!((xmax - 4.0).abs() < f64::EPSILON);
        assert!((ymin - 0.0).abs() < f64::EPSILON);
        assert!((ymax - 3.0).abs() < f64::EPSILON);
    }

    #[test]
    fn hist_data_bounds_empty_bins() {
        let a = HistArtist {
            data: Series::new(vec![]),
            bins: 0,
            bin_edges: vec![],
            counts: vec![],
            color: Color::BLACK,
            label: None,
            alpha: 1.0,
            density: false,
        };
        assert_eq!(a.data_bounds(), (0.0, 1.0, 0.0, 1.0));
    }

    #[test]
    fn hist_data_bounds_single_edge_pair() {
        let a = HistArtist {
            data: Series::new(vec![1.0]),
            bins: 1,
            bin_edges: vec![0.5, 1.5],
            counts: vec![1.0],
            color: Color::BLACK,
            label: None,
            alpha: 1.0,
            density: false,
        };
        let (xmin, xmax, ymin, ymax) = a.data_bounds();
        assert!((xmin - 0.5).abs() < f64::EPSILON);
        assert!((xmax - 1.5).abs() < f64::EPSILON);
        assert!((ymin - 0.0).abs() < f64::EPSILON);
        assert!((ymax - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn hist_data_bounds_all_zero_counts() {
        let a = HistArtist {
            data: Series::new(vec![]),
            bins: 2,
            bin_edges: vec![0.0, 1.0, 2.0],
            counts: vec![0.0, 0.0],
            color: Color::BLACK,
            label: None,
            alpha: 1.0,
            density: false,
        };
        let (_, _, _, ymax) = a.data_bounds();
        // All-zero counts should produce a fallback ymax of 1.0.
        assert!((ymax - 1.0).abs() < f64::EPSILON);
    }

    // -- FillBetweenArtist --------------------------------------------------

    #[test]
    fn fill_between_data_bounds_basic() {
        let a = sample_fill_between();
        let (xmin, xmax, ymin, ymax) = a.data_bounds();
        assert!((xmin - 0.0).abs() < f64::EPSILON);
        assert!((xmax - 2.0).abs() < f64::EPSILON);
        assert!((ymin - 0.0).abs() < f64::EPSILON);
        assert!((ymax - 3.0).abs() < f64::EPSILON);
    }

    #[test]
    fn fill_between_data_bounds_empty() {
        let a = FillBetweenArtist {
            x: Series::new(vec![]),
            y1: Series::new(vec![]),
            y2: Series::new(vec![]),
            color: Color::BLACK,
            label: None,
            alpha: 1.0,
        };
        assert_eq!(a.data_bounds(), (0.0, 1.0, 0.0, 1.0));
    }

    #[test]
    fn fill_between_data_bounds_y2_extends_beyond_y1() {
        let a = FillBetweenArtist {
            x: Series::new(vec![0.0, 1.0]),
            y1: Series::new(vec![1.0, 2.0]),
            y2: Series::new(vec![-5.0, 10.0]),
            color: Color::BLACK,
            label: None,
            alpha: 1.0,
        };
        let (_, _, ymin, ymax) = a.data_bounds();
        assert!((ymin - (-5.0)).abs() < f64::EPSILON);
        assert!((ymax - 10.0).abs() < f64::EPSILON);
    }

    #[test]
    fn fill_between_data_bounds_one_series_empty() {
        // y1 has data, y2 is empty -- bounds should come from y1 alone.
        let a = FillBetweenArtist {
            x: Series::new(vec![0.0, 1.0]),
            y1: Series::new(vec![2.0, 8.0]),
            y2: Series::new(vec![]),
            color: Color::BLACK,
            label: None,
            alpha: 1.0,
        };
        let (_, _, ymin, ymax) = a.data_bounds();
        assert!((ymin - 2.0).abs() < f64::EPSILON);
        assert!((ymax - 8.0).abs() < f64::EPSILON);
    }
}