chartml-chart-cartesian 4.0.0

ChartML bar, line, and area chart renderers
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
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
use chartml_core::data::DataTable;
use chartml_core::element::{ChartElement, ElementData, TextAnchor, TextRole, TextStyle, Transform, ViewBox, emit_dot_halo_if_enabled};
use chartml_core::error::ChartError;
use chartml_core::layout::margins::{calculate_margins, MarginConfig};
use chartml_core::plugin::ChartConfig;
use chartml_core::scales::{ScaleBand, ScaleLinear};
use chartml_core::layout::adaptive_tick_count;
use chartml_core::spec::{ChartMode, Orientation};

use chartml_core::layout::labels::{LabelStrategy, LabelStrategyConfig, TextMetrics, measure_text};

use chartml_core::layout::legend::{calculate_legend_layout, LegendConfig};

use crate::helpers::{GridConfig, emit_zero_line_if_crosses, format_value, generate_annotations, generate_x_axis, generate_x_axis_numeric, generate_x_axis_with_display, generate_y_axis_with_display, generate_y_axis_numeric, generate_y_axis_numeric_right, generate_legend, get_color_field, get_data_labels_config, get_field_name, get_x_format, get_y_axis_bounds, get_y_format, nice_domain, offset_element};

/// Build a single bar element, honoring `theme.bar_corner_radius`.
///
/// Decision tree:
/// - `BarCornerRadius::Uniform(0.0)` or `Top(0.0)` → emit a plain
///   `ChartElement::Rect` with `rx`/`ry` == `None` (byte-identical to the
///   pre-3.1 un-themed output).
/// - `BarCornerRadius::Uniform(r)` with `r > 0.0` → emit `Rect` with
///   `rx = ry = Some(r)`.
/// - `BarCornerRadius::Top(r)` with `r > 0.0` → emit a `ChartElement::Path`
///   with a `d` string that rounds only the two corners at the max-value
///   end of the bar (the top of a vertical positive bar, the bottom of a
///   vertical negative bar, the right end of a horizontal positive bar,
///   the left end of a horizontal negative bar). The radius is clamped to
///   `min(width, height) / 2.0` to prevent degenerate paths.
pub(crate) struct BarRectSpec {
    pub x: f64,
    pub y: f64,
    pub width: f64,
    pub height: f64,
    pub is_horizontal: bool,
    pub is_negative: bool,
    pub fill: String,
    pub class: String,
    pub data: Option<ElementData>,
}

/// Compute the CSS `transform-origin` anchor for a bar's entrance animation.
///
/// The anchor is the bar's value-baseline edge midpoint, in absolute SVG
/// coordinates, so a `scaleX`/`scaleY` keyframe grows the bar from the axis
/// outward toward its value end:
///
/// - vertical, positive value  → bottom-center  (grows up)
/// - vertical, negative value  → top-center     (grows down)
/// - horizontal, positive value → left-center   (grows right)
/// - horizontal, negative value → right-center  (grows left)
///
/// Computing the anchor at emission time is essential: the renderer cannot
/// recover orientation/sign from `<rect>`/`<path>` geometry alone (the
/// historical `width > height` heuristic guessed wrong for square bars and
/// for any negative bar). See `chartml-leptos/src/element.rs`, where the
/// heuristic was deleted in favor of consuming this value.
pub fn bar_animation_origin(
    x: f64,
    y: f64,
    width: f64,
    height: f64,
    is_horizontal: bool,
    is_negative: bool,
) -> (f64, f64) {
    match (is_horizontal, is_negative) {
        // Vertical positive: rect spans [y, y+h]; baseline is y+h (bottom).
        (false, false) => (x + width / 2.0, y + height),
        // Vertical negative: rect spans [y, y+h] BELOW zero line; baseline is y (top).
        (false, true) => (x + width / 2.0, y),
        // Horizontal positive: rect spans [x, x+w]; baseline is x (left).
        (true, false) => (x, y + height / 2.0),
        // Horizontal negative: rect spans [x, x+w] LEFT of zero line; baseline is x+w (right).
        (true, true) => (x + width, y + height / 2.0),
    }
}

pub(crate) fn build_bar_element(
    spec: BarRectSpec,
    theme: &chartml_core::theme::Theme,
) -> ChartElement {
    use chartml_core::theme::BarCornerRadius;
    let BarRectSpec {
        x, y, width, height, is_horizontal, is_negative, fill, class, data,
    } = spec;
    let anim_origin = Some(bar_animation_origin(x, y, width, height, is_horizontal, is_negative));

    // Extract requested radius; short-circuit the zero case to emit a plain
    // Rect (byte-identical contract).
    let (radius, top_only) = match theme.bar_corner_radius {
        BarCornerRadius::Uniform(r) => (r as f64, false),
        BarCornerRadius::Top(r) => (r as f64, true),
    };

    if radius <= 0.0 {
        return ChartElement::Rect {
            x,
            y,
            width,
            height,
            fill,
            stroke: None,
            rx: None,
            ry: None,
            class,
            data,
            animation_origin: anim_origin,
        };
    }

    if !top_only {
        return ChartElement::Rect {
            x,
            y,
            width,
            height,
            fill,
            stroke: None,
            rx: Some(radius),
            ry: Some(radius),
            class,
            data,
            animation_origin: anim_origin,
        };
    }

    // Top-only rounding: emit a Path with custom d.
    // Clamp radius to min(w,h)/2 to prevent degenerate geometry on very
    // thin bars. debug_assert flags regressions in tests.
    let max_r = (width.min(height) / 2.0).max(0.0);
    debug_assert!(
        radius <= max_r + 1e-9 || width <= 0.0 || height <= 0.0,
        "bar_corner_radius {} exceeds min(w,h)/2 = {} (w={}, h={})",
        radius, max_r, width, height
    );
    let r = radius.min(max_r);

    // Degenerate zero-dimension bars (e.g. a value-at-zero bar that has
    // height 0 on vertical orientation) collapse to a plain Rect. Emitting
    // an arc of radius 0 would pollute the path string and confuse
    // consumers.
    if r <= 0.0 {
        return ChartElement::Rect {
            x,
            y,
            width,
            height,
            fill,
            stroke: None,
            rx: None,
            ry: None,
            class,
            data,
            animation_origin: anim_origin,
        };
    }

    // Absolute coordinates of the rect corners.
    let x0 = x;
    let y0 = y;
    let x1 = x + width;
    let y1 = y + height;

    // Which two corners get rounded:
    //   vertical + !negative → top two   (y0 edge)
    //   vertical +  negative → bottom two (y1 edge)
    //   horizontal + !negative → right two (x1 edge)
    //   horizontal +  negative → left two  (x0 edge)
    //
    // Path is always traced clockwise starting from the corner immediately
    // counter-clockwise of the first rounded corner, so the arc sweep flag
    // is always 1 (clockwise).
    let d = match (is_horizontal, is_negative) {
        // Vertical, top rounding (two corners at y0)
        (false, false) => format!(
            "M {x0},{y0r} A {r},{r} 0 0 1 {x0r},{y0} L {x1mr},{y0} A {r},{r} 0 0 1 {x1},{y0r} L {x1},{y1} L {x0},{y1} Z",
            x0 = x0, y0 = y0, x1 = x1, y1 = y1, r = r,
            x0r = x0 + r, x1mr = x1 - r, y0r = y0 + r,
        ),
        // Vertical, negative value → bottom rounding (two corners at y1)
        (false, true) => format!(
            "M {x0},{y0} L {x1},{y0} L {x1},{y1mr} A {r},{r} 0 0 1 {x1mr},{y1} L {x0r},{y1} A {r},{r} 0 0 1 {x0},{y1mr} Z",
            x0 = x0, y0 = y0, x1 = x1, y1 = y1, r = r,
            x0r = x0 + r, x1mr = x1 - r, y1mr = y1 - r,
        ),
        // Horizontal, positive value → right-end rounding (two corners at x1)
        (true, false) => format!(
            "M {x0},{y0} L {x1mr},{y0} A {r},{r} 0 0 1 {x1},{y0r} L {x1},{y1mr} A {r},{r} 0 0 1 {x1mr},{y1} L {x0},{y1} Z",
            x0 = x0, y0 = y0, x1 = x1, y1 = y1, r = r,
            x1mr = x1 - r, y0r = y0 + r, y1mr = y1 - r,
        ),
        // Horizontal, negative value → left-end rounding (two corners at x0)
        (true, true) => format!(
            "M {x0r},{y0} L {x1},{y0} L {x1},{y1} L {x0r},{y1} A {r},{r} 0 0 1 {x0},{y1mr} L {x0},{y0r} A {r},{r} 0 0 1 {x0r},{y0} Z",
            x0 = x0, y0 = y0, x1 = x1, y1 = y1, r = r,
            x0r = x0 + r, y0r = y0 + r, y1mr = y1 - r,
        ),
    };

    ChartElement::Path {
        d,
        fill: Some(fill),
        stroke: None,
        stroke_width: None,
        stroke_dasharray: None,
        opacity: None,
        class,
        data,
        animation_origin: anim_origin,
    }
}

struct SingleSeriesBarParams<'a> {
    category_field: &'a str,
    value_field: &'a str,
    categories: &'a [String],
    inner_width: f64,
    inner_height: f64,
    is_horizontal: bool,
    y_fmt_ref: Option<&'a str>,
    domain_min: f64,
    domain_max: f64,
}

struct MultiSeriesBarParams<'a> {
    category_field: &'a str,
    value_field: &'a str,
    color_field: &'a str,
    categories: &'a [String],
    inner_width: f64,
    inner_height: f64,
    is_stacked: bool,
    is_normalized: bool,
    is_horizontal: bool,
    y_fmt_ref: Option<&'a str>,
    domain_min: f64,
    domain_max: f64,
}

pub fn render_bar(data: &DataTable, config: &ChartConfig) -> Result<ChartElement, ChartError> {
    use chartml_core::spec::{FieldRef, FieldRefItem, FieldSpec};
    

    // Detect multi-field rows (combo chart pattern)
    let multi_fields: Vec<FieldSpec> = match &config.visualize.rows {
        Some(FieldRef::Multiple(items)) => items.iter().map(|item| match item {
            FieldRefItem::Detailed(spec) => spec.as_ref().clone(),
            FieldRefItem::Simple(name) => FieldSpec {
                field: name.clone(), mark: None, axis: None, label: None,
                color: None, format: None, data_labels: None,
                line_style: None, upper: None, lower: None, opacity: None,
            },
        }).collect(),
        _ => vec![],
    };

    if !multi_fields.is_empty() {
        let is_horizontal = matches!(config.visualize.orientation, Some(Orientation::Horizontal));
        let has_line_fields = multi_fields.iter().any(|f| f.mark.as_deref() == Some("line"));
        let has_right_axis = multi_fields.iter().any(|f| f.axis.as_deref() == Some("right"));

        // When horizontal with only bar fields (no lines/right axis), delegate to the
        // standard bar renderer which already supports horizontal layout. The combo path
        // only handles vertical because swapping axes for lines doesn't make sense.
        if is_horizontal && !has_line_fields && !has_right_axis {
            // Build a grouped bar config: use color field to separate the bar fields.
            // Reshape wide-format data (revenue+target columns) into long-format
            // (field_name column + value column) so the standard grouped bar path handles it.
            let category_field = get_field_name(&config.visualize.columns)?;
            let mut long_rows: Vec<chartml_core::data::Row> = Vec::new();
            for i in 0..data.num_rows() {
                for field_spec in &multi_fields {
                    let cat = data.get_string(i, &category_field).unwrap_or_default();
                    let val = data.get_f64(i, &field_spec.field).unwrap_or(0.0);
                    let label = field_spec.label.clone().unwrap_or_else(|| field_spec.field.clone());
                    let mut row = std::collections::HashMap::new();
                    row.insert(category_field.clone(), serde_json::json!(cat));
                    row.insert("_value".to_string(), serde_json::json!(val));
                    row.insert("_series".to_string(), serde_json::json!(label));
                    long_rows.push(row);
                }
            }
            let long_data = DataTable::from_rows(&long_rows)
                .map_err(|e| ChartError::DataError(format!("Failed to reshape data: {}", e)))?;

            // Build a config that uses the long-format columns
            let mut viz = config.visualize.clone();
            viz.rows = Some(FieldRef::Simple("_value".to_string()));
            viz.marks = Some(chartml_core::spec::MarksSpec {
                color: Some(chartml_core::spec::MarkEncoding::Simple("_series".to_string())),
                size: None, shape: None, text: None,
            });
            viz.mode = Some(ChartMode::Grouped);
            // Assign colors from field specs or config palette
            let mut colors = Vec::new();
            for (i, f) in multi_fields.iter().enumerate() {
                colors.push(f.color.clone().unwrap_or_else(|| {
                    config.colors.get(i).cloned().unwrap_or_else(|| "#2E7D9A".to_string())
                }));
            }
            let long_config = ChartConfig {
                visualize: viz,
                title: config.title.clone(),
                width: config.width,
                height: config.height,
                colors,
                theme: config.theme.clone(),
            };
            return render_bar(&long_data, &long_config);
        }

        return render_combo(data, config, &multi_fields);
    }

    let category_field = get_field_name(&config.visualize.columns)?;
    let value_field = get_field_name(&config.visualize.rows)?;

    let color_field = get_color_field(config);

    // For single-series bars (no color field), use per-row categories to support
    // duplicate category names. Each row gets a unique band key for positioning,
    // while display_labels preserves the original (possibly duplicate) text.
    // For multi-series (with color field), use unique categories as before since
    // stacking/grouping logic depends on deduplication.
    let (categories, display_labels): (Vec<String>, Option<Vec<String>>) = if color_field.is_none() {
        let all_vals = data.all_values(&category_field);
        if all_vals.is_empty() {
            return Err(ChartError::DataError("No category values found".into()));
        }
        // Check if there are any duplicates; if so, create indexed band keys
        let has_duplicates = {
            let mut seen = std::collections::HashSet::new();
            all_vals.iter().any(|v| !seen.insert(v.as_str()))
        };
        if has_duplicates {
            let band_keys: Vec<String> = all_vals.iter().enumerate()
                .map(|(i, v)| format!("{}\x00{}", v, i))
                .collect();
            (band_keys, Some(all_vals))
        } else {
            (all_vals, None)
        }
    } else {
        let unique = data.unique_values(&category_field);
        if unique.is_empty() {
            return Err(ChartError::DataError("No category values found".into()));
        }
        (unique, None)
    };

    let is_horizontal = matches!(config.visualize.orientation, Some(Orientation::Horizontal));
    let is_normalized = matches!(config.visualize.mode, Some(ChartMode::Normalized));
    let is_stacked = matches!(config.visualize.mode, Some(ChartMode::Stacked)) || is_normalized;
    let _is_grouped = matches!(config.visualize.mode, Some(ChartMode::Grouped));

    // Step 1: Compute label strategy for margin estimation (only for vertical bars)
    let x_format = get_x_format(config);
    let y_fmt = get_y_format(config);
    let y_fmt_ref = y_fmt.as_deref();
    let (axis_min, axis_max) = get_y_axis_bounds(config);

    // Format labels the same way generate_x_axis will — margin estimation must
    // use the actual display strings, not raw data values.
    let raw_for_strategy = display_labels.as_deref().unwrap_or(&categories);
    let formatted_for_strategy = crate::helpers::format_display_labels(raw_for_strategy, x_format.as_deref());
    let x_extra_margin = if !is_horizontal {
        let estimated_width = config.width - 80.0;
        let label_strategy_config = LabelStrategyConfig {
            text_metrics: TextMetrics::from_theme_axis_label(&config.theme),
            ..LabelStrategyConfig::default()
        };
        let x_strategy = LabelStrategy::determine(&formatted_for_strategy, estimated_width, &label_strategy_config);
        match &x_strategy {
            LabelStrategy::Rotated { margin, .. } => *margin,
            _ => 0.0,
        }
    } else {
        0.0
    };

    // Step 1b: Pre-compute domain for left margin estimation (matches JS two-pass approach).
    // JS computes finalMarginLeft from actual y-axis tick label widths; we approximate here.
    let (prelim_data_min, prelim_data_max): (f64, f64) = if let Some(ref color_f) = color_field {
        if is_stacked {
            let groups = data.group_by(color_f);
            let series_names = data.unique_values(color_f);
            let stacked_vals: Vec<f64> = categories.iter().map(|cat| {
                series_names.iter().map(|s| {
                    groups.get(s).and_then(|series_data| {
                        (0..series_data.num_rows()).find_map(|i| {
                            if series_data.get_string(i, &category_field).as_deref() == Some(cat.as_str()) {
                                series_data.get_f64(i, &value_field)
                            } else {
                                None
                            }
                        })
                    }).unwrap_or(0.0)
                }).sum::<f64>()
            }).collect();
            let mn = stacked_vals.iter().cloned().fold(f64::INFINITY, f64::min);
            let mx = stacked_vals.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
            (mn, mx)
        } else {
            let vals: Vec<f64> = (0..data.num_rows()).filter_map(|i| data.get_f64(i, &value_field)).collect();
            let mn = vals.iter().cloned().fold(f64::INFINITY, f64::min);
            let mx = vals.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
            (mn, mx)
        }
    } else {
        let vals: Vec<f64> = (0..data.num_rows()).filter_map(|i| data.get_f64(i, &value_field)).collect();
        let mn = vals.iter().cloned().fold(f64::INFINITY, f64::min);
        let mx = vals.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
        (mn, mx)
    };
    let prelim_data_max = if prelim_data_max <= 0.0 { 1.0 } else { prelim_data_max };
    // Keep data_min at 0 when all values are non-negative (standard bar chart behavior)
    let prelim_data_min = if prelim_data_min >= 0.0 { 0.0 } else { prelim_data_min };

    let prelim_domain_max = if is_normalized {
        1.0
    } else {
        let raw_max = axis_max.unwrap_or(prelim_data_max);
        if axis_max.is_none() { nice_domain(axis_min.unwrap_or(prelim_data_min), raw_max, 5).1 } else { raw_max }
    };
    let prelim_domain_min = if is_normalized { 0.0 } else { axis_min.unwrap_or(prelim_data_min) };

    // Generate representative tick label (domain max is typically widest label).
    // For horizontal charts the y-axis shows categories, so use those for left margin.
    let y_tick_labels_for_margin: Vec<String> = if !is_horizontal {
        let prelim_fmt = if is_normalized { Some(".0%") } else { y_fmt_ref };
        vec![
            format_value(prelim_domain_max, prelim_fmt),
            format_value(prelim_domain_min, prelim_fmt),
        ]
    } else {
        let display = display_labels.as_deref().unwrap_or(&categories);
        display.to_vec()
    };

    // Pre-compute legend height so the bottom margin accounts for multi-row legends.
    let legend_height = if let Some(ref color_f) = color_field {
        let series_names = data.unique_values(color_f);
        let legend_config = LegendConfig {
            text_metrics: TextMetrics::from_theme_legend(&config.theme),
            ..LegendConfig::default()
        };
        calculate_legend_layout(&series_names, &config.colors, config.width, &legend_config).total_height
    } else {
        0.0
    };

    // Step 2: Calculate margins including rotation
    let has_x_axis_label = config.visualize.axes.as_ref()
        .and_then(|a| a.x.as_ref())
        .and_then(|a| a.label.as_ref())
        .is_some();
    let has_y_axis_label = config.visualize.axes.as_ref()
        .and_then(|a| a.left.as_ref())
        .and_then(|a| a.label.as_ref())
        .is_some();
    let margin_config = MarginConfig {
        has_title: config.title.is_some(),
        legend_height,
        has_x_axis_label,
        has_y_axis_label,
        x_label_strategy_margin: x_extra_margin,
        y_tick_labels: y_tick_labels_for_margin,
        // For horizontal charts the Y-axis displays category labels (axis
        // label metrics); for vertical charts it shows numeric tick values.
        tick_value_metrics: if is_horizontal {
            TextMetrics::from_theme_axis_label(&config.theme)
        } else {
            TextMetrics::from_theme_tick_value(&config.theme)
        },
        axis_label_metrics: TextMetrics::from_theme_axis_label(&config.theme),
        ..Default::default()
    };
    let margins = calculate_margins(&margin_config);

    let inner_width = margins.inner_width(config.width);
    let inner_height = margins.inner_height(config.height);

    let mut children = Vec::new();

    // Title is rendered as an HTML div outside the SVG (matches JS chartml behaviour)
    // — do NOT add it here as a SVG text element.

    let grid = GridConfig::from_config(config);

    let _tick_count = adaptive_tick_count(inner_height);

    // Compute final domain (same as prelim for vertical bars).
    let raw_data_max = prelim_data_max;

    // For normalized mode, domain is always 0-1 (the NumberFormatter handles % display).
    let (domain_min, domain_max) = if is_normalized {
        (0.0, 1.0)
    } else {
        let raw_domain_min = axis_min.unwrap_or(prelim_data_min);
        let raw_domain_max = axis_max.unwrap_or(raw_data_max);
        // Apply nice rounding to domain so ticks are round numbers with headroom (Regressions 2 & 3).
        // Only apply when no explicit axis bounds are set by the user.
        if axis_min.is_none() && axis_max.is_none() {
            // Match JS: yLeft.nice() uses default count=10 for domain rounding.
            nice_domain(raw_domain_min, raw_domain_max, 5)
        } else {
            (raw_domain_min, raw_domain_max)
        }
    };
    // For normalized mode, override Y-axis format to show percentages.
    // Otherwise, use the format from config (axes.left.format).
    let effective_y_fmt: Option<String> = if is_normalized {
        Some(".0%".to_string())
    } else {
        y_fmt.clone()
    };
    let effective_y_fmt_ref = effective_y_fmt.as_deref();

    let (_, bar_elements) = if let Some(ref color_f) = color_field {
        render_multi_series_bars(
            data,
            config,
            &MultiSeriesBarParams {
                category_field: &category_field,
                value_field: &value_field,
                color_field: color_f,
                categories: &categories,
                inner_width,
                inner_height,
                is_stacked,
                is_normalized,
                is_horizontal,
                y_fmt_ref,
                domain_min,
                domain_max,
            },
        )?
    } else {
        render_single_series_bars(
            data,
            config,
            &SingleSeriesBarParams {
                category_field: &category_field,
                value_field: &value_field,
                categories: &categories,
                inner_width,
                inner_height,
                is_horizontal,
                y_fmt_ref,
                domain_min,
                domain_max,
            },
        )?
    };

    // Axes (use domain_min/domain_max instead of 0.0/value_max)
    let axis_elements = if is_horizontal {
        // Category y-axis: generate at x=0 relative, then offset by margins.left
        let x_axis = generate_y_axis_with_display(&categories, display_labels.as_deref(), (0.0, inner_height), 0.0, None, &config.theme);
        let y_axis = generate_x_axis_numeric(&crate::helpers::XAxisNumericParams {
            domain: (domain_min, domain_max),
            range: (0.0, inner_width),
            y_position: margins.top + inner_height,
            fmt: effective_y_fmt_ref,
            tick_count: 5,
            chart_height: Some(inner_height),
            grid: &grid,
            theme: &config.theme,
        });
        let mut axes = Vec::new();
        axes.extend(x_axis.into_iter().map(|e| offset_element(e, margins.left, margins.top)));
        axes.extend(y_axis.into_iter().map(|e| offset_element(e, margins.left, 0.0)));
        // Zero-line (Phase 7): for horizontal bars the numeric axis is x,
        // so the zero line is vertical — emitted here after axes and before
        // the series group below. No-op when theme.zero_line is None or the
        // x-domain doesn't strictly cross zero.
        if let Some(zl) = emit_zero_line_if_crosses(
            &config.theme,
            (domain_min, domain_max),
            inner_width,
            inner_height,
            true,
        ) {
            axes.push(offset_element(zl, margins.left, margins.top));
        }
        axes
    } else {
        let bottom_axis_label = config.visualize.axes.as_ref()
            .and_then(|a| a.x.as_ref())
            .and_then(|a| a.label.as_deref());
        let x_axis_result = generate_x_axis_with_display(&crate::helpers::XAxisParams {
            labels: &categories,
            display_label_overrides: display_labels.as_deref(),
            range: (0.0, inner_width),
            y_position: margins.top + inner_height,
            available_width: inner_width,
            x_format: x_format.as_deref(),
            chart_height: Some(inner_height),
            grid: &grid,
            axis_label: bottom_axis_label,
            theme: &config.theme,
        });
        let left_axis_label = config.visualize.axes.as_ref()
            .and_then(|a| a.left.as_ref())
            .and_then(|a| a.label.as_deref());
        let y_axis = generate_y_axis_numeric(&crate::helpers::YAxisNumericParams {
            domain: (domain_min, domain_max),
            range: (inner_height, 0.0),
            x_position: margins.left,
            fmt: effective_y_fmt_ref,
            tick_count: adaptive_tick_count(inner_height),
            chart_width: Some(inner_width),
            grid: &grid,
            axis_label: left_axis_label,
            theme: &config.theme,
        });
        let mut axes = Vec::new();
        axes.extend(x_axis_result.elements.into_iter().map(|e| offset_element(e, margins.left, 0.0)));
        axes.extend(y_axis.into_iter().map(|e| offset_element(e, 0.0, margins.top)));
        // Zero-line (Phase 7): emitted after grid lines, before the series group
        // is pushed below — so the series paints over it. No-op when theme.zero_line
        // is None (default) or when the domain doesn't strictly cross zero.
        if let Some(zl) = emit_zero_line_if_crosses(
            &config.theme,
            (domain_min, domain_max),
            inner_width,
            inner_height,
            is_horizontal,
        ) {
            axes.push(offset_element(zl, margins.left, margins.top));
        }
        axes
    };

    children.push(ChartElement::Group {
        class: "axes".to_string(),
        transform: None,
        children: axis_elements,
    });

    children.push(ChartElement::Group {
        class: "bars".to_string(),
        transform: Some(Transform::Translate(margins.left, margins.top)),
        children: bar_elements,
    });

    // Annotations — rendered on top of bars, in inner coordinate space
    if !is_horizontal {
        if let Some(annotations) = config.visualize.annotations.as_deref() {
            if !annotations.is_empty() {
                use chartml_core::scales::ScaleLinear;
                let ann_scale = ScaleLinear::new((domain_min, domain_max), (inner_height, 0.0));
                let ann_cats = display_labels.as_deref().unwrap_or(&categories);
                let ann_elements = generate_annotations(
                    annotations,
                    &ann_scale,
                    0.0,
                    inner_width,
                    inner_height,
                    Some(ann_cats),
                    &config.theme,
                );
                if !ann_elements.is_empty() {
                    children.push(ChartElement::Group {
                        class: "annotations".to_string(),
                        transform: Some(Transform::Translate(margins.left, margins.top)),
                        children: ann_elements,
                    });
                }
            }
        }
    }

    // Legend
    if let Some(ref color_f) = color_field {
        let series_names = data.unique_values(color_f);
        let legend_config = LegendConfig {
            text_metrics: TextMetrics::from_theme_legend(&config.theme),
            ..LegendConfig::default()
        };
        let legend_layout = calculate_legend_layout(&series_names, &config.colors, config.width, &legend_config);
        let legend_y = config.height - legend_layout.total_height - 8.0;
        let legend_elements = generate_legend(&series_names, &config.colors, config.width, legend_y, &config.theme);
        children.push(ChartElement::Group {
            class: "legend".to_string(),
            transform: None,
            children: legend_elements,
        });
    }

    let svg_class = if is_horizontal { "chartml-bar chartml-horizontal" } else { "chartml-bar" };
    Ok(ChartElement::Svg {
        viewbox: ViewBox::new(0.0, 0.0, config.width, config.height),
        width: Some(config.width),
        height: Some(config.height),
        class: svg_class.to_string(),
        children,
    })
}

fn render_single_series_bars(
    data: &DataTable,
    config: &ChartConfig,
    params: &SingleSeriesBarParams,
) -> Result<(f64, Vec<ChartElement>), ChartError> {
    let category_field = params.category_field;
    let value_field = params.value_field;
    let categories = params.categories;
    let inner_width = params.inner_width;
    let inner_height = params.inner_height;
    let is_horizontal = params.is_horizontal;
    let y_fmt_ref = params.y_fmt_ref;
    let domain_min = params.domain_min;
    let domain_max = params.domain_max;
    // Find the max value (for return value only — domain_max is already caller-computed)
    let values: Vec<f64> = (0..data.num_rows())
        .filter_map(|i| data.get_f64(i, value_field))
        .collect();
    let value_max = values.iter().cloned().fold(0.0_f64, f64::max);
    let value_max = if value_max <= 0.0 { 1.0 } else { value_max };
    // Use the passed domain_max directly (caller already applied nice rounding if needed)
    let effective_max = domain_max;

    let mut elements = Vec::new();
    // Single-series bars always use one color (the first palette color).
    // Color is per-series, not per-category — matches JS d3ChartMapper.js behavior.
    let fill_color = config.colors.first()
        .cloned()
        .unwrap_or_else(|| "#2E7D9A".to_string());

    if is_horizontal {
        let band = ScaleBand::new(categories.to_vec(), (0.0, inner_height))
            .padding(crate::helpers::adaptive_bar_padding(categories.len()));
        let linear = ScaleLinear::new((domain_min, effective_max), (0.0, inner_width));
        // Match JS: barHeight = min(bandwidth, 40), centered in band
        let bar_render_height = band.bandwidth().min(40.0);
        let y_inset = (band.bandwidth() - bar_render_height) / 2.0;

        for i in 0..data.num_rows() {
            let cat = match data.get_string(i, category_field) {
                Some(c) => c,
                None => continue,
            };
            let val = data.get_f64(i, value_field).unwrap_or(0.0);
            // Use indexed band key for positioning (handles duplicate categories)
            let band_key = categories.get(i).map(|k| k.as_str()).unwrap_or(&cat);
            let y = match band.map(band_key) {
                Some(y) => y,
                None => continue,
            };
            let bar_width = linear.map(val);

            elements.push(build_bar_element(
                BarRectSpec {
                    x: 0.0,
                    y: y + y_inset,
                    width: bar_width,
                    height: bar_render_height,
                    is_horizontal: true,
                    is_negative: val < 0.0,
                    fill: fill_color.clone(),
                    class: "bar bar-rect".to_string(),
                    data: Some(ElementData::new(&cat, format_value(val, y_fmt_ref))),
                },
                &config.theme,
            ));
        }
    } else {
        let band = ScaleBand::new(categories.to_vec(), (0.0, inner_width))
            .padding(crate::helpers::adaptive_bar_padding(categories.len()));
        let linear = ScaleLinear::new((domain_min, effective_max), (inner_height, 0.0));
        // Match JS: barWidth = min(bandwidth, chartWidth * 0.2), centered in band
        let max_bar_width = inner_width * 0.2;
        let bar_render_width = band.bandwidth().min(max_bar_width);
        let x_inset = (band.bandwidth() - bar_render_width) / 2.0;

        for i in 0..data.num_rows() {
            let cat = match data.get_string(i, category_field) {
                Some(c) => c,
                None => continue,
            };
            let val = data.get_f64(i, value_field).unwrap_or(0.0);
            // Use indexed band key for positioning (handles duplicate categories)
            let band_key = categories.get(i).map(|k| k.as_str()).unwrap_or(&cat);
            let x = match band.map(band_key) {
                Some(x) => x,
                None => continue,
            };
            let bar_val_y = linear.map(val);
            let bar_zero_y = linear.map(0.0);
            let bar_height = (bar_zero_y - bar_val_y).abs();
            // For positive bars, rect y is at the value (above zero line).
            // For negative bars, rect y is at zero line (bar extends downward).
            let rect_y = bar_val_y.min(bar_zero_y);

            elements.push(build_bar_element(
                BarRectSpec {
                    x: x + x_inset,
                    y: rect_y,
                    width: bar_render_width,
                    height: bar_height,
                    is_horizontal: false,
                    is_negative: val < 0.0,
                    fill: fill_color.clone(),
                    class: "bar bar-rect".to_string(),
                    data: Some(ElementData::new(&cat, format_value(val, y_fmt_ref))),
                },
                &config.theme,
            ));

            // Data label above bar (if configured)
            if let Some(dl) = get_data_labels_config(config) {
                if dl.show == Some(true) {
                    let label_fmt = dl.format.as_deref().or(y_fmt_ref);
                    let label_y = match dl.position.as_deref() {
                        Some("center") => rect_y + bar_height / 2.0,
                        Some("bottom") => rect_y + bar_height - 5.0,
                        _ => if val >= 0.0 { rect_y - 5.0 } else { rect_y + bar_height + 12.0 }, // "top" or default
                    };
                    elements.push(ChartElement::Text {
                        x: x + band.bandwidth() / 2.0,
                        y: label_y,
                        content: format_value(val, label_fmt),
                        anchor: TextAnchor::Middle,
                        dominant_baseline: None,
                        transform: None,
                        font_family: None,
                        font_size: Some(dl.font_size.map(|s| format!("{}px", s)).unwrap_or_else(|| "12px".to_string())),
                        font_weight: None,
                        letter_spacing: None,
                        text_transform: None,
                        fill: Some(dl.color.clone().unwrap_or_else(|| config.theme.text_secondary.clone())),
                        class: "data-label".to_string(),
                        data: None,
                    });
                }
            }
        }
    }

    Ok((value_max, elements))
}

fn render_multi_series_bars(
    data: &DataTable,
    config: &ChartConfig,
    params: &MultiSeriesBarParams,
) -> Result<(f64, Vec<ChartElement>), ChartError> {
    let category_field = params.category_field;
    let value_field = params.value_field;
    let color_field = params.color_field;
    let categories = params.categories;
    let inner_width = params.inner_width;
    let inner_height = params.inner_height;
    let is_stacked = params.is_stacked;
    let is_normalized = params.is_normalized;
    let is_horizontal = params.is_horizontal;
    let y_fmt_ref = params.y_fmt_ref;
    let domain_min = params.domain_min;
    let domain_max = params.domain_max;
    use chartml_core::layout::stack::{StackLayout, StackOffset};

    let series_names = data.unique_values(color_field);
    let groups = data.group_by(color_field);

    let mut elements = Vec::new();

    if is_stacked {
        // Build values matrix: values[series_idx][category_idx]
        let mut values_matrix: Vec<Vec<f64>> = Vec::new();
        for series in &series_names {
            let mut series_vals = Vec::new();
            let series_data = groups.get(series);
            for cat in categories {
                let val = series_data
                    .map(|sd| {
                        (0..sd.num_rows())
                            .find_map(|i| {
                                if sd.get_string(i, category_field).as_deref() == Some(cat.as_str()) {
                                    sd.get_f64(i, value_field)
                                } else {
                                    None
                                }
                            })
                            .unwrap_or(0.0)
                    })
                    .unwrap_or(0.0);
                series_vals.push(val);
            }
            values_matrix.push(series_vals);
        }

        let stack = if is_normalized {
            StackLayout::new().offset(StackOffset::Normalize)
        } else {
            StackLayout::new()
        };
        let stacked_points = stack.layout(categories, &series_names, &values_matrix);

        // For normalized mode, domain is 0-1; for regular stacked, use the raw max.
        let (effective_min, effective_max) = if is_normalized {
            (0.0, 1.0)
        } else {
            let value_max = stacked_points
                .iter()
                .map(|p| p.y1)
                .fold(0.0_f64, f64::max);
            let value_max = if value_max <= 0.0 { 1.0 } else { value_max };
            (domain_min, if domain_max < f64::MAX { domain_max } else { value_max })
        };

        if is_horizontal {
            // Horizontal stacked: band on y-axis (height), linear on x-axis (width)
            let band = ScaleBand::new(categories.to_vec(), (0.0, inner_height))
                .padding(crate::helpers::adaptive_bar_padding(categories.len()));
            let linear = ScaleLinear::new((effective_min, effective_max), (0.0, inner_width));
            let bar_render_height = band.bandwidth().min(40.0);
            let y_inset = (band.bandwidth() - bar_render_height) / 2.0;

            for point in &stacked_points {
                let y = match band.map(&point.key) {
                    Some(y) => y,
                    None => continue,
                };
                let x_left = linear.map(point.y0);
                let x_right = linear.map(point.y1);
                let bar_width = (x_right - x_left).abs();

                let series_idx = series_names.iter().position(|s| s == &point.series).unwrap_or(0);
                let fill = config
                    .colors
                    .get(series_idx)
                    .cloned()
                    .unwrap_or_else(|| "#2E7D9A".to_string());

                elements.push(build_bar_element(
                    BarRectSpec {
                        x: x_left.min(x_right),
                        y: y + y_inset,
                        width: bar_width,
                        height: bar_render_height,
                        is_horizontal: true,
                        is_negative: point.value < 0.0,
                        fill,
                        class: "bar bar-rect".to_string(),
                        data: Some(
                            ElementData::new(&point.key, format_value(point.value, y_fmt_ref))
                                .with_series(&point.series),
                        ),
                    },
                    &config.theme,
                ));
            }
        } else {
            // Vertical stacked: band on x-axis (width), linear on y-axis (height)
            let band = ScaleBand::new(categories.to_vec(), (0.0, inner_width))
                .padding(crate::helpers::adaptive_bar_padding(categories.len()));
            let linear = ScaleLinear::new((effective_min, effective_max), (inner_height, 0.0));
            // Match JS: barWidth = min(bandwidth, chartWidth * 0.2), centered in band
            let max_bar_width = inner_width * 0.2;
            let bar_render_width = band.bandwidth().min(max_bar_width);
            let x_inset = (band.bandwidth() - bar_render_width) / 2.0;

            for point in &stacked_points {
                let x = match band.map(&point.key) {
                    Some(x) => x,
                    None => continue,
                };
                let y_top = linear.map(point.y1);
                let y_bottom = linear.map(point.y0);
                let bar_height = (y_bottom - y_top).abs();

                let series_idx = series_names.iter().position(|s| s == &point.series).unwrap_or(0);
                let fill = config
                    .colors
                    .get(series_idx)
                    .cloned()
                    .unwrap_or_else(|| "#2E7D9A".to_string());

                elements.push(build_bar_element(
                    BarRectSpec {
                        x: x + x_inset,
                        y: y_top,
                        width: bar_render_width,
                        height: bar_height,
                        is_horizontal: false,
                        is_negative: point.value < 0.0,
                        fill,
                        class: "bar bar-rect".to_string(),
                        data: Some(
                            ElementData::new(&point.key, format_value(point.value, y_fmt_ref))
                                .with_series(&point.series),
                        ),
                    },
                    &config.theme,
                ));
            }
        }

        Ok((effective_max, elements))
    } else {
        // Grouped (or default multi-series)
        // Find overall max value
        let value_max = (0..data.num_rows())
            .filter_map(|i| data.get_f64(i, value_field))
            .fold(0.0_f64, f64::max);
        let value_max = if value_max <= 0.0 { 1.0 } else { value_max };
        let effective_max = if domain_max < f64::MAX { domain_max } else { value_max };

        let num_series = series_names.len().max(1);

        if is_horizontal {
            // Horizontal grouped: band on y-axis (height), linear on x-axis (width)
            let band = ScaleBand::new(categories.to_vec(), (0.0, inner_height))
                .padding(0.05);
            let linear = ScaleLinear::new((domain_min, effective_max), (0.0, inner_width));
            let sub_band_height = band.bandwidth() / num_series as f64;

            for i in 0..data.num_rows() {
                let cat = match data.get_string(i, category_field) {
                    Some(c) => c,
                    None => continue,
                };
                let series = match data.get_string(i, color_field) {
                    Some(s) => s,
                    None => continue,
                };
                let val = data.get_f64(i, value_field).unwrap_or(0.0);

                let y_base = match band.map(&cat) {
                    Some(y) => y,
                    None => continue,
                };
                let series_idx = series_names.iter().position(|s| s == &series).unwrap_or(0);
                let y = y_base + series_idx as f64 * sub_band_height;

                let bar_left = linear.map(0.0);
                let bar_right = linear.map(val);
                let bar_width = (bar_right - bar_left).abs();

                let fill = config
                    .colors
                    .get(series_idx)
                    .cloned()
                    .unwrap_or_else(|| "#2E7D9A".to_string());

                elements.push(build_bar_element(
                    BarRectSpec {
                        x: bar_left.min(bar_right),
                        y,
                        width: bar_width,
                        height: sub_band_height,
                        is_horizontal: true,
                        is_negative: val < 0.0,
                        fill,
                        class: "bar bar-rect".to_string(),
                        data: Some(
                            ElementData::new(&cat, format_value(val, y_fmt_ref))
                                .with_series(&series),
                        ),
                    },
                    &config.theme,
                ));
            }
        } else {
            // Vertical grouped: band on x-axis (width), linear on y-axis (height)
            let band = ScaleBand::new(categories.to_vec(), (0.0, inner_width))
                .padding(0.05);
            let linear = ScaleLinear::new((domain_min, effective_max), (inner_height, 0.0));
            let sub_band_width = band.bandwidth() / num_series as f64;

            for i in 0..data.num_rows() {
                let cat = match data.get_string(i, category_field) {
                    Some(c) => c,
                    None => continue,
                };
                let series = match data.get_string(i, color_field) {
                    Some(s) => s,
                    None => continue,
                };
                let val = data.get_f64(i, value_field).unwrap_or(0.0);

                let x_base = match band.map(&cat) {
                    Some(x) => x,
                    None => continue,
                };
                let series_idx = series_names.iter().position(|s| s == &series).unwrap_or(0);
                let x = x_base + series_idx as f64 * sub_band_width;

                let bar_top = linear.map(val);
                let bar_bottom = linear.map(0.0);
                let bar_height = (bar_bottom - bar_top).abs();

                let fill = config
                    .colors
                    .get(series_idx)
                    .cloned()
                    .unwrap_or_else(|| "#2E7D9A".to_string());

                elements.push(build_bar_element(
                    BarRectSpec {
                        x,
                        y: bar_top,
                        width: sub_band_width,
                        height: bar_height,
                        is_horizontal: false,
                        is_negative: val < 0.0,
                        fill,
                        class: "bar bar-rect".to_string(),
                        data: Some(
                            ElementData::new(&cat, format_value(val, y_fmt_ref))
                                .with_series(&series),
                        ),
                    },
                    &config.theme,
                ));
            }
        }

        Ok((value_max, elements))
    }
}

/// Render a combo chart: multiple fields with different marks (bar/line) and optional dual axis.
fn render_combo(
    data: &DataTable,
    config: &ChartConfig,
    fields: &[chartml_core::spec::FieldSpec],
) -> Result<ChartElement, ChartError> {
    use chartml_core::shapes::LineGenerator;
    use chartml_core::layout::stack::StackLayout;

    let category_field = get_field_name(&config.visualize.columns)?;
    let categories = data.unique_values(&category_field);
    if categories.is_empty() {
        return Err(ChartError::DataError("No category values found".into()));
    }

    let y_fmt = get_y_format(config);
    let y_fmt_ref = y_fmt.as_deref();
    let grid = GridConfig::from_config(config);
    let x_format = get_x_format(config);

    // Detect stacking mode and color field for bar sub-series
    let color_field = get_color_field(config);
    let is_stacked = matches!(config.visualize.mode, Some(ChartMode::Stacked));

    // Margins — account for right axis if present
    let has_right = fields.iter().any(|f| f.axis.as_deref() == Some("right"));
    let right_fmt = config.visualize.axes.as_ref()
        .and_then(|a| a.right.as_ref())
        .and_then(|a| a.format.as_deref());

    // Pre-compute right tick labels to measure their width
    let right_tick_labels: Vec<String> = if has_right {
        // Estimate right-axis values for label width measurement
        let right_max = fields.iter()
            .filter(|f| f.axis.as_deref() == Some("right"))
            .flat_map(|f| (0..data.num_rows()).filter_map(|i| data.get_f64(i, &f.field)))
            .fold(0.0_f64, f64::max);
        let right_domain_max = config.visualize.axes.as_ref()
            .and_then(|a| a.right.as_ref())
            .and_then(|a| a.max)
            .unwrap_or(if right_max <= 0.0 { 1.0 } else { right_max });
        let tmp_scale = ScaleLinear::new((0.0, right_domain_max), (0.0, 100.0));
        tmp_scale.ticks(5).iter().map(|v| format_value(*v, right_fmt)).collect()
    } else {
        vec![]
    };

    let has_x_axis_label = config.visualize.axes.as_ref()
        .and_then(|a| a.x.as_ref())
        .and_then(|a| a.label.as_ref())
        .is_some();
    // Pre-compute combo legend height from field labels
    let combo_legend_labels: Vec<String> = fields.iter()
        .map(|f| f.label.clone().unwrap_or_else(|| f.field.clone()))
        .collect();
    let combo_legend_height = if combo_legend_labels.len() > 1 || color_field.is_some() {
        let legend_config = LegendConfig {
            text_metrics: TextMetrics::from_theme_legend(&config.theme),
            ..LegendConfig::default()
        };
        calculate_legend_layout(&combo_legend_labels, &config.colors, config.width, &legend_config).total_height
    } else {
        0.0
    };
    let margin_config = MarginConfig {
        has_title: config.title.is_some(),
        legend_height: combo_legend_height,
        // Left Y-axis label is not rendered for combo charts (see comment below),
        // so do not reserve extra left-margin space for it.
        has_y_axis_label: false,
        has_x_axis_label,
        has_right_axis: has_right,
        right_tick_labels,
        tick_value_metrics: TextMetrics::from_theme_tick_value(&config.theme),
        axis_label_metrics: TextMetrics::from_theme_axis_label(&config.theme),
        ..Default::default()
    };
    let margins = calculate_margins(&margin_config);
    let inner_width = margins.inner_width(config.width);
    let inner_height = margins.inner_height(config.height);

    let band = ScaleBand::new(categories.clone(), (0.0, inner_width))
        .padding(crate::helpers::adaptive_bar_padding(categories.len()));
    let bandwidth = band.bandwidth();

    // Separate fields by axis
    let left_fields: Vec<&chartml_core::spec::FieldSpec> = fields.iter()
        .filter(|f| f.axis.as_deref() != Some("right"))
        .collect();
    let right_fields: Vec<&chartml_core::spec::FieldSpec> = fields.iter()
        .filter(|f| f.axis.as_deref() == Some("right"))
        .collect();

    // Compute left-axis domain with D3-style nice rounding (Regressions 2 & 3).
    // When stacked with a color field, the domain max is the per-category sum of all series.
    let left_max = if let (true, Some(color_f)) = (is_stacked, color_field.as_ref()) {
        let color_series = data.unique_values(color_f);
        let mut max_stack = 0.0_f64;
        for f in &left_fields {
            for cat in &categories {
                let mut stack_total = 0.0_f64;
                for series in &color_series {
                    let val = (0..data.num_rows())
                        .find(|&i| {
                            data.get_string(i, &category_field).as_deref() == Some(cat.as_str())
                                && data.get_string(i, color_f).as_deref() == Some(series.as_str())
                        })
                        .and_then(|i| data.get_f64(i, &f.field))
                        .unwrap_or(0.0);
                    stack_total += val;
                }
                max_stack = max_stack.max(stack_total);
            }
        }
        max_stack
    } else {
        left_fields.iter()
            .flat_map(|f| (0..data.num_rows()).filter_map(|i| data.get_f64(i, &f.field)))
            .fold(0.0_f64, f64::max)
    };
    // Compute left-axis data minimum to support negative bar values.
    let left_data_min = left_fields.iter()
        .flat_map(|f| (0..data.num_rows()).filter_map(|i| data.get_f64(i, &f.field)))
        .fold(0.0_f64, f64::min);
    // Keep data_min at 0 when all values are non-negative (standard bar chart behavior)
    let left_data_min = if left_data_min >= 0.0 { 0.0 } else { left_data_min };
    let axes_left = config.visualize.axes.as_ref().and_then(|a| a.left.as_ref());
    let left_explicit_min = axes_left.and_then(|a| a.min);
    let left_explicit_max = axes_left.and_then(|a| a.max);
    let raw_left_domain_min = left_explicit_min.unwrap_or(left_data_min);
    let raw_left_domain_max = left_explicit_max.unwrap_or(if left_max <= 0.0 { 1.0 } else { left_max });
    let (left_domain_min, left_domain_max) = if left_explicit_min.is_none() && left_explicit_max.is_none() {
        // Use count=5 to align with generate_y_axis_numeric's hardcoded tick count of 5.
        nice_domain(raw_left_domain_min, raw_left_domain_max, 5)
    } else {
        (raw_left_domain_min, raw_left_domain_max)
    };
    let left_scale = ScaleLinear::new((left_domain_min, left_domain_max), (inner_height, 0.0));

    // Compute right-axis domain with D3-style nice rounding (Regressions 2 & 3).
    let right_scale = if !right_fields.is_empty() {
        let right_max = right_fields.iter()
            .flat_map(|f| (0..data.num_rows()).filter_map(|i| data.get_f64(i, &f.field)))
            .fold(0.0_f64, f64::max);
        let right_data_min = right_fields.iter()
            .flat_map(|f| (0..data.num_rows()).filter_map(|i| data.get_f64(i, &f.field)))
            .fold(0.0_f64, f64::min);
        let axes_right = config.visualize.axes.as_ref().and_then(|a| a.right.as_ref());
        let right_explicit_min = axes_right.and_then(|a| a.min);
        let right_explicit_max = axes_right.and_then(|a| a.max);
        let raw_right_domain_min = right_explicit_min.unwrap_or(if right_data_min < 0.0 { right_data_min } else { 0.0 });
        let raw_right_domain_max = right_explicit_max.unwrap_or(if right_max <= 0.0 { 1.0 } else { right_max });
        let (right_domain_min, right_domain_max) = if right_explicit_min.is_none() && right_explicit_max.is_none() {
            // Use count=5 to align with generate_y_axis_numeric's hardcoded tick count of 5.
            nice_domain(raw_right_domain_min, raw_right_domain_max, 5)
        } else {
            (raw_right_domain_min, raw_right_domain_max)
        };
        Some(ScaleLinear::new((right_domain_min, right_domain_max), (inner_height, 0.0)))
    } else {
        None
    };

    let mut children = Vec::new();

    // Title is rendered as HTML outside the SVG — not added here.

    // Axes
    let bottom_axis_label = config.visualize.axes.as_ref()
        .and_then(|a| a.x.as_ref())
        .and_then(|a| a.label.as_deref());
    let x_axis_result = generate_x_axis(&crate::helpers::XAxisParams {
        labels: &categories,
        display_label_overrides: None,
        range: (0.0, inner_width),
        y_position: margins.top + inner_height,
        available_width: inner_width,
        x_format: x_format.as_deref(),
        chart_height: Some(inner_height),
        grid: &grid,
        axis_label: bottom_axis_label,
        theme: &config.theme,
    });
    let left_axis_label = axes_left.and_then(|a| a.label.as_deref());
    let y_axis_left = generate_y_axis_numeric(&crate::helpers::YAxisNumericParams {
        domain: (left_domain_min, left_domain_max),
        range: (inner_height, 0.0),
        x_position: margins.left,
        fmt: y_fmt_ref,
        tick_count: adaptive_tick_count(inner_height),
        chart_width: Some(inner_width),
        grid: &grid,
        axis_label: left_axis_label,
        theme: &config.theme,
    });

    let mut axis_elements = Vec::new();
    axis_elements.extend(x_axis_result.elements.into_iter().map(|e| offset_element(e, margins.left, 0.0)));
    axis_elements.extend(y_axis_left.into_iter().map(|e| offset_element(e, 0.0, margins.top)));
    // Zero-line (Phase 7): applied to the LEFT numeric axis domain only. The
    // combo/grouped path is always vertically-oriented (grouped horizontal bars
    // are not supported here). No-op when theme.zero_line is None (default) or
    // when the left domain doesn't strictly cross zero.
    if let Some(zl) = emit_zero_line_if_crosses(
        &config.theme,
        (left_domain_min, left_domain_max),
        inner_width,
        inner_height,
        false,
    ) {
        axis_elements.push(offset_element(zl, margins.left, margins.top));
    }

    // Right axis — ticks and labels on the right side
    if let Some(ref rs) = right_scale {
        let right_fmt = config.visualize.axes.as_ref()
            .and_then(|a| a.right.as_ref())
            .and_then(|a| a.format.as_deref());
        // Right axis label is rendered manually below (outside this block),
        // so pass None here to avoid duplication.
        let right_axis = generate_y_axis_numeric_right(
            rs.domain(), (inner_height, 0.0), margins.left + inner_width,
            right_fmt, adaptive_tick_count(inner_height),
            None, &config.theme,
        );
        axis_elements.extend(right_axis.into_iter().map(|e| offset_element(e, 0.0, margins.top)));
    }

    // Right axis title label — rendered manually here with absolute positioning
    // (the left axis label is already handled by generate_y_axis_numeric above).
    if let Some(label) = config.visualize.axes.as_ref().and_then(|a| a.right.as_ref()).and_then(|a| a.label.clone()) {
        let rx = config.width - 12.0;
        let ts = TextStyle::for_role(&config.theme, TextRole::AxisLabel);
        axis_elements.push(ChartElement::Text {
            x: rx,
            y: margins.top + inner_height / 2.0,
            content: label,
            anchor: TextAnchor::Middle,
            dominant_baseline: None,
            transform: Some(Transform::Rotate(90.0, rx, margins.top + inner_height / 2.0)),
            font_family: ts.font_family,
            font_size: ts.font_size,
            font_weight: ts.font_weight,
            letter_spacing: ts.letter_spacing,
            text_transform: ts.text_transform,
            fill: Some(config.theme.text_secondary.clone()),
            class: "axis-label".to_string(),
            data: None,
        });
    }

    children.push(ChartElement::Group {
        class: "axes".to_string(), transform: None, children: axis_elements,
    });

    // Render each field
    let mut mark_elements = Vec::new();
    let line_gen = LineGenerator::new().curve(chartml_core::shapes::CurveType::MonotoneX);

    // Count bar fields for grouped subdivision
    let num_bar_fields = fields.iter()
        .filter(|f| f.mark.as_deref().unwrap_or("bar") == "bar")
        .count()
        .max(1);
    // Match JS: barWidth = min(bandwidth, chartWidth * 0.2), centered within band
    let max_bar_width = inner_width * 0.2;
    let effective_bandwidth = bandwidth.min(max_bar_width);
    let combo_x_inset = (bandwidth - effective_bandwidth) / 2.0;
    let sub_bar_padding = effective_bandwidth * 0.05;
    let sub_bar_width = (effective_bandwidth - sub_bar_padding * (num_bar_fields as f64 - 1.0).max(0.0)) / num_bar_fields as f64;
    let mut bar_field_idx = 0_usize;
    let mut series_names = Vec::new();
    let mut series_colors = Vec::new();
    let mut series_marks = Vec::new();

    // Pre-compute stacked bar layout if stacking with color field
    let stacked_bar_rendered = if let (true, Some(color_f)) = (is_stacked, color_field.as_ref()) {
        let color_series = data.unique_values(color_f);

        // For each bar field, render stacked bars by color series
        for field_spec in fields.iter() {
            let mark = field_spec.mark.as_deref().unwrap_or("bar");
            if mark != "bar" { continue; }

            let field_name = &field_spec.field;
            let is_right = field_spec.axis.as_deref() == Some("right");
            let scale = if is_right { right_scale.as_ref().unwrap_or(&left_scale) } else { &left_scale };
            let fmt_ref = if is_right {
                config.visualize.axes.as_ref().and_then(|a| a.right.as_ref()).and_then(|a| a.format.as_deref())
            } else {
                y_fmt_ref
            };

            // Build values matrix: values[series_idx][category_idx]
            let mut values_matrix: Vec<Vec<f64>> = Vec::new();
            for series in &color_series {
                let mut series_vals = Vec::new();
                for cat in &categories {
                    let val = (0..data.num_rows())
                        .find(|&i| {
                            data.get_string(i, &category_field).as_deref() == Some(cat.as_str())
                                && data.get_string(i, color_f).as_deref() == Some(series.as_str())
                        })
                        .and_then(|i| data.get_f64(i, field_name))
                        .unwrap_or(0.0);
                    series_vals.push(val);
                }
                values_matrix.push(series_vals);
            }

            let stack = StackLayout::new();
            let stacked_points = stack.layout(&categories, &color_series, &values_matrix);

            let bar_render_width = bandwidth.min(max_bar_width);
            let x_inset = (bandwidth - bar_render_width) / 2.0;

            for point in &stacked_points {
                let x = match band.map(&point.key) { Some(x) => x, None => continue };
                let y_top = scale.map(point.y1);
                let y_bottom = scale.map(point.y0);
                let bar_height = (y_bottom - y_top).abs();

                let series_idx = color_series.iter().position(|s| s == &point.series).unwrap_or(0);
                let fill = config.colors.get(series_idx).cloned().unwrap_or_else(|| "#2E7D9A".to_string());

                mark_elements.push(build_bar_element(
                    BarRectSpec {
                        x: x + x_inset + margins.left,
                        y: y_top + margins.top,
                        width: bar_render_width,
                        height: bar_height,
                        is_horizontal: false,
                        is_negative: point.value < 0.0,
                        fill,
                        class: "bar bar-rect".to_string(),
                        data: Some(
                            ElementData::new(&point.key, format_value(point.value, fmt_ref))
                                .with_series(&point.series),
                        ),
                    },
                    &config.theme,
                ));
            }
        }

        // Add color series to legend tracking
        for (si, series_name) in color_series.iter().enumerate() {
            let color = config.colors.get(si).cloned().unwrap_or_else(|| "#2E7D9A".to_string());
            series_names.push(series_name.clone());
            series_colors.push(color);
            series_marks.push("bar".to_string());
        }

        true
    } else {
        false
    };

    for (field_idx, field_spec) in fields.iter().enumerate() {
        let field_name = &field_spec.field;
        let is_right = field_spec.axis.as_deref() == Some("right");
        let scale = if is_right { right_scale.as_ref().unwrap_or(&left_scale) } else { &left_scale };
        let mark = field_spec.mark.as_deref().unwrap_or("bar");
        let color = field_spec.color.clone()
            .unwrap_or_else(|| config.colors.get(field_idx).cloned().unwrap_or_else(|| "#2E7D9A".to_string()));
        let label = field_spec.label.clone().unwrap_or_else(|| field_name.clone());
        let fmt_ref = if is_right {
            config.visualize.axes.as_ref().and_then(|a| a.right.as_ref()).and_then(|a| a.format.as_deref())
        } else {
            y_fmt_ref
        };

        match mark {
            "bar" if stacked_bar_rendered => {
                // Already rendered above via stacked layout — skip
            }
            "bar" => {
                let this_bar_idx = bar_field_idx;
                bar_field_idx += 1;

                for row_i in 0..data.num_rows() {
                    let cat = match data.get_string(row_i, &category_field) { Some(c) => c, None => continue };
                    let val = data.get_f64(row_i, field_name).unwrap_or(0.0);
                    let x = match band.map(&cat) { Some(x) => x, None => continue };
                    let bar_x = x + combo_x_inset + this_bar_idx as f64 * (sub_bar_width + sub_bar_padding);
                    let bar_val_y = scale.map(val);
                    let bar_zero_y = scale.map(0.0);
                    let bar_height = (bar_zero_y - bar_val_y).abs();
                    let rect_y = bar_val_y.min(bar_zero_y);

                    mark_elements.push(build_bar_element(
                        BarRectSpec {
                            x: bar_x + margins.left,
                            y: rect_y + margins.top,
                            width: sub_bar_width,
                            height: bar_height,
                            is_horizontal: false,
                            is_negative: val < 0.0,
                            fill: color.clone(),
                            class: "bar bar-rect".to_string(),
                            data: Some(
                                ElementData::new(&cat, format_value(val, fmt_ref))
                                    .with_series(&label),
                            ),
                        },
                        &config.theme,
                    ));

                    // Data labels
                    if let Some(ref dl) = field_spec.data_labels {
                        if dl.show == Some(true) {
                            let dl_fmt = dl.format.as_deref().or(fmt_ref);
                            mark_elements.push(ChartElement::Text {
                                x: bar_x + sub_bar_width / 2.0 + margins.left,
                                y: rect_y + margins.top - 5.0,
                                content: format_value(val, dl_fmt),
                                anchor: TextAnchor::Middle, dominant_baseline: None,
                                transform: None,
                                font_family: None,
                                font_size: Some(dl.font_size.map(|s| format!("{}px", s)).unwrap_or_else(|| "12px".to_string())),
                                font_weight: None,
                                letter_spacing: None,
                                text_transform: None,
                                fill: Some(dl.color.clone().unwrap_or_else(|| config.theme.text_secondary.clone())),
                                class: "data-label".to_string(), data: None,
                            });
                        }
                    }
                }
            }
            _ => {
                let mut points = Vec::new();
                let mut point_data = Vec::new();
                for cat in &categories {
                    let row_i = match (0..data.num_rows()).find(|&i| data.get_string(i, &category_field).as_deref() == Some(cat.as_str())) {
                        Some(i) => i, None => continue,
                    };
                    let val = match data.get_f64(row_i, field_name) { Some(v) => v, None => continue };
                    let x = match band.map(cat) { Some(x) => x + bandwidth / 2.0, None => continue };
                    let y = scale.map(val);
                    points.push((x + margins.left, y + margins.top));
                    point_data.push((cat.clone(), val));
                }

                if !points.is_empty() {
                    let path_d = line_gen.generate(&points);
                    mark_elements.push(ChartElement::Path {
                        d: path_d, fill: None, stroke: Some(color.clone()),
                        stroke_width: Some(config.theme.series_line_weight as f64), stroke_dasharray: None,
                        opacity: None,
                        class: "chartml-line-path series-line".to_string(),
                        data: Some(ElementData::new(&label, "").with_series(&label)),
                        animation_origin: None,
                    });

                    // Dots
                    let dot_r = config.theme.dot_radius as f64;
                    for (i, &(px, py)) in points.iter().enumerate() {
                        let (ref cat, val) = point_data[i];
                        if let Some(halo) = emit_dot_halo_if_enabled(&config.theme, px, py, dot_r) {
                            mark_elements.push(halo);
                        }
                        mark_elements.push(ChartElement::Circle {
                            cx: px, cy: py, r: dot_r,
                            fill: color.clone(), stroke: Some(config.theme.bg.clone()),
                            class: "chartml-line-dot dot-marker".to_string(),
                            data: Some(ElementData::new(cat, format_value(val, fmt_ref)).with_series(&label)),
                        });
                    }

                    // Data labels
                    if let Some(ref dl) = field_spec.data_labels {
                        if dl.show == Some(true) {
                            let dl_fmt = dl.format.as_deref().or(fmt_ref);
                            for (i, &(px, py)) in points.iter().enumerate() {
                                let (_, val) = &point_data[i];
                                mark_elements.push(ChartElement::Text {
                                    x: px, y: py - 10.0,
                                    content: format_value(*val, dl_fmt),
                                    anchor: TextAnchor::Middle, dominant_baseline: None,
                                    transform: None,
                                    font_family: None,
                                    font_size: Some(dl.font_size.map(|s| format!("{}px", s)).unwrap_or_else(|| "12px".to_string())),
                                    font_weight: None,
                                    letter_spacing: None,
                                    text_transform: None,
                                    fill: Some(dl.color.clone().unwrap_or_else(|| color.clone())),
                                    class: "data-label".to_string(), data: None,
                                });
                            }
                        }
                    }
                }
            }
        }

        // When stacked bars were rendered via color field, the color series are already
        // tracked for legend — skip adding the bar field itself.
        if !(stacked_bar_rendered && mark == "bar") {
            series_names.push(label);
            series_colors.push(color);
            series_marks.push(mark.to_string());
        }
    }

    children.push(ChartElement::Group {
        class: "marks".to_string(), transform: None, children: mark_elements,
    });

    // Annotations — rendered on top of marks, in inner coordinate space
    if let Some(annotations) = config.visualize.annotations.as_deref() {
        if !annotations.is_empty() {
            let ann_elements = generate_annotations(
                annotations,
                &left_scale,
                0.0,
                inner_width,
                inner_height,
                Some(&categories),
                &config.theme,
            );
            if !ann_elements.is_empty() {
                children.push(ChartElement::Group {
                    class: "annotations".to_string(),
                    transform: Some(Transform::Translate(margins.left, margins.top)),
                    children: ann_elements,
                });
            }
        }
    }

    // Legend with mixed marks — anchor using pre-computed legend height
    if series_names.len() > 1 {
        let combo_legend_metrics = TextMetrics::from_theme_legend(&config.theme);
        let mut legend_elements = Vec::new();
        let total_w: f64 = series_names.iter().map(|name| {
            let tw = measure_text(name, &combo_legend_metrics);
            12.0 + 6.0 + tw + 16.0
        }).sum();
        let mut x_offset = (config.width - total_w).max(0.0) / 2.0;
        let legend_y = config.height - combo_legend_height - 8.0;

        for (i, name) in series_names.iter().enumerate() {
            let color = &series_colors[i];
            let mark = series_marks[i].as_str();
            let y = legend_y;

            match mark {
                "line" => {
                    legend_elements.push(ChartElement::Line {
                        x1: x_offset, y1: y + 6.0, x2: x_offset + 12.0, y2: y + 6.0,
                        stroke: color.clone(), stroke_width: Some(2.5),
                        stroke_dasharray: None, class: "legend-symbol legend-line".to_string(),
                    });
                }
                _ => {
                    legend_elements.push(ChartElement::Rect {
                        x: x_offset, y, width: 12.0, height: 12.0,
                        fill: color.clone(), stroke: None,
                        rx: None, ry: None,
                        class: "legend-symbol".to_string(), data: None,
                        animation_origin: None,
                    });
                }
            }

            let ts = TextStyle::for_role(&config.theme, TextRole::LegendLabel);
            legend_elements.push(ChartElement::Text {
                x: x_offset + 18.0, y: y + 10.0, content: name.clone(),
                anchor: TextAnchor::Start, dominant_baseline: None,
                transform: None,
                font_family: ts.font_family,
                font_size: ts.font_size,
                font_weight: ts.font_weight,
                letter_spacing: ts.letter_spacing,
                text_transform: ts.text_transform,
                fill: Some(config.theme.text_secondary.clone()), class: "legend-label".to_string(), data: None,
            });

            let tw = measure_text(name, &combo_legend_metrics);
            x_offset += 12.0 + 6.0 + tw + 16.0;
        }

        children.push(ChartElement::Group {
            class: "legend".to_string(), transform: None, children: legend_elements,
        });
    }

    Ok(ChartElement::Svg {
        viewbox: ViewBox::new(0.0, 0.0, config.width, config.height),
        width: Some(config.width),
        height: Some(config.height),
        class: "chartml-bar chartml-combo".to_string(),
        children,
    })
}