charton 0.5.0

A high-performance, layered charting system for Rust, featuring a flexible data core and multi-backend rendering.
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
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
# Charton: Grammar of Graphics in Rust

# Chapter 1 · Introduction

## 1.1 What is Charton? (The Core Idea)

Charton is a modern Rust visualization library designed around a simple, declarative framework for data visualization.

- Declarative API: It offers an API similar to Python's Altair/Vega-Lite, allowing users to define "what to visualize" rather than "how to draw it.". That is, "the Grammar of Graphics".
- Native Polars Support: Charton is tightly integrated with the high-performance Rust DataFrame library Polars, enabling efficient, zero-copy data plotting.
- Dual Rendering Capability: You can utilize its pure Rust SVG renderer for dependency-free plotting, or leverage its IPC mechanism to seamlessly connect with external Python visualization ecosystems like Altair and Matplotlib.

## 1.2 Design Philosophy and Key Advantages

Charton is engineered to be an efficient, safe, and flexible solution, built on the principle that visualization should be declarative.
- 🚀 Performance and Safety: It leverages Rust's strong type system to achieve compile-time safety and utilizes Polars' integration for superior data handling performance.
- 💡 Layered and Expressive: It features a multi-layer plotting architecture that easily combines various marks (e.g., points, lines, bars, boxplots, error bars) within a shared coordinate system to create complex composite visualizations.
- 🌐 Frontend Ready: It can generate standard Vega-Lite JSON specifications, making it ready for easy integration into modern web applications using libraries like React-Vega or Vega-Embed.
- 🔗 Efficient Integration: Through Inter-Process Communication (IPC), it efficiently communicates with external Python libraries, avoiding slow, temporary file operations and maintaining compatibility with environments like Conda in Jupyter.
- 📓 Jupyter Interactivity: It offers native support for the evcxr Jupyter Notebook environment, enabling interactive and real-time exploratory data analysis.

## 1.3 System Architecture

Charton adopts a modern, decoupled architecture designed for high-performance data processing and cross-language interoperability.
```text
┌───────────────────────────────────────────────────────────────────────────┐
│                            Input Layer                                    │
│  ┌──────────────┐    ┌──────────────┐    ┌──────────────────────────────┐ │
│  │ Rust Polars  │    │ External     │    │ Jupyter/evcxr Interactive    │ │
│  │ DataFrame/   │    │ Datasets     │    │ Input                        │ │
│  │ LazyFrame    │    │ (CSV/Parquet)│    │ (Notebook cell data/commands)│ │
│  └──────────────┘    └──────────────┘    └──────────────────────────────┘ │
└────────────────────────────────────┬──────────────────────────────────────┘
┌────────────────────────────────────▼──────────────────────────────────────┐
│                          Core Layer                                       │
│  ┌──────────────────────────────────────────────────────────────────────┐ │
│  │            Charton Core Engine                                       │ │
│  │  ┌──────────────┐    ┌───────────────┐    ┌──────────────────────┐   │ │
│  │  │ Declarative  │    │ Layered       │    │ Cross-backend Data   │   │ │
│  │  │ API (Altair- │    │ Chart         │    │ Converter            │   │ │
│  │  │ style)       │    │ Management    │    │ (Rust ↔ Python/JSON) │   │ │
│  │  └──────────────┘    │ (LayeredChart)│    └──────────────────────┘   │ │
│  │                      └───────────────┘                               │ │
│  │  ┌──────────────┐    ┌───────────────┐    ┌──────────────────────┐   │ │
│  │  │ Data         │    │ IPC           │    │ Vega-Lite Spec       │   │ │
│  │  │ Validation/  │    │ Communication │    │ Generator            │   │ │
│  │  │ Mapping      │    │ Module        │    │                      │   │ │
│  │  └──────────────┘    └───────────────┘    └──────────────────────┘   │ │
│  └──────────────────────────────────────────────────────────────────────┘ │
└────────────────────────────────────┬──────────────────────────────────────┘
┌────────────────────────────────────▼──────────────────────────────────────┐
│                        Render Backends                                    │
│  ┌──────────────────────┐    ┌────────────────────────────────────────┐   │
│  │ Rust Native Backend  │    │ External Cross-Language Backends       │   │
│  │                      │    │                                        │   │
│  │  ┌────────────────┐  │    │  ┌─────────────┐  ┌──────────────────┐ │   │
│  │  │ Pure Rust SVG  │  │    │  │ Altair      │  │ Matplotlib       │ │   │
│  │  │ Renderer       │  │    │  │ Backend     │  │ Backend          │ │   │
│  │  └────────────────┘  │    │  │ (Python IPC)│  │ (Python IPC)     │ │   │
│  │                      │    │  └─────────────┘  └──────────────────┘ │   │
│  │  ┌────────────────┐  │    │                                        │   │
│  │  │ Wasm Renderer  │  │    │  ┌────────────┐  ┌──────────────────┐  │   │
│  │  │ (Partial       │  │    │  │ Other      │  │ Extended Backends│  │   │
│  │  │  Support)      │  │    │  │ Python     │  │ (Future)         │  │   │
│  │  └────────────────┘  │    │  │ Viz Libs   │  │ (R/Julia, etc.)  │  │   │
│  │                      │    │  └────────────┘  └──────────────────┘  │   │
│  └──────────────────────┘    └────────────────────────────────────────┘   │
└────────────────────────────────────┬──────────────────────────────────────┘
┌────────────────────────────────────▼──────────────────────────────────────┐
│                          Output Layer                                     │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐   │
│  │ SVG Vector   │  │ Vega-Lite    │  │ PNG Bitmap   │  │ Jupyter      │   │
│  │ Graphics     │  │ JSON         │  │ Image        │  │ Inline       │   │
│  │ (Native/Wasm)│  │ (for Web)    │  │ (via Ext.)   │  │ Rendering    │   │
│  └──────────────┘  └──────────────┘  └──────────────┘  └──────────────┘   │
└───────────────────────────────────────────────────────────────────────────┘
```

**1. Input Layer (Data Orchestration)**
- **Polars-Native**: Unlike other libraries that require heavy data cloning, Charton is built on **Apache Arrow** (via Polars), enabling efficient, zero-copy data access.
- **Versatile Sourcing**: It supports `DataFrame` and `LazyFrame`, allowing for out-of-core data processing before visualization.

**2. Core Layer (The Grammar Engine)**
- **Declarative DSL**: A type-safe implementation of the **Grammar of Graphics**, allowing users to compose complex visualizations using intuitive tuples (e.g., `.encode((x, y, color))`).
- **Universal Data Bridge**: This is the core innovation of Charton. It utilizes **Parquet-serialized bytes** as an intermediate format to exchange data between different Polars versions and languages, effectively bypassing Rust's orphan rules and dependency conflicts.
- **Vega-Lite Spec Generator**: A high-level compiler that transforms Rust structures into standard Vega-Lite JSON for seamless frontend integration.

**3. Render Backends (Multi-Engine)**
- **Rust Native Backend**: A **zero-dependency**, pure Rust implementation. It uses a custom SVG renderer for ultra-fast, server-side batch generation and provides partial support for WebAssembly (Wasm).
- **IPC Bridge (External)**: For features not yet in the native engine, Charton provides a high-speed Inter-Process Communication (IPC) bridge to Python’s mature ecosystem (**Altair/Matplotlib**), eliminating the need for slow temporary disk I/O.

**4. Output Layer (Multi-Format Delivery)**
- **Vector & Raster**: Support for SVG and high-resolution PNG (via `resvg`).
- **Web & Notebook**: Direct JSON output for **React/Vue** integration and inline rendering for **evcxr Jupyter** notebooks.

## 1.4 Why This Architecture Matters

🚀 **Solving the "Version Hell"**

In the Rust ecosystem, if your project depends on Polars `v0.50` and a plotting library depends on `v0.40`, your code won't compile. Charton’s **Parquet-encoded IPC** bypasses this entirely, making it the most robust visualization tool for production Rust environments.

🔌 **Hot-Swappable Backends**

You can develop interactively using the **Altair backend** to leverage its rich feature set, and then switch to the **Native SVG backend** for deployment to achieve maximum performance and minimum container size.

🌐 **Frontend-First Design**

By generating standard **Vega-Lite JSON**, Charton allows you to handle heavy data lifting in Rust while letting the browser’s GPU handle the final rendering via `Vega-Embed` or `React-Vega`.

# Chapter 2 · Quick Start

Welcome to **Charton Quick Start**! 

This chapter will guide you through creating charts in Rust using Charton from scratch. By the end of this chapter, you'll know how to:

- Initialize a Rust project and add Charton dependencies
- Load and preprocess data using Polars
- Build charts using Chart, Mark, and Encoding
- Render charts in multiple formats and environments
- Avoid common pitfalls and errors

The goal is to make you productive **within minutes**.

## 2.1 Project Setup

First, create a new Rust project:

```bash
cargo new demo
cd demo
```
Edit your `Cargo.toml` to add Charton and Polars dependencies:
```toml
[dependencies]
charton = "0.2.1"
polars = { version = "0.49", features = ["lazy", "csv", "parquet"] }
```
Run `cargo build` to ensure everything compiles.

## 2.2 Creating Your First Chart

Charton adopts a **declarative visualization** philosophy, drawing heavily from the design principles of Altair and Vega-Lite. Every Charton chart is composed of **three core elements** which allow you to specify *what* you want to see, rather than *how* to draw it:

1. **Chart** – The base container that holds your data (`Chart::build(&df)`).
2. **Mark** – The visual primitive you choose (point, bar, line, etc., defined by `.mark_point()`).
3. **Encoding** – The mapping that links data fields to visual properties (x, y, color, size, etc., defined by `.encode(...)`).

**Example: Analyzing Car Weight vs. MPG (Scatter Plot)**

This minimal Charton example uses the built-in `mtcars` dataset to create a scatter plot of car weight (`wt`) versus miles per gallon (`mpg`).
```rust
use charton::prelude::*;
use polars::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 1. Data Preparation (Polars)
    let df = load_dataset("mtcars")?
        .lazy()
        .with_columns([col("gear").cast(DataType::String)]) // Cast 'gear' for categorical coloring
        .collect()?;

    // 2. Chart Declaration (Chart, Mark, Encoding)
    Chart::build(&df)?          // Chart: Binds the data source
        .mark_point()           // Mark: Specifies the visual primitive (dots)
        .encode((
            x("wt"),            // Encoding: Maps 'wt' (weight) to the X-axis
            y("mpg"),           // Encoding: Maps 'mpg' (fuel efficiency) to the Y-axis
        ))?
        // 3. Converted to Layered Chart
        .into_layered()
        // 4. Saving the Layered Chart to SVG
        .save("./scatter_chart.svg")?;

    println!("Chart saved to scatter_chart.svg");
    Ok(())
}
```
You can also display the result directly in your evcxr jupyter notebook using the `show()` method for quick iteration:
```rust
// ... (using the same 'df' DataFrame)
Chart::build(&df)?
    .mark_point()
    .encode((x("wt"), y("mpg")))?
    .into_layered()
    .show()?;
```

You can even save the chart object to a variable and use it later. For example:
```rust
// ... (using the same 'df' DataFrame)
let chart = Chart::build(&df)?
    .mark_point()
    .encode((x("wt"), y("mpg")))?
    .into_layered();

chart.save("./scatter_chart.svg")?; // or chart.show()?
```
This mirrors the **declarative style of Altair**, now in Rust.

**Explicit form**

The code above is equivalent to the following explicit construction using LayeredChart (see chapter 5).
```rust
use charton::prelude::*;
use polars::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 1. Data Preparation (Polars)
    let df = load_dataset("mtcars")?
        .lazy()
        .with_columns([col("gear").cast(DataType::String)]) // Cast 'gear' for categorical coloring
        .collect()?;

    // 2. Chart Declaration (Chart, Mark, Encoding)
    let scatter = Chart::build(&df)?    // Chart: Binds the data source
        .mark_point()                   // Mark: Specifies the visual primitive (dots)
        .encode((
            x("wt"),                    // Encoding: Maps 'wt' (weight) to the X-axis
            y("mpg"),                   // Encoding: Maps 'mpg' (fuel efficiency) to the Y-axis
        ))?;
    
    // 3. Create a layered chart
    LayeredChart::new() 
        .add_layer(scatter)             // Add the chart as a layer of the layered chart
        .save("./scatter_chart.svg")?;  // Save the layered chart

    println!("Chart saved to scatter_chart.svg");
    Ok(())
}
```

## 2.3 Loading and Preparing Data

Before creating visualizations, Charton requires your data to be stored in a Polars `DataFrame`. Charton itself does not impose restrictions on how data is loaded, so you can rely on Polars’ powerful I/O ecosystem.

### 2.3.1 Built-in Datasets

Charton provides a few built-in datasets for quick experimentation, demos, and tutorials.
```rust
let df = load_dataset("mtcars")?;
```
This returns a Polars DataFrame ready for visualization.

### 2.3.2 Loading CSV Files

CSV is the most common format for tabular data. Using Polars:
```rust
use polars::prelude::*;

let df = CsvReadOptions::default()
    .with_has_header(true)
    .try_into_reader_with_file_path(Some("./datasets/iris.csv".into()))?
    .finish()?;
```

### 2.3.3 Loading Parquet Files

Parquet is a high-performance, columnar storage format widely used in data engineering.
```rust
let file = std::fs::File::open("./datasets/foods.parquet")?;
let df = ParquetReader::new(file).finish()?;
```
Parquet is recommended for large datasets due to compression and fast loading.

### 2.3.4 Loading Data from Parquet Bytes (`Vec<u8>`) — Cross-Version Interoperability

One of the challenges when working with the Polars ecosystem is that **different crates may depend on different Polars versions**, which prevents passing `DataFrame` values directly between libraries. Charton solves this problem by offering a **version-agnostic data exchange format** based on **Parquet-serialized bytes**.

Charton provides an implementation of:
```rust
impl TryFrom<&Vec<u8>> for DataFrameSource
```
This allows you to:

- Serialize a Polars `DataFrame` into Parquet bytes (`Vec<u8>`)
- Pass those bytes to Charton
- Let Charton deserialize them internally using its Polars version
- Avoid Polars version conflicts entirely

This is especially useful when your application depends on a uncompatible Polars version with Charton. By using Parquet bytes as the intermediate format, **data can be exchanged safely across Polars versions**.

**Example: Passing a DataFrame to Charton Using Parquet Bytes**

Below is a full example demonstrating:

1. Creating a Polars `DataFrame`
2. Serializing it into Parquet bytes using your Polars version
3. Passing those bytes to Charton
4. Rendering a scatter plot

**Cargo.toml**
```toml
[dependencies]
polars = { version = "0.51", features = ["parquet"] }
charton = { version = "0.2.1" }
```
**Source Code Example**
```rust
use charton::prelude::*;
use polars::prelude::*;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    // Create a Polars DataFrame using Polars 0.51
    let df = df![
        "length" => [5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9],
        "width"  => [3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1]
    ]?;

    // Serialize DataFrame into Parquet bytes
    let mut buf: Vec<u8> = Vec::new();
    ParquetWriter::new(&mut buf).finish(&mut df.clone())?;

    // Build a Chart using the serialized Parquet bytes
    Chart::build(&buf)?
        .mark_point()
        .encode((
            x("length"),
            y("width"),
        ))?
        .into_layered()
        .save("./scatter.svg")?;

    Ok(())
}
```

## 2.4 Simple Plotting Examples

This section introduces the most common chart types in Charton.

### 2.4.1 Line Chart

```rust
// Create a polars dataframe
let df = df![
    "length" => [4.4, 4.6, 4.7, 4.9, 5.0, 5.1, 5.4], // In ascending order
    "width" => [2.9, 3.1, 3.2, 3.0, 3.6, 3.5, 3.9]
]?;

// Create a line chart layer
Chart::build(&df)?
    .mark_line()               // Line chart
    .encode((
        x("length"),           // Map length column to X-axis
        y("width"),            // Map width column to Y-axis
    ))?
    .into_layered()
    .save("line.svg")?;
```
Useful for trends or ordered sequences.

### 2.4.2 Bar Chart

```rust
let df = df! [
    "type" => ["a", "b", "c", "d"],
    "value" => [4.9, 5.3, 5.5, 6.5],
]?;

Chart::build(&df)?
    .mark_bar()
    .encode((
        x("type"),
        y("value"),
    ))?
    .into_layered()
    .save("bar.svg")?;
```

### 2.4.3 Histogram

```rust
let df = load_dataset("iris")?;

Chart::build(&df)?
    .mark_hist()
    .encode((
        x("sepal_length"),
        // The number of data points (or Frequency) falls into the corresponding bin are named "count".
        // You can use any arbitray name for the y-axis, here we use "count".
        y("count")
    ))?
    .into_layered()
    .save("hist.svg")?;
```
Charton automatically computes bin counts when `y("count")` is specified.

### 2.4.4 Boxplot

```rust
let df = load_dataset("iris")?;

Chart::build(&df)?
    .mark_boxplot()
    .encode((x("species"), y("sepal_length")))?
    .into_layered()
    .save("boxplot.svg")?;
```
Boxplots summarize distributions using quartiles, medians, whiskers, and outliers.

### 2.4.5 Layered Charts

In Charton, complex visualizations are built by **layering multiple charts** on the same axes. Each layer defines a single mark type, and layers are composed to form a unified view with shared scales and coordinates.
```rust
use charton::prelude::*;
use polars::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a polars dataframe
    let df = df![
        "length" => [4.4, 4.6, 4.7, 4.9, 5.0, 5.1, 5.4],
        "width" => [2.9, 3.1, 3.2, 3.0, 3.6, 3.5, 3.9]
    ]?;

    // Create a line chart layer
    let line = Chart::build(&df)?
        .mark_line()                        // Line chart
        .encode((
            x("length"),                    // Map length column to X-axis
            y("width"),                     // Map width column to Y-axis
        ))?;

    // Create a scatter point layer
    let scatter = Chart::build(&df)?
        .mark_point()                       // Scatter plot
        .encode((
            x("length"),                    // Map length column to X-axis
            y("width"),                     // Map width column to Y-axis
        ))?;

    LayeredChart::new()       
        .add_layer(line)                    // Add the line layer
        .add_layer(scatter)                 // Add the scatter point layer
        .save("./layeredchart.svg")?;

    Ok(())
}
```

## 2.5 Exporting Charts

Charton supports exporting charts to different file formats depending on the selected rendering backend. All backends share the same API:

```rust
chart.save("output.png")?;
```
The file format is inferred from the extension.

This section describes the supported formats and saving behavior for each backend.

### 2.5.1 Rust Native Backend


The Rust native backend is the default renderer and supports:

- **SVG** — vector graphics output
- **PNG** — rasterized SVG (using resvg with automatic system font loading)

**Saving SVG**
```rust
chart.save("chart.svg")?;
```
**Saving PNG**

PNG is generated by rasterizing the internal SVG at 2× resolution:
```rust
chart.save("chart.png")?;
```
This produces high-quality PNG output suitable for publication.

### 2.5.2 Altair Backend (Vega-Lite)


The Altair backend uses Vega-Lite as the rendering engine and supports:

- **SVG** — via Vega → SVG conversion
- **PNG** — SVG rasterized via resvg
- **JSON** — raw Vega-Lite specification

**Saving SVG**
```rust
chart.save("chart.svg")?;
```
**Saving PNG**
```rust
chart.save("chart.png")?;
```
**Saving Vega-Lite JSON**
```rust
chart.save("chart.json")?;
```
The JSON file can be opened directly in the online Vega-Lite editor.

### 2.5.3 Matplotlib Backend


The Matplotlib backend supports:

- **PNG** — returned as base64 from Python, decoded and saved

**Saving PNG**
```rust
chart.save("chart.png")?;
```
Other formats (SVG, JSON, PDF, etc.) are not currently supported by this backend.

### 2.5.4 Unsupported Formats & Errors


Charton will return an error if:

- The file extension is missing
- The extension is not supported by the selected backend
- SVG → PNG rasterization fails
- File write errors occur

Example:
```rust
if let Err(e) = chart.save("output.bmp") {
    eprintln!("Save error: {}", e);
}
```
### 2.5.5 Summary of Supported Formats

| Backend     | SVG | PNG | JSON |
| ----------- | :-: | :-: | :--: |
| Rust Native |  ✔️ |  ✔️ ||
| Altair      |  ✔️ |  ✔️ |  ✔️  |
| Matplotlib  ||  ✔️ ||

### 2.5.6 Exporting Charts as Strings (SVG / JSON)

In addition to saving charts to files, Charton also supports exporting charts **directly as strings**.
This is useful in environments where writing to disk is undesirable or impossible, such as:

- Web servers returning chart data in API responses
- Browser/WASM applications
- Embedding charts into HTML templates
- Passing Vega-Lite specifications to front-end visualizers
- Testing and snapshot generation

Charton provides two kinds of in-memory exports depending on the backend.

#### 2.5.6.1 SVG Output (Rust Native Backend)

The Rust-native renderer can generate the complete SVG markup of a chart and return it as a `String`:
```rust
let svg_string = chart.to_svg()?;
```
This returns the full `<svg>...</svg>` element including:
- Layout
- Axes
- Marks
- Legends
- Background

The string can be:
- Embedded directly into HTML
- Returned from a web API
- Rendered inside a WASM application
- Passed to a templating engine such as Askama or Tera

**Example**
```rust
let svg = chart.to_svg()?;
```
#### 2.5.6.2 Vega-Lite JSON (Altair Backend)

When using the Altair backend, charts can be exported as raw **Vega-Lite JSON**:
```rust
let json = chart.to_json()?;
```
This produces the complete Vega-Lite specification generated by Altair. Typical usage scenarios include:
- Front-end rendering using Vega/Vega-Lite
- Sending the chart spec from a Rust API to a browser client
- Storing chart specifications in a database
- Generating reproducible visualization specs

**Example**
```rust
let json_spec = chart.to_json()?;
println!("{}", json_spec);
```
This JSON is fully compatible with the **official online Vega-Lite editor**.

#### 2.5.6.3 Summary: In-Memory Export Methods


| Backend     | `to_svg()`    | `to_json()`              |
| ----------- | ------------- | ------------------------ |
| Rust Native | ✔️ SVG string | ❌ unsupported            |
| Altair      | ❌ (file-only) | ✔️ Vega-Lite JSON string |
| Matplotlib  |||

String-based export complements file export by enabling fully in-memory rendering and programmatic integration.

## 2.7 Viewing Charts

Charton charts can be viewed directly inside **Evcxr Jupyter notebooks** using the `.show()` method.  

When running inside Evcxr Jupyter, Charton automatically prints the correct MIME content so that the chart appears inline.

Outside Jupyter (e.g., running a binary), `.show()` does nothing and simply returns `Ok(())`.

The rendering behavior differs depending on the selected backend.

## 2.6.1 Rust Native Backend

The Rust-native backend renders charts to **inline SVG**.  

When `.show()` is called inside Evcxr Jupyter, the SVG is printed using `text/html` MIME type.

### Example


```rust
use charton::prelude::*;
use polars::prelude::*;

let df = df![
    "x" => [1, 2, 3],
    "y" => [10, 20, 30]
]?;

let chart = Chart::build(&df)?
    .mark_point()
    .encode((x("x"), y("y")))?
    .into_layered();

chart.show()?;   // displays inline SVG in Jupyter
```
**Internal Behavior**
```text
EVCXR_BEGIN_CONTENT text/html
<svg>...</svg>
EVCXR_END_CONTENT
```
This enables rich inline SVG display in notebooks.

### 2.6.2 Altair Backend (Vega-Lite)

When using the Altair backend, `.show()` emits **Vega-Lite JSON** with the correct MIME type:
```bash
application/vnd.vegalite.v5+json
```
Jupyter then renders the chart using the built-in Vega-Lite renderer.

**Example**
```rust
chart.show()?;   // displays interactive Vega-Lite chart inside Jupyter
```
**Internal Behavior**
```text
EVCXR_BEGIN_CONTENT application/vnd.vegalite.v5+json
{ ... Vega-Lite JSON ... }
EVCXR_END_CONTENT
```
This produces interactive charts (tooltips, zooming, etc.) if supported by the notebook environment.

### Matplotlib Backend

The Matplotlib backend produces **base64-encoded PNG** images and sends them to the notebook using `image/png` MIME type.

**Example**
```rust
chart.show()?;   // displays inline PNG rendered by Matplotlib
```
**Internal Behavior**
```text
EVCXR_BEGIN_CONTENT image/png
<base64 image>
EVCXR_END_CONTENT
```
### 2.6.4 Summary: What `.show()` displays in Jupyter

| **Backend** | **Output Type** | **MIME Type**     |
| ----------- | ----------- | ---------------------------------- |
| Rust Native | SVG         | `text/html`                        |
| Altair      | Vega-Lite   | `application/vnd.vegalite.v5+json` |
| Matplotlib  | PNG         | `image/png`                        |

`.show()` is designed to behave naturally depending on the backend, giving the best viewing experience for each renderer.

## 2.7 Summary

In this chapter, you learned how to:
- Load datasets from CSV, Parquet, and built-in sources
- Create essential chart types: scatter, bar, line, histogram, boxplot, layered plots
- Export your charts to SVG, PNG, and Vega JSON
- Preview visualizations in the notebook

With these foundations, you now have everything you need to build **end-to-end data visualizations** quickly and reliably. The next chapters will introduce the building blocks of Charton, including marks and eocodings.

# Chapter 3 · Marks

Marks are the fundamental building blocks of Charton. A *mark* is any visible graphical primitive—points, lines, bars, areas, rectangles, arcs, text, boxplots, rules, histograms, and more.

Every chart in Charton is created by:
**1.** Constructing a base chart using `Chart::build()`.
**2.** Selecting a mark type (e.g., `mark_point()`, `mark_line()`).
**3.** Adding encodings that map data fields to visual properties.

Understanding marks is essential because **most visual expressiveness comes from combining marks with encodings.**

## 3.1 What Is a Mark?

In Charton, a mark is an object that implements the core trait:
```rust
pub trait Mark: Clone {
    fn mark_type(&self) -> &'static str;

    fn stroke(&self) -> Option<&SingleColor> { None }
    fn shape(&self) -> PointShape { PointShape::Circle }
    fn opacity(&self) -> f64 { 1.0 }
}
```
**Key Properties**
| **Property**| **Meaning**       | **Provided by Trait** |
| ----------- | ----------------- | ----------------- |
| `mark_type` | Unique identifier | required          |
| `stroke`    | Outline color     | default: none     |
| `shape`     | Point shape       | default: circle   |
| `opacity`   | Transparency      | default: 1.0      |

## 3.2 How Marks Work in Charton

A typical Charton chart:
```rust
Chart::build(&df)?
    .mark_point()
    .encode((
        x("x"),
        y("y")
    ))?
```
**Flow of Rendering**

**1.** `mark_point()` creates a `MarkPoint` object.

**2.** Encodings specify how data fields map to visual properties.

**3.** Renderer merges:
- mark defaults
- overriding encoding mappings
- automatic palettes

**4.** The final SVG/PNG is generated.

**Declarative Design Philosophy**

Charton follows an Altair-style declarative model:

> **If an encoding exists → encoding overrides mark defaults.**

> **If an encoding does not exist → use the mark’s own default appearance.**

This gives you:
- Short expressions for common charts
- Fine-grained control when needed

## 3.3 Point Mark

MarkPoint draws scattered points.

**Struct (simplified)**
```rust
pub struct MarkPoint {
    pub color: Option<SingleColor>,
    pub shape: PointShape,
    pub size: f64,
    pub opacity: f64,
    pub stroke: Option<SingleColor>,
    pub stroke_width: f64,
}
```
**Use Cases**
- Scatter plots
- Bubble charts
- Highlighting specific points
- Overlaying markers on other marks

**Correct Example**
```rust
Chart::build(&df)?
    .mark_point()
    .encode((
        x("sepal_length"),
        y("sepal_width"),
        color("species"),
        size("petal_length")
    ))?
```
## 3.4 Line Mark

MarkLine draws connected lines.

**Highlights**
- Supports LOESS smoothing
- Supports interpolation

**Struct**
```rust
pub struct MarkLine {
    pub color: Option<SingleColor>,
    pub stroke_width: f64,
    pub opacity: f64,
    pub use_loess: bool,
    pub loess_bandwidth: f64,
    pub interpolation: PathInterpolation,
}
```
**Example**
```rust
Chart::build(&df)?
    .mark_line().transform_loess(0.3)
    .encode((
        x("data"),
        y("value"),
        color("category")
    ))?
```
## 3.5 Bar Mark

A bar mark visualizes categorical comparisons.

**Struct**
```rust
pub struct MarkBar {
    pub color: Option<SingleColor>,
    pub opacity: f64,
    pub stroke: Option<SingleColor>,
    pub stroke_width: f64,
    pub width: f64,
    pub spacing: f64,
    pub span: f64,
}
```
**Use Cases**
- Vertical bars
- Grouped bars
- Stacked bars
- Horizontal bars

**Example**
```rust
Chart::build(&df)?
    .mark_bar()
    .encode((
        x("type"),
        y("value"),
    ))?
```
## 3.6 Area Mark

Area marks fill the area under a line.

**Example**
```rust
Chart::build(&df)?
    .mark_area()
    .encode((
        x("time"),
        y("value"),
        color("group")
    ))?
```
## 3.7 Arc Mark (Pie/Donut)

Arc marks draw circular segments.

**Example (donut)**
```rust
Chart::build(&df)?
    .mark_arc()  // Use arc mark for pie charts
    .encode((
        theta("value"),  // theta encoding for pie slices
        color("category"),  // color encoding for different segments
    ))?
    .with_inner_radius_ratio(0.5) // Creates a donut chart
```
## 3.8 Rect Mark (Heatmap)

Used for heatmaps and 2D densities.

**Example**
```rust
Chart::build(&df)?
    .mark_rect()
    .encode((
        x("x"),
        y("y"),
        color("value"),
    ))?
```
## 3.9 Boxplot Mark

Visualizes statistical distributions.

**Example**
```rust
Chart::build(&df_melted)?
    .mark_boxplot()
    .encode((
        x("variable"),
        y("value"),
        color("species")
    ))?
```
## 3.10 ErrorBar Mark

Represents uncertainty intervals.

**Example**
```rust
// Create error bar chart using transform_calculate to add min/max values
Chart::build(&df)?
    // Use transform_calculate to create ymin and ymax columns based on fixed std values
    .transform_calculate(
        (col("value") - col("value_std")).alias("value_min"),  // ymin = y - std
        (col("value") + col("value_std")).alias("value_max")   // ymax = y + std
    )?
    .mark_errorbar()
    .encode((
        x("type"),
        y("value_min"),
        y2("value_max")
    ))?
```
## 3.11 Histogram Mark

Internally used to draw histogram bins.

**Example**
```rust
Chart::build(&df)?
    .mark_hist()
    .encode((
        x("value"),
        y("count").with_normalize(true),
        color("variable")
    ))?
```
## 3.12 Rule Mark

Draws reference lines.

**Example**
```rust
Chart::build(&df)?
    .mark_rule()
    .encode((
        x("x"),
        y("y"),
        y2("y2"),
        color("color"),
    ))?
```
## 3.13 Text Mark

Places textual annotations.

**Example**
```rust
Chart::build(&df)?
    .mark_text().with_text_size(16.0)
    .encode((
        x("GDP"),
        y("Population"),
        text("Country"),
        color("Continent"),
    ))?
```
## 3.14 Summary

* Each mark defines a visual primitive.
* Marks are combined with *encodings* to bind data to graphics.
* Charton uses a declarative approach:
    * Encodings override mark defaults.
    * Palette and scales are automatically applied.
* By choosing the correct mark, you control how data is represented.

# Chapter 4 · Encodings

Encodings are the core of Charton’s declarative visualization system. They determine **how data fields map to visual properties** such as:
- Position (`x`, `y`, `y2`, `theta`)
- Color
- Shape
- Size
- Text labels

Every chart in Charton combines:
1. **A mark** (point, line, bar, arc, rect, etc.)
2. **Encodings** that map data fields to visual channels

This chapter explains all encoding channels, how they work, and provides complete code examples using **mtcars.**

## 4.1 What Are Encodings?

An encoding assigns a *data field* to a *visual* channel.
```rust
Chart::build(&df)?
    .mark_point()
    .encode((
        x("hp"),
        y("mpg"),
        color("cyl"),
    ))?;
```
This produces a scatter plot:

* **X axis** → horsepower
* **Y axis** → miles per gallon
* **Color** → number of cylinders

## 4.2 Encoding System Architecture

Every encoding implements the following trait:
```rust
pub trait IntoEncoding {
    fn apply(self, enc: &mut Encoding);
}
```

Users **never** interact with `Encoding` directly.
They simply write:
```rust
.encode((x("A"), y("B"), color("C")))
```

The API supports tuple-based composition of up to **9 encodings.**

## 4.3 Position Encodings

### 4.3.1 X – Horizontal Position


The **X** channel places data along the horizontal axis.

✔ **When to use** `X`

- Continuous values (e.g., `hp`, `mpg`, `disp`)
- Categorical values (`cyl`, `gear`, `carb`)
- Histogram binning
- Log scales

**API**
```rust
x("column_name")
```
**Optional settings**
```rust
x("hp")
    .with_bins(30)
    .with_scale(Scale::Log)
    .with_zero(true)
```
**Example: mtcars horsepower vs mpg**
```rust
let df = load_dataset("mtcars");

Chart::build(&df)
    .mark_point()
    .encode((
        x("hp"),
        y("mpg"),
    ));
```
Expected: Scatter plot showing `hp` vs `mpg`.

### 4.3.2 Y – Vertical Position


The **Y** channel has identical behavior to `X`.

**Example**
```rust
Chart::build(&df)
    .mark_point()
    .encode((
        x("wt"),
        y("mpg"),
    ));
```
Expected: Heavier cars generally have lower mpg.

### 4.3.3 Y2 – Second Vertical Coordinate


Used when a mark needs **two vertical positions:**

- Interval bands
- Confidence intervals
- Error bars
- Range rules

**Example: Upper & Lower MPG Bounds**
```rust
Chart::build(&df)
    .mark_area()
    .encode((
        x("hp"),
        y("mpg_low"),
        y2("mpg_high"),
    ));
```
## 4.4 Angular Position: θ (Theta)

Used in:

- Pie charts
- Donut charts
- Radial bar charts

**Example: Pie chart of cylinders**
```rust
Chart::build(&df)
    .mark_arc()
    .encode((
        theta("count"),
        color("cyl"),
    ));
```
## 4.5 Color Encoding

Color maps a field to the fill color of a mark.

✔ **When to use**

- Categorical grouping
- Continuous magnitude
- Heatmaps
- Parallel categories

**Example: Color by gears**
```rust
Chart::build(&df)
    .mark_point()
    .encode((
        x("hp"),
        y("mpg"),
        color("gear"),
    ));
```
## 4.8 Shape Encoding

**Shape – Point Symbol Mapping**

Only applies to **point marks**.

**Available shapes include:**

- Circle
- Square
- Triangle
- Cross
- Diamond
- Star

**Example**
```rust
Chart::build(&df)
    .mark_point()
    .encode((
        x("hp"),
        y("mpg"),
        shape("cyl"),
    ));
```
## 4.9 Size Encoding

**Size – Radius / Area Encoding**

Used for:

- Bubble plots
- Weighted scatter plots
- Emphasizing magnitude

**Example: Bubble plot with weight**
```rust
Chart::build(&df)
    .mark_point()
    .encode((
        x("hp"),
        y("mpg"),
        size("wt"),
    ));
```
## 4.10 Opacity Encoding

**Opacity – Transparency**

Used for:

- Reducing overplotting
- Encoding density
- Showing relative uncertainty

**Example: Opacity mapped to horsepower**
```rust
Chart::build(&df)
    .mark_point()
    .encode((
        x("wt"),
        y("mpg"),
        opacity("hp"),
    ));
```
## 4.11 Text Encoding

**Text – Label Encoding**

Works with:

- Point labels
- Bar labels
- Annotation marks

**Example: Label each point with car model**
```rust
Chart::build(&df)
    .mark_text()
    .encode((
        x("hp"),
        y("mpg"),
        text("model"),
    ));
```
## 4.12 Stroke Encoding

**Stroke – Outline Color**

Useful when:

- Fill color is already used
- Emphasizing boundaries
- Donut chart outlines

**Example**
```rust
Chart::build(&df)
    .mark_point()
    .encode((
        x("hp"),
        y("mpg"),
        stroke("gear"),
    ));
```
## 4.13 Stroke Width Encoding

**Stroke Width – Border Thickness**

Used for:

- Highlighting
- Encoding magnitude
- Interval charts

**Example**
```rust
Chart::build(&df)
    .mark_point()
    .encode((
        x("hp"),
        y("mpg"),
        stroke_width("wt"),
    ));
```
## 4.14 Combined Example: All Encodings


This chart uses eight encodings simultaneously:
```rust
Chart::build(&df)
    .mark_point()
    .encode((
        x("hp"),
        y("mpg"),
        color("cyl"),
        shape("gear"),
        size("wt"),
        opacity("qsec"),
        stroke("carb"),
        stroke_width("drat"),
    ));
```

Expected:
A rich multi-dimensional visualization of `mtcars`.

## 4.15 Tips & Best Practices


**✔ Use color for major categories**
Examples: `cyl`, `gear`, `carb`.

**✔ Use size sparingly**
Only when magnitude matters.

**✔ Avoid using both color & shape unless required**
Choose one main grouping.

**✔ Use opacity to reduce overplotting**
mtcars has many overlapping data points.

**✔ Avoid encoding more than 5 dimensions**
Human perception becomes overloaded.

## 4.16 Summary Table

| **Channel**    | **Purpose**          | **Works With** | **Example**         |
| -------------- | -------------------- | -------------- | ---------------------- |
| `x`            | horizontal position  | all marks      | `x("hp")`              |
| `y`            | vertical position    | all marks      | `y("mpg")`             |
| `y2`           | interval upper bound | area, rule     | `y2("high")`           |
| `theta`        | angle (pie/donut)    | arc            | `theta("count")`       |
| `color`        | fill color           | all            | `color("cyl")`         |
| `shape`        | symbol               | point          | `shape("gear")`        |
| `size`         | area/size            | point          | `size("wt")`           |
| `opacity`      | transparency         | point/area     | `opacity("hp")`        |
| `text`         | labels               | text mark      | `text("model")`        |
| `stroke`       | outline color        | point/rect/arc | `stroke("carb")`       |
| `stroke_width` | outline thickness    | all            | `stroke_width("drat")` |

# Chapter 5 · Layered Charts

Layered charts allow you to **stack multiple visual layers** on the same coordinate system. In Charton, a `LayeredChart` can combine any number of independent `Chart` layers—each with its own data, encodings, marks, transforms, and scale mappings. All layers automatically share the same **coordinate system, axes, scales, and overall styling**.

Layered charts are one of the most expressive visualization tools provided by Charton, enabling you to build statistical graphics, scientific plots, and analytical figures that combine multiple visual cues into a single coherent chart.

## 5.1 What Is a Layered Chart?


A `LayeredChart` is a container holding:
- global chart properties (width, height, margins, axes, theme, etc.)
- a list of layer objects (each a normal `Chart`)
- optional overrides for axis domains, labels, ticks, legends, and background

Conceptually, a layered chart is like **Photoshop layers** for data visualization:
- each layer can be a line, bar, point, area, rule, annotation, etc.
- layers are drawn in sequence (first layer at bottom, last layer on top)
- all layers share the same coordinate system

This model follows the design of **Vega-Lite’s layering, Altair’s layered charts**, and **ggplot2’s geoms**.

## 5.2 Why Layering Matters

Layering is essential for building analytical and scientific visualizations. Here are the key advantages.

### 5.2.1 Shared coordinate system

All layers automatically share:
- x/y axes
- axis labels
- axis domains
- grid lines
- coordinate transformation

This eliminates the need to manually align axis scales across layers.

### 5.2.2 Rich multi-factor visual expression

Layering is perfect for showing multiple aspects of a dataset:
- scatter points + smooth trend
- bars + error bars
- distributions + reference lines
- model predictions + raw data
- confidence intervals + regression lines

### 5.2.3 Ideal for scientific and statistical graphics

Most scientific plots are inherently layered:
- mean ± standard deviation
- line + shaded confidence interval
- theoretical vs. empirical distributions
- baseline rules
- highlight annotations

Charton makes these easy and declarative.

### 5.2.4 Fully modular

Each layer is independently constructed:
```rust
let layer1 = Chart::build(&df)?.mark_line().encode((x("time"), y("value")))?;
let layer2 = Chart::build(&df)?.mark_point().encode((x("time"), y("value")))?;
```
Then simply stack them.

### 5.2.5 Familiar to Altair/Vega-Lite users

Charton’s layering model is intentionally similar to existing declarative charting libraries, making knowledge transferable.

## 5.3 Creating a Layered Chart

The typical workflow:
```rust
LayeredChart::new()
    .add_layer(layer1)
    .add_layer(layer2)
    .save("output.svg")?;
```
Each `layer` is a complete, standalone `Chart` with its own:
- mark type
- encodings
- transforms

This modularity makes layering flexible and composable.

## 5.4 Example: Line + Scatter Overlay

A common pattern: points show raw measurements, the line shows the trend.
```rust
use charton::prelude::*;

let df = load_dataset("mtcars")?;
let df = df.sort(["hp"], SortMultipleOptions::default())?;

let line = Chart::build(&df)?
    .mark_line()
    .encode((x("hp"), y("mpg")))?;

let scatter = Chart::build(&df)?
    .mark_point()
    .encode((x("hp"), y("mpg")))?;

LayeredChart::new()
    .add_layer(line)
    .add_layer(scatter)
    .with_x_label("Horsepower")
    .with_y_label("Miles Per Gallon")
    .save("line_points.svg")?;
```
This combined visualization shows both the **trend** and the **raw variability**.

## 5.5 Example: Bar Chart + Error Bars

Charton supports statistical overlays such as error bars:
```rust
use charton::prelude::*;
use polars::prelude::*;

let df = {
    let mut df = load_dataset("mtcars")?.head(Some(3));
    df.with_column(Series::new("mpg_std", vec![1.0; df.height()]))?;
    df.lazy().with_column(col("qsec").cast(DataType::String)).collect()?
};

let errorbar = Chart::build(&df)?
    .transform_calculate(
        (col("mpg") - col("mpg_std")).alias("mpg_min"),
        (col("mpg") + col("mpg_std")).alias("mpg_max"),
    )?
    .mark_errorbar()
    .encode((x("qsec"), y("mpg_min"), y2("mpg_max")))?;

let bar = Chart::build(&df)?
    .mark_bar()
    .encode((x("qsec"), y("mpg")))?;

LayeredChart::new()
    .add_layer(bar)
    .add_layer(errorbar)
    .with_y_label("Miles Per Gallon")
    .save("bar_errorbar.svg")?;
```
This is a standard pattern in scientific and engineering reporting.

## 5.6 Example: Line + Error Bars (Time Series / Regression)

This layout is widely used for:
- regression mean ± CI
- temporal trends
- uncertainty visualization
```rust
let errorbar = Chart::build(&df)?
    .transform_calculate(
        (col("mpg") - col("mpg_std")).alias("mpg_min"),
        (col("mpg") + col("mpg_std")).alias("mpg_max"),
    )?
    .mark_errorbar()
    .encode((x("qsec"), y("mpg_min"), y2("mpg_max")))?;

let line = Chart::build(&df)?
    .mark_line()
    .encode((x("qsec"), y("mpg")))?;

LayeredChart::new()
    .add_layer(line)
    .add_layer(errorbar)
    .save("line_errorbar.svg")?;
```
## 5.7 Layering Reference Lines, Rules, and Annotations
You can combine data layers with semantic elements:
```rust
let df = load_dataset("mtcars")?;

let rule = Chart::build(&df)?
    .mark_rule()
    .encode((
        x("hp"),
        y("mpg"),
    ))?;

let scatter = Chart::build(&df)?
    .mark_point()
    .encode((
        x("hp"),
        y("mpg")
    ))?;

LayeredChart::new()
    .add_layer(scatter)
    .add_layer(rule)
    .save("reference_line.svg")?;
```

This is useful for:
- thresholds
- baselines
- highlighting important regions

## 5.8 Shared Axes, Scales, and Legends

`LayeredChart` intelligently merges properties from its constituent layers to provide a consistent, aligned visual structure.

### 5.8.1 Understanding the Core Pipeline: Scale, Coord, and Axis

In **Charton**, following the principles of the *Grammar of Graphics* theory, chart generation is structured as a pipeline composed of three core concepts—consistent with the design philosophy adopted by **ggplot2** and **Altair**. To put it simply: **Scale "translates," Coord "positions," and Axis "explains."**

**1. Scale: The Data Translator**

**Role:** Maps "Data Space" to "Aesthetic Space".

The Scale determines how your raw data (e.g., the number $100$ or the category "Apple") is transformed into visual attributes (e.g., a length of $200px$ or the color "Red").
- **Input:** Raw Data (**Domain**).
- **Output:** Normalized values between $0.0$ and $1.0$, or specific colors, shapes, etc.
- **Responsibilities:**
    - Choosing the mapping type (e.g., **Linear** vs. **Logarithmic**).
    - Handling **Discrete** data (assigning a relative position to each category).
    - Defining the boundaries of the data (**Limits/Domain**).
- **Example:** A Scale tells the system: "Data $0$ corresponds to position $0.0$, and data $100$ corresponds to position $1.0$."

**2. Coord (Coordinate System): The Space Architect**

**Role:** Converts "Normalized Coordinates" into "Screen Pixel Coordinates."

The Coord determines the geometry of the chart. It takes the $0.0$ to $1.0$ values produced by the Scale and decides their final placement on the canvas.

- **Responsibilities:**
    - **Cartesian Coord:** Standard horizontal $x$ and vertical $y$.
    - **Polar Coord:** Maps $x$ to an angle $\theta$ and $y$ to a radius $r$.
    - **Coordinate Flipping:** Transposing the rendering logic for $x$ and $y$ (e.g., horizontal bar charts).
    - **Map Projection:** Handling specialized transformations for geographic coordinates.
- **Example:** In a Cartesian system, $(0.5, 0.5)$ is the exact center of the plot; in a Polar system, it might represent a point halfway out from the center to the right.

**3. Axis: The Visual Guide for the Viewer**

**Role:** Provides a reverse-mapping guide to make the graphic readable.

An Axis is essentially the visual representation of a Scale. It does not participate in generating the geometry of the marks but tells the reader what a specific point represents in terms of data.

- **Responsibilities:**
    - **Tick Generation:** Deciding where to draw lines at intervals like $0, 10, 20$.
    - **Labeling:** Formatting numbers into strings like "$10" or "2026-01-09".
    - **Visual Alignment:** Handling text rotation, anchoring, and spacing.

**The Workflow Pipeline**

Suppose we want to render a single **Bar**:
1. **Scale:** Translates the data value `50` into a height ratio of `0.5`.
2. **Coord:**
    - If **Cartesian**: Converts `0.5` into `y = 200px` (relative to the screen).
    - If **Polar**: Converts `0.5` into the radius of a wedge.
3. **Axis:** Draws a line at the side with the label "50" so the viewer understands the bar's magnitude.

**Summary Table**

| Component | Target of Operation | Problem Solved | Core Logic |
| :--- | :--- | :--- | :--- |
| **Scale** | Data $\rightarrow$ Attribute | How large is this ratio? | $f(x) = \frac{x - min}{max - min}$ |
| **Coord** | Ratio $\rightarrow$ Pixel | Where is this on the canvas? | $x_{pixel} = x_{ratio} \times width$ |
| **Axis** | Tick $\rightarrow$ Text | How do viewers read this? | Rendering Ticks & Labels |

### 5.8.2 Axis Orientation and Swapping

In Charton, the orientation of the coordinate system is a **global property** managed exclusively by the `LayeredChart`. Individual layers (`Chart<T>`) are orientation-agnostic; they do not hold a `swapped_axes` state or provide a `.swap_axes()` method.
- **Centralized Logic:** The `LayeredChart` serves as the sole controller for the rendering context. This ensures that all layers—whether they are bars, lines, or rules—are rendered using the exact same coordinate transformation logic.
- **Simplified Layers:** By removing orientation logic from individual charts, the API becomes more intuitive. You focus on data encoding for the layers and layout configuration for the final chart.
- **One-Step Toggle:** To switch between a vertical and horizontal layout, you only need to call `.swap_axes()` once on the `LayeredChart` instance.

**Correct Usage Example**

Notice that orientation is only specified at the very end, during the assembly of the layered chart.

```rust
// Individual layers focus only on data and marks
let bar = Chart::build(&df)?
    .mark_bar()
    .encode((
        x("value"),
        y("category")
    ))?;

let rule = Chart::build(&df)?
    .mark_rule()
    .encode((
        x("threshold"),
        y("category")
    ))?;

// All layers are orientation-agnostic until added here
LayeredChart::new()
    .add_layer(bar)
    .add_layer(rule)
    .swap_axes() // The ONLY place where orientation is defined
```
### 5.8.3 Scale Merging

The `LayeredChart` automatically computes the union of data ranges across all layers:
- **x/y domains:** Unless overridden, the axes will automatically span the minimum and maximum values found across **all** layers.
- **Tick values & labels:** It merges discrete categories or calculates optimal continuous ticks that accommodate every layer's data.
- **Legends:** When multiple layers use the same encoded fields (e.g., both use `color("species")`), Charton merges them into a single coherent legend.

### 5.8.3 Overriding Defaults

While automatic merging is powerful, you can explicitly override these properties at the `LayeredChart` level.

This provides consistent, aligned visual structure.

**Overriding axis domains**
```rust
LayeredChart::new()
    .with_x_domain(0.0, 350.0)
    .with_y_domain(0.0, 40.0)
```
**Overriding tick values or labels**
```rust
.with_x_tick_values(vec![0.0, 100.0, 200.0, 300.0])
.with_y_tick_labels(vec!["Low", "Medium", "High"])
```
**Controlling the legend**
```rust
.with_legend(true)
.with_legend_title("Engine Type")
```
## 5.9 Global Styling: Size, Margins, Theme, Background

Because `LayeredChart` owns global styling, you can adjust:

**Size**
```rust
.with_size(800, 500)
```
**Margins (proportional)**

Useful to accommodate long tick labels or large legends:
```rust
.with_left_margin(0.20)
.with_bottom_margin(0.18)
```
**Theme**
```rust
.with_theme(Theme::default())
```
**Background**
```rust
.with_background("#FAFAFA")
```
## 5.10 Advanced Example: Histogram + Reference Line + Density Curve

A classic triple-layer analytic plot.
```rust
let df = load_dataset("mtcars")?;

let hist = Chart::build(&df)?
    .mark_hist()
    .encode((x("mpg"), y("count")))?;

let rule = Chart::build(&df)?
    .mark_rule()
    .encode((x("mpg"), y("cyl")))?;

let density = Chart::build(&df)?
    .transform_density(
        DensityTransform::new("mpg")
            .with_as("mpg", "cumulative_density")
            .with_cumulative(true)
    )?
    .mark_line().with_line_color(Some(SingleColor::new("red")))
    .encode((x("mpg"), y("cumulative_density")))?;

LayeredChart::new()
    .add_layer(hist)
    .add_layer(rule)
    .add_layer(density)
    .save("hist_rule_density.svg")?;
```
## 5.11 Best Practices for Layered Charts

**✔ 1. Give each layer a clear conceptual purpose**

Examples:
- Layer 1: trend
- Layer 2: raw data
- Layer 3: uncertainty
- Layer 4: annotation

**✔ 2. Avoid too many layers**

More than 4–5 layers can cause visual clutter unless carefully styled.

**✔ 3. Avoid using opacity as the primary differentiator**

Instead prefer:
- color
- shape

**✔ 4. Think about rendering order**
- bottom layers: areas, bars, intervals
- top layers: lines, points, rules, annotations

**✔ 5. When in doubt, reduce complexity**

Layered plots are powerful, but simplicity is clarity.

## 5.12 Summary Table

| **Layer Type** | **Use Case**                             | **Example Fields** |
| -------------- | ---------------------------------------- | -------------- |
| **line**       | trends, regression, temporal analysis    | hp → mpg       |
| **point**      | raw data                                 | hp, mpg        |
| **bar**        | aggregated statistics                    | mpg by cyl     |
| **errorbar**   | uncertainty, variance                    | mpg ± sd       |
| **area**       | confidence intervals, distribution bands | lower–upper    |
| **rule**       | thresholds, baselines                    | fixed x or y   |
| **annotation** | labels, highlights                       | arbitrary      |

# Chapter 6 · Styling and Themes


A chart is not complete when it merely *works*.

A great chart communicates clearly, resonates visually, and fits seamlessly into its context—whether that context is a publication, dashboard, presentation slide, or internal report.

Charton provides a rich and structured styling system that allows you to control every aspect of chart appearance: themes, axes, fonts, sizes, colors, spacing, and layout.

Styling in Charton is **declarative, layered, and predictable**, following design principles familiar from Altair (Vega-Lite) and ggplot2.

## 6.1 Styling Model and Precedence

Charton follows a **three-level styling model**:

1. **Theme-level styling** (`Theme`)
Defines global defaults such as colors, fonts, paddings, and visual identity.
2. **Chart-level styling** (`LayeredChart` / common builders)
Adjusts layout, axes, domains, labels, and global chart properties.
3. **Mark-level styling** (`mark_*`)
Controls the appearance of individual visual elements such as color, size, and shape.

**Styling Precedence**

When multiple levels specify the same visual property, Charton resolves them in the following order:

> **Mark-level overrides****Chart-level overrides****Theme defaults**

This guarantees that:
- Themes establish a consistent visual baseline
- Charts can adapt styling to a specific figure
- Marks retain precise, local control when needed

## 6.2 Themes and Presets

Themes define the overall visual identity of a chart: colors, fonts, axis strokes, paddings, and spacing. In Charton, themes are represented by the `Theme` struct and applied globally:
```rust
chart.with_theme(Theme::default())
```
**Built-in Themes**

Charton provides several built-in themes:

- **Default** — light theme suitable for most use cases
- **Minimal** — reduced visual noise, thin strokes, no grid emphasis (To be implemented)
- **Classic** — thicker axes, Matplotlib-style appearance (To be implemented)
- **Dark** — optimized for dashboards and dark backgrounds (To be implemented)

Example:
```rust
let chart = LayeredChart::new()
    .with_theme(Theme::default());
```
**Customizing Theme Fields**

All theme fields are manually adjustable by overridding at the chart level.
```rust
let chart = LayeredChart::new()
    .with_theme(Theme::default())
    .with_label_font_size(10);
```

## 6.3 Chart-Level Styling: Axes and Layout

Chart-level styling is applied via shared builder methods and affects **all layers**.

**Axis Domains**

Override automatic domain inference:
```rust
chart
    .with_x_domain(0.0, 10.0)
    .with_y_domain_min(5.0);
```
**Axis Labels**
```
chart
    .with_x_label("Time (s)")
    .with_y_label("Intensity");
```
Padding and rotation:
```rust
chart
    .with_x_label_padding(25.0)
    .with_x_label_angle(-45.0);
```
**Tick Values and Labels**

**Continuous axis:**
```rust
chart.with_x_tick_values(vec![0.0, 2.0, 4.0, 6.0, 8.0]);
```
**Discrete axis:**
```rust
chart.with_x_tick_labels(vec!["A", "B", "C"]);
```
Rotate tick labels to avoid overlap:
```rust
chart.with_x_tick_label_angle(45.0);
```
Chart-level axis settings always override theme defaults.

## 6.4 Colors, Palettes, and Colormaps

Charton provides a high-performance color system designed for both static SVG export and real-time WGPU rendering. The system is centered around three core types: `SingleColor`, `ColorPalette`, and `ColorMap`.

### 6.4.1. The `SingleColor` Type

`SingleColor` is a lightweight, stack-allocated struct that stores colors as normalized RGBA values ($[0.0, 1.0]$).
- **Memory Efficient**: Implements Copy, avoiding heap allocations.
- **Backend Ready**: Maps directly to GPU vertex buffers while providing on-the-fly CSS string generation for SVGs.
- **Flexible Creation**: Supports CSS strings (Hex, RGB, HSL, Named colors), RGBA arrays, and a special "None" state for transparency.

```rust
// Creation examples
let red = SingleColor::new("#ff0000");           // Hex
let semi_blue = SingleColor::new("rgba(0,0,255,0.5)"); // CSS Functional
let transparent = SingleColor::none();           // Fully transparent
let from_array: SingleColor = [0.0, 1.0, 0.0, 1.0].into(); // Green
```

### 6.4.2 Color Control Strategies

**A. Mark-Level Colors (Manual)**

Directly setting a color on a mark. This functions when there are no data-driven color encoding.

```
mark_point()
.configure_point(|p| p.with_point_color("steelblue")) // Accepts any into<SingleColor>
```

**B. Discrete Palettes (Categorical Data)**

When mapping data groups to colors (e.g., different species in a scatter plot), use `ColorPalette`. Charton includes industry-standard palettes from Tableau and ColorBrewer.

|  Palette Type | Variants                      |  Use Case                              |
|---------------|-------------------------------|----------------------------------------|
|**Standard**   |`Tab10`, `Tab20`               |General purpose, clear differentiation. |
|**Qualitative**|`Set1`, `Set2`, `Set3`         |Categorical data with no inherent order.|
|**Stylized**   |`Pastel1`, `Dark2`, `Accent`   |Specific aesthetic requirements.        |

```rust
// Usage: automatically wraps indices if groups exceed palette size
chart.configure_theme(|t| t.with_palette(ColorPalette::Tab20))
```

**C. Continuous Colormaps (Numerical Data)**

For heatmaps or data-driven gradients, use `ColorMap`. These provide smooth transitions based on a normalized value ($0.0 \dots 1.0$).
- **Perceptually Uniform**: `Viridis`, `Inferno`, `Magma`, `Plasma`, `Cividis`. These are mathematically designed to represent data accurately, even for color-blind viewers or when printed in grayscale.
- **Sequential**: `Blues`, `Reds`, `YlGnBu` (Yellow-Green-Blue), etc.
- **Specialized**: `Rainbow`, `Jet`, `Cool`, `Hot`.

```rust
// Usage: maps numerical intensity to a color gradient
chart.configure_theme(|t| t.with_color_map(ColorMap::Viridis))
```

### 6.4.3 Technical Performance Note

Unlike many visualization libraries that parse strings at render-time, Charton’s color system:
1. **Pre-calculates** all Palette/Map values into normalized float arrays.
2. Uses **linear interpolation** for continuous maps, ensuring zero string-processing overhead during GPU draw calls.
3. **Clamps** all inputs to valid ranges $[0.0, 1.0]$ to prevent rendering artifacts.

## 6.5 Shapes and Sizes (Mark-Level Styling)

Shape and size are **mark-specific properties** and never affect other layers.

**Point Shape and Size**
```rust
let point = mark_point()
    .with_point_shape(PointShape::Circle)
    .with_point_size(60.0);
```
**Data-Driven Shape and Size**
```rust
mark_point()
    .encode((
        x("time"),
        y("value"),
        shape("group")
    ))?;
```
or
```rust
mark_point()
    .encode((
        x("wt"),
        y("mpg"),
        size("cyl")
    ))?;
```
Encoding-driven shape and size always use the chart defaults.

## 6.6 Font and Text Styling

Typography plays a major role in readability. Font and text styles currently inherit theme settings but can be overridden at the chart level.

Rendering text consistently across different platforms is a major challenge. Charton solves this with a **Double-Safety** strategy, though the behavior differs between PNG and SVG.

**1. The Built-in "Emergency" Font (For PNG)**

Unlike many libraries, Charton **embeds** the **Inter** font directly into its binary (`include_bytes!`).
- **Zero-Dependency**: You don't need to install font packages in minimal Docker containers or headless servers.
- **Guaranteed Rendering**: When exporting to **PNG**, text is "**baked-in**" (rasterized) into pixels. If your requested system fonts are missing, Charton falls back to the internal Inter font. This ensures the PNG looks identical on every machine.

**2. The SVG Limitation: "Instructional" Rendering**

It is important to note that **SVG works differently**:
- **Browser-Dependent**: When exporting to **SVG**, Charton does not embed the font data. Instead, it writes a "font-family" instruction into the code.
- **The Result**: The final look depends on the **viewer's browser**. If the viewer doesn't have your specified font, their browser will use its own default.

**3. The Default Font Stack**

To maximize compatibility for both formats, Charton traverses a prioritized **Font Stack**:
- **Modern Sans-Serifs**: `Inter`, `-apple-system` (macOS), `BlinkMacSystemFont`.
- **Windows Standards**: `'Segoe UI'`, `Roboto`, `Arial`.
- **Linux Essentials**: `Ubuntu`, `Cantarell`, `'Noto Sans'`.
- **System Generic**: `sans-serif` (In PNG, this maps to our embedded Inter).

**4. Overriding at the Chart Level**

You can define your own stack using a comma-separated string:

```rust
chart
    .with_title("Global Genomics Report")
    // Falls back to Helvetica, then Arial, then the internal "sans-serif"
    .with_title_font_family("CustomBrandFont, Helvetica, Arial, sans-serif")
    .with_title_font_size(24);
```

## 6.7 Chart Dimensions, Margins, and Background

**Dimensions**
```rust
chart.with_size(800, 600);
```
Larger sizes improve readability for dense charts.

**Margins**

Margins are expressed as **fractions of total size**:
```rust
chart
    .with_left_margin(0.15)
    .with_right_margin(0.10)
    .with_top_margin(0.10)
    .with_bottom_margin(0.15);
```

**Background and Legend**
```rust
chart.with_background("#fafafa");

chart
    .with_legend(true)
    .with_legend_title("Experimental Groups");
```
Legend appearance is influenced by the active theme.

## 6.8 Complete Example: Before and After Styling

**Basic Chart (Default Styling)**
```rust
let chart = Chart::build(&df)?
    .mark_point()
    .encode((x("x"), y("y")))?
    .into_layered();
```
**Styled Chart**
```rust
let chart = Chart::build(&df)?
    .mark_point()
    .encode((x("x"), y("y")))?
    .into_layered();

chart
    .with_theme(Theme::default())
    .with_title("Styled Scatter Plot")
    .with_x_label("X Value")
    .with_y_label("Y Value")
    .with_size(800, 600)
    .with_background("#ffffff")
    .save("chart.svg")?;
```
This demonstrates how themes, chart-level settings, and mark-level styling compose naturally.

**Style Resolution Summary**

| **Level** | **Scope** | **Typical Usage**            |
| ----- | --------- | -------------------------------- |
| Theme | Global    | Visual identity, fonts           |
| Chart | Per chart | Axes, layout, labels, domains    |
| Mark  | Per layer | Color, size, shape               |

Charton’s styling system is designed to be:
- **Declarative** — no imperative styling logic
- **Layer-aware** — global defaults with local overrides
- **Consistent** — predictable resolution rules

This allows users to create publication-quality visualizations with minimal effort, while still enabling deep customization when required.

# Chapter 7 · Advanced Charts

Basic marks such as points, lines, and bars are sufficient for many exploratory tasks, but real-world data analysis often requires **statistical summaries, distribution analysis, and structured comparisons**.

Charton provides a set of *advanced chart types* that are built on top of the same declarative grammar introduced in earlier chapters:

- Data is expressed as a `DataFrame`
- Visual structure is defined by **marks**
- Semantics are declared via **encodings**
- Rendering is handled automatically by the engine

What changes in this chapter is **what the marks compute internally**.

Advanced charts in Charton are *computation-aware*:

they derive statistical summaries (quantiles, counts, densities, bins) directly from the input data and render the results in a visually consistent way.

This chapter covers:

- Box plots and grouped statistical summaries
- Error bars and uncertainty visualization
- Density plots and kernel density estimation (KDE)
- Cumulative distributions (CDF / ECDF)
- Histograms (1D and 2D)
- Heatmaps and rect-based charts
- Pie and donut charts
- Rule-based overlays for annotations and thresholds

By the end of this chapter, you will be able to construct **publication-grade analytical visualizations** while keeping your code concise and expressive.

## 7.1 Box Plots and Grouped Statistical Summaries

Box plots (also called *box-and-whisker plots*) provide a compact summary of a distribution using its **five-number summary**:

- Minimum
- First quartile (Q1)
- Median
- Third quartile (Q3)
- Maximum

In addition, values outside the range

`[Q1 − 1.5 × IQR, Q3 + 1.5 × IQR]`

are treated as **outliers** and rendered explicitly.

Box plots are particularly useful for:

- Comparing distributions across categories
- Identifying skewness and spread
- Detecting outliers
- Visualizing grouped experimental results

Charton supports **grouped box plots out of the box**, allowing multiple distributions to be compared side-by-side within each category.

### 7.1.1 Grouped Box Plot

The most common box plot layout maps:

- a **categorical field** to the x-axis
- a **numeric field** to the y-axis
- an optional **grouping field** to color

Example (Iris dataset):
```rust
use charton::prelude::*;
use polars::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let df = load_dataset("iris")?;

    // Convert wide-format measurements into long format
    let df_melted = df.unpivot(["sepal_length", "sepal_width", "petal_length", "petal_width"], ["species"])?;

    Chart::build(&df_melted)?
        .mark_boxplot()
        .encode((
            x("variable"),
            y("value"),
            color("species")
        ))?
        .into_layered()
        .save("./grouped_boxplot.svg")?;

    Ok(())
}
```
This produces a grouped box plot where:

- Each **measurement type** (`variable`) forms a group on the x-axis
- Each **species** is rendered as a separate box within that group
- Colors are automatically assigned from the active palette

### 7.1.2 How Grouped Box Plots Work Internally


Understanding the internal model helps explain Charton’s behavior and defaults.

For box plots, Charton:

1. Identifies the **grouping axis** (usually `x`)
2. Identifies the **value axis** (`y`)
3. Uses the **color encoding** (if present) as a secondary grouping dimension
4. Computes statistics *per (group × color) combination*

This means that:

- `x("variable")` defines where boxes are placed
- `color("species")` defines *how many boxes appear within each group*
- Missing combinations are handled gracefully and do not break layout

The order of categories is preserved from the input data, rather than being sorted alphabetically.

### 7.1.3 Visual Styling of Box Plots

Box plots are rendered using the `MarkBoxplot` mark type, which exposes a number of visual controls.

All of the following methods apply **only to box plot marks** and do not affect other layers.

**Box Fill and Opacity**
```rust
chart
    .with_box_color(Some(SingleColor::new("steelblue")))
    .with_box_opacity(0.7);
```
Note: If a color encoding exists, colors are taken from the palette.

**Stroke and Outline**
```rust
chart
    .with_box_stroke(Some(SingleColor::new("black")))
    .with_box_stroke_width(1.0);
```
- Stroke controls both the box outline and whiskers
- Set stroke to `None` to remove outlines entirely

**Outlier Appearance**
```rust
chart
    .with_outlier_color(Some(SingleColor::new("black")))
    .with_outlier_size(3.0);
```
- Outliers are rendered as point markers
- By default, they inherit a visible but unobtrusive style
- Increasing size is helpful for dense distributions

### 7.1.4 Layout Control for Grouped Boxes

When multiple box plots appear within the same category, Charton uses a **dodged layout**.

Three parameters control this layout:
```rust
chart
    .with_box_width(0.5)
    .with_box_spacing(0.2)
    .with_box_span(0.7);
```
- `width`: Base width of a single box (in data units)
- `spacing`: Gap between boxes within the same group, expressed as a ratio of box width
- `span`: Total horizontal space reserved for all boxes in a group

These values are automatically converted into pixel units during rendering, ensuring consistent appearance across output sizes.

The defaults are chosen to produce visually balanced grouped box plots without manual tuning.

### 7.1.5 Orientation and Axis Swapping

Box plots automatically respect axis orientation:

- Standard vertical boxes when `x` is categorical and `y` is numeric
- Horizontal boxes when axes are swapped

No additional configuration is required—orientation is inferred from encodings.

### 7.1.6 When to Use Box Plots

- Use box plots when you want to:
- Compare distributions across many categories
- Emphasize medians and quartiles instead of raw values
- Identify outliers quickly
- Summarize large datasets without overplotting

## 7.2 Error Bars and Uncertainty Visualization

Error bars are a fundamental tool for communicating **uncertainty, variability, and statistical confidence**.

Instead of showing individual data points, error bars summarize a group of observations by:

- A **central value** (typically the mean)
- A **lower bound** (e.g. mean − standard deviation)
- An **upper bound** (e.g. mean + standard deviation)

They are most commonly used to:
- Visualize measurement uncertainty
- Compare variability across categories
- Overlay statistical context on bar charts or point charts

In Charton, error bars are implemented as a dedicated mark type: `MarkErrorBar`.

### 7.2.1 Basic Error Bar Chart

At the API level, creating an error bar chart looks very similar to other marks:
```rust
use charton::prelude::*;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    let df = df![
        "x" => ["a", "a", "a", "b", "b", "b", "c", "c", "c", "d", "d", "d"],
        "y" => [5.1, 5.3, 5.7, 6.5, 6.9, 6.2, 4.0, 4.2, 4.4, 7.6, 8.0, 7.8],
    ]?;

    let errorbar_chart = Chart::build(&df)?
        .mark_errorbar()
        .with_errorbar_color(Some(SingleColor::new("blue")))
        .with_errorbar_stroke_width(2.0)
        .with_errorbar_cap_length(5.0)
        .with_errorbar_center(true)
        .encode((
            x("x"),
            y("y"),
        ))?;

    LayeredChart::new()
        .with_size(500, 400)
        .with_title("Error Bar Chart with Mean and Std Dev")
        .add_layer(errorbar_chart)
        .save("./examples/errorbar.svg")?;

    Ok(())
}
```
This produces a chart where:
- Each unique `x` category forms one error bar
- The **center point** is the mean of `y`
- The **lower and upper bounds** are computed automatically
- Caps and line styles are rendered consistently across categories

### 7.2.2 Automatic Statistical Aggregation

A key design principle of Charton error bars is that **users declare intent, not computation**.

When you write:
```rust
.mark_errorbar()
.encode((x("x"), y("y")))
```
Charton automatically:
1. Groups data by the x encoding
2. Computes:
    - mean(`y`)
    - standard deviation(`y`)
3. Generates three internal fields:
    - `y` (mean)
    - `__charton_temp_y_min`
    - `__charton_temp_y_max`
This transformation happens transparently via the internal `transform_errorbar_data` step.

As a result:
- You do **not** need to pre-aggregate your data
- You do **not** need to manually compute error bounds
- The same chart definition works for raw observations

### 7.2.3 Error Bars with Explicit Bounds

In some cases, you may already have explicit min/max values (e.g. confidence intervals computed upstream).

Charton supports this via a secondary encoding:
```rust
.encode((
    x("x"),
    y("y_min"),
    y2("y_max"),
))
```
When `y2` is present:
- Charton **skips automatic aggregation**
- The provided fields are used directly
- This allows full control over uncertainty semantics

This design mirrors Vega-Lite’s `y` / `y2` pattern while remaining Rust-native.

### 7.2.4 Visual Styling of Error Bars

All visual styling is controlled via `MarkErrorBar`.

These methods affect only error bar marks and can be safely combined with other layers.

**Color and Opacity**
```rust
chart
    .with_errorbar_color(Some(SingleColor::new("black")))
    .with_errorbar_opacity(0.8);
```
- If no color encoding is present, a single color is used for all error bars
- Opacity is useful when overlaying error bars on dense charts

**Stroke Width**
```rust
chart.with_errorbar_stroke_width(1.5);
```
Controls the thickness of:
- The main error bar line
- The cap lines at both ends

**Cap Length**
```rust
chart.with_errorbar_cap_length(4.0);
```
Caps help visually distinguish the extent of uncertainty.
- Larger caps improve readability for sparse charts
- Smaller caps produce a cleaner, minimalist look

**Center Point Visibility**
```rust
chart.with_errorbar_center(true);
```
When enabled, a small marker is drawn at the center (mean) position.

This is especially useful when:
- Error bars are not overlaid on another mark
- The central tendency needs emphasis

### 7.2.5 Orientation and Axis Behavior

Error bars always represent **variation in the continuous axis**.

Internally:
- Bounds are transformed using the **y-axis scale**
- This remains true even when axes are swapped

As a result:
- Vertical error bars appear by default
- Horizontal error bars are rendered automatically when axes are swapped
- Log scales are handled correctly by transforming bounds before rendering

No additional configuration is required.

### 7.2.6 Layering Error Bars with Other Marks

Error bars are most powerful when combined with other chart types.

Typical patterns include:
- Bars + error bars
- Points + error bars
- Lines + error bars

Because error bars are standard `Chart` layers, you can simply add them:
```rust
LayeredChart::new()
    .add_layer(bar_chart)
    .add_layer(errorbar_chart);
```
Legend rendering is intentionally omitted for error bars, since they typically encode uncertainty rather than categories.

### 7.2.7 When to Use Error Bars

Use error bars when you want to:
- Communicate uncertainty or variability
- Summarize repeated measurements
- Compare stability across groups
- Avoid visual clutter from raw points

## 7.3 Density and Kernel Density Estimation (KDE)

While box plots and error bars summarize distributions using a small number of statistics, **density plots aim to visualize the full shape of a distribution**.

A density plot estimates a smooth probability distribution from raw samples, allowing you to:
- Identify multimodality
- Compare distribution shapes across groups
- Visualize skewness and tails
- Produce cumulative distribution curves (CDF)

In Charton, density estimation is implemented as a **data transformation**, not a mark.

This design cleanly separates:

- **Statistical computation** (`transform_density`)
- **Visual encoding** (`mark_line`, `mark_area`)

### 7.3.1 Density as a Data Transformation

Unlike histograms or box plots, density estimation is not a primitive mark.

Instead, you explicitly request a transformation:
```rust
chart.transform_density(DensityTransform::new("values"))?
```
This transformation:
1. Takes raw observations
2. Performs kernel density estimation (KDE)
3. Produces a new DataFrame with:
    - Evaluation points (x-axis)
    - Density values (y-axis)

You then visualize the result using any continuous mark, typically:
- `mark_line()` for density curves
- `mark_area()` for filled densities
- `mark_line().with_interpolation(StepAfter)` for ECDFs

This mirrors Charton’s philosophy:
> **statistics transform data; marks only draw geometry**

### 7.3.2 Basic Density Plot

A minimal density plot looks like this:
```rust
let chart = Chart::build(&df)?
    .transform_density(
        DensityTransform::new("IMDB_Rating")
    )?
    .mark_line()
    .encode((
        x("value"),
        y("density"),
    ))?;
```
By default:
- The kernel is **Gaussian (Normal)**
- The bandwidth is chosen via **Scott’s rule**
- The output columns are:
    - `"value"` (x-axis)
    - `"density"` (y-axis)

These defaults are chosen to produce reasonable results for most datasets.

### 7.3.3 Cumulative Density (CDF)

Density estimation can also produce **cumulative distributions**.

This is especially useful for:
- Comparing percentiles
- Visualizing probabilities (“what fraction is below x?”)
- ECDF-style plots
```rust
let chart = Chart::build(&df)?
    .transform_density(
        DensityTransform::new("IMDB_Rating")
            .with_as("IMDB_Rating", "cumulative_density")
            .with_cumulative(true)
    )?
    .mark_area()
    .encode((
        x("IMDB_Rating"),
        y("cumulative_density"),
    ))?
    .with_area_opacity(0.3);
```
Internally:
    - `pdf()` is replaced with `cdf()`
    - The output remains continuous and monotonic
    - Any mark capable of drawing a curve can be used

This design makes **PDF and CDF just configuration differences**, not separate chart types.

### 7.3.4 Kernel Functions

Charton supports multiple kernel functions via `KernelType`:
```rust
pub enum KernelType {
    Normal,
    Epanechnikov,
    Uniform,
}
```
You can select the kernel explicitly:
```rust
DensityTransform::new("values")
    .with_kernel(KernelType::Epanechnikov)
```
Conceptually:
- **Normal**: smooth, infinitely supported (default)
- **Epanechnikov**: compact support, optimal MSE
- **Uniform**: box kernel, step-like smoothing

The choice affects smoothness and boundary behavior but not API structure.

### 7.3.5 Bandwidth Selection

Bandwidth controls how smooth the density curve is.

Charton supports three bandwidth strategies:
```rust
pub enum BandwidthType {
    Scott,
    Silverman,
    Fixed(f64),
}
```
Examples:
```rust
DensityTransform::new("values")
    .with_bandwidth(BandwidthType::Silverman)
```
```rust
DensityTransform::new("values")
    .with_bandwidth(BandwidthType::Fixed(0.2))
```
Internally:
- Scott and Silverman rules are delegated to the KDE backend
- Fixed bandwidth is wrapped as a closure
- Bandwidth choice is applied per group

This makes bandwidth **explicit, reproducible, and inspectable**.

### 7.3.6 Grouped Density Estimation

One of the most powerful features of Charton density estimation is **grouped KDE**.
```rust
DensityTransform::new("IMDB_Rating")
    .with_groupby("Genre".to_string())
```
This causes Charton to:
1. Split the data by group
2. Perform KDE **independently for each group**
3. Concatenate the results into a single DataFrame
4. Preserve group labels for encoding

You can then encode color:
```rust
.mark_line()
.encode((
    x("value"),
    y("density"),
    color("Genre"),
))
```
Each group becomes a separate density curve, colored consistently.

### 7.3.7 Counts vs Probability Density

By default, density values integrate to 1 (probability density).

If you want **smoothed counts instead**, enable `counts`:
```rust
DensityTransform::new("values")
    .with_counts(true)
```
Internally:
- Density values are multiplied by group size
- The resulting curve approximates histogram counts
- Useful when comparing absolute frequencies

This allows density plots to function as **continuous histograms**.

### 7.3.8 Evaluation Grid and Numerical Stability

Internally, Charton:
- Computes global min/max across all data
- Expands degenerate ranges automatically
- Evaluates KDE on a fixed grid (default: 200 points)

This ensures:
- Consistent x-ranges across groups
- Stable rendering
- Comparable densities in layered charts

The evaluation grid is intentionally abstracted away from the API.

### 7.3.9 Rendering Density Curves

Once transformed, density data behaves like any other continuous dataset.

Typical rendering choices:

**Line Density**
```rust
.mark_line()
.with_line_stroke_width(2.0)
```
**Filled Density**
```rust
.mark_area()
.with_area_opacity(0.4)
```

### 7.3.10 Density vs Histogram vs Box Plot


| **Chart Type** | **Purpose**              | **Strength**       |
| ------------- | ------------------------- | ------------------ |
| Histogram     | Frequency approximation   | Simple, intuitive  |
| Density (KDE) | Smooth distribution shape | Reveals structure  |
| Box Plot      | Robust summary            | Compact comparison |
| ECDF          | Cumulative probability    | Exact percentiles  |

Charton intentionally supports **all four**, each optimized for a different analytical task.

**Design Philosophy Recap**

Density estimation in Charton is:
- **Explicit**: no hidden magic
- **Composable**: works with any continuous mark
- **Grouped by design**: first-class support
- **Statistically honest**: kernel and bandwidth are visible choices

Rather than introducing a dedicated `density` mark, Charton treats KDE as what it truly is:

> **a statistical transformation, not a geometric primitive**

## 7.4 Empirical Cumulative Distribution Function (ECDF)

The **Empirical Cumulative Distribution Function (ECDF)** describes how data accumulates over its range.

For a value 𝑥, the ECDF gives:

> the number (or proportion) of observations less than or equal to 𝑥

Unlike density plots, ECDFs:
- Do **not require bandwidth selection**
- Are **exact** representations of the data
- Preserve all distributional information
- Are especially useful for percentile-based comparisons

In Charton, ECDF is implemented as a **window transformation**, not as a dedicated mark.

This design reflects a core principle:

> ECDF is a *cumulative statistic*, not a geometric primitive.

### 7.4.1 ECDF as a Window Transformation

Charton computes ECDF using the **window transform system**, specifically the `CumeDist` operation.
```rust
WindowOnlyOp::CumeDist
```
Conceptually, this corresponds to SQL-style cumulative distribution:
```sql
CUME_DIST() OVER (PARTITION BY group ORDER BY value)
```
The result is a new column that increases monotonically within each group.

### 7.4.2 Minimal ECDF Example

A basic ECDF chart consists of three parts:
1. A window transformation
2. A line mark
3. Step interpolation
```rust
let chart = Chart::build(&df)?
    .transform_window(
        WindowTransform::new(
            WindowFieldDef::new("value", WindowOnlyOp::CumeDist, "ecdf")
        )
    )?
    .mark_line()
        .with_interpolation(PathInterpolation::StepAfter)
    .encode((
        x("value"),
        y("ecdf"),
    ))?;
```
Here:
- `"value"` is the sorted variable
- `"ecdf"` is the cumulative frequency
- `StepAfter` ensures a mathematically correct ECDF shape

### 7.4.3 Why Step Interpolation Is Required

An ECDF is a **right-continuous step function**.

Charton enforces this visually by requiring:
```rust
.with_interpolation(PathInterpolation::StepAfter)
```
This interpolation means:
- The function holds its value *after* each observation
- Jumps occur exactly at data points
- No artificial smoothing is introduced

Internally, this choice also triggers ECDF-specific rendering logic, described later.

### 7.4.4 Grouped ECDFs

ECDFs are often compared across categories.

Charton supports grouped ECDF computation via `with_groupby`:
```rust
.transform_window(
    WindowTransform::new(
        WindowFieldDef::new("sepal_length", WindowOnlyOp::CumeDist, "ecdf")
    )
    .with_groupby("species")
)
```
This causes Charton to:
**1.** Partition data by group
**2.** Sort values within each group
**3.** Compute cumulative distributions independently
**4.** Preserve group identity for encoding

You can then encode color:
```rust
.encode((
    x("sepal_length"),
    y("ecdf"),
    color("species"),
))
```
Each group becomes a separate ECDF curve.

### 7.4.5 Normalized vs Raw ECDF

By default, `CumeDist` returns **raw cumulative counts**:
- The maximum value equals the group size

You can normalize ECDFs to the [0, 1] range:
```rust
.with_normalize(true)
```
This transforms ECDFs into **cumulative probability functions**, making them directly comparable across groups with different sample sizes.

Internally, normalization is applied as:
```rust
cumulative / total_count
```
This logic is implemented entirely within the lazy Polars pipeline.

### 7.4.6 WindowTransform API Overview

The ECDF behavior is controlled by `WindowTransform`:
```rust
pub struct WindowTransform {
    pub window: WindowFieldDef,
    pub frame: [Option<f64>; 2],
    pub groupby: Option<String>,
    pub ignore_peers: bool,
    pub normalize: bool,
}
```
For ECDF:
- `window.op = CumeDist`
- `frame = [None, Some(0.0)]` (unbounded preceding → current row)
- `ignore_peers = false` (ties handled correctly)
- `normalize` is optional

The frame definition ensures cumulative behavior rather than sliding windows.

### 7.4.7 How ECDF Is Computed Internally

When `CumeDist` is selected, Charton performs the following steps:

**1. Create a working DataFrame**
Ensures a group column exists (real or temporary).

**2. Compute rank-based cumulative frequency**

Uses Polars’ `rank()` with:
* `RankMethod::Max`
* Ascending order

**3. Preserve group appearance order**

Groups are rendered in the order they appear in the data.

**4. Deduplicate ECDF steps**

Only unique `(group, cumulative_value)` pairs are kept.

**5. Optionally normalize**

Divides by total group size if requested.

**6. Clean up temporary columns**

The result is a minimal, clean dataset suitable for plotting.

### 7.4.8 ECDF Rendering Details


ECDF rendering is handled by the line mark renderer with ECDF-aware logic.

**Global X Range Alignment**

Before rendering, Charton computes:
```rust
global_x_min
global_x_max
```
These values ensure that **all ECDF curves share the same horizontal extent**, which is critical for correct comparison.

**Automatic Start and End Points**

For `StepAfter` interpolation, Charton automatically:
- Prepends a starting point `(global_x_min, 0)`
- Appends an ending point `(global_x_max, max_y)`

This guarantees:
- ECDF starts at zero
- ECDF extends horizontally to the full data range
- Visual completeness even with sparse data

This behavior is ECDF-specific and does **not** apply to ordinary line charts.

### 7.4.9 Axis Swapping Support


ECDF rendering fully respects coordinate transformations.

When axes are swapped, the charts are transposed:
```rust
(x, y) → (y, x)
```
This allows ECDFs to be rendered horizontally without any change to statistical logic.

### 7.4.10 Complete ECDF Example

```rust
let chart = Chart::build(&df.select(["species", "sepal_length"])?)?
    .transform_window(
        WindowTransform::new(
            WindowFieldDef::new(
                "sepal_length",
                WindowOnlyOp::CumeDist,
                "ecdf"
            )
        )
        .with_groupby("species")
        .with_normalize(false)
    )?
    .mark_line()
        .with_interpolation(PathInterpolation::StepAfter)
    .encode((
        x("sepal_length"),
        y("ecdf"),
        color("species")
    ))?;
```
This produces a grouped ECDF with exact cumulative counts.

### 7.4.11 ECDF vs Density vs Histogram


| **Method**    | **Exact**   | **Smooth** | **Requires Parameters** | **Best Use**   |
| ------------- | ----------- | ------ | ------------------- | ------------------------ |
| Histogram     | Approximate | No     | Bin size            | Frequency overview       |
| Density (KDE) | Approximate | Yes    | Kernel, bandwidth   | Shape analysis           |
| ECDF          | Exact       | No     | None                | Percentiles, comparisons |

Charton intentionally supports all three, recognizing that **no single method fits all analytical goals**

**Design Philosophy Recap**

ECDF in Charton is:
**- Statistically exact**
**- Explicitly computed**
**- Composable with marks**
**- Faithful to mathematical definition**

Rather than introducing a special ECDF mark, Charton models ECDF as what it truly is:
> a cumulative window statistic rendered with step geometry..

## 7.5 Histograms (1D & Grouped)


Histograms are one of the most fundamental tools for understanding data distributions.

They approximate an underlying distribution by:

**1. Partitioning continuous values into bins**
**2. Counting observations within each bin**
**3. Rendering counts (or normalized frequencies) as adjacent bars**

Unlike bar charts, histogram bars represent **continuous intervals**, not discrete categories.
This distinction drives both the API design and the internal implementation in Charton.

### 7.5.1 Histogram as a Data Transformation

In Charton, a histogram is **not a primitive mark alone**.

It is defined by the combination of:
- a **binning transform** on the x-axis
- an **aggregation** (count)
- an optional **normalization**
- a **rectangular mark renderer**

This separation allows histograms to integrate naturally with:
- color grouping
- layered charts
- axis swapping
- shared legends

### 7.5.2 Basic 1D Histogram Example

```rust
let chart = Chart::build(&df)?
    .mark_hist()
    .encode((
        x("value"),
        y("count"),
    ))?;
```
At the API level:
- `x("value")` indicates the continuous variable to be binned
- `y("count")` signals an aggregation rather than raw data
- `mark_hist()` selects the histogram renderer

No explicit binning step is required from the user.

### 7.5.3 Normalized Histograms

Histograms are often normalized to show **relative frequencies** instead of raw counts.

Charton exposes normalization through the y encoding:
```rust
y("count").with_normalize(true)
```
This mirrors the mental model used in density plots and ECDFs:

> normalization is a property of aggregation, not geometry

Internally, normalization is applied *after grouping* but *before rendering*.

### 7.5.4 Grouped Histograms via Color Encoding

Histograms can be grouped using color encoding:
```rust
.encode((
    x("value"),
    y("count").with_normalize(true),
    color("variable"),
))
```
This produces a **grouped histogram**, where each group:
- shares the same bin boundaries
- is normalized independently
- is rendered with a distinct palette color

Charton ensures that **empty bins are preserved** across all groups, avoiding misleading shapes.

### 7.5.5 Histogram Data Transformation Pipeline

Histogram computation is implemented in:
```rust
Chart<T>::transform_histogram_data
```
This method is invoked automatically when `mark_hist()` is active.

The pipeline consists of six stages.

### 7.5.5.1 Determining the Number of Bins

Charton determines the number of bins as follows:
```rust
let n_bins = x_encoding.bins.unwrap_or_else(|| {
    ((unique_count as f64).sqrt() as usize).max(5).min(50)
});
```
Rules:
- If all values are identical → 1 bin
- Otherwise:
    - default: √n rule
    - minimum: 5 bins
    - maximum: 50 bins
- Users may override this explicitly via encoding

This strikes a balance between robustness and simplicity.

#### 7.5.5.2 Computing Bin Boundaries

Once `n_bins` is known:
```rust
let bin_width = (max_val - min_val) / (n_bins as f64);
```
Charton constructs:
- bin edges
- bin labels (`bin_0`, `bin_1`, …)
- bin midpoints (used later for rendering)

The midpoints ensure histogram bars align naturally with continuous axes.

#### 7.5.5.3 Assigning Observations to Bins

Binning is performed using a dedicated statistical utility:
```rust
crate::stats::stat_binning::cut
```
This produces a categorical bin label for each observation, which is then:
- added as a temporary column
- used as a grouping key

This design keeps binning logic reusable and testable.

#### 7.5.5.4 Grouping and Counting

Aggregation depends on whether color encoding is present.

**Without color encoding:**
```rust
group_by_stable([bin_field])
```
**With color encoding:**
```rust
group_by_stable([bin_field, color_field])
```
In both cases, counts are computed using:
```rust
col(&bin_field).count()
```
Stable grouping preserves the visual order of categories.

#### 7.5.5.5 Filling Empty Bins

A critical detail: **empty bins must be rendered explicitly**.

Charton guarantees this by:
- generating all possible bin labels
- creating all bin × color combinations (if grouped)
- left-joining aggregated results
- filling missing counts with zero

This avoids gaps and misleading shapes in grouped histograms.

#### 7.5.5.6 Normalization

If normalization is enabled:
- **without color**: all bins sum to 1
- **with color**: each color group sums to 1 independently

This mirrors the behavior of statistical plotting systems such as Vega-Lite and ggplot2.

#### 7.5.5.7 From Transformed Data to Geometry

After transformation, the histogram renderer takes over.

Rendering is implemented in:
```rust
Chart<MarkHist>::render_histogram
```
#### 7.5.5.8 Bar Width Calculation

Histogram bins are equally spaced, so bar width is inferred from data:
```rust
bar_width = (max - min) / (n_bins - 1) * 0.95
```
This:
- prevents bars from touching exactly
- preserves the continuous nature of the axis
- adapts automatically to axis scaling

A fallback width is used if spacing cannot be inferred safely.

#### 7.5.5.9 Color Handling Strategy

Color is resolved per group:
- If `color` encoding exists:
    - colors are assigned from the active palette
- Otherwise:
    - `MarkHist.color` is used as a uniform fill

This matches the behavior of other marks and keeps styling orthogonal to data semantics.

#### 7.5.5.10

Vertical and Horizontal Histograms

Histogram rendering respects axis swapping automatically.
- **Vertical histogram**
    - x → bin midpoint
    - y → count

- **Horizontal histogram**
    - y → bin midpoint
    - x → count

The same transformed data supports both layouts without recomputation.

#### 7.5.5.11 MarkHist: Visual Properties

The `MarkHist` struct controls appearance only:
```rust
pub struct MarkHist {
    color: Option<SingleColor>,
    opacity: f64,
    stroke: Option<SingleColor>,
    stroke_width: f64,
}
```
This strict separation ensures:
- no statistical logic leaks into rendering
- consistent behavior across themes and backends

#### 7.5.5.12 Layering and Legends

Histograms integrate naturally with layered charts:
```rust
LayeredChart::new()
    .add_layer(histogram_chart)
    .with_legend(true)
```
Legend rendering is delegated to the shared color legend renderer, ensuring consistency across all mark types.

### 7.5.6 A Complete Histogram Example

The following example demonstrates a complete workflow for creating a grouped, normalized histogram in Charton:
- reading real-world data
- reshaping it for visualization
- binning continuous values
- grouping by category
- rendering a layered histogram with a legend

```rust
use charton::prelude::*;
use polars::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let df = load_dataset("iris")?;

    let df_melted = df.unpivot(
        ["sepal_length", "sepal_width", "petal_length", "petal_width"],
        ["species"]
    )?;

    let histogram_chart = Chart::build(&df_melted.head(Some(200)))?
        .mark_hist()
        .encode((
            x("value"),
            y("count").with_normalize(true),
            color("variable")
        ))?
        .with_hist_color(Some(SingleColor::new("steelblue")))
        .with_hist_opacity(0.5)
        .with_hist_stroke(Some(SingleColor::new("black")))
        .with_hist_stroke_width(0.0)
        .with_color_palette(ColorPalette::Tab10);

    LayeredChart::new()
        .with_size(500, 400)
        .with_title("Histogram Example")
        .with_x_label("Value")
        .with_y_label("Frequency")
        .add_layer(histogram_chart)
        .with_legend(true)
        .save("histogram.svg")?;

    Ok(())
}
```

### 7.5.7 Design Philosophy Recap

Charton’s histogram implementation emphasizes:
- **Explicit statistical transformation**
- **Stable grouping and binning**
- **Correct handling of empty bins**
- **Clear separation of data, geometry, and style**

Rather than treating histograms as “fat bar charts,” Charton models them as what they are:
> a structured approximation of continuous distributions.

## 7.6 Pie & donut charts

Pie and donut charts represent **categorical proportions** using angular spans. Each category is mapped to a slice whose angle is proportional to its value.

In Charton, pie and donut charts are implemented using the `arc` **mark** together with **theta encoding**, rather than Cartesian x/y axes.

### 7.6.1 A Complete Pie / Donut Chart Example

The following example demonstrates how to create a **donut chart** using Charton. It covers:
- categorical aggregation
- angular (theta) encoding
- color-based grouping
- converting a pie chart into a donut chart
```rust
use charton::prelude::*;
use polars::prelude::*;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    // Create sample data frame
    let df = df![
        "category" => ["A", "B", "C", "D", "E"],
        "value" => [25.0, 30.0, 15.0, 20.0, 10.0]
    ]?;

    // Create a pie / donut chart
    let pie_chart = Chart::build(&df)?
        .mark_arc()
        .encode((
            theta("value"),      // angular size of each slice
            color("category"),   // category for each slice
        ))?
        .with_inner_radius_ratio(0.5); // turn pie into donut

    // Render and save
    LayeredChart::new()
        .with_size(400, 400)
        .with_title("Donut Chart Example")
        .add_layer(pie_chart)
        .with_legend(true)
        .save("./examples/donut.svg")?;

    Ok(())
}
```
This example produces a donut chart where each category occupies a slice whose angle is proportional to its value.

The remainder of this section explains how this behavior is implemented.

### 7.6.2 Arc Marks and Polar Geometry

Pie and donut charts in Charton are rendered using the `MarkArc` mark type.

Unlike Cartesian marks (points, bars, lines), arc marks:
- do **not** use x/y scales
- are rendered in **polar coordinates**
- rely on angular accumulation around a center point

Internally, the chart computes:
- a fixed center position
- a shared outer radius
- a cumulative start and end angle for each slice

```rust
let slice_angle = 2.0 * PI * value / total;
```
All slices together sum to a full circle.

### 7.6.3 Theta Encoding

The `theta()` encoding maps a numeric field to **angular magnitude**.
```rust
.encode((
    theta("value"),
    color("category"),
))
```
Key properties:
- theta values **must be non-negative**
- values are summed by their category label and automatically normalized by the total sum of all values
- the order of slices follows the **original data order**, not sorted order

Internally, Charton aggregates data by category and sums the theta field before rendering.

### 7.6.4 Color Encoding and Legends

Pie and donut charts almost always rely on color to distinguish categories.

When `color()` encoding is present:
- each slice is assigned a color from the active palette
- legends are rendered automatically
- slice order matches the first appearance of each category
```rust
color("category")
```
If no color encoding is provided, the mark’s default color is used.

### 7.6.5 Donut Charts via Inner Radius Ratio

Donut charts are created by specifying an inner radius ratio:
```rust
.with_inner_radius_ratio(0.5)
```
- `0.0` → full pie chart
- `(0.0, 1.0)` → donut chart
- `1.0` → fully hollow (not useful)

The inner radius is computed as:
```rust
inner_radius = outer_radius × inner_radius_ratio
```
This allows smooth transitions between pie and donut representations without changing data or encodings.

### 7.6.6 Rendering Model

Each slice is rendered as an SVG path representing an annular sector:
- start angle
- end angle
- outer radius
- optional inner radius

Stroke and opacity are applied uniformly across slices:
```rust
.with_arc_opacity(0.9)
.with_arc_stroke(SingleColor::new("white"))
.with_arc_stroke_width(1.0)
```

### 7.6.7 Design Considerations

Pie and donut charts are best suited for:
- small numbers of categories
- part-to-whole comparisons

emphasizing proportions rather than precise values.

For larger category counts or precise comparisons, bar charts or stacked charts are often more effective.

Charton intentionally keeps pie/donut charts:
- simple
- declarative
- aggregation-driven

to encourage correct and intentional usage.

## 7.7 Heatmaps & rect-based charts

**Grid-based scalar maps and intensity fields**

Heatmaps and other rectangle-based charts visualize scalar values over a 2D grid.

In Charton, this family of charts is implemented using the `rect` **mark**, where each data point is rendered as a colored rectangle whose position is determined by *(x, y)* encodings and whose color represents a numeric intensity.

Typical use cases include:
- Categorical × categorical heatmaps
- Continuous 2D density / binned heatmaps
- Correlation matrices
- Time × category intensity maps

### 7.7.1 API Overview

Rect-based charts are enabled by calling `mark_rect()` on a `Chart`.
```rust
Chart::build(&df)?
    .mark_rect()
    .encode((
        x("x"),
        y("y"),
        color("value"),
    ))?;
```
**Required encodings**

| **Encoding** | **Type**                   | **Description**                    |
| -------- | ---------------------- | ------------------------------ |
| `x`      | discrete or continuous | Horizontal grid coordinate     |
| `y`      | discrete or continuous | Vertical grid coordinate       |
| `color`  | numeric                | Scalar value mapped to a color |

> **Note**
> Unlike point or bar charts, `rect` charts **require** a `color` encoding. The rectangle fill color is the primary visual channel.

### 7.7.2 Rect Mark Configuration

The `rect` mark controls the appearance of each grid cell.
```rust
pub struct MarkRect {
    color: Option<SingleColor>,
    opacity: f64,
    stroke: Option<SingleColor>,
    stroke_width: f64,
}
```
**Configuration methods**
```rust
.mark_rect()
.with_rect_color(Some(SingleColor::new("black")))
.with_rect_opacity(0.8)
.with_rect_stroke(Some(SingleColor::new("white")))
.with_rect_stroke_width(1.0)
```
| **Method**               | **Effect**                                           |
| ------------------------ | ---------------------------------------------------- |
| `with_rect_color`        | Fallback fill color (used only if no color encoding) |
| `with_rect_opacity`      | Rectangle transparency                               |
| `with_rect_stroke`       | Border color                                         |
| `with_rect_stroke_width` | Border thickness in pixels                           |

In typical heatmap usage, **the fill color comes from the colormap**, not from `with_rect_color`.

### 7.7.3 Data Processing Pipeline (Source-level Explanation)

Rect-based charts have the most complex data pipeline in Charton because they support **both discrete and continuous axes**.

This logic lives in:
```rust
Chart<T>::transform_rect_data()
```
**Step 1: Detect axis scale types**
```rust
let x_is_discrete = matches!(x_scale, Scale::Discrete);
let y_is_discrete = matches!(y_scale, Scale::Discrete);
```
- Discrete × discrete → categorical heatmap
- Continuous × continuous → 2D binned heatmap
- Mixed → hybrid binning

**Step 2: Automatic binning (continuous axes)**

If an axis is continuous:
- Values are **binned**
- Bin count is determined by:
    - Explicit `bins` setting, or
    - √N heuristic (bounded to 5–50)
- Bin labels are generated (`bin_0`, `bin_1`, …)
- Bin **midpoints** are stored for rendering
```rust
cut(&series, &bins, &labels)
```
This ensures **rectangles occupy equal spatial extents**, which is essential for heatmaps.

**Step 3: Aggregation**

After binning (if needed), data is aggregated by `(x, y)`:
```rust
.group_by_stable([x, y])
.agg([color.sum()])
```
If multiple records fall into the same grid cell, their values are **summed**.

**Step 4: Fill missing grid cells**

For binned data, Charton generates **all possible x × y combinations** and fills missing cells with `0`.

This guarantees:
- Rectangles form a complete grid
- No visual gaps appear due to missing data

**Step 5: Replace bin labels with numeric midpoints**

For continuous axes:
- Bin labels → numeric midpoints
- Enables numeric coordinate mapping during rendering

### 7.7.4 Rendering Logic

Rendering is handled by `render_rects()`.

**Rectangle size computation**
```rust
rect_width  = Δx_pixels / (unique_x_count - 1)
rect_height = Δy_pixels / (unique_y_count - 1)
```
This ensures:

- Uniform rectangle sizes
- Correct alignment in both discrete and continuous modes

**Color mapping**
```rust
let color = self.mark_cmap.get_color(normalized_value);
```
- Values are normalized in `ProcessedChartData`
- Colors are drawn from the active `ColorMap`
- A continuous colorbar legend is automatically rendered

### 7.7.5 Legend Behavior

Rect charts always use a **continuous color legend**:
```rust
render_colorbar(svg, self, theme, context)
```
This produces a vertical color scale showing:
- Min → max value mapping
- Associated numeric ticks

### 7.7.6 Complete Examples

**Example 1: Categorical Heatmap**
```rust
use charton::prelude::*;
use polars::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let df = df! [
        "x" => ["A", "B", "C", "A", "B", "C", "A", "B", "C"],
        "y" => ["X", "X", "X", "Y", "Y", "Y", "Z", "Z", "Z"],
        "value" => [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
    ]?;

    let heatmap = Chart::build(&df)?
        .mark_rect()
        .encode((
            x("x"),
            y("y"),
            color("value"),
        ))?;

    LayeredChart::new()
        .add_layer(heatmap)
        .save("heatmap.svg")?;

    Ok(())
}
```

**Characteristics**
- Discrete × discrete grid
- One rectangle per category pair
- Continuous color legend

**Example 2: Continuous 2D Density / Binned Heatmap**
```rust
use charton::prelude::*;
use polars::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let df = df! [
        "x" => [1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.8, 2.05, 2.2, 2.5, 2.6, 2.7],
        "y" => [1.2, 1.3, 1.4, 1.5, 1.8, 1.83, 2.0, 1.9, 2.2, 2.3, 2.4, 2.5],
        "value" => [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0],
    ]?;

    let density = Chart::build(&df)?
        .mark_rect()
        .encode((
            x("x"),
            y("y"),
            color("value"),
        ))?
        .with_color_map(ColorMap::GnBu);

    LayeredChart::new()
        .add_layer(density)
        .save("2d_density.svg")?;

    Ok(())
}
```

**Characteristics**
- Continuous × continuous
- Automatic binning
- Grid filled with aggregated values
- Smooth perceptual color mapping

### 7.7.7 Design Notes

- Rect charts are **data-dense** and scale well to large datasets
- Automatic binning keeps the API minimal while remaining flexible
- Separation of:
    - **Data transformation** (`transform_rect_data`)
    - **Rendering** (`render_rects`) ensures extensibility (e.g. treemaps, calendar heatmaps)


## 7.8 Rule-based Charts

**Thresholds, reference markers, and annotation overlays**

Rule-based charts are used to draw **reference lines** rather than data glyphs. They are typically overlaid on other charts to indicate:
- Thresholds (e.g. mean, target, warning line)
- Ranges (from `y` to `y2`)
- Event markers or annotations
- Baselines and guides

In Charton, rule charts are implemented via the `rule` **mark**, which renders horizontal or vertical lines aligned to data coordinates.

Unlike bars or points, rule marks do not encode magnitude through area or position alone — instead, they **annotate the coordinate space itself**.

### 7.8.1 API Overview

A rule chart is created by calling `mark_rule()` on a `Chart`.
```rust
Chart::build(&df)?
    .mark_rule()
    .encode((
        x("x"),
        y("y"),
    ))?;
```
**Supported encodings**

| **Encoding** | **Required** | **Description**                       |
| -------- | -------- | ----------------------------------------------- |
| `x`      | yes      | Position of the rule along the x-axis           |
| `y`      | yes      | Starting position along the y-axis              |
| `y2`     | no       | Optional end position (defines a segment/range) |
| `color`  | no       | Category or value-based coloring                |

### 7.8.2 MarkRule Configuration

The visual appearance of rule lines is controlled by `MarkRule`.
```rust
pub struct MarkRule {
    color: Option<SingleColor>,
    opacity: f64,
    stroke_width: f64,
}
```
**Configuration methods**
```rust
.mark_rule()
.with_rule_color(Some(SingleColor::new("red")))
.with_rule_opacity(0.6)
.with_rule_stroke_width(2.0)
```
| **Method**               | **Effect**                                            |
| ------------------------ | ----------------------------------------------------- |
| `with_rule_color`        | Default stroke color (when no color encoding is used) |
| `with_rule_opacity`      | Line transparency                                     |
| `with_rule_stroke_width` | Line thickness in pixels                              |

If a `color` encoding is present, colors are automatically assigned using:
- **Palette** for discrete scales
- **Colormap** for continuous scales

### 7.8.3 Conceptual Model

Rule charts support **two conceptual forms**:

**1. Infinite reference rules**

If `y2` is **not** provided:
- Each rule spans the **entire vertical range** of the plotting region.
- Used for global thresholds or x-axis baselines.
- **Technical Note:** While the `y` encoding is required by the Cartesian coordinate system, its specific value is ignored for rendering the line; the line will always extend from the bottom to the top of the chart.

```rust
// y is required for the API but its value does not restrict the line length
x = constant
y = plot_min → plot_max
```
**2. Finite range rules**

If `y2` **is** provided:
- Each rule spans from `y` to `y2`
- Used for intervals, confidence bands, or annotations
```rust
x = constant
y → y2
```
This dual behavior is handled automatically at render time.

### 7.8.4 Data Processing Pipeline

Rule charts reuse the shared processing logic via:
```rust
ProcessedChartData::new(self, coord_system)
```
This provides:
- Transformed x / y values (linear, log, discrete)
- Optional color normalization

**Handling** `y2`

If a `y2` encoding exists:
```rust
let y2_series = self.data.column(&y2_encoding.field)?;
```
- Values are transformed according to the y-axis scale
- Log scales apply `log10`
- Linear and discrete scales pass through unchanged

This ensures **consistent geometry across coordinate systems**.

### 7.8.5 Rendering Logic (Source-Level Explanation)

Rendering is implemented in `render_rules()`.

**Step 1: Resolve stroke color**
```rust
if let Some(color_info) = processed_data.color_info {
    // palette or colormap
} else {
    mark.color.clone()
}
```
| **Color scale** | **Behavior**          |
| ----------- | ------------------------- |
| Discrete    | Uses categorical palette  |
| Continuous  | Uses colormap             |
| None        | Uses mark’s default color |

**Step 2: Map data → pixel space**
```rust
let x_pos = (context.x_mapper)(x_vals[i]);
let y_pos = (context.y_mapper)(y_vals[i]);
```
Axis swapping is handled transparently via `context.swapped_axes`.

**Step 3: Render rules**
**Without** `y2`
```rust
render_vertical_rule(
    x_pos,
    plot_top,
    plot_bottom,
)
```
Draws a full-height rule.

**With** `y2`
```rust
render_vertical_rule(
    x_pos,
    y_pos,
    y2_pos,
)
```
Draws a bounded segment.

If axes are swapped, horizontal rules are drawn instead.

### 7.8.6 Legend Behavior

Rule charts support **both legend types**:
- **Discrete color legend** (categorical rules)
- **Continuous colorbar** (value-driven rules)

Legend rendering is delegated to:
```rust
colorbar_renderer::render_colorbar(...)
color_legend_renderer::render_color_legend(...)
```
Only the appropriate legend is displayed, depending on the encoding scale.

### 7.8.7 Complete Example: Rule Chart with Y and Y2

The following example demonstrates:

- Vertical rules positioned at x
- Finite rule segments using y → y2
- Discrete color encoding
- Full chart metadata (title, labels)
```rust
use charton::prelude::*;
use polars::prelude::*;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    // Create sample data with x, y, y2, and color columns
    let df = df![
        "x" => [1.0, 2.0, 3.0, 4.0, 5.0],
        "y" => [2.0, 3.0, 1.0, 4.0, 2.0],
        "y2" => [4.0, 5.0, 3.0, 6.0, 4.0],
        "color" => ["A", "B", "A", "B", "A"]
    ]?;

    // Create rule chart
    let chart = Chart::build(&df)?
        .mark_rule()
        .encode((
            x("x"),
            y("y"),
            y2("y2"),
            color("color"),
        ))?
        .into_layered()
        .with_title("Rule Chart with Y and Y2")
        .with_x_label("X Values")
        .with_y_label("Y Values");

    // Save to SVG
    chart.save("rule.svg")?;

    Ok(())
}
```
**Resulting Visualization**
- Each rule is a vertical line at a given `x`
- The visible segment spans from `y` to `y2`
- Colors distinguish categories
- Rules can be layered on top of other marks

### 7.8.8 Design Notes

- Rule marks are **annotation-first**, not data-first
- They are designed to be layered with bars, lines, or heatmaps
- Explicit support for `y2` avoids special-case “range” marks
- Shared rendering infrastructure keeps rule logic minimal and composable

# Chapter 8 · External Backend Integration

Charton can render charts using native Rust rendering, but it also integrates seamlessly with external visualization backends such as Altair  and Matplotlib.  

This chapter explains how backend switching works and why external backends can be useful for leveraging established visualization ecosystems. You will also learn how to run raw Python plotting code from Rust, allowing complete flexibility.

This is especially useful when mixing Rust data pipelines with existing Python workflows.

## 8.1 Why external backends?

Rust visualization ecosystem — including Charton — is still relatively young, it may not always meet all user requirements. In contrast, other languages have mature and feature-rich visualization tools, such as Altair and Matplotlib. Therefore, in situations where Charton’s native capabilities are not sufficient, it is necessary to rely on these external visualization tools as complementary backends.

## 8.2 Altair backend

Charton provides first-class integration with the Altair visualization ecosystem through the Altair backend. This backend allows Rust programs to generate Altair charts, render them using Python, and output either SVG images or Vega-Lite JSON specifications. This enables seamless interoperability between Rust data pipelines and any existing Python-based visualization workflow.

Internally, Charton sends data to Python using an IPC (Apache Arrow) buffer, executes user-provided Altair code, and returns either SVG or Vega-Lite JSON back to Rust.

**🔧 Requirements**

Before using the Altair backend, ensure Python and required packages are installed:
```shell
pip install altair vl-convert-python polars pyarrow
```
### 8.2.1 Loading the Example Dataset (`mtcars`)

Below is a built-in function to load `mtcars` into a Polars `DataFrame`:
```rust
let df = load_dataset("mtcars")?;
```

### 8.2.2 Basic Usage: Executing Altair Code

This example shows the minimal usage of the Altair backend:
- Load `mtcars`
- Send it to the Altair backend
- Execute a small Altair script
- Display result (in Jupyter) or do nothing (CLI)

```rust
use charton::prelude::*;
use polars::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let df = load_dataset("mtcars")?;

    let code = r#"
import altair as alt
chart = alt.Chart(df).mark_point().encode(
    x='mpg',
    y='hp',
    color='cyl:O'
)
"#;

    Plot::<Altair>::build(data!(&df)?)?
        .with_exe_path("python")?
        .with_plotting_code(code)
        .show()?;   // Works in evcxr notebook

    Ok(())
}
```
### 8.2.3 Saving Altair Charts as SVG

The Altair backend supports exporting the chart as **SVG** by calling `.save("chart.svg")`.
```rust
use charton::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let df = load_mtcars();

    let code = r#"
import altair as alt
chart = alt.Chart(df).mark_circle(size=80).encode(
    x='wt',
    y='mpg',
    color='cyl:O',
    tooltip=['mpg','cyl','wt']
)
"#;

    Plot::<Altair>::build(data!(&df)?)?
        .with_exe_path("python")?
        .with_plotting_code(code)
        .save("scatter.svg")?;

    println!("Saved to scatter.svg");
    Ok(())
}
```
### 8.2.4 Export as Vega-Lite JSON

To get a **Vega-Lite JSON specification**, call `.to_json()` or save with `.json` extension:

**Method 1 — get JSON string in Rust:**
```rust
let json: String = Plot::<Altair>::build(data!(&df)?)?
    .with_exe_path("python")?
    .with_plotting_code(code)
    .to_json()?;

println!("{}", json);
```
**Method 2 — save JSON file:**
```rust
Plot::<Altair>::build(data!(&df)?)?
    .with_exe_path("python")?
    .with_plotting_code(code)
    .save("chart.json")?;
```
### 8.2.5 Example: Converting to Vega-Lite JSON and Rendering in the Browser

You can embed the exported JSON into an HTML file and render it directly in the browser using Vega-Lite.

**Step 1 — generate JSON from Rust:**
```rust
Plot::<Altair>::build(data!(&df)?)?
    .with_exe_path("python")?
    .with_plotting_code(code)
    .save("mtcars.json")?;
```

**Step 2 — embed JSON in HTML:**
```html
<!DOCTYPE html>

<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/vega@5"></script>
  <script src="https://cdn.jsdelivr.net/npm/vega-lite@5"></script>
  <script src="https://cdn.jsdelivr.net/npm/vega-embed@6"></script>
</head>

<body>
<div id="vis"></div>

<script>
fetch("mtcars.json")
  .then(r => r.json())
  .then(spec => vegaEmbed("#vis", spec));
</script>


</body>
</html>
```
Open in browser → you get an Altair-rendered visualization displayed via Vega-Lite.

### 8.2.6 Full Example: A More Complete Altair Chart

```rust
let df = load_dataset("mtcars")?;

let code = r#"
import altair as alt

chart = alt.Chart(df).mark_point(filled=True).encode(
    x=alt.X('hp', title='Horsepower'),
    y=alt.Y('mpg', title='Miles/Gallon'),
    color=alt.Color('cyl:O', title='Cylinders'),
    size='wt',
    tooltip=['mpg','hp','wt','cyl']
)
"#;

Plot::<Altair>::build(data!(&df)?)?
    .with_exe_path("python")?
    .with_plotting_code(code)
    .save("full.svg")?;
```
## 8.3 Matplotlib backend

The Matplotlib backend enables Charton to generate high-quality static visualizations using Python’s Matplotlib library. This backend is ideal when users need:
- Scientific publication–grade plots
- Fine-grained control over rendering
- Access to the mature, feature-rich Matplotlib ecosystem
- Compatibility with existing Python visualization workflows

Just like the Altair backend, Charton transfers data to Python through an IPC buffer (Apache Arrow), executes user-provided Matplotlib code, and returns the resulting SVG image back to Rust.

**🔧 Requirements**

Before using the Matplotlib backend, ensure the required Python packages are installed:
```bash
pip install matplotlib polars pyarrow
```
### 8.3.1 Basic Usage: Executing Matplotlib Code

The minimal workflow for the Matplotlib backend is similar to Altair:
**1.** Load data
**2.** Provide a snippet of Python Matplotlib code
**3.** Charton runs it in Python and captures the SVG output

```rust
use charton::prelude::*;
use polars::prelude::*;

let df = load_dataset("mtcars")?;

let code = r#"
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(5, 4))
ax.scatter(df['mpg'], df['hp'], c=df['cyl'], cmap='viridis')
ax.set_xlabel('MPG')
ax.set_ylabel('Horsepower')
"#;

Plot::<Matplotlib>::build(data!(&df))?
    .with_exe_path("python")?
    .with_plotting_code(code)
    .show()?;   // Works in evcxr notebook
```
### 8.3.2 Saving Matplotlib Output as SVG

You can export Matplotlib-rendered figures to SVG files using `.save("file.svg")`.

```rust
use charton::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let df = load_dataset("mtcars")?;

    let code = r#"
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(5, 4))
scatter = ax.scatter(df['wt'], df['mpg'], c=df['cyl'], cmap='tab10')
ax.set_xlabel('Weight')
ax.set_ylabel('MPG')
"#;

    Plot::<Matplotlib>::build(data!(&df))?
        .with_exe_path("python")?
        .with_plotting_code(code)
        .save("mat_mtcars.svg")?;

    println!("Saved to mat_mtcars.svg");
    Ok(())
}
```
The saved SVG can be embedded in Markdown, LaTeX, HTML, Jupyter notebooks, or included in publication figures.

### 8.3.3 Using Subplots

Matplotlib excels at multi-panel scientific figures. Here is an example showing two subplots:

```rust
let code = r#"
import matplotlib.pyplot as plt

fig, axes = plt.subplots(1, 2, figsize=(8, 4))

axes[0].hist(df['mpg'], bins=8, color='steelblue')
axes[0].set_title('MPG Distribution')

axes[1].scatter(df['hp'], df['wt'], c=df['cyl'], cmap='viridis')
axes[1].set_xlabel('Horsepower')
axes[1].set_ylabel('Weight')

fig.tight_layout()
"#;

Plot::<Matplotlib>::build(data!(&df)?)?
    .with_exe_path("python")?
    .with_plotting_code(code)
    .save("mat_subplots.svg")?;
```
### 8.3.4 Adding Titles, Legends, and Styles

Matplotlib’s flexibility allows full customization:

```rust
let code = r#"
import matplotlib.pyplot as plt
plt.style.use('ggplot')

fig, ax = plt.subplots(figsize=(6, 4))

scatter = ax.scatter(
    df['hp'], df['mpg'],
    c=df['cyl'],
    cmap='viridis',
    s=df['wt'] * 40,
    alpha=0.8
)

fig.colorbar(scatter, ax=ax, label='Cylinders')
ax.set_title('HP vs MPG (Sized by Weight)')
ax.set_xlabel('Horsepower')
ax.set_ylabel('MPG')
"#;

Plot::<Matplotlib>::build(data!(&df)?)?
    .with_exe_path("python")?
    .with_plotting_code(code)
    .save("mat_custom.svg")?;
```

### 8.3.5 Using Python Libraries with Matplotlib (not tested)

Because the backend runs arbitrary Python code, you can integrate any Python library, for example:
* seaborn for statistical plots
* scipy for fitting or curves
* sklearn for clustering overlay
* pandas for quick plotting

Example with seaborn:

```rust
let code = r#"
import seaborn as sns
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(6, 4))
sns.regplot(data=df, x='hp', y='mpg', ax=ax)
ax.set_title('Regression Line of HP vs MPG')
"#;
```

This freedom allows Rust code to leverage the entire Python scientific visualization stack.

### 8.3.6 Full Example: Multi-encoding Scatterplot

This example uses color mapping, different marker sizes, and labels:
```rust
let code = r#"
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(6, 4))

sc = ax.scatter(
    df['hp'], df['mpg'],
    c=df['cyl'], cmap='tab10',
    s=df['wt'] * 35,
    alpha=0.85, edgecolor='black'
)

ax.set_title('HP vs MPG (Colored by Cylinders, Sized by Weight)')
ax.set_xlabel('Horsepower')
ax.set_ylabel('Miles/Gallon')

cbar = fig.colorbar(sc, ax=ax)
cbar.set_label('Cylinders')

fig.tight_layout()
"#;

Plot::<Matplotlib>::build(data!(&df))?
    .with_exe_path("python")?
    .with_plotting_code(code)
    .save("mat_full.svg")?;
```

# Chapter 9: Interactive Workflows & WebAssembly Integration

Charton provides two categories of visualization output:

1. **Static rendering** (native Charton SVG)
2. **Interactive rendering** (via the Altair backend and Vega-Lite)

Although Charton’s native renderer produces static SVG files, it can still participate in interactive workflows in several environments (e.g., Jupyter), and Charton can also generate *fully interactive* visualizations by delegating to the Altair/Vega-Lite ecosystem.

This chapter explains these modes and clarifies the underlying architecture.

## 9.1 Static interactive-style display in Jupyter (via `evcxr`)

Charton integrates with `evcxr` to display static charts *inline* inside Jupyter notebooks. This mode is “static” because the output is a fixed SVG, but it behaves “interactive-style” because:
- Each execution immediately re-renders the chart inside the notebook  
- Any changes to code/data result in instant visual updates  
- Ideal for exploration, education, and iterative refinement

This is similar to how Plotters or PlotPy integrate with `evcxr`.

### Example: Displaying a Charton chart inline in Jupyter

```rust
:dep charton = { version="0.2.1" }
:dep polars = { version="0.49.1" }

use charton::prelude::*;
use polars::prelude::*;

// Create sample data
let df = df![
    "length" => [5.1, 4.9, 4.7, 4.6, 5.0],
    "width"  => [3.5, 3.0, 3.2, 3.1, 3.6]
]?;

// Build a simple scatter plot
Chart::build(&df)?
    .mark_point()
    .encode((x("length"), y("width")))?
    .into_layered()
    .show()?;   // <-- Displays directly inside the Jupyter cell
```
Even though the chart itself is static, the *workflow* feels interactive due to the rapid feedback loop.

## 9.2 Static SVG vs. Interactive Rendering in WebAssembly

Although Charton’s native output is a **static SVG**, this does *not* prevent it from supporting interactive rendering when compiled to Wasm. In fact, the combination of **Charton + Rust + Wasm** enables a high-performance interaction model that is often *faster* than traditional JavaScript visualization libraries.

To understand this correctly, we must distinguish two different concepts:
- **Static** — refers to the file format: SVG is a declarative XML graphics format.
- **Dynamic** — refers to the rendering and update pipeline: how a chart is recomputed and replaced in response to user input.

**🔑 Key Idea: Charton SVGs Are Not “Immutable”**

The SVG that Charton produces is a static file format, but this does **not** mean the visualization must remain static in the browser. The core principle of the Charton + Wasm model is:

> Interactions do not modify the SVG in-place.
> Instead, Charton’s Rust/Wasm runtime dynamically recomputes and regenerates a new SVG whenever needed.

Thus, the browser simply re-renders the updated SVG structure.

This architecture provides both simplicity (SVG is easy to embed, style, and display) and performance (Wasm + Polars + Rust for fast recomputation).

### 9.2.1 Interaction Does Not Require Canvas

Interactive visualization is *not* exclusive to Canvas or WebGL.
SVG supports two fundamentally different interaction models:
| **Interaction Model**                           | **Description**                                                                                          | **Suitable For**                                              |
| ----------------------------------------------- | -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- |
| **DOM-driven interactions (CSS/JS)**            | Browser handles hover, click, and small style changes by directly modifying SVG element attributes.      | Tooltips, highlighting, simple UI responses.                  |
| **Wasm-driven interactions (high-performance)** | Rust/Wasm computes a completely new SVG (or a DOM patch) on each interaction and replaces the old chart. | Zooming, panning, filtering, re-aggregating, re-scaling axes. |

Charton’s design focuses on *the second model*, where Rust/Wasm performs the heavy lifting.

### 9.2.2 Wasm-Driven Interactive Rendering Pipeline

When a user interacts with a Charton chart compiled to Wasm, the pipeline works as follows:
- The browser captures a user event—e.g., a drag event for zooming or a brush gesture for selecting a range.
- Using `wasm-bindgen`, the event details are passed into the Charton Rust core.
- The Rust engine performs full or partial chart recomputation. These operations run at native-like speed inside Wasm.
- Charton generates a new SVG string or structured DOM patch representing the new view.
- The browser replaces the old SVG node with the new one.

Charton’s Wasm-driven model has several performance advantages:

**1. Polars performance inside Wasm**
Traditional JS libraries rely on JavaScript arrays, D3 computations, or slower JS-based DataFrame libraries.
Charton instead executes **Polars** in Wasm—offering:
- zero-copy columnar data
- vectorized operations
- multi-threaded execution (where supported)

**2. Rust efficiency**
All chart logic—scales, encodings, transforms, layouts—is executed in **compiled Rust**, not interpreted JS.

**3. SVG rendering advantages**
SVG is declarative; modern browsers:
- batch DOM updates
- optimize SVG rendering paths
- offload rendering to GPU when possible

This drastically reduces UI-thread blocking compared to manual JS DOM manipulation.

### 9.2.3 Charton + Polars + wasm-bindgen — step-by-step example

> Goal: expose a `draw_chart()` function from Rust → returns an SVG string → JavaScript inserts that SVG into the DOM.

**0) Prerequisites**
- Rust toolchain (stable), with `rustup`.
- `wasm-pack` (recommended) OR `wasm-bindgen-cli` + `cargo build --target wasm32-unknown-unknown`.
    - Install `wasm-pack` (recommended):

      `cargo install wasm-pack`
- `clang` (required)
    - **Linux**: `apt install clang`
    - **Windows**: Download and run the **LLVM installer** from [LLVM Releases]https://github.com/llvm/llvm-project/releases. During installation, select **"Add LLVM to the system PATH"**.
- A simple static file server (e.g. `basic-http-server` from cargo, `python -m http.server`, or `serve` via npm).
- Node/ npm only if you want to integrate into an NPM workflow; not required for the simple demo.

> **Important compatibility note (read before you start):**

Many crates (especially heavy ones like `polars` or visualization crates) may have limited or no support for `wasm32-unknown-unknown` out of the box. If Polars and Charton compile to wasm in your environment, the steps below will work. If they don't, read the **Caveats & alternatives** section at the end.

**1) Project layout**

Assume you created a project:
```text
web
├── Cargo.toml
├── index.html
├── pkg
│   ├── package.json
│   ├── web_bg.wasm
│   ├── web_bg.wasm.d.ts
│   ├── web.d.ts
│   └── web.js
└── src
    └── lib.rs
```
We will build a `cdylib` wasm package that `wasm-pack` will wrap into `pkg/`.

**2)** `Cargo.toml`**(example)**

Put this into `web/Cargo.toml`.
```toml
[package]
name = "web"
version = "0.1.0"
edition = "2021" # Important: Stable standard for Wasm/Polars. Don't upgrade to 2024 yet to avoid toolchain conflicts.

# Produce a cdylib for wasm

[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "0.2"
polars = { version = "0.49", default-features = false }
# Avoids transitive mio dependency to ensure Wasm compatibility.

polars-io = { version = "0.49", default-features = false, features = ["parquet"] }
charton = { version = "0.2" }

[profile.release]
opt-level = "z"  # or "s" to speed up
lto = true
codegen-units = 1
panic = "abort"
```

**3)** `src/lib.rs`**-Rust (wasm entry points)**

Create `web/src/lib.rs`.
```rust
use wasm_bindgen::prelude::*;
use polars::prelude::*;
use charton::prelude::*;

// Build a small scatter plot and return the SVG string.
#[wasm_bindgen]

pub fn draw_chart() -> Result<String, JsValue> {
    // Create a tiny DataFrame
    let df = df![
        "length" => [5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9],
        "width" => [3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1]
    ].map_err(|e| JsValue::from_str(&e.to_string()))?;

    // Build a Charton Chart
    let scatter = Chart::build(&df)
        .map_err(|e| JsValue::from_str(&e.to_string()))?
        .mark_point()
        .encode((x("length"), y("width")))
        .map_err(|e| JsValue::from_str(&e.to_string()))?;

    let chart = LayeredChart::new().add_layer(scatter);

    let svg = chart.to_svg()
        .map_err(|e| JsValue::from_str(&e.to_string()))?; // Returns SVG string

    Ok(svg)
}
```
Key points:

- `#[wasm_bindgen]` exposes functions to JS.
- We return `Result<String, JsValue>` so JS receives errors as exceptions.

**4) Build with** `wasm-pack` **(recommended)**

From project root (`web/`):
```bash
wasm-pack build --release --target web --out-dir pkg
```
`wasm-pack` will:
- compile to `wasm32-unknown-unknown`,
- run `wasm-bindgen` to generate JS wrapper(s),
- produce a `pkg/` folder containing:

    - `web_bg.wasm`
    - `web_bg.wasm.d.ts`
    - `web.d.ts`
    - `web.js` (ES module bootstrap)
> 💡**Optimization Note: Binary Size**

> After building in `--release` mode, the resulting `web_bg.wasm` is approximately **4 MB**. However, for web production:
> - **Gzip compression** reduces it to about **900 KB**.
> - **Brotli compression** can shrink it even further.
> This compact footprint makes it highly suitable for browser-side data processing without long loading times.

**5) Creating `index.html` (Client-Side Loader)**

The final step is to create a minimal HTML file (`web/index.html`) that loads the generated WASM module and renders the SVG chart into the page.
```html
<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Charton WASM Demo</title>
</head>
<body>
    <div id="chart-container"></div>

    <script type="module">
        import init, { draw_chart } from './pkg/web.js';

        async function run() {
            // Initialize and load the WebAssembly module
            await init();

            // Call the Rust function that returns an SVG string
            const svg = draw_chart();

            // Insert the SVG into the page
            document.getElementById("chart-container").innerHTML = svg;
        }

        run();
    </script>
</body>
</html>
```
This minimal version:
- Loads the WASM module generated by `wasm-pack`
- Calls the Rust function `draw_chart()` to generate the SVG string
- Injects the SVG directly into the DOM
- Contains no additional CSS, error handling, or panic hooks — keeping the example simple and focused

This is the recommended simplest setup for demonstrating Charton rendering through WebAssembly.

**6) Serve the folder**

Browsers enforce CORS for WASM; open the page via HTTP server rather than `file://`.

Minimal options:
```bash
cd web
python -m http.server 8080
```
Then open http://localhost:8080/index.html and you'll see the chart in the browser:
![wasm](../assets/wasm1.png)

**7) Troubleshooting**

Processing heavy libraries like Polars in WASM can strain your system. Here is how to handle common bottlenecks:
- **Compilation Hangs/Freezes:** Building Polars for WASM is extremely CPU and RAM intensive. If your computer "freezes" during the `Optimizing with wasm-opt` stage, you can manually stop the process. The compiled `.wasm` file in `pkg/` is usually already functional; it will simply be larger in size without the final optimization. For a smooth experience, a machine with high-core counts and 16GB+ RAM is recommended.
- **wasm-opt Errors:** If `wasm-pack` fails because it cannot install or run `wasm-opt`, you can simply ignore the error if the `pkg/` folder was already populated. The unoptimized WASM file will still run in the browser.
- **Polars Version Incompatibility:** If your project requires a Polars version uncompatible with the one used by Charton, passing a DataFrame directly will cause a compilation error. In this case, you can use the Parquet Interoperability method described in Section 2.3.4.

### 9.2.4 Charton + Polars + wasm-bindgen — advanced example: dynamic CSV visualization

**Goal:** Beyond a static demo, we now build a functional tool: users upload a local CSV file (e.g., `iris.csv`, which can be found and downloaded from the `datasets/` folder in this project) → JavaScript reads it as a string → Rust/Polars parses the data in-browser → Charton generates a multi-colored scatter plot → The resulting SVG is rendered instantly.

**1) Updated** `Cargo.toml`
**Update Note:** This file updates the dependencies from 9.2.2 by enabling the `csv` feature in polars-io (to handle user uploads) and switching to the `charton` crate for more advanced encoding.
```toml
[package]
name = "web"
version = "0.1.0"
edition = "2021" # Important: Stable standard for Wasm/Polars. Don't upgrade to 2024 yet to avoid toolchain conflicts.

# Produce a cdylib for wasm

[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "0.2"
polars = { version = "0.49", default-features = false }
# Avoids transitive mio dependency to ensure Wasm compatibility.

polars-io = { version = "0.49", default-features = false, features = ["parquet", "csv"] }
charton = { version = 0.2.1 }

[profile.release]
opt-level = "z"  # or "s" to speed up
lto = true
codegen-units = 1
panic = "abort"
```
**2) Updated** `src/lib.rs`
**Update Note:** This replaces the hard-coded `draw_chart` from 9.2.2. The new `draw_chart_from_csv` function accepts a `String` from JavaScript and uses `std::io::Cursor` to treat that string as a readable file stream for Polars.
```rust
use wasm_bindgen::prelude::*;
use polars::prelude::*;
use charton::prelude::*;
use std::io::Cursor;

#[wasm_bindgen]

pub fn draw_chart_from_csv(csv_content: String) -> Result<String, JsValue> {
    /* * 1. Parse CSV data from String.
     * We use a Cursor to treat the String as a readable stream for Polars.
     */
    let cursor = Cursor::new(csv_content);

    /* * 2. Initialize the Polars DataFrame.
     * CsvReader is highly optimized but runs in a single thread in standard WASM.
     */
    let df = CsvReader::new(cursor)
        .finish()
        .map_err(|e| JsValue::from_str(&format!("Polars Error: {}", e)))?;

    /* * 3. Construct the Scatter Plot.
     * Ensure that the columns "length" and "width" exist in your CSV file.
     */
    let scatter = Chart::build(&df)
        .map_err(|e| JsValue::from_str(&e.to_string()))?
        .mark_point()
        .encode((x("sepal_length"), y("sepal_width"), color("species")))
        .map_err(|e| JsValue::from_str(&e.to_string()))?;

    /* * 4. Wrap the scatter plot into a LayeredChart and generate SVG.
     * The to_svg() method returns a raw XML string representing the vector graphic.
     */
    let chart = LayeredChart::new().add_layer(scatter);

    let svg = chart.to_svg()
        .map_err(|e| JsValue::from_str(&e.to_string()))?;

    Ok(svg)
}
```
**3) Updated** `index.html`
**Update Note:** This expands the simple loader from 9.2.2 by adding a File Input UI and a `FileReader` event loop. This allows the WASM module to process "live" data provided by the user.
```html
<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WASM CSV Visualizer</title>
    <style>
        #chart-container { margin-top: 20px; border: 1px solid #ccc; }
    </style>
</head>
<body>
    <h2>Upload CSV to Generate Chart</h2>
    <input type="file" id="csv-upload" accept=".csv" />
    
    <div id="chart-container"></div>

    <script type="module">
        import init, { draw_chart_from_csv } from './pkg/web.js';

        async function run() {
            // Initialize the WASM module
            await init();

            const fileInput = document.getElementById("csv-upload");
            const container = document.getElementById("chart-container");

            // Event listener for file selection
            fileInput.addEventListener("change", async (event) => {
                const file = event.target.files[0];
                if (!file) return;

                /* * Use FileReader to read the file content as text.
                 * This text is then passed across the JS-WASM boundary.
                 */
                const reader = new FileReader();
                reader.onload = (e) => {
                    const csvContent = e.target.result;

                    try {
                        // Call the Rust function with the CSV string
                        const svg = draw_chart_from_csv(csvContent);
                        
                        // Inject the returned SVG string directly into the DOM
                        container.innerHTML = svg;
                    } catch (err) {
                        console.error("Computation Error:", err);
                        alert("Error: Make sure CSV has 'length' and 'width' columns.");
                    }
                };
                
                // Trigger the file read
                reader.readAsText(file);
            });
        }

        run();
    </script>
</body>
</html>
```
**4) Build and Serve Update Note:** The build command remains the same as 9.2.3, but the compilation time may increase due to the added CSV and color encoding features.
```bash
# Build the package
wasm-pack build --release --target web --out-dir pkg

# Serve the files

python -m http.server 8080
```
**Summary of Improvements over 9.2.3**
- **Data Handling:** Shifted from static `df!` macros to dynamic `CsvReader` parsing.
- **Complexity:** Added `color` encoding in Charton to demonstrate multi-dimensional data mapping.
- **User Interaction:** Introduced the `FileReader` API to bridge the gap between the local file system and WASM linear memory.

### 9.2.5 Conclusion

The combination of *static* SVG and *dynamic* Rust/Wasm computation forms a powerful model for interactive visualization:
- SVG provides simple, portable output for embedding and styling.
- Rust/Wasm enables high-performance chart recomputation.
- Polars accelerates data transformations dramatically.
- Browser handles final rendering efficiently.

**Charton does not attempt to patch SVGs with JavaScript like traditional libraries. Instead, it regenerates a complete static SVG—fast enough to support real-time interactivity.**

This architecture makes high-performance, browser-based interaction not only possible but highly efficient.

## 9.3 True interactive visualization via the Altair backend

Charton can generate fully interactive charts by delegating to **Altair**, which compiles to Vega-Lite specifications capable of:
- Hover tooltips
- Selections
- Brush interactions
- Zoom and pan
- Linked views
- Filtering and conditional styling
- Rich UI semantics

**Charton’s role in this workflow**

Charton does:
1. Run Rust-side preprocessing (Polars)
2. Transfer data to Python
3. Embed user-provided Altair plotting code
4. Invoke Python to generate Vega-Lite JSON
5. Display the result (browser/Jupyter) or export JSON

All *actual* interactivity comes from **Altair/Vega-Lite**, not from Charton.

**Example: interactive Altair chart via Charton**
```rust
:dep charton = { version="0.2.1" }
:dep polars = { version="0.49.1" }

use charton::prelude::*;
use polars::prelude::df;

let exe_path = r"D:\Programs\miniconda3\envs\cellpy\python.exe";

let df1 = df![
    "Model" => ["S1", "M1", "R2", "P8", "M4", "T5", "V1"],
    "Price" => [2430, 3550, 5700, 8750, 2315, 3560, 980],
    "Discount" => [Some(0.65), Some(0.73), Some(0.82), None, Some(0.51), None, Some(0.26)],
].unwrap();

// Any valid Altair code can be placed here.
let raw_plotting_code = r#"
import altair as alt

chart = alt.Chart(df1).mark_point().encode(
    x='Price',
    y='Discount',
    color='Model',
    tooltip=['Model', 'Price', 'Discount']
).interactive()        # <-- zoom + pan + scroll
"#;

Plot::<Altair>::build(data!(&df1)?)?
    .with_exe_path(exe_path)?
    .with_plotting_code(raw_plotting_code)
    .show()?;  // Jupyter or browser
```

This provides **real interactivity** entirely through Altair.

## 9.4 Exporting Vega-Lite JSON for browser/Web app usage

Since Altair compiles to Vega-Lite, Charton can generate the JSON specification directly.

This is ideal for:
- Web dashboards
- React / Vue / Svelte components
- Embedding charts in HTML
- APIs returning visualization specs
- Reproducible visualization pipelines

**Example: Export to JSON**
```rust
let chart_json: String = Plot::<Altair>::build(data!(&df1)?)?
    .with_exe_path(exe_path)?
    .with_plotting_code(raw_plotting_code)
    .to_json()?;

// save, embed, or send via API
println!("{}", chart_json);
```

**Embedding in a webpage**:
```html
<div id="vis"></div>
<script>
  var spec = /* paste JSON here */;
  vegaEmbed('#vis', spec);
</script>
```

## 9.5 Summary: What kinds of interactivity does Charton support?

| **Feature**                                          | **Supported?** | **Provided by**         |
| ---------------------------------------------------- | ----------     | ----------------------- |
| Hover tooltips                                       | ✔ Yes         | Altair/Vega-Lite        |
| Selection / brushing                                 | ✔ Yes         | Vega-Lite               |
| Zoom / pan                                           | ✔ Yes         | Altair `.interactive()` |
| Dynamic UI-driven filtering                          | ✔ Yes         | Vega-Lite               |
| Inline static charts in Jupyter                      | ✔ Yes         | Charton SVG via `evcxr`  |
| True reactive Rust-side charts (recompute on events) | ❌ No         ||
| Charton-native browser interactivity                  | ❌ No         ||

**When to use which mode?**
| **Use Case**                    | **Recommended Mode**                           |
| ------------------------------- | ----------------------------------------------- |
| Fast feedback in Rust           | Jupyter + `evcxr` static SVG                    |
| Publication-quality plots       | Native Charton SVG                               |
| Hover/tooltip/zoom              | Altair backend                                  |
| Web dashboards or JS frameworks | Export Vega-Lite JSON                           |
| Rust/WASM interactive apps      | Use Charton as SVG generator + custom WASM logic |

# Chapter 10 · Performance Optimization


Charton is optimized for large datasets and high-performance workflows, especially when paired with Polars.  
This chapter explains best practices for handling large data, minimizing memory usage, improving rendering speed, and leveraging lazy evaluation.  
It offers guidelines for scaling your visualizations to millions of data points without sacrificing responsiveness.  
By applying these techniques, you can confidently use charton in production-scale environments.

### 10.1 Polars + charton performance pipeline

How Polars lazy engine and charton complement each other.

### 10.2 Handling large datasets

Sampling, binning, aggregation strategies.

### 10.3 Rendering performance

When to use native vs external backends.

### 10.4 Memory management

Strategies to avoid unnecessary copies and overhead.

### 10.5 Parallel or multi-stage pipelines

Leverage Rust concurrency for performance gains.

---

# Chapter 11 · Extensibility, Troubleshooting & Community


Charton is designed to be extensible.  
This final chapter shows how to create custom marks, define new encodings, extend palettes, and contribute to the project.  
It also includes a comprehensive troubleshooting guide, FAQs, and links to community resources.  
After reading this chapter, you will be fully equipped not only to use charton, but also to extend and improve it—joining the ecosystem as a contributor.

## 11.1 Creating custom marks

Implementing new mark types.

## 11.2 Adding new encodings

Defining custom encoding channels.

## 11.3 Extending themes and palettes

Building reusable visual themes.

## 11.4 Troubleshooting common issues

**Q: Why do I get this error?**
`the trait bound `InputData: From<(&str, &DataFrame)>` is not satisfied`
or:
`the trait bound `InputData: From<(&str, &LazyFrame)>` is not satisfied`

**A**: Because your Polars version is not compatible with the Polars version used inside Charton, so the types cannot be converted. You have two options:
- Use a Polars version that matches the version required by Charton.
- If you don’t want to change versions, write your DataFrame to a Parquet file and let Charton read it (see Section 2.4.4).

## 11.5 FAQ

Answers to common user questions.

## 11.6 Contributing to charton

Code structure, contribution guidelines, and development workflow.

## 11.7 Community & resources

Links to docs, repo, issues, examples, and community channels.

# Chapter 12 · Layout

Layout in Charton is more than just positioning elements; it is a mathematical negotiation between the **shape of your data** and the **geometry of the coordinate system**. While many libraries rely on hard-coded logic to switch between grouped and single-column charts, Charton uses a unified strategy to handle all positioning.

## 12.1. The Row-Stub Layout Strategy

The **Row-Stub Layout Strategy** is the core engine behind how Charton decides the width, spacing, and offset of marks (bars, sectors, or boxes). Instead of asking "Is this a grouped chart?", the renderer asks: "**How many rows of data exist for this specific coordinate?**"

### 12.1.1. The Philosophy: Data Shapes Geometry

In the Row-Stub model, the "physical" presence of a data row in the transformed DataFrame acts as a "stub" or a placeholder in the visual layout.
* **Single Row per Category**: If a category (e.g., "Category A" on the X-axis) contains exactly one row, the mark occupies the **full available span**.
* **Multiple Rows per Category**: If a category contains $N$ rows, the layout engine automatically carves the available space into $N$ sub-slots.

### 12.1.2. The Mechanism: Cartesian Normalization

To ensure consistent layouts, Charton performs a **Cartesian Product** during the data transformation phase. If you encode both `x` and `color`, Charton ensures that every unique value of `x` has a row for every unique value of `color`.

1. **Gap Filling**: If "Category A" has data for "Male" but not "Female," Charton inserts a "Female" row with a value of `0`.
2. **Stable Count**: This ensures that every X-axis slot has the exact same number of rows ($N$).
3. **Implicit Positioning**: he renderer simply iterates through these $N$ rows. The $i$-th row is automatically placed at the $i$-th sub-slot.

### 12.1.3. Dimension Deduplication: Intent Recognition

The layout engine must first distinguish between **visual aesthetics** (just adding color) and **structural dimensions** (splitting data into groups). Charton achieves this through **Automatic Column Deduplication** during the preprocessing phase.

Before the Row-Stub engine calculates $N$ (the number of rows per slot), it performs the following check:

1. **The Dimension Set**: Charton collects all fields used in `x`, `color`, `size`, and `shape`.
2. **Deduplication**: If a field is used in both a positional channel (like `x`) and a styling channel (like `color`), it is only counted **once** as a grouping dimension.
3. **Intent Recognition**:
- `x("type"), color("type")`: After deduplication, there is only **one** grouping dimension (`type`). The engine recognizes this as a **Self-Mapping** intent—use colors to distinguish categories, but keep them in a single, full-width slot.
- `x("type"), color("gender")`: There are **two** distinct dimensions. The engine recognizes this as a **Grouping** intent—sub-divide each `type` slot by `gender`.

Without this deduplication step, a Rose Chart would mistakenly try to "dodge" (place side-by-side) the same category against itself, leading to overlapping marks or unnecessarily thin sectors.

### 12.1.4. The Mechanism: Cartesian Normalization

To ensure consistent layouts across all categories, Charton performs a **Cartesian Product** based on the *deduplicated* dimension set.

1. **Grid Creation**: If `x` has 5 unique values and `color` (a different field) has 2, Charton creates a "Layout Grid" of $5 \times 2 = 10$ rows.
2. **Gap Filling**: If "Category A" has data for "Male" but not "Female," Charton joins the grid with the raw data and inserts a "Female" row with a value of `0`.
3. **Physical Alignment**: This ensures that every X-axis slot has the exact same number of physical row stubs ($N=2$).
4. **Predictable Offsets**: The renderer simply iterates through these $N$ rows. The $i$-th row is always placed at the $i$-th sub-slot, ensuring that "Male" is always the left bar and "Female" is always the right bar, even if the data for one is missing.

### 12.1.5. Mathematical Resolution

The physical width of a mark in normalized space is calculated using the following derived formula:
$$\text{Mark Width} = \frac{\text{Slot Span}}{N + (N - 1) \times \text{Spacing}}$$
Where:
- **$N$**: The number of deduplicated row stubs for that category.
- **Slot Span**: The total percentage of the category width used (default 0.7-1.0).
- **Spacing**: The inner padding between bars within the same group.

### 12.1.6. Advantages of Row-Stub Layout

1. **Consistency**: Bars never "jump" positions if data is missing; the "0" value row keeps the slot occupied.
2. **Polar-Cartesian Parity**: The same logic that creates side-by-side bars in Cartesian coordinates creates perfectly partitioned sectors in a Rose Chart.
3. **Zero Hard-coding**: The renderer doesn't need to know if the chart is "Grouped" or "Stacked"—it simply follows the rows provided by the deduplicated data engine.

## 12.2. The Legend Layout Strategy

## 12.3 The Axis Layout Strategy











# Chapter 13 · Coordinate Systems

## 13.1 Cartesian

## 13.2 Polar