ferrochart-core 0.3.0

Platform-independent core of the FerroChart financial charting engine
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
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
//! `Chart`: the public object that owns data, scales and layout and builds frames.
//!
//! Scope so far: any number of panes holding series of the built-in
//! kinds. All series share one time axis, the union of their times;
//! volume series read the volume column of an OHLC series.
//!
//! `build_frame` is a hot path: once the frame pools and scratch buffers have
//! warmed up, panning, zooming and real-time updates do not allocate.
//!

use crate::data::lod::{bucket_of, bucket_start};
use core::any::Any;

use crate::data::{
    BandId, BarUpdate, ChangeLog, Column, DataChange, DataDelta, DrawingId, IdxSpan, IndicatorId,
    LayerPluginId, LogicalIndex, OhlcLevelView, PaneId, PriceLineId, SeriesId, TimeIndex,
    Timestamp, ValidationMode,
};
use crate::drawing::{DrawingCtx, DrawingManager, FrameBuilder};
use crate::error::{Error, Result};
use crate::format::NumberFormat;
use crate::format::auto_decimals;
use crate::frame::{
    CustomDraw, DrawCmd, FontId, FontStyleRef, Frame, Invalidation, LabelBg, LayerId, LineDash,
    LineInst, Reveal, SeriesDraw, SeriesKind, SeriesStyle, StrokeStyle, TextAlign, TextRun,
    XTransform,
};
use crate::geom::{Css, PointCss, PointPx, Px, RectPx, Rgba, SizeCss, SizePx, Span};
use crate::interaction::{CrosshairMode, Drag, Interaction};
use crate::pane::{PRICE_MARGINS, Pane, PaneLayout, PaneOptions, SEPARATOR_PX, stack};
use crate::plugin::{
    AxisMode, CustomChange, CustomSeries, CustomStore, Indicator, IndicatorInput, IndicatorOutputs,
    LayerCtx, LayerPlugin, LayerPosition, OutputKind, Placement, SeriesCtx, SeriesTransform,
};
use crate::price_scale::{Normalize, PriceScale, ScaleSide};
use crate::series::{Op, Store, is_ohlc_kind, is_value_kind};
use crate::sync::{SyncMessage, SyncState};
use crate::text::{StackStr, TextMeasurer, text_width};
use crate::theme::Theme;
use crate::time_scale::{
    TimeScale, TimeTick, TimeZoneProvider, Utc, collect_ticks, format_crosshair_time, format_tick,
};
use crate::transform::store::{TransformStore, input_columns};

/// Options of a new chart.
///
/// # Examples
///
/// ```
/// use ferrochart_core::{Chart, ChartOptions};
/// use ferrochart_core::theme::Theme;
///
/// let chart = Chart::new(ChartOptions { theme: Theme::dark(), ..ChartOptions::default() });
/// # let _ = chart;
/// ```
pub struct ChartOptions {
    /// Colors and sizes.
    pub theme: Theme,
    /// Time zone for time labels.
    pub time_zone: Box<dyn TimeZoneProvider>,
    /// Handling of bars that violate the OHLC constraints.
    pub ohlc_validation: ValidationMode,
    /// Initial bar spacing.
    pub bar_spacing: Css,
    /// Lower bound of the bar spacing (further limited by the data size).
    pub min_bar_spacing: Css,
    /// Upper bound of the bar spacing.
    pub max_bar_spacing: Css,
    /// Initial empty space right of the last bar, in bars.
    pub right_offset: f64,
}

impl Default for ChartOptions {
    fn default() -> Self {
        Self {
            theme: Theme::light(),
            time_zone: Box::new(Utc),
            ohlc_validation: ValidationMode::Reject,
            bar_spacing: Css(6.0),
            min_bar_spacing: Css(0.5),
            max_bar_spacing: Css(50.0),
            right_offset: 3.0,
        }
    }
}

pub use crate::series::SeriesData;

/// Options of a series. Colors left at `None` come from the theme.
///
/// # Examples
///
/// ```
/// use ferrochart_core::chart::SeriesOptions;
/// use ferrochart_core::data::SeriesId;
/// use ferrochart_core::geom::{Css, Rgba};
///
/// let opts = SeriesOptions::volume_of(SeriesId(0));
/// assert_eq!(opts.source, Some(SeriesId(0)));
///
/// let line = SeriesOptions {
///     color: Some(Rgba::hex(0x00FF_6D00)),
///     line_width: Css(1.0),
///     ..SeriesOptions::default()
/// };
/// # let _ = line;
/// ```
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SeriesOptions {
    /// Decimal places of price labels; `None` (the default) is automatic:
    /// at least 2 decimals and 4 significant digits of the visible values,
    /// with a subscript count of zeros for small prices (`0.0â‚„1234`).
    pub price_precision: Option<u8>,
    /// For `SeriesKind::Volume`: the OHLC series whose volume is drawn.
    pub source: Option<SeriesId>,
    /// Main color: the line of `Line`/`Area`, the bars of `Histogram`, up
    /// candles and bars, the line of `Baseline` above its base.
    pub color: Option<Rgba>,
    /// Down candles and bars; the line of `Baseline` below its base.
    pub down_color: Option<Rgba>,
    /// Fill at the top of an `Area`; fill above the base of a `Baseline`.
    pub top_fill: Option<Rgba>,
    /// Fill at the bottom of an `Area`; fill below the base of a `Baseline`.
    pub bottom_fill: Option<Rgba>,
    /// Line width of `Line`, `Area` and `Baseline`.
    pub line_width: Css,
    /// Dash pattern of the line.
    pub line_style: LineDash,
    /// Step line: horizontal to the next point's time, then vertical.
    pub step: bool,
    /// Base value of `Baseline` (fill boundary) and `Histogram` (bar base).
    pub base_value: f64,
    /// Label with the last value on the price axis. Not drawn for
    /// volume series.
    pub last_value_visible: bool,
    /// Dotted horizontal line at the last value. Not drawn for volume
    /// series.
    pub price_line_visible: bool,
    /// Display shift in bars: the point at logical index `j` is drawn at
    /// `j + shift` (Ichimoku's leading spans and lagging line).
    /// Autoscale and the crosshair follow the drawn positions.
    pub shift: i32,
}

impl Default for SeriesOptions {
    fn default() -> Self {
        Self {
            price_precision: None,
            source: None,
            color: None,
            down_color: None,
            top_fill: None,
            bottom_fill: None,
            line_width: Css(2.0),
            line_style: LineDash::Solid,
            step: false,
            base_value: 0.0,
            last_value_visible: true,
            price_line_visible: true,
            shift: 0,
        }
    }
}

/// Colors of a band between two series.
///
/// # Examples
///
/// ```
/// use ferrochart_core::chart::{BandOptions, SeriesData, SeriesOptions};
/// use ferrochart_core::data::ValueColumns;
/// use ferrochart_core::frame::SeriesKind;
/// use ferrochart_core::geom::Rgba;
/// use ferrochart_core::{Chart, ChartOptions};
///
/// let mut chart = Chart::new(ChartOptions::default());
/// let pane = chart.main_pane();
/// let a = chart.add_series(pane, SeriesKind::Line, SeriesOptions::default())?;
/// let b = chart.add_series(pane, SeriesKind::Line, SeriesOptions::default())?;
/// let line = |v: Vec<f64>| SeriesData::Value(ValueColumns { time: vec![0, 60_000, 120_000], value: v, color: None });
/// chart.set_data(a, line(vec![2.0, 3.0, 1.0]))?;
/// chart.set_data(b, line(vec![1.0, 1.0, 2.0]))?;
/// // Green where a is above b, red where it is below.
/// let band = chart.add_band(a, b, BandOptions {
///     fill: Some(Rgba::hex(0x0026_A69A).with_alpha(0x40)),
///     fill_crossed: Some(Rgba::hex(0x00EF_5350).with_alpha(0x40)),
/// })?;
/// assert!(chart.band_options(band).is_some());
/// chart.remove_band(band)?;
/// # Ok::<(), ferrochart_core::Error>(())
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct BandOptions {
    /// Fill where the upper series is at or above the lower one; `None`: the
    /// upper series' color at 15% opacity.
    pub fill: Option<Rgba>,
    /// Fill where the upper series is below the lower one; `None`: as `fill`.
    pub fill_crossed: Option<Rgba>,
}

/// Whether a series is drawn on the pane's volume scale: volume series and
/// lines placed with `Placement::Volume`.
fn volume_like(e: &SeriesEntry) -> bool {
    e.kind == SeriesKind::Volume || e.volume_scale
}

/// A marker drawn in the last frame, for click hit tests.
#[derive(Clone, Copy, Debug)]
struct MarkerHit {
    series: SeriesId,
    index: usize,
    pane: PaneId,
    pos: PointPx,
    radius: f32,
}

/// Hit tolerance of lines (CSS px).
const HIT_TOLERANCE: f32 = 4.0;

/// Distance from `p` to the segment `a`–`b`.
fn segment_distance(p: PointPx, a: PointPx, b: PointPx) -> f32 {
    let (dx, dy) = (b.x - a.x, b.y - a.y);
    let len2 = dx * dx + dy * dy;
    let t = if len2 > 0.0 {
        (((p.x - a.x) * dx + (p.y - a.y) * dy) / len2).clamp(0.0, 1.0)
    } else {
        0.0
    };
    (p.x - (a.x + dx * t)).hypot(p.y - (a.y + dy * t))
}

/// A band.
#[derive(Clone, Copy, Debug)]
struct BandEntry {
    id: BandId,
    upper: SeriesId,
    lower: SeriesId,
    opts: BandOptions,
}

/// A horizontal line at a price of a series, with an optional label on the
/// price axis.
///
/// # Examples
///
/// ```
/// use ferrochart_core::chart::{PriceLineOptions, SeriesOptions};
/// use ferrochart_core::frame::SeriesKind;
/// use ferrochart_core::{Chart, ChartOptions};
///
/// let mut chart = Chart::new(ChartOptions::default());
/// let id = chart.add_series(chart.main_pane(), SeriesKind::Line, SeriesOptions::default())?;
/// let stop = chart.create_price_line(id, PriceLineOptions {
///     price: 95.0,
///     title: "stop".into(),
///     ..PriceLineOptions::default()
/// })?;
/// chart.remove_price_line(stop)?;
/// # Ok::<(), ferrochart_core::Error>(())
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct PriceLineOptions {
    /// Price, in the series' units.
    pub price: f64,
    /// Line and label color; `None` uses the series' main color.
    pub color: Option<Rgba>,
    /// Line width.
    pub line_width: Css,
    /// Dash pattern.
    pub line_style: LineDash,
    /// Whether the price is labelled on the price axis.
    pub axis_label_visible: bool,
    /// Text drawn above the line at the right edge of the plot; empty for none.
    pub title: String,
}

impl Default for PriceLineOptions {
    fn default() -> Self {
        Self {
            price: 0.0,
            color: None,
            line_width: Css(1.0),
            line_style: LineDash::Dashed,
            axis_label_visible: true,
            title: String::new(),
        }
    }
}

/// Where a [`Marker`] sits relative to its bar.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum MarkerPosition {
    /// Above the bar's high (above the value for single-value series).
    #[default]
    AboveBar,
    /// Below the bar's low.
    BelowBar,
}

/// Shape of a [`Marker`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum MarkerShape {
    /// Filled circle.
    #[default]
    Circle,
    /// Filled square.
    Square,
    /// Arrow pointing up.
    ArrowUp,
    /// Arrow pointing down.
    ArrowDown,
}

impl MarkerShape {
    fn icon(self) -> u16 {
        use crate::transform_helpers::{ICON_ARROW_DOWN, ICON_ARROW_UP, ICON_CIRCLE, ICON_SQUARE};
        match self {
            Self::Circle => ICON_CIRCLE,
            Self::Square => ICON_SQUARE,
            Self::ArrowUp => ICON_ARROW_UP,
            Self::ArrowDown => ICON_ARROW_DOWN,
        }
    }
}

/// An icon with optional text above or below a bar, e.g. a buy or sell
/// signal. Anchored by time; markers on the same bar
/// and side stack away from it in the order given.
///
/// # Examples
///
/// ```
/// use ferrochart_core::chart::{Marker, MarkerPosition, MarkerShape, SeriesOptions};
/// use ferrochart_core::frame::SeriesKind;
/// use ferrochart_core::{Chart, ChartOptions};
///
/// let mut chart = Chart::new(ChartOptions::default());
/// let id = chart.add_series(chart.main_pane(), SeriesKind::Candlestick, SeriesOptions::default())?;
/// chart.set_markers(id, vec![Marker {
///     time: 1_700_000_000_000,
///     position: MarkerPosition::BelowBar,
///     shape: MarkerShape::ArrowUp,
///     text: "buy".into(),
///     ..Marker::default()
/// }])?;
/// assert_eq!(chart.markers(id).map(<[Marker]>::len), Some(1));
/// # Ok::<(), ferrochart_core::Error>(())
/// ```
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Marker {
    /// Time of the bar. A time that is not on the time axis snaps to the next
    /// bar (the last one for times after the end).
    pub time: Timestamp,
    /// Above or below the bar.
    pub position: MarkerPosition,
    /// Icon.
    pub shape: MarkerShape,
    /// Icon and text color; `None` uses the series' main color.
    pub color: Option<Rgba>,
    /// Text next to the icon, on the side away from the bar; empty for none.
    pub text: String,
}

/// Marker icon size and gap to the bar or the next marker, in CSS pixels.
const MARKER_SIZE: f32 = 10.0;
const MARKER_GAP: f32 = 4.0;

#[derive(Clone, Debug)]
struct PriceLineEntry {
    id: PriceLineId,
    series: SeriesId,
    opts: PriceLineOptions,
}

/// A label on the price axis (last values, price lines).
#[derive(Clone, Copy, Debug)]
struct AxisLabel {
    /// Wanted center y.
    want: f32,
    /// Placed center y.
    at: f32,
    text: Span,
    bg: Rgba,
    fg: Rgba,
}

impl SeriesOptions {
    /// Options of a volume series that draws `source`'s volume column.
    pub fn volume_of(source: SeriesId) -> Self {
        Self {
            source: Some(source),
            ..Self::default()
        }
    }
}

/// Non-fatal problems reported through [`ChartEvent::Warning`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum WarningKind {
    /// `ValidationMode::Repair` widened high/low of `bars` bars.
    OhlcRepaired {
        /// The series.
        series: SeriesId,
        /// Number of repaired bars.
        bars: usize,
    },
    /// Times of `series` fell between bars of the time axis, so the axis was
    /// rebuilt as the union of all series' times and the logical
    /// indices after the first inserted bar moved.
    TimeAxisMerged {
        /// The series whose data was added.
        series: SeriesId,
        /// Number of bars inserted between existing ones.
        slots: usize,
    },
    /// The data of `series` could not be moved onto a rebuilt time axis (out
    /// of memory) and was cleared.
    DataDropped {
        /// The series.
        series: SeriesId,
    },
}

/// OHLCV values of one bar.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BarValues {
    /// Open.
    pub open: f64,
    /// High.
    pub high: f64,
    /// Low.
    pub low: f64,
    /// Close.
    pub close: f64,
    /// Volume, if the series has one.
    pub volume: Option<f64>,
}

/// Where the crosshair points.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CrosshairPoint {
    /// Logical index of the bar under the crosshair (may be outside the data).
    pub logical_index: i64,
    /// Time of that bar, `None` in the empty space beyond the data.
    pub time: Option<Timestamp>,
    /// The pane under the pointer.
    pub pane: PaneId,
    /// Price at the crosshair's y, on that pane's price scale.
    pub price: f64,
    /// Values of the first OHLC series that has a bar there.
    pub bar: Option<BarValues>,
}

/// What a click hit: the first in priority order (drawing, marker, series).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ClickTarget {
    /// A drawing (its body or a handle).
    Drawing(DrawingId),
    /// Marker `index` of `series`, in the time order [`Chart::markers`]
    /// returns them.
    Marker {
        /// The series the marker belongs to.
        series: SeriesId,
        /// Its index in [`Chart::markers`].
        index: usize,
    },
    /// A series: a candle's body or wick, within 4 CSS px of a line, inside
    /// a histogram bar, or on a dot.
    Series(SeriesId),
}

/// Events for the host, pulled with [`Chart::drain_events`].
///
/// Pending `CrosshairMove` and `VisibleRangeChanged` events are coalesced: if
/// the host has not drained the previous one, it is replaced, so the queue
/// does not grow (and does not allocate) while the user interacts.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub enum ChartEvent {
    /// The crosshair moved; `None` when it left the plot or is hidden.
    CrosshairMove {
        /// The new position.
        point: Option<CrosshairPoint>,
    },
    /// A press and release in a plot without moving more than the drag
    /// threshold (3 CSS px); not sent while a drawing is being created.
    Click {
        /// Where, in CSS pixels from the chart's top left.
        pos: PointCss,
        /// The pane clicked.
        pane: PaneId,
        /// Logical index at `pos` (fractional).
        logical_index: f64,
        /// Time of the bar nearest to `logical_index`; `None` beyond the data.
        time: Option<Timestamp>,
        /// Price at `pos`, on the pane's price scale.
        price: f64,
        /// What was clicked; `None` for empty space.
        target: Option<ClickTarget>,
    },
    /// The visible logical range changed.
    VisibleRangeChanged {
        /// Left edge.
        from: LogicalIndex,
        /// Right edge.
        to: LogicalIndex,
    },
    /// The left edge of the view is within one screen of the first bar: the
    /// host should `prepend` older data. Sent once per first-bar position.
    NeedMoreHistory {
        /// Bars between the first bar and the left edge (negative when the
        /// view already shows empty space before the data).
        bars_before_start: i64,
    },
    /// A drawing was created (by the user, `DrawingManager::add`, or an
    /// undo/redo that brought it back).
    DrawingCreated(DrawingId),
    /// A drawing was moved or restyled.
    DrawingModified(DrawingId),
    /// A drawing was removed.
    DrawingRemoved(DrawingId),
    /// The selection changed.
    DrawingSelected(Option<DrawingId>),
    /// A non-fatal problem.
    Warning(WarningKind),
    /// The chart posted a message to its sync group. Hosts that sync
    /// charts across workers forward it with [`Chart::apply_sync`]; charts
    /// in the same [`SyncGroup`](crate::sync::SyncGroup) need nothing.
    Sync(SyncMessage),
    /// The chart sent drawing sync operations; hosts that forward
    /// them to charts in other workers take them with
    /// [`Chart::take_drawing_sync_ops`]. Reported once until they are taken.
    DrawingSync,
}

/// Rectangles of the last built layout, in physical pixels. Per-pane
/// rectangles are available from [`Chart::pane_layout`].
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Layout {
    /// The plot area of the main (top) pane.
    pub plot: RectPx,
    /// The right price axis, over all panes.
    pub price_axis: RectPx,
    /// The time axis at the bottom.
    pub time_axis: RectPx,
}

#[derive(Debug)]
struct SeriesEntry {
    id: SeriesId,
    pane: PaneId,
    kind: SeriesKind,
    opts: SeriesOptions,
    store: Store,
    /// Price-to-display map of the last frame (relative price scales);
    /// `None` when the series has no usable base value.
    norm: Option<Normalize>,
    /// Markers, sorted by time.
    markers: Vec<Marker>,
    /// An output of an indicator: follows its source, so it is left
    /// out of the time-axis union and recomputed after a rebuild.
    derived: bool,
    /// Drawn on the pane's volume scale (`Placement::Volume`).
    volume_scale: bool,
    /// Decimals of automatic precision from the last visible values.
    auto_decimals: u8,
}

/// The new data of the series being rebuilt.
enum Target<'a> {
    Data(SeriesData),
    /// A custom series, taken out of its entry: its plugin has the data.
    Custom(&'a mut CustomStore),
}

impl Target<'_> {
    fn times(&self) -> &[Timestamp] {
        match self {
            Self::Data(d) => d.times(),
            Self::Custom(c) => c.plugin.times(),
        }
    }
}

/// Segments of a Point & Figure O.
const CIRCLE_SEGMENTS: u16 = 16;

const NOT_CUSTOM: Error = Error::InvalidArgument("not a custom series");
const NEXT_TO_TRANSFORM: Error =
    Error::InvalidArgument("a transform series cannot share the chart with other series");

/// A layer plugin attached to the chart.
struct LayerEntry {
    id: LayerPluginId,
    /// `None`: the whole chart.
    pane: Option<PaneId>,
    position: LayerPosition,
    plugin: Box<dyn LayerPlugin>,
}

/// An indicator attached to the chart.
struct IndicatorEntry {
    id: IndicatorId,
    source: SeriesId,
    ind: Box<dyn Indicator>,
    /// The output series, in the order of `Indicator::outputs`.
    series: Vec<SeriesId>,
    /// The pane `add_indicator` created, removed with the indicator if empty.
    own_pane: Option<PaneId>,
    /// Bands of `OutputSpec::fill_to`.
    bands: Vec<BandId>,
}

/// Runs the indicator of `entry` for `change` into `out` (reads only the
/// chart, so several can run in parallel). Returns the change actually
/// computed (`Replace` when an incremental one does not fit) and the first
/// slot written.
fn compute_indicator(
    entry: &mut IndicatorEntry,
    source: crate::series::Source<'_>,
    index: &crate::data::TimeIndex,
    change: DataChange,
    out: &mut IndicatorOutputs,
) -> (DataChange, usize) {
    let input = IndicatorInput::new(source, index);
    let len = input.len();
    let change = match change {
        DataChange::Append { count } if count <= len => change,
        DataChange::UpdateLast if len > 0 => change,
        _ => DataChange::Replace,
    };
    let start = match change {
        DataChange::Append { count } => len - count,
        DataChange::UpdateLast => len - 1,
        _ => 0,
    };
    out.reset(entry.ind.outputs().len(), start, len);
    entry.ind.update(&input, change, out);
    (change, start)
}

/// Colors of indicator outputs without a default color.
const INDICATOR_PALETTE: [u32; 6] = [
    0x0029_62FF,
    0x00FF_9800,
    0x009C_27B0,
    0x0000_BCD4,
    0x00E9_1E63,
    0x0079_5548,
];

/// A placed time label.
#[derive(Clone, Copy, Debug)]
struct PlacedTick {
    x: f32,
    width: f32,
    tick: TimeTick,
}

/// A financial chart.
///
/// # Examples
///
/// ```
/// use ferrochart_core::{Chart, ChartOptions};
/// use ferrochart_core::chart::{SeriesData, SeriesOptions};
/// use ferrochart_core::frame::{Invalidation, SeriesKind};
/// use ferrochart_core::geom::SizeCss;
/// use ferrochart_core::testing::{random_walk, MonoMeasurer};
///
/// let mut chart = Chart::new(ChartOptions::default());
/// chart.resize(SizeCss { w: 800.0, h: 450.0 }, 1.0);
/// let pane = chart.main_pane();
/// let candles = chart.add_series(pane, SeriesKind::Candlestick, SeriesOptions::default())?;
/// chart.add_series(pane, SeriesKind::Volume, SeriesOptions::volume_of(candles))?;
/// chart.set_data(candles, SeriesData::Ohlc(random_walk(1, 500, 0, 60_000)?))?;
///
/// assert_ne!(chart.needs_redraw(), Invalidation::None);
/// chart.build_frame(0.0, &MonoMeasurer);
/// assert!(!chart.frame().layers[1].cmds.is_empty());
/// assert_eq!(chart.needs_redraw(), Invalidation::None);
/// # Ok::<(), ferrochart_core::Error>(())
/// ```
pub struct Chart {
    theme: Theme,
    tz: Box<dyn TimeZoneProvider>,
    validation: ValidationMode,
    default_right_offset: f64,
    pub(crate) default_bar_spacing: Css,
    size: SizeCss,
    pub(crate) dpr: f32,
    pub(crate) panes: Vec<Pane>,
    next_pane: u32,
    pub(crate) time_index: TimeIndex,
    series: Vec<SeriesEntry>,
    next_id: u32,
    pub(crate) time_scale: TimeScale,
    pub(crate) invalidation: Invalidation,
    revisions: [u64; 3],
    frame: Frame,
    pub(crate) layout: Layout,
    pub(crate) input: Interaction,
    last_range: (f64, f64),
    last_crosshair: Option<CrosshairPoint>,
    history_requested: Option<i64>,
    axis_width: f32,
    events: Vec<ChartEvent>,
    price_lines: Vec<PriceLineEntry>,
    indicators: Vec<IndicatorEntry>,
    next_indicator: u32,
    /// Colors handed out to indicator outputs so far.
    palette_next: usize,
    /// Scratch of indicator updates (kept small between full updates, so the
    /// real-time path does not allocate).
    ind_out: IndicatorOutputs,
    /// Set by `rebuild`: every indicator must be recomputed.
    axis_rebuilt: bool,
    next_price_line: u32,
    /// Axis labels of the pane being built (scratch).
    axis_labels: Vec<AxisLabel>,
    time_ticks: Vec<TimeTick>,
    placed: Vec<PlacedTick>,
    pub(crate) drawings: DrawingManager,
    /// Text metrics of the last frame, for hit tests between frames.
    pub(crate) text_cache: CachedText,
    /// Handles of the selected drawing (reused: no allocation per frame or
    /// hit test).
    pub(crate) handle_buf: core::cell::RefCell<Vec<PointPx>>,
    /// Multi-chart sync.
    pub(crate) sync: SyncState,
    /// Layer plugins, in the order they were added.
    layer_plugins: Vec<LayerEntry>,
    next_layer: u32,
    /// Custom series kinds the renderer draws natively.
    native_kinds: Vec<&'static str>,
    /// Bands between two series, drawn in this order.
    bands: Vec<BandEntry>,
    next_band: u32,
    /// Markers of the last frame (click hit tests).
    marker_hits: Vec<MarkerHit>,
    /// Gradual display of the data; `None`: everything.
    reveal: Option<Reveal>,
}

/// The ASCII and subscript advances and line height of the last frame's
/// font: hit tests of drawings run between frames, without the host's
/// measurer.
#[derive(Clone, Copy, Debug)]
pub(crate) struct CachedText {
    advances: [f32; 128],
    subscripts: [f32; 10],
    /// Line height per px of font size.
    line: f32,
}

impl Default for CachedText {
    fn default() -> Self {
        Self {
            advances: [0.6; 128],
            subscripts: [0.6; 10],
            line: 1.2,
        }
    }
}

impl TextMeasurer for CachedText {
    fn ascii_advances(&self, _: FontStyleRef) -> &[f32; 128] {
        &self.advances
    }

    fn subscript_advances(&self, _: FontStyleRef) -> Option<&[f32; 10]> {
        Some(&self.subscripts)
    }

    fn measure(&self, text: &str, style: FontStyleRef) -> SizePx {
        SizePx {
            w: text.chars().count() as f32 * 0.6 * style.size,
            h: self.line * style.size,
        }
    }

    fn line_height(&self, style: FontStyleRef) -> f32 {
        self.line * style.size
    }
}

/// Diameter of drawing handles (CSS px).
pub(crate) const HANDLE_CSS: f32 = 9.0;

/// Pool lengths before a drawing is built: what it adds is its bounds.
#[derive(Clone, Copy)]
struct PoolMark {
    lines: usize,
    rects: usize,
    points: usize,
    runs: usize,
}

impl PoolMark {
    fn of(frame: &Frame) -> Self {
        let p = &frame.pools;
        Self {
            lines: p.lines.len(),
            rects: p.rects.len(),
            points: p.points.len(),
            runs: p.text_runs.len(),
        }
    }

    /// Bounds of everything added since the mark (empty if nothing).
    fn bounds(self, frame: &Frame, ctx: &DrawingCtx<'_>) -> RectPx {
        let p = &frame.pools;
        let (mut x0, mut y0, mut x1, mut y1) = (
            f32::INFINITY,
            f32::INFINITY,
            f32::NEG_INFINITY,
            f32::NEG_INFINITY,
        );
        let mut add = |x: f32, y: f32| {
            x0 = x0.min(x);
            y0 = y0.min(y);
            x1 = x1.max(x);
            y1 = y1.max(y);
        };
        for l in p.lines.get(self.lines..).unwrap_or(&[]) {
            add(l.from.x, l.from.y);
            add(l.to.x, l.to.y);
        }
        for r in p.rects.get(self.rects..).unwrap_or(&[]) {
            add(r.rect.x, r.rect.y);
            add(r.rect.right(), r.rect.bottom());
        }
        for q in p.points.get(self.points..).unwrap_or(&[]) {
            add(q.x, q.y);
        }
        for run in p.text_runs.get(self.runs..).unwrap_or(&[]) {
            let w = ctx.text_width(p.text_of(run.text), run.style);
            let h = ctx.line_height(run.style) * 0.5 + run.bg.map_or(0.0, |b| b.padding.0);
            let x = match run.align {
                TextAlign::Left => run.pos.x,
                TextAlign::Center => run.pos.x - w * 0.5,
                TextAlign::Right => run.pos.x - w,
            };
            add(x - h, run.pos.y - h);
            add(x + w + h, run.pos.y + h);
        }
        if x0 > x1 {
            return RectPx::default();
        }
        RectPx {
            x: x0,
            y: y0,
            w: x1 - x0,
            h: y1 - y0,
        }
    }
}

/// Time tick candidates are sampled at least this far apart (physical px).
const TICK_SAMPLE_PX: f64 = 8.0;

impl Chart {
    /// Creates an empty chart with one pane.
    pub fn new(options: ChartOptions) -> Self {
        let dpr = 1.0;
        let mut frame = Frame::default();
        frame.reserve();
        Self {
            theme: options.theme,
            tz: options.time_zone,
            validation: options.ohlc_validation,
            default_right_offset: options.right_offset,
            default_bar_spacing: options.bar_spacing,
            size: SizeCss::default(),
            dpr,
            panes: vec![Pane::new(PaneId(0), 1.0)],
            next_pane: 1,
            time_index: TimeIndex::new(),
            series: Vec::new(),
            next_id: 0,
            time_scale: TimeScale::new(
                f64::from(options.bar_spacing.to_px(dpr).0),
                options.right_offset,
                f64::from(options.min_bar_spacing.to_px(dpr).0),
                f64::from(options.max_bar_spacing.to_px(dpr).0),
            ),
            invalidation: Invalidation::Full,
            revisions: [0; 3],
            frame,
            layout: Layout::default(),
            input: Interaction::default(),
            last_range: (f64::NAN, f64::NAN),
            last_crosshair: None,
            history_requested: None,
            axis_width: 0.0,
            events: Vec::with_capacity(64),
            price_lines: Vec::new(),
            indicators: Vec::new(),
            next_indicator: 0,
            palette_next: 0,
            ind_out: IndicatorOutputs::default(),
            axis_rebuilt: false,
            next_price_line: 0,
            axis_labels: Vec::with_capacity(64),
            time_ticks: Vec::with_capacity(2048),
            placed: Vec::with_capacity(256),
            drawings: DrawingManager::new(PaneId(0)),
            text_cache: CachedText::default(),
            handle_buf: core::cell::RefCell::new(Vec::new()),
            sync: SyncState::default(),
            layer_plugins: Vec::new(),
            next_layer: 0,
            bands: Vec::new(),
            next_band: 0,
            marker_hits: Vec::new(),
            reveal: None,
            native_kinds: Vec::new(),
        }
    }

    /// Sets the size in CSS pixels and the device pixel ratio. All geometry is
    /// computed in physical pixels (`size × dpr`). Non-finite or non-positive
    /// values are ignored.
    pub fn resize(&mut self, size: SizeCss, dpr: f32) {
        let finite = size.w.is_finite() && size.h.is_finite() && dpr.is_finite();
        if !finite || dpr <= 0.0 || size.w < 0.0 || size.h < 0.0 {
            return;
        }
        if (dpr - self.dpr).abs() > f32::EPSILON {
            self.time_scale.rescale(f64::from(dpr / self.dpr));
            self.dpr = dpr;
        }
        self.size = size;
        self.invalidation = Invalidation::Full;
    }

    /// The top pane (the pane every chart starts with, unless it was removed).
    pub fn main_pane(&self) -> PaneId {
        self.panes.first().map_or(PaneId(0), |p| p.id)
    }

    /// Adds a pane below the existing ones.
    ///
    /// # Examples
    ///
    /// ```
    /// use ferrochart_core::{Chart, ChartOptions};
    /// use ferrochart_core::chart::{SeriesData, SeriesOptions};
    /// use ferrochart_core::frame::SeriesKind;
    /// use ferrochart_core::geom::SizeCss;
    /// use ferrochart_core::pane::PaneOptions;
    /// use ferrochart_core::testing::{random_walk, MonoMeasurer};
    ///
    /// let mut chart = Chart::new(ChartOptions::default());
    /// chart.resize(SizeCss { w: 800.0, h: 600.0 }, 1.0);
    /// let candles = chart.add_series(chart.main_pane(), SeriesKind::Candlestick, SeriesOptions::default())?;
    /// // Volume in its own pane, a quarter of the height of the main pane.
    /// let below = chart.add_pane(PaneOptions { stretch_factor: 0.25 });
    /// chart.add_series(below, SeriesKind::Volume, SeriesOptions::volume_of(candles))?;
    /// chart.set_data(candles, SeriesData::Ohlc(random_walk(1, 500, 0, 60_000)?))?;
    /// chart.build_frame(0.0, &MonoMeasurer);
    ///
    /// let main = chart.pane_layout(chart.main_pane()).unwrap();
    /// let vol = chart.pane_layout(below).unwrap();
    /// assert!(vol.plot.y > main.plot.bottom());
    /// assert!((main.plot.h / vol.plot.h - 4.0).abs() < 0.1);
    /// # Ok::<(), ferrochart_core::Error>(())
    /// ```
    pub fn add_pane(&mut self, opts: PaneOptions) -> PaneId {
        let id = PaneId(self.next_pane);
        self.next_pane = self.next_pane.wrapping_add(1);
        self.panes.push(Pane::new(id, opts.stretch_factor));
        self.drawings.pane_added(id);
        self.invalidation = Invalidation::Full;
        id
    }

    /// Removes a pane and the series in it (removing the candlestick series
    /// also removes its volume series, wherever it is).
    ///
    /// # Errors
    ///
    /// [`Error::UnknownId`] if the pane does not exist,
    /// [`Error::InvalidArgument`] for the last remaining pane.
    pub fn remove_pane(&mut self, id: PaneId) -> Result<()> {
        let i = self.pane_index(id).ok_or(Error::UnknownId)?;
        if self.panes.len() == 1 {
            return Err(Error::InvalidArgument("a chart keeps at least one pane"));
        }
        while let Some(sid) = self.series.iter().find(|e| e.pane == id).map(|e| e.id) {
            self.remove_series(sid)?;
        }
        self.panes.remove(i);
        self.drawings.pane_removed(id);
        self.layer_plugins.retain(|l| l.pane != Some(id));
        if self.input.drag.involves(id) {
            self.input.drag = Drag::Idle;
        }
        self.invalidation = Invalidation::Full;
        Ok(())
    }

    /// The panes from top to bottom.
    pub fn panes(&self) -> impl Iterator<Item = PaneId> + '_ {
        self.panes.iter().map(|p| p.id)
    }

    /// Rectangles of a pane in the last built layout; `None` for unknown ids.
    pub fn pane_layout(&self, id: PaneId) -> Option<PaneLayout> {
        let pane = self.panes.get(self.pane_index(id)?)?;
        Some(PaneLayout {
            plot: pane.plot,
            price_axis: RectPx {
                x: self.layout.price_axis.x,
                y: pane.plot.y,
                w: self.layout.price_axis.w,
                h: pane.plot.h,
            },
        })
    }

    /// The stretch factor of a pane; `None` for unknown ids.
    pub fn pane_stretch(&self, id: PaneId) -> Option<f32> {
        Some(self.panes.get(self.pane_index(id)?)?.stretch)
    }

    /// Sets the stretch factor of a pane (dragging a separator changes the
    /// factors of the two panes next to it).
    ///
    /// # Errors
    ///
    /// [`Error::UnknownId`] if the pane does not exist,
    /// [`Error::InvalidArgument`] if `stretch` is not finite and positive.
    pub fn set_pane_stretch(&mut self, id: PaneId, stretch: f32) -> Result<()> {
        let i = self.pane_index(id).ok_or(Error::UnknownId)?;
        if !(stretch.is_finite() && stretch > 0.0) {
            return Err(Error::InvalidArgument(
                "a stretch factor must be finite and positive",
            ));
        }
        if let Some(p) = self.panes.get_mut(i) {
            p.stretch = stretch;
        }
        self.invalidation = Invalidation::Full;
        Ok(())
    }

    /// Prices at the bottom and the top of pane `id`'s plot area in the last
    /// built layout (display values in the relative modes); `None` for an
    /// unknown pane or before the first frame. Hosts copy it to a
    /// `ferrochart-plot` side panel that shares the price axis.
    ///
    /// ```
    /// use ferrochart_core::chart::{SeriesData, SeriesOptions};
    /// use ferrochart_core::frame::SeriesKind;
    /// use ferrochart_core::geom::SizeCss;
    /// use ferrochart_core::testing::{random_walk, MonoMeasurer};
    /// use ferrochart_core::{Chart, ChartOptions};
    ///
    /// let mut chart = Chart::new(ChartOptions::default());
    /// let pane = chart.main_pane();
    /// assert_eq!(chart.visible_price_range(pane), None);
    /// let id = chart.add_series(pane, SeriesKind::Candlestick, SeriesOptions::default())?;
    /// chart.set_data(id, SeriesData::Ohlc(random_walk(1, 100, 0, 60_000)?))?;
    /// chart.resize(SizeCss { w: 400.0, h: 300.0 }, 1.0);
    /// chart.build_frame(0.0, &MonoMeasurer);
    /// let (lo, hi) = chart.visible_price_range(pane).unwrap();
    /// assert!(lo < hi);
    /// # Ok::<(), ferrochart_core::Error>(())
    /// ```
    pub fn visible_price_range(&self, id: PaneId) -> Option<(f64, f64)> {
        let pane = self.panes.get(self.pane_index(id)?)?;
        let h = f64::from(pane.plot.h);
        if !(pane.has_content && h > 0.0) {
            return None;
        }
        let a = pane.price_scale.y_to_price(h, 0.0, h);
        let b = pane.price_scale.y_to_price(0.0, 0.0, h);
        (a.is_finite() && b.is_finite()).then_some((a.min(b), a.max(b)))
    }

    pub(crate) fn pane_index(&self, id: PaneId) -> Option<usize> {
        self.panes.iter().position(|p| p.id == id)
    }

    pub(crate) fn pane_mut(&mut self, id: PaneId) -> Option<&mut Pane> {
        self.panes.iter_mut().find(|p| p.id == id)
    }

    fn entry(&self, id: SeriesId) -> Result<&SeriesEntry> {
        self.series
            .iter()
            .find(|e| e.id == id)
            .ok_or(Error::UnknownId)
    }

    fn entry_index(&self, id: SeriesId) -> Result<usize> {
        self.series
            .iter()
            .position(|e| e.id == id)
            .ok_or(Error::UnknownId)
    }

    /// Adds a candlestick series on the main pane with the default options:
    /// `add_series(main_pane(), SeriesKind::Candlestick, SeriesOptions::default())`.
    ///
    /// # Errors
    ///
    /// As [`Chart::add_series`].
    ///
    /// # Examples
    ///
    /// ```
    /// use ferrochart_core::testing::random_walk;
    /// use ferrochart_core::{Chart, ChartOptions};
    ///
    /// let mut chart = Chart::new(ChartOptions::default());
    /// let candles = chart.add_candlestick()?;
    /// chart.add_volume(candles)?;
    /// chart.set_data(candles, random_walk(1, 500, 0, 60_000)?)?;
    /// # Ok::<(), ferrochart_core::Error>(())
    /// ```
    pub fn add_candlestick(&mut self) -> Result<SeriesId> {
        self.add_series(
            self.main_pane(),
            SeriesKind::Candlestick,
            SeriesOptions::default(),
        )
    }

    /// Adds a volume histogram of `source`'s volume column on the main pane:
    /// `add_series(main_pane(), SeriesKind::Volume, SeriesOptions::volume_of(source))`.
    ///
    /// # Errors
    ///
    /// As [`Chart::add_series`] ([`Error::UnknownId`] if `source` does not
    /// exist).
    pub fn add_volume(&mut self, source: SeriesId) -> Result<SeriesId> {
        self.add_series(
            self.main_pane(),
            SeriesKind::Volume,
            SeriesOptions::volume_of(source),
        )
    }

    /// Adds a series to a pane.
    ///
    /// All series share one time axis, the union of their times. A
    /// series that skips bars of the axis is drawn across them (line-like
    /// series) or leaves gaps (histograms, OHLC series).
    ///
    /// # Errors
    ///
    /// - [`Error::UnknownId`] if `pane` does not exist;
    /// - [`Error::InvalidArgument`] for a volume series whose `opts.source` is
    ///   not an OHLC or transform series of this chart, for `Custom`, `Kagi`
    ///   and `PointFigure` (added with their own methods), and for any
    ///   series but volume when the chart has a transform series.
    ///
    /// # Examples
    ///
    /// ```
    /// use ferrochart_core::{Chart, ChartOptions};
    /// use ferrochart_core::chart::SeriesOptions;
    /// use ferrochart_core::data::ValueColumns;
    /// use ferrochart_core::chart::SeriesData;
    /// use ferrochart_core::frame::SeriesKind;
    ///
    /// let mut chart = Chart::new(ChartOptions::default());
    /// let line = chart.add_series(chart.main_pane(), SeriesKind::Line, SeriesOptions::default())?;
    /// chart.set_data(line, SeriesData::Value(ValueColumns {
    ///     time: vec![0, 60_000, 120_000],
    ///     value: vec![1.0, f64::NAN, 3.0], // NaN: a gap
    ///     color: None,
    /// }))?;
    /// # Ok::<(), ferrochart_core::Error>(())
    /// ```
    pub fn add_series(
        &mut self,
        pane: PaneId,
        kind: SeriesKind,
        opts: SeriesOptions,
    ) -> Result<SeriesId> {
        if self.transform_entry().is_some() && kind != SeriesKind::Volume {
            return Err(NEXT_TO_TRANSFORM);
        }
        self.insert_series(pane, kind, opts)
    }

    /// `add_series` without the transform-series check (indicator outputs).
    fn insert_series(
        &mut self,
        pane: PaneId,
        kind: SeriesKind,
        opts: SeriesOptions,
    ) -> Result<SeriesId> {
        if self.pane_index(pane).is_none() {
            return Err(Error::UnknownId);
        }
        if kind == SeriesKind::Custom {
            return Err(Error::InvalidArgument(
                "custom series are added with add_custom_series",
            ));
        }
        if matches!(kind, SeriesKind::Kagi | SeriesKind::PointFigure) {
            return Err(Error::InvalidArgument(
                "Kagi and Point & Figure series are added with add_transform_series",
            ));
        }
        if kind == SeriesKind::Band {
            return Err(Error::InvalidArgument("bands are added with add_band"));
        }
        if kind == SeriesKind::Volume {
            let source_ok = opts
                .source
                .and_then(|s| self.entry(s).ok())
                .is_some_and(|e| is_ohlc_kind(e.kind) || e.store.transform().is_some());
            if !source_ok {
                return Err(Error::InvalidArgument(
                    "a volume series needs an OHLC series of this chart as its source",
                ));
            }
            if !self.series.iter().any(|e| e.pane == pane)
                && let Some(p) = self.pane_mut(pane)
            {
                // Bars stand on the bottom of a pane of their own.
                let _ = p.price_scale.set_margins(PRICE_MARGINS.0, 0.0);
            }
        }
        let id = SeriesId(self.next_id);
        self.next_id = self.next_id.checked_add(1).ok_or(Error::OutOfMemory)?;
        self.series.try_reserve(1).map_err(|_| Error::OutOfMemory)?;
        self.series.push(SeriesEntry {
            id,
            pane,
            kind,
            opts,
            store: Store::for_kind(kind),
            norm: None,
            markers: Vec::new(),
            derived: false,
            volume_scale: false,
            auto_decimals: 2,
        });
        self.invalidation = Invalidation::Full;
        Ok(id)
    }

    /// The options of a series; `None` for an unknown id.
    pub fn series_options(&self, id: SeriesId) -> Option<&SeriesOptions> {
        self.entry(id).ok().map(|e| &e.opts)
    }

    /// The kind of a series; `None` for an unknown id.
    pub fn series_kind(&self, id: SeriesId) -> Option<SeriesKind> {
        self.entry(id).ok().map(|e| e.kind)
    }

    /// The pane of a series; `None` for an unknown id.
    pub fn series_pane(&self, id: SeriesId) -> Option<PaneId> {
        self.entry(id).ok().map(|e| e.pane)
    }

    /// Changes the options of a series (colors, line style, labels, shift,
    /// …), e.g. for `applyOptions` of the lightweight-charts compatibility
    /// layer. The data is kept.
    ///
    /// # Errors
    ///
    /// - [`Error::UnknownId`] if the series does not exist;
    /// - [`Error::InvalidArgument`] if `opts.source` differs from the
    ///   series' source (the source of a volume series is fixed).
    ///
    /// # Examples
    ///
    /// ```
    /// use ferrochart_core::chart::SeriesOptions;
    /// use ferrochart_core::frame::SeriesKind;
    /// use ferrochart_core::geom::Rgba;
    /// use ferrochart_core::{Chart, ChartOptions};
    ///
    /// let mut chart = Chart::new(ChartOptions::default());
    /// let line = chart.add_series(chart.main_pane(), SeriesKind::Line, SeriesOptions::default())?;
    /// let mut opts = *chart.series_options(line).unwrap();
    /// opts.color = Some(Rgba::hex(0x00FF_6D00));
    /// chart.set_series_options(line, opts)?;
    /// assert_eq!(chart.series_options(line).unwrap().color, Some(Rgba::hex(0x00FF_6D00)));
    /// # Ok::<(), ferrochart_core::Error>(())
    /// ```
    pub fn set_series_options(&mut self, id: SeriesId, opts: SeriesOptions) -> Result<()> {
        let k = self.entry_index(id)?;
        if opts.source != self.series[k].opts.source {
            return Err(Error::InvalidArgument(
                "the source of a series cannot change",
            ));
        }
        self.series[k].opts = opts;
        self.invalidation = Invalidation::Full;
        Ok(())
    }

    /// Removes a series and its data. Removing an OHLC series also removes the
    /// volume series that read it.
    ///
    /// # Errors
    ///
    /// [`Error::UnknownId`] if the series does not exist.
    pub fn remove_series(&mut self, id: SeriesId) -> Result<()> {
        if self.entry(id)?.derived {
            return Err(Error::InvalidArgument(
                "an indicator output; remove the indicator instead",
            ));
        }
        self.drop_indicators_of(id);
        self.series
            .retain(|e| e.id != id && e.opts.source != Some(id));
        self.price_lines
            .retain(|l| self.series.iter().any(|e| e.id == l.series));
        self.prune_bands();
        // The other series keep the time axis; without data it is reset.
        if self.series.iter().all(|e| e.store.is_empty()) {
            self.time_index = TimeIndex::new();
            self.time_scale.set_extent(0, 0);
        }
        self.invalidation = Invalidation::Full;
        Ok(())
    }

    fn after_change(&mut self, id: SeriesId, repaired: usize) {
        self.time_scale
            .set_extent(self.time_index.first_index(), self.time_index.end_index());
        self.invalidation = self.invalidation.max(Invalidation::Data);
        if repaired > 0 {
            self.events
                .push(ChartEvent::Warning(WarningKind::OhlcRepaired {
                    series: id,
                    bars: repaired,
                }));
        }
    }

    /// Index of series `id`, which must take data of its own.
    fn data_entry(&self, id: SeriesId) -> Result<usize> {
        let k = self.entry_index(id)?;
        if self.series[k].store.takes_data() {
            Ok(k)
        } else if self.series[k].kind == SeriesKind::Custom {
            Err(Error::InvalidArgument(
                "a custom series owns its data; use update_custom",
            ))
        } else {
            Err(Error::InvalidArgument(
                "this series has no data of its own; use its source series",
            ))
        }
    }

    /// Runs a data operation on series `k` with the current time axis. If it
    /// fails, the `(front, back)` slots `cover` added are removed again.
    fn run_op<T>(
        &mut self,
        k: usize,
        added: (usize, usize),
        f: impl FnOnce(&Op<'_>, &mut Store) -> Result<T>,
    ) -> Result<T> {
        let entry = &mut self.series[k];
        let op = Op {
            kind: entry.kind,
            index: &self.time_index,
            mode: self.validation,
        };
        let out = f(&op, &mut entry.store);
        if out.is_err() {
            self.time_index.truncate_ends(added.0, added.1);
        }
        out
    }

    /// Makes `times` slots of the time axis where that keeps every logical
    /// index: times before the first slot are prepended, times after
    /// the last appended. Returns the `(front, back)` numbers added, or `None`
    /// if a time falls between existing slots (the axis must be rebuilt).
    fn cover(&mut self, times: &[Timestamp]) -> Result<Option<(usize, usize)>> {
        let index = &mut self.time_index;
        let (Some(&t0), Some(&tn)) = (times.first(), times.last()) else {
            return Ok(Some((0, 0)));
        };
        let (Some(&first), Some(&last)) = (index.times().first(), index.times().last()) else {
            index.append(times)?;
            return Ok(Some((0, times.len())));
        };
        if t0 > last {
            index.append(times)?;
            return Ok(Some((0, times.len())));
        }
        if tn < first {
            index.prepend(times)?;
            return Ok(Some((times.len(), 0)));
        }
        if let [t] = times {
            // The real-time path: no allocation.
            return Ok(index.index_of(*t).map(|_| (0, 0)));
        }
        let head = times.partition_point(|&t| t < first);
        let body_end = times.partition_point(|&t| t <= last).max(head);
        if index.map_series(&times[head..body_end])?.is_none() {
            return Ok(None);
        }
        index.prepend(&times[..head])?;
        if let Err(e) = index.append(&times[body_end..]) {
            index.truncate_ends(head, 0);
            return Err(e);
        }
        Ok(Some((head, times.len() - body_end)))
    }

    /// Logical-index spans of the series other than `k` that have data,
    /// sorted and merged.
    fn other_spans(&self, k: usize) -> Vec<(i64, i64)> {
        let mut spans: Vec<(i64, i64)> = self
            .series
            .iter()
            .enumerate()
            .filter(|&(j, e)| j != k && !e.derived)
            .filter_map(|(_, e)| e.store.span())
            .collect();
        spans.sort_unstable();
        let mut merged: Vec<(i64, i64)> = Vec::with_capacity(spans.len());
        for (a, b) in spans {
            match merged.last_mut() {
                Some(last) if a <= last.1 => last.1 = last.1.max(b),
                _ => merged.push((a, b)),
            }
        }
        merged
    }

    /// Whether a slot of the axis outside `others` (sorted, disjoint spans)
    /// is not one of `times` (which are all slots of the axis): such a slot
    /// would belong to no series after the operation.
    fn has_orphans(&self, others: &[(i64, i64)], times: &[Timestamp]) -> bool {
        let index = &self.time_index;
        let end = index.end_index();
        let mut at = index.first_index();
        for &(a, b) in others.iter().chain(core::iter::once(&(end, end))) {
            if at < a {
                let (Some(ta), Some(tb)) = (index.time_at(at), index.time_at(a - 1)) else {
                    return true;
                };
                let lo = times.partition_point(|&t| t < ta);
                let hi = times.partition_point(|&t| t <= tb);
                if i64::try_from(hi - lo).ok() != Some(a - at) {
                    return true;
                }
            }
            at = at.max(b);
        }
        false
    }

    /// Rebuilds the time axis as the union of `target`'s times (the complete
    /// new data of series `k`) and the slots the `others` spans cover, then
    /// sets every series' data on it. Logical indices are kept for the
    /// first slot the old and new axis share. Reports a
    /// [`WarningKind::TimeAxisMerged`] if times were inserted between
    /// existing slots. The chart is unchanged if `target` is invalid.
    fn rebuild(&mut self, k: usize, target: Target<'_>, others: &[(i64, i64)]) -> Result<usize> {
        let old = &self.time_index;
        let mut kept = Vec::new();
        for &(a, b) in others {
            let from = usize::try_from(a - old.first_index()).unwrap_or(0);
            let to = usize::try_from(b - old.first_index()).unwrap_or(0);
            kept.extend_from_slice(old.times().get(from..to).unwrap_or(&[]));
        }
        let mut index = TimeIndex::from_times(kept)?;
        index.merge(target.times())?;
        let inserted = match (old.times().first(), old.times().last()) {
            (Some(&first), Some(&last)) => target
                .times()
                .iter()
                .filter(|&&t| t > first && t < last && old.index_of(t).is_none())
                .count(),
            _ => 0,
        };
        let shared = index
            .times()
            .iter()
            .enumerate()
            .find_map(|(p, &t)| Some((old.index_of(t)?, p)));
        if let Some((j, p)) = shared
            && let Some(first) = j.checked_sub(i64::try_from(p).unwrap_or(i64::MAX))
        {
            index.rebase(first);
        }
        let snapshots: Vec<(usize, SeriesData)> = self
            .series
            .iter()
            .enumerate()
            .filter(|&(j, e)| j != k && !e.store.is_empty() && !e.derived)
            .filter_map(|(j, e)| Some((j, e.store.snapshot(old)?)))
            .collect();
        // The target first: its errors leave the chart unchanged.
        let entry = &mut self.series[k];
        let repaired = match target {
            Target::Data(data) => Op {
                kind: entry.kind,
                index: &index,
                mode: self.validation,
            }
            .set(&mut entry.store, data)?,
            Target::Custom(c) => {
                c.map_onto(&index)?;
                c.log.replace();
                0
            }
        };
        let id = entry.id;
        for (j, snap) in snapshots {
            let entry = &mut self.series[j];
            let op = Op {
                kind: entry.kind,
                index: &index,
                mode: ValidationMode::Repair,
            };
            // Only an allocation failure can make a snapshot not fit.
            if op.set(&mut entry.store, snap).is_err() {
                entry.store = Store::for_kind(entry.kind);
                self.events
                    .push(ChartEvent::Warning(WarningKind::DataDropped {
                        series: entry.id,
                    }));
            }
        }
        // Custom series are re-mapped from their own times.
        for e in &mut self.series {
            if let Store::Custom(c) = &mut e.store
                && c.mode == AxisMode::Slots
                && c.len() > 0
            {
                c.log.replace();
                if c.map_onto(&index).is_err() {
                    c.clear();
                    self.events
                        .push(ChartEvent::Warning(WarningKind::DataDropped {
                            series: e.id,
                        }));
                }
            }
        }
        // Indicator outputs are recomputed on the new axis (`after_data`).
        for e in self.series.iter_mut().filter(|e| e.derived) {
            e.store = Store::for_kind(e.kind);
        }
        self.axis_rebuilt = true;
        self.time_index = index;
        self.time_index.shrink_to_fit();
        if inserted > 0 {
            self.events
                .push(ChartEvent::Warning(WarningKind::TimeAxisMerged {
                    series: id,
                    slots: inserted,
                }));
        }
        Ok(repaired)
    }

    /// The complete data of series `k` after adding `new` before (`prepend`)
    /// or after its current points, for a rebuild.
    fn combined(&self, k: usize, new: SeriesData, prepend: bool) -> Result<SeriesData> {
        let store = &self.series[k].store;
        let Some(old) = store
            .snapshot(&self.time_index)
            .filter(|_| !store.is_empty())
        else {
            return Ok(new);
        };
        let (Some(&new_first), Some(&new_last)) = (new.times().first(), new.times().last()) else {
            return Ok(old);
        };
        let (Some(&old_first), Some(&old_last)) = (old.times().first(), old.times().last()) else {
            return Ok(new);
        };
        if prepend {
            if new_last >= old_first {
                return Err(Error::TimeNotIncreasing {
                    index: new.times().len() - 1,
                });
            }
            new.concat(old)
        } else {
            if new_first <= old_last {
                return Err(Error::TimeNotIncreasing { index: 0 });
            }
            old.concat(new)
        }
    }

    /// Replaces all data of a series.
    ///
    /// The time axis becomes the union of the new times and the times of the
    /// other series: times the other series do not have are added (a
    /// time between existing bars rebuilds the axis and reports
    /// [`WarningKind::TimeAxisMerged`]), and bars only this series had are
    /// removed.
    ///
    /// # Errors
    ///
    /// [`Error::UnknownId`], [`Error::InvalidArgument`] (wrong series or data
    /// kind), and the data validation errors
    /// ([`Error::LengthMismatch`], [`Error::InvalidOhlc`],
    /// [`Error::TimeNotIncreasing`]). The chart is unchanged on error.
    pub fn set_data(&mut self, id: SeriesId, data: impl Into<SeriesData>) -> Result<()> {
        self.set_data_data(id, data.into())
    }

    fn set_data_data(&mut self, id: SeriesId, data: SeriesData) -> Result<()> {
        let k = self.data_entry(id)?;
        if self.series[k].store.transform().is_some() {
            let (input, repaired) = input_columns(data, self.validation)?;
            self.run_transform(k, repaired, |t, index| {
                t.set(index, input).map(|()| Some(DataChange::Replace))
            })?;
            return Ok(());
        }
        let others = self.other_spans(k);
        let repaired = if others.is_empty() {
            // The only series with data: its times are the axis.
            let (times, data) = data.take_times()?;
            let index = TimeIndex::from_times(times)?;
            let entry = &mut self.series[k];
            let repaired = Op {
                kind: entry.kind,
                index: &index,
                mode: self.validation,
            }
            .set_aligned(&mut entry.store, index.first_index(), data)?;
            self.time_index = index;
            self.time_index.shrink_to_fit();
            // A new axis: indicator outputs are recomputed on it.
            self.axis_rebuilt = true;
            repaired
        } else if self.time_index.map_series(data.times())?.is_some()
            && !self.has_orphans(&others, data.times())
        {
            self.run_op(k, (0, 0), |op, store| op.set(store, data))?
        } else {
            self.rebuild(k, Target::Data(data), &others)?
        };
        self.after_change(id, repaired);
        self.after_data(id, DataChange::Replace);
        Ok(())
    }

    /// Appends points after the last one. Times the axis does not have yet
    /// are added to it.
    ///
    /// # Errors
    ///
    /// As [`set_data`](Self::set_data); additionally the times must come after
    /// the last point, and volume must be present exactly when the series has it.
    pub fn append(&mut self, id: SeriesId, data: impl Into<SeriesData>) -> Result<()> {
        self.append_data(id, data.into())
    }

    fn append_data(&mut self, id: SeriesId, data: SeriesData) -> Result<()> {
        let k = self.data_entry(id)?;
        if self.series[k].store.transform().is_some() {
            let (input, repaired) = input_columns(data, self.validation)?;
            self.run_transform(k, repaired, |t, index| t.append(index, input))?;
            return Ok(());
        }
        let old_end = self.series[k].store.span().map(|(_, e)| e);
        let repaired = if let Some(added) = self.cover(data.times())? {
            self.run_op(k, added, |op, store| op.append(store, data))?
        } else {
            let all = self.combined(k, data, false)?;
            let others = self.other_spans(k);
            self.rebuild(k, Target::Data(all), &others)?
        };
        self.after_change(id, repaired);
        let change = match (old_end, self.series[k].store.span()) {
            (Some(old), Some((_, end))) => DataChange::Append {
                count: usize::try_from(end - old).unwrap_or(0),
            },
            _ => DataChange::Replace,
        };
        self.after_data(id, change);
        Ok(())
    }

    /// Inserts older points before the first one (history loading). Existing
    /// points keep their logical indices unless a time falls between
    /// existing bars of the axis.
    ///
    /// # Errors
    ///
    /// As [`append`](Self::append), with times before the first point.
    pub fn prepend(&mut self, id: SeriesId, data: impl Into<SeriesData>) -> Result<()> {
        self.prepend_data(id, data.into())
    }

    fn prepend_data(&mut self, id: SeriesId, data: SeriesData) -> Result<()> {
        let k = self.data_entry(id)?;
        if self.series[k].store.transform().is_some() {
            let (input, repaired) = input_columns(data, self.validation)?;
            self.run_transform(k, repaired, |t, index| {
                t.prepend(index, input).map(|()| Some(DataChange::Replace))
            })?;
            return Ok(());
        }
        let repaired = if let Some(added) = self.cover(data.times())? {
            self.run_op(k, added, |op, store| op.prepend(store, data))?
        } else {
            let all = self.combined(k, data, true)?;
            let others = self.other_spans(k);
            self.rebuild(k, Target::Data(all), &others)?
        };
        self.after_change(id, repaired);
        self.after_data(id, DataChange::Replace);
        Ok(())
    }

    /// Real-time update. Replaces the last point if `bar.time` equals its time
    /// (no allocation), appends one if it is later. Returns the applied change.
    /// Value series (line, area, baseline, histogram) take `bar.close` as the
    /// value and ignore the other fields. For a transform series `bar` is an
    /// input and the result is the change of the output (`Append { count: 0
    /// }` if the output did not change); replacing the last input does not
    /// allocate while the number of forming elements stays the same.
    ///
    /// # Errors
    ///
    /// [`Error::UnknownId`], [`Error::InvalidArgument`] (a volume series),
    /// [`Error::TimeNotIncreasing`] (time before the last point), and the
    /// validation errors.
    pub fn update_last(&mut self, id: SeriesId, bar: BarUpdate) -> Result<DataChange> {
        let k = self.data_entry(id)?;
        if self.series[k].store.transform().is_some() {
            let (bar, repaired) = crate::data::ohlc::OhlcStore::check_one(bar, self.validation)?;
            return self.run_transform(k, repaired, |t, index| t.update_last(index, bar));
        }
        let (change, repaired) = if let Some(added) = self.cover(&[bar.time])? {
            self.run_op(k, added, |op, store| op.update_last(store, bar))?
        } else {
            let point = SeriesData::point(self.series[k].kind, bar);
            let all = self.combined(k, point, false)?;
            let others = self.other_spans(k);
            (
                DataChange::Append { count: 1 },
                self.rebuild(k, Target::Data(all), &others)?,
            )
        };
        if change == DataChange::UpdateLast {
            self.invalidation = self.invalidation.max(Invalidation::Data);
            if repaired > 0 {
                self.after_change(id, repaired);
            }
        } else {
            self.after_change(id, repaired);
        }
        self.after_data(id, change);
        Ok(change)
    }

    /// Adds a transform series to a pane: the data given to it
    /// with [`set_data`](Self::set_data), [`append`](Self::append),
    /// [`prepend`](Self::prepend) and [`update_last`](Self::update_last) is
    /// its input, and its output elements, at synthetic times, are the time
    /// axis. See [`crate::transform`] for the built-in transforms.
    ///
    /// # Errors
    ///
    /// - [`Error::UnknownId`] if `pane` does not exist;
    /// - [`Error::InvalidArgument`] if the chart already has a series other
    ///   than volume (a chart has at most one transform series and no
    ///   time-based series next to it), if the output kind is not
    ///   `Candlestick`, `OhlcBar`, `Kagi` or `PointFigure`, or if a
    ///   `PointFigure` transform has no positive box size.
    ///
    /// # Examples
    ///
    /// ```
    /// use ferrochart_core::chart::{SeriesData, SeriesOptions};
    /// use ferrochart_core::data::BarUpdate;
    /// use ferrochart_core::frame::SeriesKind;
    /// use ferrochart_core::testing::{random_walk, MonoMeasurer};
    /// use ferrochart_core::transform::{Kagi, Reversal};
    /// use ferrochart_core::{Chart, ChartOptions};
    ///
    /// let mut chart = Chart::new(ChartOptions::default());
    /// let pane = chart.main_pane();
    /// let kagi = chart.add_transform_series(
    ///     pane,
    ///     Box::new(Kagi::new(Reversal::Percent(2.0))?),
    ///     SeriesOptions::default(),
    /// )?;
    /// let bars = random_walk(1, 5_000, 0, 60_000)?;
    /// let last = *bars.time.last().unwrap();
    /// chart.set_data(kagi, SeriesData::Ohlc(bars))?;
    /// // Far fewer lines than input bars.
    /// assert!(chart.data_view().time_index().len() < 1_000);
    /// // Real-time input.
    /// let tick = BarUpdate { time: last + 60_000, open: 100.0, high: 100.0, low: 100.0, close: 100.0, volume: Some(1.0) };
    /// chart.update_last(kagi, tick)?;
    /// // No time-based series next to it.
    /// assert!(chart.add_series(pane, SeriesKind::Line, SeriesOptions::default()).is_err());
    /// chart.build_frame(0.0, &MonoMeasurer);
    /// # Ok::<(), ferrochart_core::Error>(())
    /// ```
    pub fn add_transform_series(
        &mut self,
        pane: PaneId,
        t: Box<dyn SeriesTransform>,
        opts: SeriesOptions,
    ) -> Result<SeriesId> {
        if self.pane_index(pane).is_none() {
            return Err(Error::UnknownId);
        }
        if self
            .series
            .iter()
            .any(|e| !e.derived && e.kind != SeriesKind::Volume)
        {
            return Err(NEXT_TO_TRANSFORM);
        }
        let kind = t.output_kind();
        if !matches!(
            kind,
            SeriesKind::Candlestick
                | SeriesKind::OhlcBar
                | SeriesKind::Kagi
                | SeriesKind::PointFigure
        ) {
            return Err(Error::InvalidArgument(
                "a transform is drawn as candlesticks, OHLC bars, Kagi or Point & Figure",
            ));
        }
        if kind == SeriesKind::PointFigure
            && !t.box_size().is_some_and(|b| b.is_finite() && b > 0.0)
        {
            return Err(Error::InvalidArgument(
                "a Point & Figure transform needs a positive box size",
            ));
        }
        let id = SeriesId(self.next_id);
        self.next_id = self.next_id.checked_add(1).ok_or(Error::OutOfMemory)?;
        self.series.try_reserve(1).map_err(|_| Error::OutOfMemory)?;
        self.series.push(SeriesEntry {
            id,
            pane,
            kind,
            opts,
            store: Store::Transform(Box::new(TransformStore::new(t))),
            norm: None,
            markers: Vec::new(),
            derived: false,
            volume_scale: false,
            auto_decimals: 2,
        });
        self.invalidation = Invalidation::Full;
        Ok(id)
    }

    /// The transform series, if the chart has one.
    fn transform_entry(&self) -> Option<usize> {
        self.series
            .iter()
            .position(|e| e.store.transform().is_some())
    }

    /// Runs a data operation on transform series `k`, whose output is the
    /// whole time axis, and passes the output's change on.
    fn run_transform(
        &mut self,
        k: usize,
        repaired: usize,
        f: impl FnOnce(&mut TransformStore, &mut TimeIndex) -> Result<Option<DataChange>>,
    ) -> Result<DataChange> {
        let Store::Transform(t) = &mut self.series[k].store else {
            return Err(Error::InvalidArgument("not a transform series"));
        };
        let out = f(t, &mut self.time_index);
        let id = self.series[k].id;
        let change = match out {
            Ok(c) => c,
            Err(e) => {
                // The output may have been cleared.
                self.after_change(id, 0);
                self.after_data(id, DataChange::Replace);
                return Err(e);
            }
        };
        match change {
            None => {
                if repaired > 0 {
                    self.after_change(id, repaired);
                }
                Ok(DataChange::Append { count: 0 })
            }
            Some(DataChange::UpdateLast) => {
                self.invalidation = self.invalidation.max(Invalidation::Data);
                if repaired > 0 {
                    self.after_change(id, repaired);
                }
                self.after_data(id, DataChange::UpdateLast);
                Ok(DataChange::UpdateLast)
            }
            Some(change) => {
                self.after_change(id, repaired);
                self.after_data(id, change);
                Ok(change)
            }
        }
    }

    /// Adds a custom series to a pane. Its current data is taken as a
    /// `Replace`; later changes go through [`update_custom`](Self::update_custom).
    /// See [`CustomSeries`] for an example.
    ///
    /// # Errors
    ///
    /// [`Error::UnknownId`] if `pane` does not exist;
    /// [`Error::InvalidArgument`] if the chart has a transform series;
    /// [`Error::TimeNotIncreasing`] if the plugin's times are not strictly
    /// increasing (the series is not added); [`Error::OutOfMemory`].
    pub fn add_custom_series(
        &mut self,
        pane: PaneId,
        series: Box<dyn CustomSeries>,
        opts: SeriesOptions,
    ) -> Result<SeriesId> {
        if self.pane_index(pane).is_none() {
            return Err(Error::UnknownId);
        }
        if self.transform_entry().is_some() {
            return Err(NEXT_TO_TRANSFORM);
        }
        let id = SeriesId(self.next_id);
        let next = self.next_id.checked_add(1).ok_or(Error::OutOfMemory)?;
        self.series.try_reserve(1).map_err(|_| Error::OutOfMemory)?;
        let has_data = !series.times().is_empty();
        self.series.push(SeriesEntry {
            id,
            pane,
            kind: SeriesKind::Custom,
            opts,
            store: Store::Custom(Box::new(CustomStore::new(series))),
            norm: None,
            markers: Vec::new(),
            derived: false,
            volume_scale: false,
            auto_decimals: 2,
        });
        self.next_id = next;
        self.invalidation = Invalidation::Full;
        if has_data && let Err(e) = self.change_custom(id, |_| Ok(DataChange::Replace)) {
            self.series.pop();
            return Err(e);
        }
        Ok(id)
    }

    /// The plugin of custom series `id` as `T`; `None` for an unknown id,
    /// another kind of series or another type.
    pub fn custom_series<T: CustomSeries>(&self, id: SeriesId) -> Option<&T> {
        let any: &dyn Any = self.entry(id).ok()?.store.custom()?.plugin.as_ref();
        any.downcast_ref::<T>()
    }

    /// Changes the data of custom series `id`: `f` modifies the plugin and
    /// returns what changed, and core updates the time axis, generation and
    /// change log. `DataChange::UpdateLast` keeping the last time does
    /// not allocate. A change that does not match the data (wrong count, the
    /// last time moved) is applied as a `Replace`.
    ///
    /// # Errors
    ///
    /// - [`Error::UnknownId`];
    /// - [`Error::InvalidArgument`] if it is not a custom series or its plugin
    ///   is not a `T` (`f` is not called);
    /// - [`Error::TimeNotIncreasing`] if the times are no longer strictly
    ///   increasing: the series then shows nothing until a valid change;
    /// - [`Error::OutOfMemory`].
    pub fn update_custom<T: CustomSeries>(
        &mut self,
        id: SeriesId,
        f: impl FnOnce(&mut T) -> DataChange,
    ) -> Result<()> {
        self.change_custom(id, |c| {
            let any: &mut dyn Any = c.plugin.as_mut();
            let t = any.downcast_mut::<T>().ok_or(Error::InvalidArgument(
                "the custom series is of another type",
            ))?;
            Ok(f(t))
        })
    }

    /// Takes custom series `id` out of its entry, lets `modify` change the
    /// plugin and say what changed, and applies the change.
    fn change_custom(
        &mut self,
        id: SeriesId,
        modify: impl FnOnce(&mut CustomStore) -> Result<DataChange>,
    ) -> Result<()> {
        let k = self.entry_index(id)?;
        let store = core::mem::replace(&mut self.series[k].store, Store::None);
        let Store::Custom(mut c) = store else {
            self.series[k].store = store;
            return Err(NOT_CUSTOM);
        };
        let out = modify(&mut c).and_then(|change| {
            let applied = c
                .classify(change)
                .and_then(|kind| self.apply_custom(k, &mut c, kind));
            if applied.is_err() {
                c.clear();
                self.invalidation = Invalidation::Full;
            }
            applied
        });
        self.series[k].store = Store::Custom(c);
        match out? {
            None => {}
            Some(DataChange::UpdateLast) => {
                self.invalidation = self.invalidation.max(Invalidation::Data);
                self.after_data(id, DataChange::UpdateLast);
            }
            Some(change) => {
                self.after_change(id, 0);
                self.after_data(id, change);
            }
        }
        Ok(())
    }

    /// Brings the time axis and the mapping of custom series `k` (taken out
    /// of its entry as `c`) up to the plugin's data.
    fn apply_custom(
        &mut self,
        k: usize,
        c: &mut CustomStore,
        kind: CustomChange,
    ) -> Result<Option<DataChange>> {
        let old = c.len();
        let n = c.plugin.times().len();
        let span = |a: usize, b: usize| IdxSpan {
            start: i64::try_from(a).unwrap_or(i64::MAX),
            end: i64::try_from(b).unwrap_or(i64::MAX),
        };
        let change = match kind {
            CustomChange::None => return Ok(None),
            CustomChange::UpdateLast => {
                // The real-time path: no allocation.
                c.log.record(span(old.saturating_sub(1), old));
                return Ok(Some(DataChange::UpdateLast));
            }
            CustomChange::Append => DataChange::Append { count: n - old },
            CustomChange::Prepend { count } => DataChange::Prepend { count },
            CustomChange::Replace => DataChange::Replace,
        };
        if c.mode == AxisMode::Overlay {
            c.map_onto(&self.time_index)?;
            if kind == CustomChange::Append {
                c.log.record(span(old, n));
            } else {
                c.log.replace();
            }
            return Ok(Some(change));
        }
        match kind {
            CustomChange::Append | CustomChange::Prepend { .. } => {
                let times = c.plugin.times();
                let new = match kind {
                    CustomChange::Prepend { count } => times.get(..count),
                    _ => times.get(old..),
                };
                if let Some(added) = self.cover(new.unwrap_or(&[]))? {
                    let mapped = if kind == CustomChange::Append {
                        c.extend_onto(&self.time_index)
                    } else {
                        c.map_onto(&self.time_index)
                    };
                    if let Err(e) = mapped {
                        self.time_index.truncate_ends(added.0, added.1);
                        return Err(e);
                    }
                    if kind == CustomChange::Append {
                        c.log.record(span(old, n));
                    } else {
                        // Every point number moved.
                        c.log.replace();
                    }
                } else {
                    let others = self.other_spans(k);
                    self.rebuild(k, Target::Custom(c), &others)?;
                }
            }
            _ => {
                let others = self.other_spans(k);
                let times = c.plugin.times();
                if others.is_empty() {
                    // The only series with data: its times are the axis.
                    let mut own = Vec::new();
                    own.try_reserve_exact(times.len())
                        .map_err(|_| Error::OutOfMemory)?;
                    own.extend_from_slice(times);
                    let index = TimeIndex::from_times(own)?;
                    c.map_onto(&index)?;
                    self.time_index = index;
                    self.axis_rebuilt = true;
                } else if let Some(m) = self.time_index.map_series(times)?
                    && !self.has_orphans(&others, times)
                {
                    c.accept(m);
                } else {
                    self.rebuild(k, Target::Custom(c), &others)?;
                }
                c.log.replace();
            }
        }
        Ok(Some(change))
    }

    /// Adds a price line to a series. Its price is in the series'
    /// units, so on a relative scale it moves with the series.
    ///
    /// # Errors
    ///
    /// [`Error::UnknownId`] if the series does not exist;
    /// [`Error::InvalidArgument`] for a volume series or a price that is not
    /// finite.
    ///
    /// # Examples
    ///
    /// See [`PriceLineOptions`].
    pub fn create_price_line(
        &mut self,
        series: SeriesId,
        opts: PriceLineOptions,
    ) -> Result<PriceLineId> {
        let e = self.entry(series)?;
        if e.kind == SeriesKind::Volume {
            return Err(Error::InvalidArgument("volume series have no price lines"));
        }
        if !opts.price.is_finite() {
            return Err(Error::InvalidArgument("the price must be finite"));
        }
        let id = PriceLineId(self.next_price_line);
        self.next_price_line += 1;
        self.price_lines.push(PriceLineEntry { id, series, opts });
        self.invalidation = self.invalidation.max(Invalidation::Data);
        Ok(id)
    }

    /// Replaces the options of a price line.
    ///
    /// # Errors
    ///
    /// [`Error::UnknownId`] if the price line does not exist;
    /// [`Error::InvalidArgument`] for a price that is not finite.
    ///
    /// # Examples
    ///
    /// ```
    /// use ferrochart_core::chart::{PriceLineOptions, SeriesOptions};
    /// use ferrochart_core::frame::SeriesKind;
    /// use ferrochart_core::{Chart, ChartOptions};
    ///
    /// let mut chart = Chart::new(ChartOptions::default());
    /// let id = chart.add_series(chart.main_pane(), SeriesKind::Line, SeriesOptions::default())?;
    /// let line = chart.create_price_line(id, PriceLineOptions { price: 1.0, ..Default::default() })?;
    /// chart.update_price_line(line, PriceLineOptions { price: 2.0, ..Default::default() })?;
    /// assert_eq!(chart.price_line(line).map(|o| o.price), Some(2.0));
    /// # Ok::<(), ferrochart_core::Error>(())
    /// ```
    pub fn update_price_line(&mut self, id: PriceLineId, opts: PriceLineOptions) -> Result<()> {
        if !opts.price.is_finite() {
            return Err(Error::InvalidArgument("the price must be finite"));
        }
        let line = self
            .price_lines
            .iter_mut()
            .find(|l| l.id == id)
            .ok_or(Error::UnknownId)?;
        line.opts = opts;
        self.invalidation = self.invalidation.max(Invalidation::Data);
        Ok(())
    }

    /// Options of a price line; `None` if it does not exist.
    pub fn price_line(&self, id: PriceLineId) -> Option<&PriceLineOptions> {
        self.price_lines
            .iter()
            .find(|l| l.id == id)
            .map(|l| &l.opts)
    }

    /// Removes a price line.
    ///
    /// # Errors
    ///
    /// [`Error::UnknownId`] if it does not exist.
    pub fn remove_price_line(&mut self, id: PriceLineId) -> Result<()> {
        let n = self.price_lines.len();
        self.price_lines.retain(|l| l.id != id);
        if self.price_lines.len() == n {
            return Err(Error::UnknownId);
        }
        self.invalidation = self.invalidation.max(Invalidation::Data);
        Ok(())
    }

    /// Attaches an indicator to `source`. Each output becomes a value
    /// series ([`indicator_series`](Self::indicator_series)) on the pane
    /// `placement` names; the chart keeps them up to date as the source
    /// changes, incrementally for `append` and `update_last`.
    ///
    /// # Errors
    ///
    /// [`Error::UnknownId`] for an unknown source or pane;
    /// [`Error::InvalidArgument`] for a volume source, an indicator without
    /// outputs, or one that needs volume on a source without it.
    ///
    /// # Examples
    ///
    /// A minimal indicator: the close, doubled.
    ///
    /// ```
    /// use ferrochart_core::chart::SeriesOptions;
    /// use ferrochart_core::data::DataChange;
    /// use ferrochart_core::frame::SeriesKind;
    /// use ferrochart_core::plugin::{
    ///     Indicator, IndicatorInput, IndicatorOutputs, OutputSpec, Placement,
    /// };
    /// use ferrochart_core::testing::random_walk;
    /// use ferrochart_core::chart::SeriesData;
    /// use ferrochart_core::{Chart, ChartOptions};
    ///
    /// struct Double;
    /// impl Indicator for Double {
    ///     fn name(&self) -> &str { "double" }
    ///     fn outputs(&self) -> &[OutputSpec] {
    ///         const OUT: [OutputSpec; 1] = [OutputSpec::line("double")];
    ///         &OUT
    ///     }
    ///     fn default_placement(&self) -> Placement { Placement::NewPane }
    ///     fn warmup(&self) -> usize { 0 }
    ///     fn update(&mut self, input: &IndicatorInput<'_>, _: DataChange, out: &mut IndicatorOutputs) {
    ///         let start = out.start();
    ///         for (i, v) in out.column(0).iter_mut().enumerate() {
    ///             if let Some(b) = input.bar(start + i) {
    ///                 *v = 2.0 * b.close;
    ///             }
    ///         }
    ///     }
    /// }
    ///
    /// let mut chart = Chart::new(ChartOptions::default());
    /// let candles = chart.add_series(chart.main_pane(), SeriesKind::Candlestick, SeriesOptions::default())?;
    /// chart.set_data(candles, SeriesData::Ohlc(random_walk(1, 50, 0, 60_000)?))?;
    /// let id = chart.add_indicator(candles, Box::new(Double), Placement::NewPane)?;
    /// assert_eq!(chart.indicator_series(id).map(<[_]>::len), Some(1));
    /// assert_eq!(chart.panes().count(), 2);
    /// # Ok::<(), ferrochart_core::Error>(())
    /// ```
    pub fn add_indicator(
        &mut self,
        source: SeriesId,
        ind: Box<dyn Indicator>,
        placement: Placement,
    ) -> Result<IndicatorId> {
        let src = self.entry(source)?;
        if src.kind == SeriesKind::Volume {
            return Err(Error::InvalidArgument(
                "volume series have no data of their own; use the source series",
            ));
        }
        if src.kind == SeriesKind::Custom {
            return Err(Error::InvalidArgument(
                "indicators take built-in series as their source",
            ));
        }
        if ind.outputs().is_empty() {
            return Err(Error::InvalidArgument("the indicator has no outputs"));
        }
        let n_out = ind.outputs().len();
        if ind
            .outputs()
            .iter()
            .enumerate()
            .any(|(i, o)| o.fill_to.is_some_and(|(to, _)| to >= n_out || to == i))
        {
            return Err(Error::InvalidArgument(
                "fill_to must name another output of the indicator",
            ));
        }
        let has_data = !src.store.is_empty();
        if ind.needs_volume() && (is_value_kind(src.kind) || has_data && !src.store.has_volume()) {
            return Err(Error::InvalidArgument("the indicator needs volume"));
        }
        let src_pane = src.pane;
        let volume_pane = self
            .series
            .iter()
            .find(|e| e.kind == SeriesKind::Volume && e.opts.source == Some(source))
            .map(|e| e.pane);
        let on_volume = placement == Placement::Volume && volume_pane.is_some();
        let (pane, own_pane) = match (placement, volume_pane) {
            (Placement::Volume, Some(p)) => (p, None),
            (Placement::Overlay, _) => (src_pane, None),
            (Placement::Pane(p), _) => {
                if !self.panes.iter().any(|x| x.id == p) {
                    return Err(Error::UnknownId);
                }
                (p, None)
            }
            (Placement::NewPane | Placement::Volume, _) => {
                let p = self.add_pane(PaneOptions::default());
                (p, Some(p))
            }
        };
        let mut series = Vec::with_capacity(ind.outputs().len());
        for spec in ind.outputs() {
            let color = spec.color.unwrap_or_else(|| {
                let c = INDICATOR_PALETTE[self.palette_next % INDICATOR_PALETTE.len()];
                self.palette_next += 1;
                Rgba::hex(c)
            });
            let kind = match spec.kind {
                OutputKind::Line => SeriesKind::Line,
                OutputKind::Histogram => SeriesKind::Histogram,
                OutputKind::Points => SeriesKind::Points,
            };
            let opts = SeriesOptions {
                color: Some(color),
                // Dots are `line_width` across: 1 px would hardly show.
                line_width: Css(if kind == SeriesKind::Points { 3.0 } else { 1.0 }),
                price_line_visible: false,
                shift: spec.shift,
                ..SeriesOptions::default()
            };
            let id = self.insert_series(pane, kind, opts)?;
            if let Some(e) = self.series.iter_mut().find(|e| e.id == id) {
                e.derived = true;
                e.volume_scale = on_volume;
            }
            series.push(id);
        }
        let mut bands = Vec::new();
        for (i, spec) in ind.outputs().iter().enumerate() {
            if let Some((to, opts)) = spec.fill_to
                && let (Some(&upper), Some(&lower)) = (series.get(i), series.get(to))
            {
                bands.push(self.add_band(upper, lower, opts)?);
            }
        }
        let id = IndicatorId(self.next_indicator);
        self.next_indicator += 1;
        self.indicators.push(IndicatorEntry {
            id,
            source,
            ind,
            series,
            own_pane,
            bands,
        });
        self.update_indicator(self.indicators.len() - 1, DataChange::Replace);
        self.invalidation = Invalidation::Full;
        Ok(id)
    }

    /// Removes an indicator, its output series, the indicators attached to
    /// them, and the pane `add_indicator` created for it if it is now empty.
    ///
    /// # Errors
    ///
    /// [`Error::UnknownId`] if it does not exist.
    pub fn remove_indicator(&mut self, id: IndicatorId) -> Result<()> {
        let n = self
            .indicators
            .iter()
            .position(|x| x.id == id)
            .ok_or(Error::UnknownId)?;
        self.drop_indicator(n);
        self.invalidation = Invalidation::Full;
        Ok(())
    }

    /// The output series of an indicator, in the order of its outputs;
    /// `None` if it does not exist.
    pub fn indicator_series(&self, id: IndicatorId) -> Option<&[SeriesId]> {
        self.indicators
            .iter()
            .find(|x| x.id == id)
            .map(|x| x.series.as_slice())
    }

    /// The bands an indicator's `OutputSpec::fill_to` created, e.g.
    /// to change their colors with [`set_band_options`](Self::set_band_options);
    /// `None` if it does not exist. They are removed with the indicator.
    ///
    /// # Examples
    ///
    /// ```
    /// use ferrochart_core::chart::{BandOptions, SeriesData, SeriesOptions};
    /// use ferrochart_core::frame::SeriesKind;
    /// use ferrochart_core::geom::Rgba;
    /// use ferrochart_core::plugin::{Indicator, IndicatorInput, IndicatorOutputs, OutputSpec, Placement};
    /// use ferrochart_core::data::DataChange;
    /// use ferrochart_core::testing::random_walk;
    /// use ferrochart_core::{Chart, ChartOptions};
    ///
    /// /// The high and the low, filled between.
    /// struct Range([OutputSpec; 2]);
    /// impl Indicator for Range {
    ///     fn name(&self) -> &str { "range" }
    ///     fn outputs(&self) -> &[OutputSpec] { &self.0 }
    ///     fn default_placement(&self) -> Placement { Placement::Overlay }
    ///     fn warmup(&self) -> usize { 0 }
    ///     fn update(&mut self, input: &IndicatorInput<'_>, _: DataChange, out: &mut IndicatorOutputs) {
    ///         let start = out.start();
    ///         for k in 0..2 {
    ///             for (i, v) in out.column(k).iter_mut().enumerate() {
    ///                 if let Some(b) = input.bar(start + i) {
    ///                     *v = if k == 0 { b.high } else { b.low };
    ///                 }
    ///             }
    ///         }
    ///     }
    /// }
    ///
    /// let mut chart = Chart::new(ChartOptions::default());
    /// let candles = chart.add_series(chart.main_pane(), SeriesKind::Candlestick, SeriesOptions::default())?;
    /// chart.set_data(candles, SeriesData::Ohlc(random_walk(1, 50, 0, 60_000)?))?;
    /// let high = OutputSpec { fill_to: Some((1, BandOptions::default())), ..OutputSpec::line("high") };
    /// let id = chart.add_indicator(candles, Box::new(Range([high, OutputSpec::line("low")])), Placement::Overlay)?;
    /// let band = chart.indicator_bands(id).unwrap()[0];
    /// chart.set_band_options(band, BandOptions { fill: Some(Rgba::hex(0x0026_A69A).with_alpha(0x30)), fill_crossed: None })?;
    /// # Ok::<(), ferrochart_core::Error>(())
    /// ```
    pub fn indicator_bands(&self, id: IndicatorId) -> Option<&[BandId]> {
        self.indicators
            .iter()
            .find(|x| x.id == id)
            .map(|x| x.bands.as_slice())
    }

    fn drop_indicator(&mut self, n: usize) {
        if n >= self.indicators.len() {
            return;
        }
        let entry = self.indicators.remove(n);
        for &out in &entry.series {
            self.drop_indicators_of(out);
        }
        self.series.retain(|e| !entry.series.contains(&e.id));
        self.price_lines
            .retain(|l| self.series.iter().any(|e| e.id == l.series));
        self.prune_bands();
        if let Some(p) = entry.own_pane
            && self.panes.len() > 1
            && !self.series.iter().any(|e| e.pane == p)
        {
            let _ = self.remove_pane(p);
        }
    }

    /// Drops the indicators whose source is `series`.
    fn drop_indicators_of(&mut self, series: SeriesId) {
        while let Some(n) = self.indicators.iter().position(|x| x.source == series) {
            self.drop_indicator(n);
        }
    }

    /// Brings the indicators up to date after `change` to series `id`.
    fn after_data(&mut self, id: SeriesId, change: DataChange) {
        if core::mem::take(&mut self.axis_rebuilt) {
            // Everything, starting with the indicators on data series.
            let roots = (0..self.indicators.len())
                .filter(|&n| {
                    let src = self.indicators[n].source;
                    !self.indicators.iter().any(|x| x.series.contains(&src))
                })
                .collect();
            self.recompute_levels(roots);
        } else {
            self.run_indicators(id, change);
        }
    }

    /// Updates the indicators on `source`, then those on their outputs.
    fn run_indicators(&mut self, source: SeriesId, change: DataChange) {
        if !matches!(change, DataChange::Append { .. } | DataChange::UpdateLast) {
            let roots = (0..self.indicators.len())
                .filter(|&n| self.indicators[n].source == source)
                .collect();
            self.recompute_levels(roots);
            return;
        }
        for n in 0..self.indicators.len() {
            if self.indicators[n].source == source {
                self.update_indicator(n, change);
                for o in 0..self.indicators[n].series.len() {
                    let out = self.indicators[n].series[o];
                    self.run_indicators(out, change);
                }
            }
        }
    }

    /// Full recomputation of the indicators `level` and then, level by
    /// level, of the indicators on their outputs. With the `rayon`
    /// feature the indicators of a level are computed in parallel; outputs
    /// are written in order, so the result does not depend on the feature.
    fn recompute_levels(&mut self, mut level: Vec<usize>) {
        while !level.is_empty() {
            self.recompute_level(&level);
            let outs: Vec<SeriesId> = level
                .iter()
                .flat_map(|&n| self.indicators[n].series.iter().copied())
                .collect();
            level = (0..self.indicators.len())
                .filter(|&n| outs.contains(&self.indicators[n].source))
                .collect();
        }
    }

    fn recompute_level(&mut self, level: &[usize]) {
        #[cfg(feature = "rayon")]
        if level.len() > 1 {
            use rayon::prelude::*;
            let index = &self.time_index;
            // Sources first: series entries hold plugins that are not `Sync`.
            // Plain sources only; indicators on custom series run after.
            let sources: Vec<Option<crate::series::PlainSource<'_>>> = (0..self.indicators.len())
                .map(|n| {
                    let src = self.indicators[n].source;
                    level
                        .contains(&n)
                        .then(|| self.series.iter().find(|e| e.id == src))
                        .flatten()
                        .and_then(|e| match e.store.source() {
                            crate::series::Source::Plain(p) => Some(p),
                            crate::series::Source::Custom(_) => None,
                        })
                })
                .collect();
            let results: Vec<_> = self
                .indicators
                .par_iter_mut()
                .zip(sources.par_iter())
                .enumerate()
                .filter_map(|(n, (entry, source))| {
                    let mut out = IndicatorOutputs::default();
                    let source = crate::series::Source::Plain((*source)?);
                    let done =
                        compute_indicator(entry, source, index, DataChange::Replace, &mut out);
                    Some((n, done, out))
                })
                .collect();
            let done: Vec<usize> = results.iter().map(|r| r.0).collect();
            for (n, (change, start), out) in results {
                self.write_indicator(n, change, start, &out);
            }
            for &n in level.iter().filter(|n| !done.contains(n)) {
                self.update_indicator(n, DataChange::Replace);
            }
            return;
        }
        for &n in level {
            self.update_indicator(n, DataChange::Replace);
        }
    }

    /// Runs indicator `n` for `change` and writes its outputs.
    fn update_indicator(&mut self, n: usize, change: DataChange) {
        let Some(entry) = self.indicators.get_mut(n) else {
            return;
        };
        let Some(source) = self.series.iter().find(|e| e.id == entry.source) else {
            return;
        };
        let mut out = core::mem::take(&mut self.ind_out);
        let (change, start) = compute_indicator(
            entry,
            source.store.source(),
            &self.time_index,
            change,
            &mut out,
        );
        self.write_indicator(n, change, start, &out);
        if change == DataChange::Replace {
            out.shrink();
        }
        self.ind_out = out;
    }

    /// Writes the outputs `out` of indicator `n` computed for `change` from
    /// slot `start` into its output series.
    fn write_indicator(
        &mut self,
        n: usize,
        change: DataChange,
        start: usize,
        out: &IndicatorOutputs,
    ) {
        let Some(entry) = self.indicators.get(n) else {
            return;
        };
        let Some(k) = self.series.iter().position(|e| e.id == entry.source) else {
            return;
        };
        let input = IndicatorInput::new(self.series[k].store.source(), &self.time_index);
        let len = input.len();
        let first = input.first();
        let outputs = out.columns().len();
        for o in 0..outputs.min(entry.series.len()) {
            let id = entry.series[o];
            let Some(ko) = self.series.iter().position(|e| e.id == id) else {
                continue;
            };
            let col = out.columns().get(o).map_or(&[][..], Vec::as_slice);
            let src = &self.series[k].store;
            let index = &self.time_index;
            // Points at the source's bars; gap slots are left to the value
            // series' rules (lines interpolate, histograms break).
            if change == DataChange::UpdateLast {
                let j = first + i64::try_from(start).unwrap_or(0);
                let (Some(time), Some(&v)) = (index.time_at(j), col.first()) else {
                    continue;
                };
                if src.bar_at(j).is_none() {
                    continue;
                }
                let bar = BarUpdate {
                    time,
                    open: v,
                    high: v,
                    low: v,
                    close: v,
                    volume: None,
                };
                let entry = &mut self.series[ko];
                let op = Op {
                    kind: entry.kind,
                    index,
                    mode: ValidationMode::Repair,
                };
                let _ = op.update_last(&mut entry.store, bar);
                continue;
            }
            let mut time = Vec::new();
            let mut value = Vec::new();
            for (i, &v) in col.iter().enumerate() {
                let j = first + i64::try_from(start + i).unwrap_or(0);
                if src.bar_at(j).is_some()
                    && let Some(t) = index.time_at(j)
                {
                    time.push(t);
                    value.push(v);
                }
            }
            if time.is_empty() && change != DataChange::Replace {
                continue;
            }
            let data = SeriesData::Value(crate::data::ValueColumns {
                time,
                value,
                color: None,
            });
            let entry = &mut self.series[ko];
            let op = Op {
                kind: entry.kind,
                index,
                mode: ValidationMode::Repair,
            };
            let done = if change == DataChange::Replace {
                if len == 0 {
                    entry.store = Store::for_kind(entry.kind);
                    Ok(0)
                } else {
                    op.set(&mut entry.store, data)
                }
            } else {
                op.append(&mut entry.store, data)
            };
            if done.is_err() {
                // Only an allocation failure: show nothing rather than stale values.
                entry.store = Store::for_kind(entry.kind);
            }
        }
    }

    /// Replaces the markers of a series. They are kept sorted by time
    /// (stable, so markers on the same bar stack in the given order).
    ///
    /// # Errors
    ///
    /// [`Error::UnknownId`] if the series does not exist;
    /// [`Error::InvalidArgument`] for a volume series.
    ///
    /// # Examples
    ///
    /// See [`Marker`].
    pub fn set_markers(&mut self, series: SeriesId, mut markers: Vec<Marker>) -> Result<()> {
        let k = self.entry_index(series)?;
        if self.series[k].kind == SeriesKind::Volume {
            return Err(Error::InvalidArgument("volume series have no markers"));
        }
        markers.sort_by_key(|m| m.time);
        self.series[k].markers = markers;
        self.invalidation = self.invalidation.max(Invalidation::Data);
        Ok(())
    }

    /// Markers of a series, sorted by time; `None` if it does not exist.
    pub fn markers(&self, series: SeriesId) -> Option<&[Marker]> {
        self.entry(series).ok().map(|e| e.markers.as_slice())
    }

    /// The (global) time scale.
    pub fn time_scale(&mut self) -> &mut TimeScale {
        &mut self.time_scale
    }

    /// A price scale of a pane; `None` if it does not exist (for now every
    /// pane has only its right scale).
    pub fn price_scale(&mut self, pane: PaneId, side: ScaleSide) -> Option<&mut PriceScale> {
        if side != ScaleSide::Right {
            return None;
        }
        self.pane_mut(pane).map(|p| &mut p.price_scale)
    }

    /// Shows all bars.
    pub fn fit_content(&mut self) {
        self.time_scale.fit_content();
    }

    /// Scrolls so that the last bar is visible with the configured
    /// [`ChartOptions::right_offset`].
    pub fn scroll_to_realtime(&mut self) {
        self.time_scale.set_right_offset(self.default_right_offset);
    }

    /// What needs to be redrawn. The host only builds a frame when this is not
    /// `None`; without interaction or new data, nothing is redrawn.
    pub fn needs_redraw(&self) -> Invalidation {
        let mut inv = self.invalidation;
        if self.time_scale.changed
            || self.drawings.dirty
            || self.input.kinetic.is_some()
            || self.panes.iter().any(|p| p.price_scale.changed)
        {
            inv = inv.max(Invalidation::Light);
        }
        inv.max(self.sync_pending())
    }

    /// The last built frame.
    pub fn frame(&self) -> &Frame {
        &self.frame
    }

    /// Read-only view of the chart data for renderers.
    pub fn data_view(&self) -> ChartDataView<'_> {
        ChartDataView {
            series: &self.series,
            time_index: &self.time_index,
        }
    }

    /// The last built layout.
    pub fn layout(&self) -> Layout {
        self.layout
    }

    /// Shows the data gradually: with `Some`, series, indicators,
    /// bands, markers, drawings and pane plugin layers are clipped to the
    /// left `progress` of each pane, while grid and axes are drawn whole and
    /// the price scale still fits all visible data; price lines, value
    /// labels and the crosshair wait for progress 1. `None` (the default)
    /// shows everything. The host moves the progress over time.
    ///
    /// ```
    /// use ferrochart_core::chart::{Chart, ChartOptions};
    /// use ferrochart_core::frame::Reveal;
    ///
    /// let mut chart = Chart::new(ChartOptions::default());
    /// chart.set_reveal(Some(Reveal { progress: 0.5, pen: None }));
    /// assert_eq!(chart.reveal().map(|r| r.progress), Some(0.5));
    /// chart.set_reveal(None);
    /// assert!(chart.reveal().is_none());
    /// ```
    pub fn set_reveal(&mut self, reveal: Option<Reveal>) {
        if self.reveal != reveal {
            self.reveal = reveal;
            self.invalidation = self.invalidation.max(Invalidation::Light);
        }
    }

    /// The gradual display set by [`set_reveal`](Self::set_reveal).
    pub fn reveal(&self) -> Option<Reveal> {
        self.reveal
    }

    /// Where the data of a pane with plot area `plot` is drawn.
    fn data_clip(&self, plot: RectPx) -> RectPx {
        self.reveal.map_or(plot, |r| r.clip(plot))
    }

    /// Whether a reveal is under way: price lines, value labels and the
    /// crosshair wait for its end.
    fn revealing(&self) -> bool {
        self.reveal.is_some_and(|r| !r.is_done())
    }

    /// Heap bytes held by the chart's series data: columns, LOD pyramid and time
    /// index. Used by the memory benchmark (P2).
    pub fn heap_bytes(&self) -> usize {
        self.series
            .iter()
            .map(|e| e.store.heap_bytes())
            .sum::<usize>()
            + self.time_index.len() * core::mem::size_of::<i64>()
    }

    /// Takes all pending events.
    pub fn drain_events(&mut self) -> impl Iterator<Item = ChartEvent> + '_ {
        self.events.append(&mut self.drawings.events);
        self.events.drain(..)
    }

    /// Resolved colors and line settings of a series.
    fn style_of(&self, e: &SeriesEntry) -> SeriesStyle {
        let t = &self.theme;
        let o = &e.opts;
        let mut st = SeriesStyle {
            line_width: Px(o.line_width.to_px(self.dpr).0.round().max(1.0)),
            line_dash: o.line_style,
            step: o.step,
            base_value: o.base_value,
            ..SeriesStyle::default()
        };
        let (up, down) = match e.kind {
            SeriesKind::Line | SeriesKind::Area | SeriesKind::Histogram | SeriesKind::Points => {
                let c = o.color.unwrap_or(if e.kind == SeriesKind::Histogram {
                    t.up
                } else {
                    t.line
                });
                (c, c)
            }
            SeriesKind::Volume => (t.volume_up, t.volume_down),
            _ => (o.color.unwrap_or(t.up), o.down_color.unwrap_or(t.down)),
        };
        st.up = up;
        st.down = down;
        st.up_border = up;
        st.down_border = down;
        let themed_wicks = o.color.is_none() && o.down_color.is_none();
        st.up_wick = if themed_wicks { t.up_wick } else { up };
        st.down_wick = if themed_wicks { t.down_wick } else { down };
        let (top, bottom) = match e.kind {
            SeriesKind::Area => (up.with_alpha(0x66), up.with_alpha(0x05)),
            _ => (up.with_alpha(0x33), down.with_alpha(0x33)),
        };
        st.fill_top = o.top_fill.unwrap_or(top);
        st.fill_bottom = o.bottom_fill.unwrap_or(bottom);
        st
    }

    /// Auto-scales every pane's price scale to the visible data of its series
    /// and chooses its label format.
    fn autoscale(&mut self) {
        let (from, to) = self.time_scale.visible_range();
        let (a, b) = (from.floor() as i64, to.ceil() as i64 + 1);
        let spacing = self.time_scale.bar_spacing();
        for i in 0..self.panes.len() {
            let id = self.panes[i].id;
            let (mut lo, mut hi) = (f64::INFINITY, f64::NEG_INFINITY);
            let mut precision = None;
            let mut any_price = false;
            let mut max_volume: Option<f64> = None;
            for k in 0..self.series.len() {
                if self.series[k].pane != id {
                    continue;
                }
                if self.series[k].kind == SeriesKind::Volume {
                    let source = self.series[k]
                        .opts
                        .source
                        .and_then(|s| self.series.iter().find(|e| e.id == s));
                    if let Some(src) = source
                        && src.store.has_volume()
                    {
                        let level = lod_level(src.store.lod(), spacing);
                        if let Some(view) = src.store.level_view(level) {
                            let span = visible_span(&view, from, to);
                            let m = max_volume_in(&view, span);
                            max_volume = Some(max_volume.unwrap_or(0.0).max(m));
                        }
                    }
                    continue;
                }
                if self.series[k].volume_scale {
                    // A line on the volume scale (`Placement::Volume`).
                    let e = &mut self.series[k];
                    e.norm = Some(Normalize::IDENTITY);
                    let s = i64::from(e.opts.shift);
                    if let Some((_, h)) = e.store.extremes(a - s, b - s) {
                        max_volume = Some(max_volume.unwrap_or(0.0).max(h));
                    }
                    continue;
                }
                let mode = self.panes[i].price_scale.mode();
                let e = &mut self.series[k];
                precision.get_or_insert(e.opts.price_precision);
                if e.store.is_empty() {
                    e.norm = None;
                    continue;
                }
                any_price = true;
                let s = i64::from(e.opts.shift);
                let (a, b) = (a - s, b - s);
                let base = mode
                    .is_relative()
                    .then(|| first_value(&e.store, a, b))
                    .flatten();
                e.norm = Normalize::new(mode, base);
                let Some(norm) = e.norm else { continue };
                let extremes = match e.store.custom() {
                    Some(c) => c.extremes(&self.time_index, a, b),
                    None => e.store.extremes(a, b),
                };
                if let Some((l, h)) = extremes {
                    e.auto_decimals = auto_decimals(l.abs().max(h.abs()));
                    // k > 0: the order is kept.
                    lo = lo.min(norm.apply(l));
                    hi = hi.max(norm.apply(h));
                    if e.kind == SeriesKind::Histogram {
                        let base = norm.apply(e.opts.base_value);
                        lo = lo.min(base);
                        hi = hi.max(base);
                    }
                }
            }
            let pane = &mut self.panes[i];
            pane.has_content = any_price || max_volume.is_some();
            pane.labels = match pane.price_scale.mode() {
                _ if !any_price && max_volume.is_some() => NumberFormat::Compact,
                crate::price_scale::PriceScaleMode::Percentage => NumberFormat::Percent(2),
                crate::price_scale::PriceScaleMode::IndexedTo100 => NumberFormat::Fixed(2),
                _ => match precision.flatten() {
                    Some(p) => NumberFormat::Fixed(p),
                    None if lo <= hi => NumberFormat::Auto(auto_decimals(lo.abs().max(hi.abs()))),
                    None => NumberFormat::Auto(2),
                },
            };
            if any_price && lo <= hi {
                pane.price_scale.fit(lo, hi);
            }
            if let Some(m) = max_volume {
                if any_price {
                    pane.volume_scale.fit(0.0, m);
                } else {
                    pane.price_scale.fit(0.0, m);
                }
            }
        }
    }

    /// Whether pane `i` has something to scale (and therefore ticks).
    fn pane_has_content(&self, i: usize) -> bool {
        self.panes.get(i).is_some_and(|p| p.has_content)
    }

    /// Builds the frame for the current state. Call when
    /// [`needs_redraw`](Self::needs_redraw) is not `None`, then read it with
    /// [`frame`](Self::frame) and [`data_view`](Self::data_view).
    ///
    /// `now_ms` drives kinetic scrolling; it must use the same clock as the
    /// `time_ms` of input events (see [`interaction`](crate::interaction)).
    ///
    /// Every layer is always rebuilt, so the frame is complete for any
    /// renderer; only the revisions of layers whose content changed are bumped:
    /// `Cursor` bumps only the overlay.
    pub fn build_frame(&mut self, now_ms: f64, text: &dyn TextMeasurer) {
        self.step_kinetic(now_ms);
        self.pull_sync();
        let inv = self.needs_redraw();
        if inv >= Invalidation::Light {
            self.revisions[LayerId::Background as usize] += 1;
            self.revisions[LayerId::Main as usize] += 1;
        }
        if inv >= Invalidation::Cursor {
            self.revisions[LayerId::Overlay as usize] += 1;
        }
        let w = (self.size.w * self.dpr).round();
        let h = (self.size.h * self.dpr).round();
        self.frame
            .begin(SizePx { w, h }, self.theme.background, inv);
        for (layer, rev) in self.frame.layers.iter_mut().zip(self.revisions) {
            layer.revision = rev;
        }
        self.invalidation = Invalidation::None;
        self.time_scale.changed = false;
        self.drawings.dirty = false;
        for pane in &mut self.panes {
            pane.price_scale.changed = false;
        }
        if w < 1.0 || h < 1.0 {
            return;
        }

        let font = FontStyleRef {
            font: FontId(0),
            size: self.theme.font_size.to_px(self.dpr).0,
        };
        let line_h = text.line_height(font);
        if font.size > 0.0 {
            self.text_cache = CachedText {
                advances: *text.ascii_advances(font),
                subscripts: text.subscript_advances(font).copied().unwrap_or([0.6; 10]),
                line: line_h / font.size,
            };
        }
        let pad = self.theme.axis_padding.to_px(self.dpr).0.round();
        let time_h = (line_h + 2.0 * pad).ceil().min(h);
        let panes_h = (h - time_h).max(0.0);

        // The price axis width (shared by all panes) depends on the labels,
        // which depend on the visible range, which depends on the plot width:
        // iterate (at most twice).
        for _ in 0..2 {
            let plot_w = (w - self.axis_width).max(0.0);
            self.time_scale.set_width(f64::from(plot_w));
            stack(&mut self.panes, 0.0, panes_h, plot_w);
            self.autoscale();
            let mut widest: f32 = 0.0;
            for i in 0..self.panes.len() {
                let has_content = self.pane_has_content(i);
                let pane = &mut self.panes[i];
                if has_content {
                    pane.price_scale.ticks(
                        f64::from(pane.plot.h),
                        f64::from(line_h) * 2.5,
                        &mut pane.ticks,
                    );
                } else {
                    pane.ticks.clear();
                }
                let labels = pane.labels.for_ticks(&pane.ticks);
                for &p in &pane.ticks {
                    let mut s = StackStr::<64>::new();
                    write_label(&mut s, p, labels);
                    widest = widest.max(text_width(text, s.as_str(), font));
                }
            }
            let required = (widest + 2.0 * pad).ceil().min(w);
            if required > self.axis_width || required < self.axis_width * 0.75 {
                self.axis_width = required;
            } else {
                break;
            }
        }
        let plot_w = (w - self.axis_width).max(0.0);
        self.time_scale.set_width(f64::from(plot_w));
        self.time_scale.settle();
        stack(&mut self.panes, 0.0, panes_h, plot_w);
        self.layout = Layout {
            plot: self.panes.first().map_or(RectPx::default(), |p| p.plot),
            price_axis: RectPx {
                x: plot_w,
                y: 0.0,
                w: w - plot_w,
                h: panes_h,
            },
            time_axis: RectPx {
                x: 0.0,
                y: panes_h,
                w,
                h: h - panes_h,
            },
        };

        self.place_time_ticks(text, font);
        self.emit_layers(None, LayerPosition::BelowSeries, text);
        self.marker_hits.clear();
        for i in 0..self.panes.len() {
            self.emit_grid(i);
            self.emit_layers(Some(i), LayerPosition::Background, text);
            self.emit_layers(Some(i), LayerPosition::BelowSeries, text);
            self.emit_series(i, text);
            self.emit_markers(i, line_h, font);
            self.emit_layers(Some(i), LayerPosition::AboveSeries, text);
            self.emit_drawings(i, text);
            self.emit_marks(i, line_h, pad, font);
            self.emit_price_labels(i, line_h, pad, font);
        }
        self.emit_layers(None, LayerPosition::Background, text);
        self.emit_layers(None, LayerPosition::AboveSeries, text);
        self.emit_axes(time_h, font);
        for i in 0..self.panes.len() {
            self.emit_layers(Some(i), LayerPosition::Overlay, text);
        }
        self.emit_layers(None, LayerPosition::Overlay, text);
        if let Some(r) = self.reveal {
            for pane in &self.panes {
                r.push_pen(&mut self.frame, LayerId::Overlay, pane.plot, self.dpr);
            }
        }
        let local_crosshair = self.emit_crosshair(time_h, pad, font);
        self.emit_view_events();
        self.push_sync(local_crosshair.map(|i| i as f64));
    }

    /// The drawings.
    pub fn drawings(&mut self) -> &mut DrawingManager {
        &mut self.drawings
    }

    /// Adds a band: the area between two single-value series of the same
    /// pane, filled with `opts.fill` where `upper` is at or above `lower`
    /// and `opts.fill_crossed` where it is below. Bands are drawn
    /// after the volume and before the other series of the pane, and are
    /// removed with either series. See [`BandOptions`] for an example.
    ///
    /// # Errors
    ///
    /// [`Error::UnknownId`] if a series does not exist;
    /// [`Error::InvalidArgument`] unless both are single-value series
    /// (line, area, baseline, histogram, points) on the same pane and
    /// different.
    pub fn add_band(
        &mut self,
        upper: SeriesId,
        lower: SeriesId,
        opts: BandOptions,
    ) -> Result<BandId> {
        let (u, l) = (self.entry(upper)?, self.entry(lower)?);
        if !is_value_kind(u.kind) || !is_value_kind(l.kind) {
            return Err(Error::InvalidArgument(
                "a band is between two single-value series",
            ));
        }
        if u.pane != l.pane || upper == lower {
            return Err(Error::InvalidArgument(
                "a band is between two different series of the same pane",
            ));
        }
        let id = BandId(self.next_band);
        self.next_band += 1;
        self.bands.push(BandEntry {
            id,
            upper,
            lower,
            opts,
        });
        self.invalidation = Invalidation::Full;
        Ok(id)
    }

    /// Changes the colors of a band.
    ///
    /// # Errors
    ///
    /// [`Error::UnknownId`] if it does not exist.
    pub fn set_band_options(&mut self, id: BandId, opts: BandOptions) -> Result<()> {
        let b = self
            .bands
            .iter_mut()
            .find(|b| b.id == id)
            .ok_or(Error::UnknownId)?;
        b.opts = opts;
        self.invalidation = Invalidation::Full;
        Ok(())
    }

    /// The colors of a band; `None` if it does not exist.
    pub fn band_options(&self, id: BandId) -> Option<&BandOptions> {
        self.bands.iter().find(|b| b.id == id).map(|b| &b.opts)
    }

    /// Removes a band.
    ///
    /// # Errors
    ///
    /// [`Error::UnknownId`] if it does not exist.
    pub fn remove_band(&mut self, id: BandId) -> Result<()> {
        let n = self
            .bands
            .iter()
            .position(|b| b.id == id)
            .ok_or(Error::UnknownId)?;
        self.bands.remove(n);
        self.invalidation = Invalidation::Full;
        Ok(())
    }

    /// Reports a click at `p` in `pane`; `drawing` is the drawing
    /// pressed, if any.
    pub(crate) fn emit_click(&mut self, pane: PaneId, p: PointPx, drawing: Option<DrawingId>) {
        let Some(pn) = self.pane_index(pane).and_then(|i| self.panes.get(i)) else {
            return;
        };
        let plot = pn.plot;
        let logical_index = self.time_scale.x_to_index(Px(p.x - plot.x));
        let price = pn
            .price_scale
            .y_to_price(f64::from(p.y), f64::from(plot.y), f64::from(plot.h));
        let time = self.time_index.time_at(logical_index.round() as i64);
        let target = drawing
            .or_else(|| self.hit_drawing(pane, p).map(|(d, _)| d))
            .map(ClickTarget::Drawing)
            .or_else(|| self.hit_marker(pane, p))
            .or_else(|| self.hit_series(pane, p).map(ClickTarget::Series));
        self.events.push(ChartEvent::Click {
            pos: PointCss {
                x: p.x / self.dpr,
                y: p.y / self.dpr,
            },
            pane,
            logical_index,
            time,
            price,
            target,
        });
    }

    /// The marker of `pane` under `p` in the last frame.
    fn hit_marker(&self, pane: PaneId, p: PointPx) -> Option<ClickTarget> {
        let tol = HIT_TOLERANCE * 0.5 * self.dpr;
        self.marker_hits
            .iter()
            .rev()
            .find(|m| m.pane == pane && (p.x - m.pos.x).hypot(p.y - m.pos.y) <= m.radius + tol)
            .map(|m| ClickTarget::Marker {
                series: m.series,
                index: m.index,
            })
    }

    /// The topmost series of `pane` under `p`: on a candle's body or
    /// wick, within the tolerance of a line, inside a bar, on a dot.
    fn hit_series(&self, pane: PaneId, p: PointPx) -> Option<SeriesId> {
        let pn = self.panes.get(self.pane_index(pane)?)?;
        let plot = pn.plot;
        let (top, height) = (f64::from(plot.y), f64::from(plot.h));
        let has_price = self
            .series
            .iter()
            .any(|e| e.pane == pane && !volume_like(e) && !e.store.is_empty());
        let tol = HIT_TOLERANCE * self.dpr;
        let m = crate::transform_helpers::bar_metrics(self.time_scale.bar_spacing() as f32);
        let half_bar = (m.body * 0.5).max(tol * 0.5);
        let center = self.time_scale.x_to_index(Px(p.x - plot.x)).round() as i64;
        let within = |a: f32, b: f32| p.y >= a.min(b) - 1.0 && p.y <= a.max(b) + 1.0;
        for e in self.series.iter().rev().filter(|e| e.pane == pane) {
            let (store, shift) = if e.kind == SeriesKind::Volume {
                let src = e
                    .opts
                    .source
                    .and_then(|s| self.series.iter().find(|x| x.id == s));
                let Some(src) = src else { continue };
                (&src.store, 0)
            } else {
                (&e.store, i64::from(e.opts.shift))
            };
            let x_of = |j: i64| plot.x + self.time_scale.index_to_x((j + shift) as f64).0;
            let y_of = |v: f64| -> Option<f32> {
                let y = if volume_like(e) {
                    let scale = if has_price {
                        &pn.volume_scale
                    } else {
                        &pn.price_scale
                    };
                    scale.price_to_y(v, top, height)
                } else {
                    pn.price_scale.price_to_y(e.norm?.apply(v), top, height)
                };
                y.is_finite().then_some(y as f32)
            };
            let point = |j: i64| -> Option<PointPx> {
                let v = store.bar_at(j)?[3];
                Some(PointPx {
                    x: x_of(j),
                    y: y_of(v)?,
                })
            };
            let j = center - shift;
            let hit = match e.kind {
                k if is_ohlc_kind(k) => (|| {
                    let [o, h, l, c] = store.bar_at(j)?;
                    let dx = (p.x - x_of(j)).abs();
                    let (yo, yh, yl, yc) = (y_of(o)?, y_of(h)?, y_of(l)?, y_of(c)?);
                    Some(
                        (dx <= tol * 0.5 && within(yh, yl))
                            || (dx <= m.body * 0.5 && within(yo, yc)),
                    )
                })(),
                SeriesKind::Volume | SeriesKind::Histogram => (|| {
                    let (v, base) = if e.kind == SeriesKind::Volume {
                        (store.volume_at(j)?, 0.0)
                    } else {
                        (store.bar_at(j)?[3], e.opts.base_value)
                    };
                    let dx = (p.x - x_of(j)).abs();
                    Some(dx <= half_bar && within(y_of(v)?, y_of(base)?))
                })(),
                SeriesKind::Points => point(j).map(|c| {
                    let r = e.opts.line_width.to_px(self.dpr).0 * 0.5;
                    (p.x - c.x).hypot(p.y - c.y) <= r + tol * 0.5
                }),
                SeriesKind::Line | SeriesKind::Area | SeriesKind::Baseline => point(j).map(|c| {
                    [point(j - 1), point(j + 1)]
                        .into_iter()
                        .map(|n| {
                            n.map_or_else(
                                || (p.x - c.x).hypot(p.y - c.y),
                                |n| segment_distance(p, c, n),
                            )
                        })
                        .fold(f32::INFINITY, f32::min)
                        <= tol
                }),
                _ => None,
            };
            if hit == Some(true) {
                return Some(e.id);
            }
        }
        None
    }

    /// Drops the bands of removed series.
    fn prune_bands(&mut self) {
        let series = &self.series;
        self.bands.retain(|b| {
            series.iter().any(|e| e.id == b.upper) && series.iter().any(|e| e.id == b.lower)
        });
    }

    /// Adds a layer plugin to a pane, or to the whole chart (`pane: None`),
    /// drawn after the plugins added before it. See
    /// [`LayerPlugin`] for an example.
    ///
    /// # Errors
    ///
    /// [`Error::UnknownId`] if the pane does not exist;
    /// [`Error::OutOfMemory`].
    pub fn add_layer(
        &mut self,
        pane: Option<PaneId>,
        plugin: Box<dyn LayerPlugin>,
    ) -> Result<LayerPluginId> {
        if pane.is_some_and(|p| self.pane_index(p).is_none()) {
            return Err(Error::UnknownId);
        }
        let id = LayerPluginId(self.next_layer);
        self.next_layer = self.next_layer.checked_add(1).ok_or(Error::OutOfMemory)?;
        self.layer_plugins
            .try_reserve(1)
            .map_err(|_| Error::OutOfMemory)?;
        self.layer_plugins.push(LayerEntry {
            id,
            pane,
            position: plugin.position(),
            plugin,
        });
        self.invalidation = self.invalidation.max(Invalidation::Light);
        Ok(id)
    }

    /// Removes a layer plugin. Plugins of a pane are also removed with the
    /// pane.
    ///
    /// # Errors
    ///
    /// [`Error::UnknownId`] if it does not exist.
    pub fn remove_layer(&mut self, id: LayerPluginId) -> Result<()> {
        let n = self
            .layer_plugins
            .iter()
            .position(|l| l.id == id)
            .ok_or(Error::UnknownId)?;
        self.layer_plugins.remove(n);
        self.invalidation = self.invalidation.max(Invalidation::Light);
        Ok(())
    }

    /// Requests a redraw after a layer plugin's own content changed.
    ///
    /// # Errors
    ///
    /// [`Error::UnknownId`] if it does not exist.
    pub fn invalidate_layer(&mut self, id: LayerPluginId) -> Result<()> {
        if !self.layer_plugins.iter().any(|l| l.id == id) {
            return Err(Error::UnknownId);
        }
        self.invalidation = self.invalidation.max(Invalidation::Light);
        Ok(())
    }

    /// Layer plugins of pane `i` (or chart-wide ones, `None`) at `position`.
    fn emit_layers(
        &mut self,
        pane: Option<usize>,
        position: LayerPosition,
        text: &dyn TextMeasurer,
    ) {
        let pane_id = match pane {
            Some(i) => match self.panes.get(i) {
                Some(p) => Some(p.id),
                None => return,
            },
            None => None,
        };
        if !self
            .layer_plugins
            .iter()
            .any(|l| l.pane == pane_id && l.position == position)
        {
            return;
        }
        let norm = pane.and_then(|i| self.drawing_norm(i));
        let (rect, coords) = match pane {
            Some(i) => {
                let p = &self.panes[i];
                let coords = (!self.time_index.is_empty() && p.has_content)
                    .then_some(norm)
                    .flatten()
                    .map(|norm| DrawingCtx {
                        index: &self.time_index,
                        time_scale: &self.time_scale,
                        price_scale: &p.price_scale,
                        norm,
                        plot: p.plot,
                        dpr: self.dpr,
                        labels: p.labels,
                        text,
                    });
                (p.plot, coords)
            }
            None => (
                RectPx {
                    x: 0.0,
                    y: 0.0,
                    w: self.layout.price_axis.x,
                    h: self.layout.price_axis.h,
                },
                None,
            ),
        };
        let ctx = LayerCtx {
            rect,
            dpr: self.dpr,
            text,
            coords,
        };
        let layer = match position {
            LayerPosition::Background => LayerId::Background,
            LayerPosition::BelowSeries | LayerPosition::AboveSeries => LayerId::Main,
            LayerPosition::Overlay => LayerId::Overlay,
        };
        // Pane layers next to the series are revealed with them.
        let clip = match (pane, position) {
            (Some(_), LayerPosition::BelowSeries | LayerPosition::AboveSeries) => {
                self.data_clip(rect)
            }
            _ => rect,
        };
        let frame = &mut self.frame;
        frame.push_cmd(layer, DrawCmd::PushClip(clip));
        for l in &self.layer_plugins {
            if l.pane == pane_id && l.position == position {
                l.plugin.build(
                    &ctx,
                    &mut FrameBuilder {
                        frame: &mut *frame,
                        layer,
                    },
                );
            }
        }
        frame.push_cmd(layer, DrawCmd::PopClip);
    }

    /// Price map of the drawings of pane `i`: the map of the pane's first
    /// price series; `None` if a relative scale has no base yet.
    pub(crate) fn drawing_norm(&self, i: usize) -> Option<Normalize> {
        let pane = self.panes.get(i)?;
        if !pane.price_scale.mode().is_relative() {
            return Some(Normalize::IDENTITY);
        }
        self.series
            .iter()
            .find(|e| e.pane == pane.id && !e.derived && e.kind != SeriesKind::Volume)
            .and_then(|e| e.norm)
    }

    /// The coordinate context of drawings on pane `i`.
    pub(crate) fn drawing_ctx<'a>(
        &'a self,
        i: usize,
        text: &'a dyn TextMeasurer,
    ) -> Option<DrawingCtx<'a>> {
        let pane = self.panes.get(i)?;
        if self.time_index.is_empty() || !pane.has_content {
            return None;
        }
        Some(DrawingCtx {
            index: &self.time_index,
            time_scale: &self.time_scale,
            price_scale: &pane.price_scale,
            norm: self.drawing_norm(i)?,
            plot: pane.plot,
            dpr: self.dpr,
            labels: pane.labels,
            text,
        })
    }

    /// The drawings of pane `i` (main layer, above series and markers) and
    /// the handles of the selected one; records their screen bounds.
    fn emit_drawings(&mut self, i: usize, text: &dyn TextMeasurer) {
        let Some(pane) = self.panes.get(i) else {
            return;
        };
        let (id, plot) = (pane.id, pane.plot);
        if !self.drawings.entries.iter().any(|e| e.pane == id) {
            return;
        }
        let Some(norm) = self.drawing_norm(i) else {
            return;
        };
        if self.time_index.is_empty() || !pane.has_content {
            return;
        }
        let ctx = DrawingCtx {
            index: &self.time_index,
            time_scale: &self.time_scale,
            price_scale: &pane.price_scale,
            norm,
            plot,
            dpr: self.dpr,
            labels: pane.labels,
            text,
        };
        let clip = self.data_clip(plot);
        let frame = &mut self.frame;
        frame.push_cmd(LayerId::Main, DrawCmd::PushClip(clip));
        let selected = self.drawings.selected();
        let mut handles = self.handle_buf.borrow_mut();
        handles.clear();
        for e in &mut self.drawings.entries {
            if e.pane != id {
                continue;
            }
            let mark = PoolMark::of(frame);
            if e.visible {
                e.tool.build(
                    &ctx,
                    &mut FrameBuilder {
                        frame: &mut *frame,
                        layer: LayerId::Main,
                    },
                );
            }
            e.bbox = mark.bounds(frame, &ctx);
            if e.visible && selected == Some(e.id) {
                e.tool.handles(&ctx, &mut handles);
            }
        }
        // Handles: rings in the line color around the background color.
        let outer = (HANDLE_CSS * self.dpr).round();
        let inner = (outer - 2.0 * (1.5 * self.dpr).round()).max(2.0);
        let color = selected
            .and_then(|s| self.drawings.entry(s))
            .map_or(self.theme.line, |e| e.tool.style().line);
        for p in handles.iter() {
            let pos = PointPx {
                x: p.x.round(),
                y: p.y.round(),
            };
            for (size, c) in [(outer, color), (inner, self.theme.background)] {
                frame.push_icon(
                    LayerId::Main,
                    crate::frame::IconInst {
                        pos,
                        size: Px(size),
                        icon: crate::transform_helpers::ICON_CIRCLE,
                        color: c,
                    },
                );
            }
        }
        frame.push_cmd(LayerId::Main, DrawCmd::PopClip);
    }

    /// Adds an event, replacing a pending one of the same kind for
    /// position-like events.
    pub(crate) fn push_event(&mut self, event: ChartEvent) {
        let coalesce = matches!(
            event,
            ChartEvent::CrosshairMove { .. }
                | ChartEvent::VisibleRangeChanged { .. }
                | ChartEvent::Sync(_)
        );
        let same_kind = |e: &ChartEvent| match (e, &event) {
            (ChartEvent::Sync(a), ChartEvent::Sync(b)) => {
                core::mem::discriminant(a) == core::mem::discriminant(b)
            }
            _ => core::mem::discriminant(e) == core::mem::discriminant(&event),
        };
        if coalesce && let Some(e) = self.events.iter_mut().find(|e| same_kind(e)) {
            *e = event;
            return;
        }
        self.events.push(event);
    }

    fn emit_view_events(&mut self) {
        if self.time_index.is_empty() {
            return;
        }
        let (from, to) = self.time_scale.visible_range();
        if (from, to) != self.last_range {
            self.last_range = (from, to);
            self.push_event(ChartEvent::VisibleRangeChanged { from, to });
        }
        let first = self.time_index.first_index();
        let before = from.floor() as i64 - first;
        if (before as f64) < to - from && self.history_requested != Some(first) {
            self.history_requested = Some(first);
            self.push_event(ChartEvent::NeedMoreHistory {
                bars_before_start: before,
            });
        }
    }

    /// Values at logical index `j` of the first OHLC series that has a bar
    /// there.
    fn bar_at(&self, j: i64) -> Option<BarValues> {
        let (e, [open, high, low, close]) = self
            .series
            .iter()
            .filter(|e| is_ohlc_kind(e.kind))
            .find_map(|e| Some((e, e.store.bar_at(j)?)))?;
        Some(BarValues {
            open,
            high,
            low,
            close,
            volume: e.store.volume_at(j),
        })
    }

    /// The bar a magnet snaps to at logical index `j` of `pane`: that of the
    /// pane's first price series, in its own prices.
    pub(crate) fn magnet_bar(&self, pane: PaneId, j: i64) -> Option<[f64; 4]> {
        self.series
            .iter()
            .find(|e| e.pane == pane && !volume_like(e) && !e.store.is_empty())
            .and_then(|e| e.store.bar_at(j - i64::from(e.opts.shift)))
    }

    /// Index of the pane whose plot contains `p`.
    pub(crate) fn pane_at(&self, p: PointPx) -> Option<usize> {
        self.panes.iter().position(|pane| pane.plot.contains(p))
    }

    /// The crosshair in the overlay layer, and its `CrosshairMove` event.
    /// The vertical line crosses every pane; the horizontal line and the price
    /// label belong to the pane under the pointer. Without a pointer over the
    /// plot, a crosshair received from a synced chart is drawn as the
    /// vertical line and time label only. Returns the logical index under the
    /// pointer.
    fn emit_crosshair(&mut self, time_h: f32, pad: f32, font: FontStyleRef) -> Option<i64> {
        let hovered = self
            .input
            .pointer
            .filter(|_| self.input.crosshair != CrosshairMode::Hidden && !self.revealing())
            .and_then(|p| Some((p, self.pane_at(p)?)));
        let local = hovered.and_then(|(p, i)| {
            let pane = self.panes.get(i)?;
            let plot = pane.plot;
            let idx = self.time_scale.x_to_index(Px(p.x - plot.x)).round() as i64;
            let x = plot.x + self.time_scale.index_to_x_f64(idx as f64) as f32;
            let bar = self.bar_at(idx);
            let (top, height) = (f64::from(plot.y), f64::from(plot.h));
            let mut y = p.y;
            let magnet = self
                .series
                .iter()
                .find(|e| e.pane == pane.id && !volume_like(e) && !e.store.is_empty())
                .and_then(|e| Some((e.store.bar_at(idx - i64::from(e.opts.shift))?, e.norm?)));
            if self.input.crosshair == CrosshairMode::Magnet
                && let Some((b, norm)) = magnet
            {
                let mut best = f32::INFINITY;
                for v in b {
                    let vy = pane.price_scale.price_to_y(norm.apply(v), top, height) as f32;
                    if (vy - p.y).abs() < (best - p.y).abs() {
                        best = vy;
                    }
                }
                if best.is_finite() {
                    y = best.clamp(plot.y, plot.bottom() - 1.0);
                }
            }
            let price = pane.price_scale.y_to_price(f64::from(y), top, height);
            Some((
                Some((i, y)),
                x,
                CrosshairPoint {
                    logical_index: idx,
                    time: self.time_index.time_at(idx),
                    pane: pane.id,
                    price,
                    bar,
                },
            ))
        });
        let local_index = local.map(|(_, _, c)| c.logical_index);
        let point = local.or_else(|| {
            if self.input.crosshair == CrosshairMode::Hidden {
                return None;
            }
            let idx = self.remote_crosshair_index()?;
            let plot = self.panes.first()?.plot;
            let x = plot.x + self.time_scale.index_to_x_f64(idx as f64) as f32;
            Some((
                None,
                x,
                CrosshairPoint {
                    logical_index: idx,
                    time: self.time_index.time_at(idx),
                    pane: self.main_pane(),
                    price: f64::NAN,
                    bar: self.bar_at(idx),
                },
            ))
        });
        let event_point = point.map(|(_, _, c)| c);
        if !same_crosshair(event_point.as_ref(), self.last_crosshair.as_ref()) {
            self.last_crosshair = event_point;
            self.push_event(ChartEvent::CrosshairMove { point: event_point });
        }
        let (row, x, info) = point?;
        let plot0 = self.panes.first().map_or(RectPx::default(), |p| p.plot);
        if row.is_none() && !(x >= plot0.x && x < plot0.right()) {
            return local_index;
        }
        let stroke = StrokeStyle {
            width: Px(1.0),
            color: self.theme.crosshair,
            dash: LineDash::Dashed,
        };
        let cx = x.floor() + 0.5;
        for k in 0..self.panes.len() {
            let plot = self.panes[k].plot;
            let (from, to) = (
                PointPx { x: cx, y: plot.y },
                PointPx {
                    x: cx,
                    y: plot.bottom(),
                },
            );
            self.frame
                .push_line(LayerId::Overlay, LineInst { from, to, stroke });
        }
        let bg = Some(LabelBg {
            color: self.theme.crosshair_label_bg,
            padding: Px((pad * 0.5).round()),
        });
        let color = self.theme.crosshair_label_text;
        if let Some((i, y)) = row
            && let Some(pane) = self.panes.get(i)
        {
            let (plot, labels) = (pane.plot, pane.labels);
            let cy = y.floor() + 0.5;
            let (from, to) = (
                PointPx { x: plot.x, y: cy },
                PointPx {
                    x: plot.right(),
                    y: cy,
                },
            );
            self.frame
                .push_line(LayerId::Overlay, LineInst { from, to, stroke });
            let start = self.frame.pools.text.len();
            write_label(&mut self.frame.pools.text, info.price, labels);
            let span = Span {
                start: u32::try_from(start).unwrap_or(0),
                len: u32::try_from(self.frame.pools.text.len() - start).unwrap_or(0),
            };
            self.frame.push_text(
                LayerId::Overlay,
                TextRun {
                    pos: PointPx {
                        x: plot.right() + pad,
                        y: y.round(),
                    },
                    text: span,
                    style: font,
                    color,
                    align: TextAlign::Left,
                    bg,
                },
            );
        }
        if let Some(t) = info.time {
            let mut s = StackStr::<32>::new();
            if format_crosshair_time(t + self.tz.offset_ms(t), &mut s).is_ok() {
                let span = self.frame.push_str(s.as_str());
                self.frame.push_text(
                    LayerId::Overlay,
                    TextRun {
                        pos: PointPx {
                            x: x.round(),
                            y: (self.layout.time_axis.y + time_h * 0.5).round(),
                        },
                        text: span,
                        style: font,
                        color,
                        align: TextAlign::Center,
                        bg,
                    },
                );
            }
        }
        local_index
    }

    /// Chooses time labels: candidates by calendar weight, highest weight first,
    /// keeping labels at least 1.5 label widths apart.
    fn place_time_ticks(&mut self, text: &dyn TextMeasurer, font: FontStyleRef) {
        self.placed.clear();
        if self.time_index.is_empty() {
            return;
        }
        let spacing = self.time_scale.bar_spacing();
        let mut shift = 0;
        while ((1_u64 << shift) as f64) * spacing < TICK_SAMPLE_PX && shift < 40 {
            shift += 1;
        }
        let (from, to) = self.time_scale.visible_range();
        collect_ticks(
            &self.time_index,
            self.tz.as_ref(),
            from.floor() as i64,
            to.ceil() as i64,
            shift,
            &mut self.time_ticks,
        );
        self.time_ticks
            .sort_unstable_by(|a, b| b.weight.cmp(&a.weight).then(a.index.cmp(&b.index)));
        let plot_w = self.layout.plot.w;
        for &tick in &self.time_ticks {
            let x = self.time_scale.index_to_x_f64(tick.index as f64) as f32;
            let Some(label) = self.tick_label(tick) else {
                continue;
            };
            let width = text_width(text, label.as_str(), font);
            if x - width * 0.5 < 0.0 || x + width * 0.5 > plot_w {
                continue;
            }
            let free = self
                .placed
                .iter()
                .all(|p| (p.x - x).abs() >= 1.5 * p.width.max(width));
            if free && self.placed.len() < self.placed.capacity() {
                self.placed.push(PlacedTick { x, width, tick });
            }
        }
    }

    fn tick_label(&self, tick: TimeTick) -> Option<StackStr<32>> {
        let t = self.time_index.time_at(tick.index)?;
        let mut s = StackStr::<32>::new();
        format_tick(t + self.tz.offset_ms(t), tick.weight, &mut s).ok()?;
        Some(s)
    }

    fn line(from: PointPx, to: PointPx, color: crate::geom::Rgba) -> LineInst {
        LineInst {
            from,
            to,
            stroke: StrokeStyle {
                width: Px(1.0),
                color,
                dash: LineDash::Solid,
            },
        }
    }

    fn emit_grid(&mut self, i: usize) {
        let grid = self.theme.grid;
        let Some(plot) = self.panes.get(i).map(|p| p.plot) else {
            return;
        };
        for k in 0..self.panes[i].ticks.len() {
            let pane = &self.panes[i];
            let y = pane
                .price_scale
                .price_to_y(pane.ticks[k], f64::from(plot.y), f64::from(plot.h))
                as f32;
            let y = y.floor() + 0.5;
            if y > plot.y && y < plot.bottom() {
                let l = Self::line(
                    PointPx { x: plot.x, y },
                    PointPx { x: plot.right(), y },
                    grid,
                );
                self.frame.push_line(LayerId::Background, l);
            }
        }
        for k in 0..self.placed.len() {
            let x = self.placed[k].x.floor() + 0.5 + plot.x;
            let l = Self::line(
                PointPx { x, y: plot.y },
                PointPx {
                    x,
                    y: plot.bottom(),
                },
                grid,
            );
            self.frame.push_line(LayerId::Background, l);
        }
    }

    /// Element range and x mapping of `view` for the visible range, for a
    /// series drawn `shift` bars later.
    fn draw_range(
        &self,
        view: &OhlcLevelView<'_>,
        plot: RectPx,
        shift: i32,
    ) -> (crate::data::IdxSpan, XTransform) {
        let (from, to) = self.time_scale.visible_range();
        let shift = f64::from(shift);
        let span = visible_span(view, from - shift, to - shift);
        let k = u32::from(view.level);
        let size = (1_i64 << k) as f64;
        let center = if k == 0 {
            span.start as f64
        } else {
            bucket_start(span.start, k) as f64 + (size - 1.0) * 0.5
        };
        let xt = XTransform {
            base: span.start,
            step: (self.time_scale.bar_spacing() * size) as f32,
            offset: (f64::from(plot.x) + self.time_scale.index_to_x_f64(center + shift)) as f32,
        };
        (span, xt)
    }

    /// The series of pane `i`: volume series first, then the others in the
    /// order they were added.
    fn emit_series(&mut self, i: usize, text: &dyn TextMeasurer) {
        let Some(pane) = self.panes.get(i) else {
            return;
        };
        let (id, plot) = (pane.id, pane.plot);
        let clip = self.data_clip(plot);
        self.frame.push_cmd(LayerId::Main, DrawCmd::PushClip(clip));
        let has_price = self
            .series
            .iter()
            .any(|e| e.pane == id && !volume_like(e) && !e.store.is_empty());
        for volumes in [true, false] {
            if !volumes {
                for b in 0..self.bands.len() {
                    self.emit_band(i, b);
                }
            }
            for k in 0..self.series.len() {
                let e = &self.series[k];
                if e.pane == id && (e.kind == SeriesKind::Volume) == volumes {
                    match e.kind {
                        SeriesKind::Custom => self.emit_custom(i, k, text),
                        SeriesKind::Kagi | SeriesKind::PointFigure => self.emit_figure(i, k),
                        _ => self.emit_one(i, k, has_price),
                    }
                }
            }
        }
        self.frame.push_cmd(LayerId::Main, DrawCmd::PopClip);
    }

    /// One `SeriesDraw` for series `k` in pane `i`.
    fn emit_one(&mut self, i: usize, k: usize, has_price: bool) {
        let (Some(pane), Some(e)) = (self.panes.get(i), self.series.get(k)) else {
            return;
        };
        let plot = pane.plot;
        let (top, height) = (f64::from(plot.y), f64::from(plot.h));
        let (data_id, store) = if e.kind == SeriesKind::Volume {
            let Some(src) = e
                .opts
                .source
                .and_then(|s| self.series.iter().find(|x| x.id == s))
            else {
                return;
            };
            if !src.store.has_volume() {
                return;
            }
            (src.id, &src.store)
        } else {
            (e.id, &e.store)
        };
        let Some(log) = store.log() else { return };
        if store.is_empty() {
            return;
        }
        let level = lod_level(store.lod(), self.time_scale.bar_spacing());
        let Some(view) = store.level_view(level) else {
            return;
        };
        let shift = if e.kind == SeriesKind::Volume {
            0
        } else {
            e.opts.shift
        };
        let (span, xt) = self.draw_range(&view, plot, shift);
        let yt = if volume_like(e) {
            if has_price {
                pane.volume_scale.transform(top, height)
            } else {
                pane.price_scale.transform(top, height)
            }
        } else {
            // A relative scale without a base for this series: nothing to draw.
            let Some(norm) = e.norm else { return };
            pane.price_scale.series_transform(top, height, norm)
        };
        let generation = log.generation();
        let (kind, style) = (e.kind, self.style_of(e));
        let style = self.frame.push_style(style);
        self.frame.push_cmd(
            LayerId::Main,
            DrawCmd::Series(SeriesDraw {
                series: data_id,
                generation,
                style,
                kind,
                lod_level: level,
                index_range: span,
                x_transform: xt,
                y_transform: yt,
                clip: plot,
                pair: None,
            }),
        );
    }

    /// The `SeriesDraw` of band `b` if it is in pane `i`. Not drawn when the
    /// two series are mapped differently (different shifts, or relative
    /// price scales with different bases).
    fn emit_band(&mut self, i: usize, b: usize) {
        let (Some(pane), Some(band)) = (self.panes.get(i), self.bands.get(b)) else {
            return;
        };
        let find = |id: SeriesId| self.series.iter().find(|e| e.id == id);
        let (Some(u), Some(l)) = (find(band.upper), find(band.lower)) else {
            return;
        };
        if u.pane != pane.id || u.store.is_empty() || l.store.is_empty() {
            return;
        }
        let (Some(norm), Some(lower_norm)) = (u.norm, l.norm) else {
            return;
        };
        if norm != lower_norm || u.opts.shift != l.opts.shift {
            return;
        }
        let (Some(ul), Some(ll)) = (u.store.log(), l.store.log()) else {
            return;
        };
        let level = lod_level(u.store.lod(), self.time_scale.bar_spacing());
        let (Some(view), Some(_)) = (u.store.level_view(level), l.store.level_view(level)) else {
            return;
        };
        let plot = pane.plot;
        let (span, xt) = self.draw_range(&view, plot, u.opts.shift);
        let yt = pane
            .price_scale
            .series_transform(f64::from(plot.y), f64::from(plot.h), norm);
        let generation = ul
            .generation()
            .wrapping_add(ll.generation().rotate_left(32));
        let fill = band
            .opts
            .fill
            .unwrap_or_else(|| self.style_of(u).up.with_alpha(0x26));
        let style = SeriesStyle {
            fill_top: fill,
            fill_bottom: band.opts.fill_crossed.unwrap_or(fill),
            ..SeriesStyle::default()
        };
        let (upper, lower) = (band.upper, band.lower);
        let style = self.frame.push_style(style);
        self.frame.push_cmd(
            LayerId::Main,
            DrawCmd::Series(SeriesDraw {
                series: upper,
                generation,
                style,
                kind: SeriesKind::Band,
                lod_level: level,
                index_range: span,
                x_transform: xt,
                y_transform: yt,
                clip: plot,
                pair: Some(lower),
            }),
        );
    }

    /// The primitives of custom series `k` in pane `i`.
    fn emit_custom(&mut self, i: usize, k: usize, text: &dyn TextMeasurer) {
        let (Some(pane), Some(e)) = (self.panes.get(i), self.series.get(k)) else {
            return;
        };
        let (Some(c), Some(norm)) = (e.store.custom(), e.norm) else {
            return;
        };
        let shift = f64::from(e.opts.shift);
        let (from, to) = self.time_scale.visible_range();
        let visible = c.points_in(&self.time_index, from - shift, to - shift);
        if visible.0 >= visible.1 {
            return;
        }
        let ctx = SeriesCtx {
            coords: DrawingCtx {
                index: &self.time_index,
                time_scale: &self.time_scale,
                price_scale: &pane.price_scale,
                norm,
                plot: pane.plot,
                dpr: self.dpr,
                labels: pane.labels,
                text,
            },
            store: c,
            shift,
            visible,
            style: self.style_of(e),
        };
        if !self.native_kinds.contains(&c.kind) {
            c.plugin.build(
                &ctx,
                visible.0,
                visible.1,
                &mut FrameBuilder {
                    frame: &mut self.frame,
                    layer: LayerId::Main,
                },
            );
            return;
        }
        let mut params = [0.0; 8];
        c.plugin.native_params(&ctx, &mut params);
        let plot = pane.plot;
        let base = from.floor() as i64;
        let x_transform = XTransform {
            base,
            step: self.time_scale.bar_spacing() as f32,
            offset: (f64::from(plot.x) + self.time_scale.index_to_x_f64(base as f64 + shift))
                as f32,
        };
        let y_transform =
            pane.price_scale
                .series_transform(f64::from(plot.y), f64::from(plot.h), norm);
        let (series, generation, kind) = (e.id, c.log.generation(), c.kind);
        let style = self.frame.push_style(ctx.style);
        let points = IdxSpan {
            start: i64::try_from(visible.0).unwrap_or(i64::MAX),
            end: i64::try_from(visible.1).unwrap_or(i64::MAX),
        };
        self.frame.push_cmd(
            LayerId::Main,
            DrawCmd::Custom(CustomDraw {
                series,
                generation,
                kind,
                points,
                x_transform,
                y_transform,
                style,
                params,
                clip: plot,
            }),
        );
    }

    /// Kagi lines or Point & Figure marks of transform series `k` in pane
    /// `i`, as primitives. Below 1 px per element, one min–max line
    /// per pixel column (from the LOD levels), so the primitives never
    /// outnumber the plot's pixel columns.
    fn emit_figure(&mut self, i: usize, k: usize) {
        let (Some(pane), Some(e)) = (self.panes.get(i), self.series.get(k)) else {
            return;
        };
        let (Some(t), Some(norm)) = (e.store.transform(), e.norm) else {
            return;
        };
        let style = self.style_of(e);
        let out = &t.out;
        if out.len() == 0 {
            return;
        }
        let plot = pane.plot;
        let (top, height) = (f64::from(plot.y), f64::from(plot.h));
        let scale = &pane.price_scale;
        let ts = &self.time_scale;
        let y = |p: f64| -> Option<f32> {
            let y = scale.price_to_y(norm.apply(p), top, height);
            y.is_finite().then(|| y.clamp(-1e7, 1e7) as f32)
        };
        let shift = f64::from(e.opts.shift);
        let x = |j: f64| (f64::from(plot.x) + ts.index_to_x_f64(j + shift)) as f32;
        let spacing = ts.bar_spacing();
        let (from, to) = ts.visible_range();
        let mut b = FrameBuilder {
            frame: &mut self.frame,
            layer: LayerId::Main,
        };
        let thin = Px((style.line_width.0 / 2.0).max(1.0));
        let stroke = |flag: u8| StrokeStyle {
            width: if flag == 1 { style.line_width } else { thin },
            color: if flag == 1 { style.up } else { style.down },
            dash: LineDash::Solid,
        };
        if spacing < 1.0 {
            let level = lod_level(Some(out.lod()), spacing);
            let Some(view) = out.level_view(level) else {
                return;
            };
            let span = visible_span(&view, from - shift, to - shift);
            let lvl = u32::from(level);
            let size = (1_i64 << lvl) as f64;
            let one = StrokeStyle {
                width: Px(1.0),
                color: style.up,
                dash: LineDash::Solid,
            };
            // The current pixel column and its range of y.
            let mut col: Option<(f32, f32, f32)> = None;
            let flush = |b: &mut FrameBuilder<'_>, c: (f32, f32, f32)| {
                let x = c.0 + 0.5;
                b.line(
                    PointPx { x, y: c.1 },
                    PointPx {
                        x,
                        y: c.2.max(c.1 + 1.0),
                    },
                    one,
                );
            };
            for j in span.start..span.end {
                let center = if lvl == 0 {
                    j as f64
                } else {
                    bucket_start(j, lvl) as f64 + (size - 1.0) * 0.5
                };
                let (Some(hi), Some(lo)) =
                    (view.high.at(view.first, j), view.low.at(view.first, j))
                else {
                    continue;
                };
                let (Some(y0), Some(y1)) = (y(hi), y(lo)) else {
                    continue;
                };
                let px = x(center).floor();
                col = match col {
                    // Both are whole pixels.
                    Some(c) if (c.0 - px).abs() < 0.5 => Some((px, c.1.min(y0), c.2.max(y1))),
                    Some(c) => {
                        flush(&mut b, c);
                        Some((px, y0, y1))
                    }
                    None => Some((px, y0, y1)),
                };
            }
            if let Some(c) = col {
                flush(&mut b, c);
            }
            return;
        }
        let first = out.first();
        let a = ((from - shift).floor() as i64 - 1).max(first);
        let end = ((to - shift).ceil() as i64 + 2).min(out.end());
        let flag_at = |j: i64| {
            usize::try_from(j - first)
                .ok()
                .and_then(|p| t.flags.get(p).copied())
                .unwrap_or(0)
        };
        if e.kind == SeriesKind::Kagi {
            for j in a..end {
                let Some([open, _, _, close]) = out.bar_at(j) else {
                    continue;
                };
                let (Some(yo), Some(yc)) = (y(open), y(close)) else {
                    continue;
                };
                let xj = x(j as f64);
                let flag = flag_at(j);
                let prev = out.bar_at(j - 1);
                let start = if prev.is_some() { flag_at(j - 1) } else { flag };
                if prev.is_some() {
                    // The shoulder or waist joining the previous line.
                    let from = PointPx {
                        x: x((j - 1) as f64),
                        y: yo,
                    };
                    b.line(from, PointPx { x: xj, y: yo }, stroke(start));
                }
                match prev.and_then(|p| y(p[0])) {
                    // The thickness changes at the previous line's start.
                    Some(ys) if start != flag => {
                        b.line(
                            PointPx { x: xj, y: yo },
                            PointPx { x: xj, y: ys },
                            stroke(start),
                        );
                        b.line(
                            PointPx { x: xj, y: ys },
                            PointPx { x: xj, y: yc },
                            stroke(flag),
                        );
                    }
                    _ => b.line(
                        PointPx { x: xj, y: yo },
                        PointPx { x: xj, y: yc },
                        stroke(flag),
                    ),
                }
            }
            return;
        }
        // Point & Figure.
        let Some(size) = t.plugin.box_size().filter(|s| s.is_finite() && *s > 0.0) else {
            return;
        };
        let w = (spacing * 0.8) as f32;
        let mark = Px((style.line_width.0 * 0.75).max(1.0));
        for j in a..end {
            let Some([_, high, low, _]) = out.bar_at(j) else {
                continue;
            };
            let (Some(y_hi), Some(y_lo)) = (y(high), y(low)) else {
                continue;
            };
            let flag = flag_at(j);
            let color = if flag == 1 { style.up } else { style.down };
            let xc = x(j as f64);
            let boxes = ((high - low) / size).round();
            let box_px = (y_lo - y_hi).abs() / boxes.max(1.0) as f32;
            if spacing < 4.0 || box_px < 4.0 || boxes > 10_000.0 {
                let r = RectPx {
                    x: xc - w / 2.0,
                    y: y_hi.min(y_lo),
                    w: w.max(1.0),
                    h: (y_lo - y_hi).abs().max(1.0),
                };
                b.rect(r, color);
                continue;
            }
            let pen = StrokeStyle {
                width: mark,
                color,
                dash: LineDash::Solid,
            };
            let (l, r) = (xc - w / 2.0, xc + w / 2.0);
            for m in 0..boxes as i64 {
                let (Some(y0), Some(y1)) =
                    (y(low + m as f64 * size), y(low + (m + 1) as f64 * size))
                else {
                    continue;
                };
                // Inset so neighbouring boxes do not touch.
                let inset = (y0 - y1).abs() * 0.1;
                let (yb, yt) = (y0.max(y1) - inset, y0.min(y1) + inset);
                if flag == 1 {
                    b.line(PointPx { x: l, y: yt }, PointPx { x: r, y: yb }, pen);
                    b.line(PointPx { x: l, y: yb }, PointPx { x: r, y: yt }, pen);
                } else {
                    let (cy, rx, ry) = (f32::midpoint(yb, yt), w / 2.0, (yb - yt) / 2.0);
                    b.polyline(
                        (0..=CIRCLE_SEGMENTS).map(|s| {
                            let a =
                                f32::from(s) * core::f32::consts::TAU / f32::from(CIRCLE_SEGMENTS);
                            PointPx {
                                x: xc + rx * a.cos(),
                                y: cy + ry * a.sin(),
                            }
                        }),
                        pen,
                    );
                }
            }
        }
    }

    /// Declares the custom series kinds the renderer draws natively
    /// ([`Renderer::native_kinds`](crate::render::Renderer::native_kinds));
    /// for them the frame holds a [`DrawCmd::Custom`] instead of the plugin's
    /// primitives. Host helpers call it before every frame; it does
    /// nothing (and does not allocate) when the kinds are unchanged.
    pub fn set_native_kinds(&mut self, kinds: &[&'static str]) {
        if self.native_kinds.as_slice() == kinds {
            return;
        }
        self.native_kinds.clear();
        self.native_kinds.extend_from_slice(kinds);
        self.invalidation = Invalidation::Full;
    }

    fn push_label(&mut self, pos: PointPx, align: TextAlign, font: FontStyleRef, span: Span) {
        self.frame.push_text(
            LayerId::Main,
            TextRun {
                pos,
                text: span,
                style: font,
                color: self.theme.text,
                align,
                bg: None,
            },
        );
    }

    /// Price labels of pane `i` on the right axis.
    fn emit_price_labels(&mut self, i: usize, line_h: f32, pad: f32, font: FontStyleRef) {
        let Some(pane) = self.panes.get(i) else {
            return;
        };
        let (plot, labels) = (pane.plot, pane.labels.for_ticks(&pane.ticks));
        for k in 0..self.panes[i].ticks.len() {
            let pane = &self.panes[i];
            let p = pane.ticks[k];
            let y = pane
                .price_scale
                .price_to_y(p, f64::from(plot.y), f64::from(plot.h)) as f32;
            if y < plot.y + line_h * 0.5 || y > plot.bottom() - line_h * 0.5 {
                continue;
            }
            // Value labels have priority over ticks.
            let label_h = line_h + pad;
            if self.axis_labels.iter().any(|l| (l.at - y).abs() < label_h) {
                continue;
            }
            let start = self.frame.pools.text.len();
            write_label(&mut self.frame.pools.text, p, labels);
            let span = Span {
                start: u32::try_from(start).unwrap_or(0),
                len: u32::try_from(self.frame.pools.text.len() - start).unwrap_or(0),
            };
            self.push_label(
                PointPx {
                    x: plot.right() + pad,
                    y: y.round(),
                },
                TextAlign::Left,
                font,
                span,
            );
        }
        // Value labels: a background over the whole axis width.
        let label_h = (line_h + pad).round();
        let axis_right = self.frame.size_px.w;
        for n in 0..self.axis_labels.len() {
            let l = self.axis_labels[n];
            let y = l.at.round();
            self.frame.push_rect(
                LayerId::Main,
                crate::frame::RectInst {
                    rect: RectPx {
                        x: plot.right() + 1.0,
                        y: (y - label_h * 0.5).round(),
                        w: (axis_right - plot.right() - 1.0).max(0.0),
                        h: label_h,
                    },
                    color: l.bg,
                },
            );
            self.frame.push_text(
                LayerId::Main,
                TextRun {
                    pos: PointPx {
                        x: plot.right() + pad,
                        y,
                    },
                    text: l.text,
                    style: font,
                    color: l.fg,
                    align: TextAlign::Left,
                    bg: None,
                },
            );
        }
    }

    /// The last value of series `k` (the close of its last bar that is not a
    /// gap) and its color.
    fn last_value(&self, k: usize) -> Option<(f64, Rgba)> {
        let e = self.series.get(k)?;
        if let Some(c) = e.store.custom() {
            let v = c.plugin.last_value().filter(|v| v.is_finite())?;
            return Some((v, self.style_of(e).up));
        }
        let (first, end) = e.store.span()?;
        let [open, _, _, close] = (first..end).rev().find_map(|j| e.store.bar_at(j))?;
        let style = self.style_of(e);
        let up = match e.kind {
            SeriesKind::Baseline => close >= e.opts.base_value,
            k if is_ohlc_kind(k) || matches!(k, SeriesKind::Kagi | SeriesKind::PointFigure) => {
                close >= open
            }
            _ => true,
        };
        Some((close, if up { style.up } else { style.down }))
    }

    /// Markers of the series in pane `i` (main layer, clipped to the plot). Markers on
    /// the same bar and side stack away from the bar.
    fn emit_markers(&mut self, i: usize, line_h: f32, font: FontStyleRef) {
        let Some(pane) = self.panes.get(i) else {
            return;
        };
        let (id, plot) = (pane.id, pane.plot);
        let (top, height) = (f64::from(plot.y), f64::from(plot.h));
        if self.time_index.is_empty()
            || !self
                .series
                .iter()
                .any(|e| e.pane == id && !e.markers.is_empty())
        {
            return;
        }
        let (first, end) = (self.time_index.first_index(), self.time_index.end_index());
        let (a, b) = self.time_scale.visible_range();
        let ja = (a.floor() as i64).clamp(first, end - 1);
        let jb = (b.ceil() as i64).clamp(first, end - 1);
        let Some(t_hi) = self.time_index.time_at(jb) else {
            return;
        };
        // Markers snap to the next bar: those before `t_skip` land left of
        // the first visible bar.
        let t_skip = self.time_index.time_at(ja - 1).unwrap_or(i64::MIN);
        let size = (MARKER_SIZE * self.dpr).round().max(4.0);
        let gap = (MARKER_GAP * self.dpr).round();
        let clip = self.data_clip(plot);
        self.frame.push_cmd(LayerId::Main, DrawCmd::PushClip(clip));
        for k in 0..self.series.len() {
            let e = &self.series[k];
            let Some(norm) = e.norm.filter(|_| e.pane == id && !e.markers.is_empty()) else {
                continue;
            };
            let main = self.style_of(e).up;
            let start = e.markers.partition_point(|m| m.time <= t_skip);
            // (bar, offset above, offset below) of the stack being built.
            let mut stack = (i64::MIN, 0.0_f32, 0.0_f32);
            for n in start..e.markers.len() {
                let m = &e.markers[n];
                if m.time > t_hi && jb < end - 1 {
                    break;
                }
                let j = self.time_index.lower_bound(m.time).min(end - 1);
                let Some([_, high, low, _]) = e.store.bar_at(j) else {
                    continue;
                };
                if j != stack.0 {
                    stack = (j, 0.0, 0.0);
                }
                let above = m.position == MarkerPosition::AboveBar;
                let price = if above { high } else { low };
                let y_bar = self.panes[i]
                    .price_scale
                    .price_to_y(norm.apply(price), top, height) as f32;
                let text_h = if m.text.is_empty() { 0.0 } else { line_h };
                let (cy, ty) = if above {
                    let cy = y_bar - gap - stack.1 - size * 0.5;
                    stack.1 += gap + size + text_h;
                    (cy, cy - size * 0.5 - text_h * 0.5)
                } else {
                    let cy = y_bar + gap + stack.2 + size * 0.5;
                    stack.2 += gap + size + text_h;
                    (cy, cy + size * 0.5 + text_h * 0.5)
                };
                let shown = j + i64::from(e.opts.shift);
                let x = (plot.x + self.time_scale.index_to_x(shown as f64).0).round();
                let color = m.color.unwrap_or(main);
                self.marker_hits.push(MarkerHit {
                    series: e.id,
                    index: n,
                    pane: id,
                    pos: PointPx { x, y: cy.round() },
                    radius: size * 0.5,
                });
                self.frame.push_icon(
                    LayerId::Main,
                    crate::frame::IconInst {
                        pos: PointPx { x, y: cy.round() },
                        size: Px(size),
                        icon: m.shape.icon(),
                        color,
                    },
                );
                if text_h > 0.0 {
                    let span = self.frame.push_str(&e.markers[n].text);
                    self.frame.push_text(
                        LayerId::Main,
                        TextRun {
                            pos: PointPx { x, y: ty.round() },
                            text: span,
                            style: font,
                            color,
                            align: TextAlign::Center,
                            bg: None,
                        },
                    );
                }
            }
        }
        self.frame.push_cmd(LayerId::Main, DrawCmd::PopClip);
    }

    /// Last-value lines and price lines of pane `i` (main layer, above the
    /// series), and the placement of their axis labels in `axis_labels`:
    /// labels are placed in priority order (series in the order
    /// they were added, then price lines), each at the free position nearest
    /// to its price.
    fn emit_marks(&mut self, i: usize, line_h: f32, pad: f32, font: FontStyleRef) {
        self.axis_labels.clear();
        if self.revealing() {
            return; // after the reveal
        }
        let Some(pane) = self.panes.get(i) else {
            return;
        };
        let (id, plot, labels) = (pane.id, pane.plot, pane.labels);
        let (top, height) = (f64::from(plot.y), f64::from(plot.h));
        let inside = |y: f32| y >= plot.y && y < plot.bottom();
        let format = |e: &SeriesEntry| match labels {
            NumberFormat::Fixed(_) | NumberFormat::Auto(_) => match e.opts.price_precision {
                Some(p) => NumberFormat::Fixed(p),
                None => NumberFormat::Auto(e.auto_decimals),
            },
            other => other,
        };
        for k in 0..self.series.len() {
            let e = &self.series[k];
            if e.pane != id || volume_like(e) {
                continue;
            }
            let (Some(norm), Some((value, color))) = (e.norm, self.last_value(k)) else {
                continue;
            };
            let (show_line, show_label, fmt) = (
                e.opts.price_line_visible,
                e.opts.last_value_visible,
                format(e),
            );
            let d = norm.apply(value);
            let y = self.panes[i].price_scale.price_to_y(d, top, height) as f32;
            if !inside(y) {
                continue;
            }
            if show_line {
                let yl = y.floor() + 0.5;
                self.frame.push_line(
                    LayerId::Main,
                    LineInst {
                        from: PointPx { x: plot.x, y: yl },
                        to: PointPx {
                            x: plot.right(),
                            y: yl,
                        },
                        stroke: StrokeStyle {
                            width: Px(1.0),
                            color,
                            dash: LineDash::Dotted,
                        },
                    },
                );
            }
            if show_label {
                self.push_axis_label(y, d, fmt, color);
            }
        }
        for n in 0..self.price_lines.len() {
            let line = &self.price_lines[n];
            let Some(k) = self.series.iter().position(|e| e.id == line.series) else {
                continue;
            };
            let e = &self.series[k];
            let Some(norm) = e.norm.filter(|_| e.pane == id) else {
                continue;
            };
            let color = line.opts.color.unwrap_or_else(|| self.style_of(e).up);
            let fmt = format(e);
            let d = norm.apply(line.opts.price);
            let y = self.panes[i].price_scale.price_to_y(d, top, height) as f32;
            if !inside(y) {
                continue;
            }
            let width = line.opts.line_width.to_px(self.dpr).0.round().max(1.0);
            let yl = if width as i32 % 2 == 1 {
                y.floor() + 0.5
            } else {
                y.round()
            };
            self.frame.push_line(
                LayerId::Main,
                LineInst {
                    from: PointPx { x: plot.x, y: yl },
                    to: PointPx {
                        x: plot.right(),
                        y: yl,
                    },
                    stroke: StrokeStyle {
                        width: Px(width),
                        color,
                        dash: line.opts.line_style,
                    },
                },
            );
            let show_label = line.opts.axis_label_visible;
            if !line.opts.title.is_empty() {
                let span = self.frame.push_str(&self.price_lines[n].opts.title);
                self.frame.push_text(
                    LayerId::Main,
                    TextRun {
                        pos: PointPx {
                            x: plot.right() - pad,
                            y: (y - line_h * 0.5 - 2.0).round(),
                        },
                        text: span,
                        style: font,
                        color,
                        align: TextAlign::Right,
                        bg: None,
                    },
                );
            }
            if show_label {
                self.push_axis_label(y, d, fmt, color);
            }
        }
        // Place in priority order: the candidate positions are the wanted
        // one and the slots right above and below each placed label.
        let h = line_h + pad;
        let (lo, hi) = (plot.y + h * 0.5, plot.bottom() - h * 0.5);
        for n in 0..self.axis_labels.len() {
            let want = self.axis_labels[n].want.clamp(lo, hi.max(lo));
            let placed = &self.axis_labels[..n];
            let free = |c: f32| {
                c >= lo - 0.01
                    && c <= hi + 0.01
                    && placed.iter().all(|p| (p.at - c).abs() >= h - 0.01)
            };
            let mut best = want;
            if !free(want) {
                let mut dist = f32::INFINITY;
                for p in placed {
                    for c in [p.at - h, p.at + h] {
                        if free(c) && (c - want).abs() < dist {
                            dist = (c - want).abs();
                            best = c;
                        }
                    }
                }
            }
            self.axis_labels[n].at = best;
        }
    }

    /// Writes a value label's text and queues it for placement.
    fn push_axis_label(&mut self, want: f32, value: f64, format: NumberFormat, bg: Rgba) {
        let start = self.frame.pools.text.len();
        write_label(&mut self.frame.pools.text, value, format);
        let text = Span {
            start: u32::try_from(start).unwrap_or(0),
            len: u32::try_from(self.frame.pools.text.len() - start).unwrap_or(0),
        };
        if self.axis_labels.len() < self.axis_labels.capacity() {
            self.axis_labels.push(AxisLabel {
                want,
                at: want,
                text,
                bg,
                fg: contrast(bg),
            });
        }
    }

    /// Axis lines, pane separators and time labels.
    fn emit_axes(&mut self, time_h: f32, font: FontStyleRef) {
        let axis = self.theme.axis_line;
        let (w, h) = (self.frame.size_px.w, self.frame.size_px.h);
        let layout = self.layout;
        let x = layout.price_axis.x + 0.5;
        let l = Self::line(
            PointPx { x, y: 0.0 },
            PointPx {
                x,
                y: layout.price_axis.bottom(),
            },
            axis,
        );
        self.frame.push_line(LayerId::Main, l);
        // Separators: the 1-px rows between panes, across the price axis too.
        for k in 1..self.panes.len() {
            let y = self.panes[k].plot.y - SEPARATOR_PX + 0.5;
            let l = Self::line(PointPx { x: 0.0, y }, PointPx { x: w, y }, axis);
            self.frame.push_line(LayerId::Main, l);
        }
        let y = layout.time_axis.y + 0.5;
        if y < h {
            let l = Self::line(PointPx { x: 0.0, y }, PointPx { x: w, y }, axis);
            self.frame.push_line(LayerId::Main, l);
        }

        let y = (layout.time_axis.y + time_h * 0.5).round();
        for k in 0..self.placed.len() {
            let placed = self.placed[k];
            let Some(label) = self.tick_label(placed.tick) else {
                continue;
            };
            let span = self.frame.push_str(label.as_str());
            self.push_label(
                PointPx {
                    x: layout.plot.x + placed.x.round(),
                    y,
                },
                TextAlign::Center,
                font,
                span,
            );
        }
    }
}

/// Read-only view of the chart data for renderers.
///
/// # Examples
///
/// ```
/// use ferrochart_core::{Chart, ChartOptions};
/// use ferrochart_core::chart::{SeriesData, SeriesOptions};
/// use ferrochart_core::data::DataDelta;
/// use ferrochart_core::frame::SeriesKind;
/// use ferrochart_core::testing::random_walk;
///
/// let mut chart = Chart::new(ChartOptions::default());
/// let id = chart.add_series(chart.main_pane(), SeriesKind::Candlestick, SeriesOptions::default())?;
/// chart.set_data(id, SeriesData::Ohlc(random_walk(1, 100, 0, 60_000)?))?;
///
/// let view = chart.data_view();
/// assert_eq!(view.changes_since(id, 0), DataDelta::Full); // never synced
/// let synced = view.generation(id).unwrap();
/// assert_eq!(view.changes_since(id, synced), DataDelta::None);
/// assert_eq!(view.ohlc(id, 0).unwrap().len(), 100);
/// # Ok::<(), ferrochart_core::Error>(())
/// ```
#[derive(Clone, Copy)]
pub struct ChartDataView<'a> {
    series: &'a [SeriesEntry],
    time_index: &'a TimeIndex,
}

/// The time index of [`ChartDataView::empty`].
static EMPTY_TIME_INDEX: TimeIndex = TimeIndex::new();

impl<'a> ChartDataView<'a> {
    /// A view without series, for frames that only hold immediate
    /// primitives (`ferrochart-plot`).
    ///
    /// ```
    /// use ferrochart_core::chart::ChartDataView;
    /// use ferrochart_core::data::SeriesId;
    ///
    /// let view = ChartDataView::empty();
    /// assert_eq!(view.generation(SeriesId(1)), None);
    /// assert!(view.time_index().is_empty());
    /// ```
    pub fn empty() -> ChartDataView<'static> {
        ChartDataView {
            series: &[],
            time_index: &EMPTY_TIME_INDEX,
        }
    }

    /// The store holding `id`'s data (a volume series reads its source).
    fn store(self, id: SeriesId) -> Option<&'a Store> {
        let series = self.series;
        let e = series.iter().find(|e| e.id == id)?;
        match e.kind {
            SeriesKind::Volume => {
                let src = e.opts.source?;
                series.iter().find(|x| x.id == src).map(|x| &x.store)
            }
            _ => Some(&e.store),
        }
    }

    /// Current data generation of a series, `None` for unknown ids.
    pub fn generation(&self, id: SeriesId) -> Option<u64> {
        self.store(id)?.log().map(ChangeLog::generation)
    }

    /// What changed in a series since generation `since_gen`. Unknown ids
    /// give `Full`.
    pub fn changes_since(&self, id: SeriesId, since_gen: u64) -> DataDelta {
        self.store(id)
            .and_then(Store::log)
            .map_or(DataDelta::Full, |log| log.changes_since(since_gen))
    }

    /// Data of one level (0 = original, `>= 2` = LOD). Value series appear as
    /// bars with `open == high == low == close == value` at level 0 and
    /// `(first, max, min, last)` per bucket at LOD levels; NaN values are gaps.
    pub fn ohlc(&self, id: SeriesId, level: u8) -> Option<OhlcLevelView<'a>> {
        self.store(id)?.level_view(level)
    }

    /// Per-point colors of a value series (RGBA8, `0` = the series color) and
    /// the logical index of the first one. Level 0 only.
    pub fn point_colors(&self, id: SeriesId) -> Option<(i64, &'a [u32])> {
        self.store(id)?.colors()
    }

    /// The plugin of a custom series, for native renderers to read its data
    /// (downcast with `as &dyn Any`); `None` for other series.
    pub fn custom(&self, id: SeriesId) -> Option<&'a dyn CustomSeries> {
        let e = self.series.iter().find(|e| e.id == id)?;
        e.store.custom().map(|c| c.plugin.as_ref())
    }

    /// Fractional logical index of point `point` of custom series `id` (its
    /// slot, or its time interpolated between slots for `AxisMode::Overlay`),
    /// without `SeriesOptions::shift` (which is in [`CustomDraw::x_transform`]).
    pub fn custom_index(&self, id: SeriesId, point: usize) -> Option<f64> {
        let e = self.series.iter().find(|e| e.id == id)?;
        e.store.custom()?.x_index(self.time_index, point)
    }

    /// The global time index.
    pub fn time_index(&self) -> &'a TimeIndex {
        self.time_index
    }
}

/// LOD level for the current bar spacing.
fn lod_level(lod: Option<&crate::data::lod::Lod>, bar_spacing: f64) -> u8 {
    let bars_per_px = 1.0 / bar_spacing;
    if bars_per_px < 4.0 {
        return 0;
    }
    let Some(max) = lod.and_then(crate::data::lod::Lod::max_level) else {
        return 0;
    };
    let want = bars_per_px.log2().ceil().max(2.0).min(f64::from(max));
    want as u8
}

/// Elements of `view` covering the visible logical range `from..to`, one
/// extra on each side.
fn visible_span(view: &OhlcLevelView<'_>, from: f64, to: f64) -> crate::data::IdxSpan {
    let (i0, i1) = (from.floor() as i64 - 1, to.ceil() as i64 + 1);
    let k = u32::from(view.level);
    let (start, end) = if k == 0 {
        (i0, i1 + 1)
    } else {
        (bucket_of(i0, k), bucket_of(i1, k) + 1)
    };
    crate::data::IdxSpan {
        start: start.max(view.first),
        end: end.min(view.end()),
    }
}

/// Largest volume of `view` in `span`.
fn max_volume_in(view: &OhlcLevelView<'_>, span: crate::data::IdxSpan) -> f64 {
    let mut max_volume: f64 = 0.0;
    if let Some(col) = view.volume {
        for j in span.start..span.end {
            let p = usize::try_from(j - view.first).unwrap_or(usize::MAX);
            max_volume = max_volume.max(col.get(p).unwrap_or(0.0));
        }
    }
    max_volume
}

/// Writes an axis label in the pane's format.
fn write_label(out: &mut impl core::fmt::Write, v: f64, format: NumberFormat) {
    format.write(out, v);
}

/// Black or white text, whichever reads better on `bg`.
fn contrast(bg: Rgba) -> Rgba {
    let l = 0.299 * f32::from(bg.r) + 0.587 * f32::from(bg.g) + 0.114 * f32::from(bg.b);
    if l > 160.0 {
        Rgba::hex(0x0013_1722)
    } else {
        Rgba::hex(0x00FF_FFFF)
    }
}

/// The first value (close) at logical indices `from..to` that is not a gap:
/// the base of relative price scales.
fn first_value(store: &Store, from: i64, to: i64) -> Option<f64> {
    let (first, end) = store.span()?;
    (from.max(first)..to.min(end)).find_map(|j| store.bar_at(j).map(|b| b[3]))
}

impl Column<'_> {
    /// Value at logical index / bucket `j` of a view whose first element is `first`.
    pub fn at(&self, first: i64, j: i64) -> Option<f64> {
        self.get(usize::try_from(j - first).ok()?)
    }
}

/// Crosshair equality with prices compared bit for bit (a synced crosshair
/// has a NaN price; gap bars have NaN values).
fn same_crosshair(a: Option<&CrosshairPoint>, b: Option<&CrosshairPoint>) -> bool {
    match (a, b) {
        (None, None) => true,
        (Some(a), Some(b)) => {
            let bits = |v: Option<BarValues>| {
                v.map(|v| {
                    [v.open, v.high, v.low, v.close, v.volume.unwrap_or(f64::NAN)].map(f64::to_bits)
                })
            };
            a.logical_index == b.logical_index
                && a.time == b.time
                && a.pane == b.pane
                && a.price.to_bits() == b.price.to_bits()
                && bits(a.bar) == bits(b.bar)
                && a.bar.map(|v| v.volume.is_some()) == b.bar.map(|v| v.volume.is_some())
        }
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::data::{OhlcColumns, ValueColumns};

    fn ohlc(times: &[i64], close: f64) -> SeriesData {
        let n = times.len();
        SeriesData::Ohlc(OhlcColumns {
            time: times.to_vec(),
            open: vec![close; n],
            high: vec![close + 1.0; n],
            low: vec![close - 1.0; n],
            close: vec![close; n],
            volume: Some(vec![10.0; n]),
        })
    }

    fn values(times: &[i64], v: &[f64]) -> SeriesData {
        SeriesData::Value(ValueColumns {
            time: times.to_vec(),
            value: v.to_vec(),
            color: None,
        })
    }

    fn two_series(a: SeriesKind, b: SeriesKind) -> (Chart, SeriesId, SeriesId) {
        let mut chart = Chart::new(ChartOptions::default());
        let pane = chart.main_pane();
        let x = chart.add_series(pane, a, SeriesOptions::default()).unwrap();
        let y = chart.add_series(pane, b, SeriesOptions::default()).unwrap();
        (chart, x, y)
    }

    /// Value of series `id` at time `t`, through the time axis.
    fn at(chart: &Chart, id: SeriesId, t: i64) -> Option<f64> {
        let j = chart.time_index.index_of(t)?;
        let e = chart.series.iter().find(|e| e.id == id)?;
        e.store.bar_at(j).map(|b| b[3])
    }

    fn merged_warnings(chart: &mut Chart) -> usize {
        chart
            .drain_events()
            .filter(|e| matches!(e, ChartEvent::Warning(WarningKind::TimeAxisMerged { .. })))
            .count()
    }

    /// The axis is the union of all series' times; a time between
    /// existing bars rebuilds it with a warning, data stays at its times.
    #[test]
    fn time_axis_is_the_union_of_all_series() {
        let (mut chart, candles, line) = two_series(SeriesKind::Candlestick, SeriesKind::Line);
        chart
            .set_data(candles, ohlc(&[10, 20, 30, 40], 5.0))
            .unwrap();
        chart.drain_events().for_each(drop);
        // Extending past the end keeps every logical index; no warning.
        chart
            .set_data(line, values(&[30, 40, 50, 60], &[1.0, 2.0, 3.0, 4.0]))
            .unwrap();
        assert_eq!(chart.time_index.times(), &[10, 20, 30, 40, 50, 60]);
        assert_eq!(chart.time_index.index_of(10), Some(0));
        assert_eq!(merged_warnings(&mut chart), 0);
        // A time between bars: rebuilt, with a warning.
        chart
            .set_data(line, values(&[15, 30, 60], &[1.0, 2.0, 4.0]))
            .unwrap();
        assert_eq!(chart.time_index.times(), &[10, 15, 20, 30, 40, 60]);
        assert_eq!(merged_warnings(&mut chart), 1);
        assert_eq!(chart.time_index.index_of(10), Some(0)); // the first shared slot keeps its index
        for t in [10, 20, 30, 40] {
            assert_eq!(at(&chart, candles, t), Some(5.0), "t = {t}");
        }
        assert_eq!(at(&chart, candles, 15), None); // a gap bar
        assert_eq!(at(&chart, line, 15), Some(1.0));
        assert_eq!(at(&chart, line, 20), Some(1.5)); // interpolated
        // Appending a candle between line points rebuilds again.
        chart.append(candles, ohlc(&[50], 6.0)).unwrap();
        assert_eq!(chart.time_index.times(), &[10, 15, 20, 30, 40, 50, 60]);
        assert_eq!(merged_warnings(&mut chart), 1);
        assert_eq!(at(&chart, candles, 50), Some(6.0));
        assert_eq!(at(&chart, line, 60), Some(4.0));
        // update_last past the end extends the axis without a rebuild.
        let bar = BarUpdate {
            time: 70,
            open: 7.0,
            high: 8.0,
            low: 6.0,
            close: 7.0,
            volume: Some(1.0),
        };
        assert_eq!(
            chart.update_last(candles, bar),
            Ok(DataChange::Append { count: 2 })
        );
        assert_eq!(at(&chart, candles, 60), None);
        assert_eq!(at(&chart, candles, 70), Some(7.0));
        assert_eq!(merged_warnings(&mut chart), 0);
    }

    /// Replacing every series (e.g. another symbol) leaves no stale bars.
    #[test]
    fn replaced_data_leaves_no_stale_bars() {
        let (mut chart, candles, line) = two_series(SeriesKind::Candlestick, SeriesKind::Area);
        chart.set_data(candles, ohlc(&[1, 2, 3], 5.0)).unwrap();
        chart.set_data(line, values(&[1, 2, 3], &[1.0; 3])).unwrap();
        chart
            .set_data(candles, ohlc(&[101, 102, 103], 9.0))
            .unwrap();
        // The line still has the old times.
        assert_eq!(chart.time_index.len(), 6);
        chart
            .set_data(line, values(&[101, 102, 103], &[2.0; 3]))
            .unwrap();
        assert_eq!(chart.time_index.times(), &[101, 102, 103]);
        assert_eq!(at(&chart, candles, 102), Some(9.0));
        assert_eq!(at(&chart, line, 103), Some(2.0));
    }

    /// In relative modes every series starts at 0 % / 100 at the first
    /// visible bar, whatever its price level.
    #[test]
    fn relative_scales_normalize_each_series() {
        use crate::price_scale::PriceScaleMode;
        use crate::testing::MonoMeasurer;
        use crate::transform_helpers::y_of_price;

        let (mut chart, a, b) = two_series(SeriesKind::Line, SeriesKind::Line);
        chart.resize(SizeCss { w: 400.0, h: 300.0 }, 1.0);
        let times: Vec<i64> = (0..50).collect();
        let va: Vec<f64> = (0..50).map(|i| 10.0 + f64::from(i) * 0.1).collect();
        let vb: Vec<f64> = (0..50).map(|i| 1000.0 - f64::from(i)).collect();
        chart.set_data(a, values(&times, &va)).unwrap();
        chart.set_data(b, values(&times, &vb)).unwrap();
        chart.fit_content();
        let pane = chart.main_pane();
        for (mode, start) in [
            (PriceScaleMode::Percentage, 0.0),
            (PriceScaleMode::IndexedTo100, 100.0),
        ] {
            chart
                .price_scale(pane, ScaleSide::Right)
                .unwrap()
                .set_mode(mode)
                .unwrap();
            chart.build_frame(0.0, &MonoMeasurer);
            let draws: Vec<SeriesDraw> = chart.frame().layers[LayerId::Main as usize]
                .cmds
                .iter()
                .filter_map(|c| match c {
                    DrawCmd::Series(s) => Some(*s),
                    _ => None,
                })
                .collect();
            assert_eq!(draws.len(), 2);
            // The base is the first bar whose index is in the visible range.
            let j0 = chart.time_scale.visible_range().0.floor().max(0.0) as usize;
            let ya = y_of_price(&draws[0].y_transform, va[j0]);
            let yb = y_of_price(&draws[1].y_transform, vb[j0]);
            assert!((ya - yb).abs() < 0.01, "{mode:?}: {ya} vs {yb}");
            // The scale's start value sits at the same y.
            let p = &chart.panes[0];
            let y0 = p
                .price_scale
                .price_to_y(start, f64::from(p.plot.y), f64::from(p.plot.h));
            assert!((y0 as f32 - ya).abs() < 0.01, "{mode:?}");
            // Rising and falling series end on opposite sides of the start.
            let (ea, eb) = (
                y_of_price(&draws[0].y_transform, va[49]),
                y_of_price(&draws[1].y_transform, vb[49]),
            );
            assert!(ea < ya && eb > yb, "{mode:?}");
        }
        // Percent labels.
        let text = &chart.frame().pools.text;
        assert!(!text.contains('%'), "indexed labels have no %");
        chart
            .price_scale(pane, ScaleSide::Right)
            .unwrap()
            .set_mode(PriceScaleMode::Percentage)
            .unwrap();
        chart.build_frame(0.0, &MonoMeasurer);
        assert!(chart.frame().pools.text.contains('%'));
    }

    /// Errors leave the time axis and the data unchanged.
    #[test]
    fn failed_operations_leave_the_axis_unchanged() {
        let (mut chart, candles, line) = two_series(SeriesKind::Candlestick, SeriesKind::Line);
        chart.set_data(candles, ohlc(&[10, 20, 30], 5.0)).unwrap();
        chart
            .set_data(line, values(&[10, 20, 30], &[1.0; 3]))
            .unwrap();
        let before = chart.time_index.clone();
        let mut bad = ohlc(&[40, 50], 5.0);
        if let SeriesData::Ohlc(c) = &mut bad {
            c.high[1] = f64::INFINITY;
        }
        assert!(chart.append(candles, bad.clone()).is_err()); // past the end
        assert!(matches!(
            chart.append(line, ohlc(&[40], 1.0)),
            Err(Error::InvalidArgument(_))
        )); // wrong kind
        assert_eq!(chart.time_index, before);
        if let SeriesData::Ohlc(c) = &mut bad {
            c.time = vec![15, 25];
        }
        assert!(chart.set_data(candles, bad).is_err()); // a rebuild
        assert_eq!(chart.time_index, before);
        assert_eq!(at(&chart, candles, 20), Some(5.0));
        assert_eq!(
            chart.append(line, values(&[20], &[3.0])),
            Err(Error::TimeNotIncreasing { index: 0 })
        );
    }

    #[test]
    fn chart_is_send() {
        fn assert_send<T: Send>() {}
        assert_send::<Chart>();
    }

    #[test]
    fn invalid_ids_are_errors() {
        let mut chart = Chart::new(ChartOptions::default());
        let pane = chart.main_pane();
        assert_eq!(
            chart.add_series(PaneId(9), SeriesKind::Candlestick, SeriesOptions::default()),
            Err(Error::UnknownId)
        );
        assert_eq!(chart.remove_series(SeriesId(3)), Err(Error::UnknownId));
        let line = chart
            .add_series(pane, SeriesKind::Line, SeriesOptions::default())
            .unwrap();
        // A line cannot be the source of a volume series.
        assert!(matches!(
            chart.add_series(pane, SeriesKind::Volume, SeriesOptions::volume_of(line)),
            Err(Error::InvalidArgument(_))
        ));
        assert!(matches!(
            chart.add_series(
                pane,
                SeriesKind::Volume,
                SeriesOptions::volume_of(SeriesId(0))
            ),
            Err(Error::InvalidArgument(_))
        ));
        assert!(chart.price_scale(pane, ScaleSide::Left).is_none());
        assert_eq!(
            chart.set_series_options(SeriesId(9), SeriesOptions::default()),
            Err(Error::UnknownId)
        );
        assert!(chart.series_options(SeriesId(9)).is_none());
        // The source cannot change.
        assert!(matches!(
            chart.set_series_options(line, SeriesOptions::volume_of(line)),
            Err(Error::InvalidArgument(_))
        ));
        assert_eq!(chart.series_kind(line), Some(SeriesKind::Line));
        assert_eq!(chart.series_pane(line), Some(pane));
        assert_eq!(chart.series_pane(SeriesId(9)), None);
    }

    fn loaded(n: usize, options: ChartOptions) -> (Chart, SeriesId) {
        let mut chart = Chart::new(options);
        chart.resize(SizeCss { w: 800.0, h: 400.0 }, 1.0);
        let pane = chart.main_pane();
        let id = chart
            .add_series(pane, SeriesKind::Candlestick, SeriesOptions::default())
            .unwrap();
        chart
            .add_series(pane, SeriesKind::Volume, SeriesOptions::volume_of(id))
            .unwrap();
        let data = crate::testing::random_walk(9, n, 0, 60_000).unwrap();
        chart.set_data(id, SeriesData::Ohlc(data)).unwrap();
        (chart, id)
    }

    #[test]
    fn redraw_only_when_something_changed() {
        let (mut chart, _) = loaded(1000, ChartOptions::default());
        assert_eq!(chart.needs_redraw(), Invalidation::Full);
        chart.build_frame(0.0, &crate::testing::MonoMeasurer);
        assert_eq!(chart.needs_redraw(), Invalidation::None);
        chart.time_scale().scroll_by(Px(10.0));
        assert_eq!(chart.needs_redraw(), Invalidation::Light);
        chart.build_frame(0.0, &crate::testing::MonoMeasurer);
        assert_eq!(chart.needs_redraw(), Invalidation::None);
    }

    #[test]
    fn prepend_keeps_the_view() {
        let (mut chart, id) = loaded(1000, ChartOptions::default());
        chart.build_frame(0.0, &crate::testing::MonoMeasurer);
        let before = chart.time_scale().visible_range();
        let older = crate::testing::random_walk(10, 500, -500 * 60_000, 60_000).unwrap();
        chart.prepend(id, SeriesData::Ohlc(older)).unwrap();
        chart.build_frame(0.0, &crate::testing::MonoMeasurer);
        assert_eq!(chart.time_scale().visible_range(), before);
        assert_eq!(chart.data_view().time_index().first_index(), -500);
    }

    #[test]
    fn repaired_bars_produce_a_warning() {
        let (mut chart, id) = loaded(
            10,
            ChartOptions {
                ohlc_validation: ValidationMode::Repair,
                ..ChartOptions::default()
            },
        );
        let mut bad = crate::testing::random_walk(1, 2, 10 * 60_000, 60_000).unwrap();
        bad.high[1] = bad.low[1] - 1.0;
        chart.append(id, SeriesData::Ohlc(bad)).unwrap();
        let events: Vec<_> = chart.drain_events().collect();
        assert_eq!(
            events,
            [ChartEvent::Warning(WarningKind::OhlcRepaired {
                series: id,
                bars: 1
            })]
        );
    }

    #[test]
    fn removing_candles_removes_their_volume() {
        let (mut chart, id) = loaded(100, ChartOptions::default());
        let volume = SeriesId(1);
        assert!(chart.data_view().ohlc(volume, 0).is_some());
        chart.remove_series(id).unwrap();
        assert_eq!(chart.remove_series(volume), Err(Error::UnknownId));
        assert!(chart.data_view().ohlc(id, 0).is_none());
        // An empty chart still builds a (background-only) frame.
        chart.build_frame(0.0, &crate::testing::MonoMeasurer);
    }

    #[test]
    fn volume_series_has_no_data_of_its_own() {
        let (mut chart, _) = loaded(10, ChartOptions::default());
        let data = crate::testing::random_walk(1, 10, 0, 60_000).unwrap();
        assert!(matches!(
            chart.set_data(SeriesId(1), SeriesData::Ohlc(data)),
            Err(Error::InvalidArgument(_))
        ));
    }

    /// Text runs of the main layer as `(text, y)`.
    fn main_texts(chart: &Chart) -> Vec<(String, f32)> {
        let f = chart.frame();
        f.layers[LayerId::Main as usize]
            .cmds
            .iter()
            .filter_map(|c| match c {
                DrawCmd::Text(s) => Some(*s),
                _ => None,
            })
            .flat_map(|s| f.pools.text_runs[s.start as usize..(s.start + s.len) as usize].to_vec())
            .map(|r| (f.pools.text_of(r.text).to_owned(), r.pos.y))
            .collect()
    }

    /// Price lines are tied to a series, validated, and removed with it.
    #[test]
    fn price_lines_follow_their_series() {
        let mut chart = Chart::new(ChartOptions::default());
        let pane = chart.main_pane();
        let line = chart
            .add_series(pane, SeriesKind::Candlestick, SeriesOptions::default())
            .unwrap();
        let vol = chart
            .add_series(pane, SeriesKind::Volume, SeriesOptions::volume_of(line))
            .unwrap();
        let opts = |price| PriceLineOptions {
            price,
            ..PriceLineOptions::default()
        };
        assert_eq!(
            chart.create_price_line(vol, opts(1.0)),
            Err(Error::InvalidArgument("volume series have no price lines"))
        );
        assert!(chart.create_price_line(line, opts(f64::NAN)).is_err());
        assert_eq!(
            chart.create_price_line(SeriesId(99), opts(1.0)),
            Err(Error::UnknownId)
        );
        let a = chart.create_price_line(line, opts(1.0)).unwrap();
        let b = chart.create_price_line(line, opts(2.0)).unwrap();
        assert_ne!(a, b);
        chart.update_price_line(a, opts(3.0)).unwrap();
        assert_eq!(chart.price_line(a).map(|o| o.price), Some(3.0));
        chart.remove_price_line(b).unwrap();
        assert_eq!(chart.remove_price_line(b), Err(Error::UnknownId));
        chart.remove_series(line).unwrap();
        assert!(chart.price_line(a).is_none());
        assert_eq!(chart.update_price_line(a, opts(1.0)), Err(Error::UnknownId));
    }

    /// Automatic precision shows meme-coin prices with subscript
    /// zeros; a set precision is kept as it is.
    #[test]
    fn automatic_precision_reaches_tiny_prices() {
        use crate::testing::MonoMeasurer;

        let (mut chart, a, _) = two_series(SeriesKind::Line, SeriesKind::Line);
        chart.resize(SizeCss { w: 400.0, h: 300.0 }, 1.0);
        let times: Vec<i64> = (0..20).collect();
        let v: Vec<f64> = (0..20)
            .map(|i| 0.000_012_34 + f64::from(i) * 1e-9)
            .collect();
        chart.set_data(a, values(&times, &v)).unwrap();
        chart.fit_content();
        chart.build_frame(0.0, &MonoMeasurer);
        let texts: Vec<String> = main_texts(&chart).into_iter().map(|(t, _)| t).collect();
        // The last value 0.000012359, and ticks widened to their step
        // (0.0â‚„12345).
        assert!(texts.iter().any(|t| t == "0.0â‚„1236"), "{texts:?}");
        let ticks = texts.iter().filter(|t| t.starts_with("0.0â‚„123")).count();
        assert!(ticks >= 3, "{texts:?}");

        let k = chart.series.iter().position(|e| e.id == a).unwrap();
        chart.series[k].opts.price_precision = Some(2);
        chart.invalidation = Invalidation::Full;
        chart.build_frame(0.0, &MonoMeasurer);
        let texts: Vec<String> = main_texts(&chart).into_iter().map(|(t, _)| t).collect();
        assert!(texts.iter().any(|t| t == "0.00"), "{texts:?}");
        assert!(!texts.iter().any(|t| t.contains('â‚„')), "{texts:?}");
    }

    /// Price ticks finer than the series precision get the decimals
    /// they need, so no two ticks read the same.
    #[test]
    fn fine_ticks_are_not_rounded_together() {
        use crate::testing::MonoMeasurer;

        let (mut chart, a, _) = two_series(SeriesKind::Line, SeriesKind::Line);
        chart.resize(SizeCss { w: 400.0, h: 600.0 }, 1.0);
        let times: Vec<i64> = (0..20).collect();
        let v: Vec<f64> = (0..20).map(|i| 1.0 + f64::from(i) * 0.0002).collect();
        chart.set_data(a, values(&times, &v)).unwrap();
        chart.fit_content();
        chart.build_frame(0.0, &MonoMeasurer);
        let ticks: Vec<String> = main_texts(&chart)
            .into_iter()
            .map(|(t, _)| t)
            .filter(|t| t.parse::<f64>().is_ok() && t.len() > "1.00".len())
            .collect();
        assert!(ticks.len() >= 3, "{ticks:?}");
        let mut unique = ticks.clone();
        unique.dedup();
        assert_eq!(unique, ticks);
    }

    /// Value labels never overlap, stay inside the plot, and hide the
    /// ticks they cover.
    #[test]
    fn axis_labels_do_not_overlap() {
        use crate::testing::MonoMeasurer;

        let (mut chart, a, b) = two_series(SeriesKind::Line, SeriesKind::Line);
        chart.resize(SizeCss { w: 400.0, h: 300.0 }, 1.0);
        let times: Vec<i64> = (0..20).collect();
        let va: Vec<f64> = (0..20).map(f64::from).collect();
        let mut vb = va.clone();
        vb[19] = 19.01;
        chart.set_data(a, values(&times, &va)).unwrap();
        chart.set_data(b, values(&times, &vb)).unwrap();
        for price in [19.02, 18.99] {
            chart
                .create_price_line(
                    a,
                    PriceLineOptions {
                        price,
                        title: "t".into(),
                        ..PriceLineOptions::default()
                    },
                )
                .unwrap();
        }
        chart.fit_content();
        chart.build_frame(0.0, &MonoMeasurer);
        assert_eq!(chart.axis_labels.len(), 4);
        let plot = chart.panes[0].plot;
        let mut ys: Vec<f32> = chart.axis_labels.iter().map(|l| l.at).collect();
        // The first label (series a) keeps its wanted position.
        assert!((ys[0] - chart.axis_labels[0].want).abs() < 0.01);
        ys.sort_by(f32::total_cmp);
        let texts = main_texts(&chart);
        let gap = ys
            .windows(2)
            .map(|w| w[1] - w[0])
            .fold(f32::INFINITY, f32::min);
        assert!(gap >= 10.0, "labels overlap: {ys:?}");
        assert!(ys.iter().all(|&y| y > plot.y && y < plot.bottom()));
        // Titles are drawn; labels show the values.
        assert_eq!(texts.iter().filter(|(t, _)| t == "t").count(), 2);
        for v in ["19.00", "19.01", "19.02", "18.99"] {
            assert!(texts.iter().any(|(t, _)| t == v), "missing {v}: {texts:?}");
        }
        // Turning the marks off removes the labels.
        let k = chart.series.iter().position(|e| e.id == b).unwrap();
        chart.series[k].opts.last_value_visible = false;
        chart.invalidation = Invalidation::Full;
        chart.build_frame(0.0, &MonoMeasurer);
        assert_eq!(chart.axis_labels.len(), 3);
    }

    /// Markers are sorted, snap to the next bar, stack away from the
    /// bar, and skip gap bars.
    #[test]
    fn markers_stack_and_snap() {
        use crate::testing::MonoMeasurer;
        use MarkerPosition::{AboveBar, BelowBar};

        let (mut chart, candles, line) = two_series(SeriesKind::Candlestick, SeriesKind::Line);
        chart.resize(SizeCss { w: 400.0, h: 300.0 }, 1.0);
        chart
            .set_data(candles, ohlc(&[10, 20, 30, 40], 5.0))
            .unwrap();
        // Time 25 puts a gap bar into the candles.
        chart
            .set_data(line, values(&[10, 25, 40], &[5.0, 5.0, 5.0]))
            .unwrap();
        let m = |time, position, text: &str| Marker {
            time,
            position,
            text: text.into(),
            ..Marker::default()
        };
        chart
            .set_markers(
                candles,
                vec![
                    m(40, AboveBar, ""),
                    m(19, AboveBar, "a"), // snaps to 20
                    m(20, AboveBar, ""),
                    m(20, BelowBar, ""),
                    m(25, AboveBar, ""), // gap bar: not drawn
                ],
            )
            .unwrap();
        assert_eq!(
            chart
                .markers(candles)
                .unwrap()
                .iter()
                .map(|m| m.time)
                .collect::<Vec<_>>(),
            [19, 20, 20, 25, 40]
        );
        chart.fit_content();
        chart.build_frame(0.0, &MonoMeasurer);
        let icons = chart.frame().pools.icons.clone();
        assert_eq!(icons.len(), 4);
        let x20 = chart.panes[0].plot.x + chart.time_scale.index_to_x(1.0).0;
        let at20: Vec<_> = icons
            .iter()
            .filter(|i| (i.pos.x - x20.round()).abs() < 0.5)
            .collect();
        assert_eq!(at20.len(), 3);
        // Above the bar: the second marker sits above the first and its text.
        let (y_text, y1, y2, y_below) = (
            main_texts(&chart).iter().find(|(t, _)| t == "a").unwrap().1,
            at20[0].pos.y,
            at20[1].pos.y,
            at20[2].pos.y,
        );
        assert!(y2 < y_text && y_text < y1, "{y2} < {y_text} < {y1}");
        assert!(y_below > y1);
        // Errors.
        assert_eq!(
            chart.set_markers(SeriesId(99), vec![]),
            Err(Error::UnknownId)
        );
        let vol = chart
            .add_series(
                chart.main_pane(),
                SeriesKind::Volume,
                SeriesOptions::volume_of(candles),
            )
            .unwrap();
        assert!(chart.set_markers(vol, vec![]).is_err());
        assert!(chart.markers(SeriesId(99)).is_none());
    }

    /// Draws one rectangle filling its area, and reports whether it had
    /// price coordinates.
    struct Fill(crate::plugin::LayerPosition, Rgba);

    impl LayerPlugin for Fill {
        fn position(&self) -> LayerPosition {
            self.0
        }
        fn build(&self, ctx: &LayerCtx<'_>, out: &mut FrameBuilder<'_>) {
            let mut r = ctx.rect();
            if ctx.coords().is_some() {
                r.h /= 2.0;
            }
            out.rect(r, self.1);
        }
    }

    fn rects_of(chart: &Chart, layer: LayerId) -> Vec<(Rgba, RectPx)> {
        let f = chart.frame();
        f.layers[layer as usize]
            .cmds
            .iter()
            .filter_map(|c| match c {
                DrawCmd::Rects(span) => Some(*span),
                _ => None,
            })
            .flat_map(|span| {
                let a = span.start as usize;
                f.pools.rects[a..a + span.len as usize]
                    .iter()
                    .map(|r| (r.color, r.rect))
            })
            .collect()
    }

    #[test]
    fn layer_plugins_draw_in_their_layer() {
        let (mut chart, _) = loaded(200, ChartOptions::default());
        let red = Rgba::hex(0xFF_0000);
        let blue = Rgba::hex(0x00_00FF);
        let green = Rgba::hex(0x00_FF00);
        let pane = chart.add_pane(PaneOptions::default());
        let wide = chart
            .add_layer(None, Box::new(Fill(LayerPosition::Background, red)))
            .unwrap();
        chart
            .add_layer(
                Some(chart.main_pane()),
                Box::new(Fill(LayerPosition::AboveSeries, blue)),
            )
            .unwrap();
        chart
            .add_layer(Some(pane), Box::new(Fill(LayerPosition::Overlay, green)))
            .unwrap();
        chart.build_frame(0.0, &crate::testing::MonoMeasurer);
        let bg = rects_of(&chart, LayerId::Background);
        let main = rects_of(&chart, LayerId::Main);
        let overlay = rects_of(&chart, LayerId::Overlay);
        let plot = chart.layout().plot;
        // Chart-wide: all plots, no price coordinates.
        assert!(
            bg.iter()
                .any(|&(c, r)| c == red && r.y == 0.0 && r.h > plot.h)
        );
        // Main pane, with coordinates: the top half of its plot.
        assert!(main.iter().any(|&(c, r)| c == blue
            && r == RectPx {
                h: plot.h / 2.0,
                ..plot
            }));
        // The empty pane has no coordinates: its whole plot.
        let lower = chart.pane_layout(pane).unwrap().plot;
        assert!(overlay.iter().any(|&(c, r)| c == green && r == lower));

        chart.remove_pane(pane).unwrap();
        chart.remove_layer(wide).unwrap();
        assert_eq!(chart.remove_layer(wide), Err(Error::UnknownId));
        assert_eq!(chart.invalidate_layer(wide), Err(Error::UnknownId));
        chart.build_frame(0.0, &crate::testing::MonoMeasurer);
        assert!(
            !rects_of(&chart, LayerId::Background)
                .iter()
                .any(|&(c, _)| c == red)
        );
        assert!(
            !rects_of(&chart, LayerId::Overlay)
                .iter()
                .any(|&(c, _)| c == green)
        );
        assert!(
            rects_of(&chart, LayerId::Main)
                .iter()
                .any(|&(c, _)| c == blue)
        );
        assert!(
            chart
                .add_layer(Some(pane), Box::new(Fill(LayerPosition::Overlay, green)))
                .is_err()
        );
    }

    /// A custom series: a rect from `lo` to `hi` per point.
    #[derive(Default)]
    struct Ranges {
        mode: Option<AxisMode>,
        time: Vec<i64>,
        range: Vec<(f64, f64)>,
    }

    impl Ranges {
        fn overlay() -> Self {
            Self {
                mode: Some(AxisMode::Overlay),
                ..Self::default()
            }
        }

        fn push(&mut self, t: i64, lo: f64, hi: f64) -> DataChange {
            self.time.push(t);
            self.range.push((lo, hi));
            DataChange::Append { count: 1 }
        }
    }

    impl CustomSeries for Ranges {
        fn kind(&self) -> &'static str {
            "test.ranges"
        }
        fn axis_mode(&self) -> AxisMode {
            self.mode.unwrap_or(AxisMode::Slots)
        }
        fn times(&self) -> &[Timestamp] {
            &self.time
        }
        fn extremes(&self, from: usize, to: usize) -> Option<(f64, f64)> {
            // Linear: fine for a test.
            self.range[from..to]
                .iter()
                .fold(None, |acc, &(l, h)| match acc {
                    None => Some((l, h)),
                    Some((a, b)) => Some((l.min(a), h.max(b))),
                })
        }
        fn build(&self, ctx: &SeriesCtx<'_>, from: usize, to: usize, out: &mut FrameBuilder<'_>) {
            for i in from..to {
                let (lo, hi) = self.range[i];
                let (Some(x), Some(y0), Some(y1)) = (
                    ctx.x_of_point(i),
                    ctx.coords().y_of(hi),
                    ctx.coords().y_of(lo),
                ) else {
                    continue;
                };
                let rect = RectPx {
                    x: x - 1.0,
                    y: y0,
                    w: 2.0,
                    h: y1 - y0,
                };
                out.rect(rect, ctx.style().up);
            }
        }
        fn last_value(&self) -> Option<f64> {
            self.range.last().map(|r| r.1)
        }
        fn crosshair_value(&self, point: usize) -> Option<f64> {
            self.range.get(point).map(|r| r.1)
        }
        fn native_params(&self, ctx: &SeriesCtx<'_>, params: &mut [f32; 8]) {
            params[0] = ctx.visible().1 as f32;
        }
    }

    const RED: u32 = 0x00FF_0000;

    /// Another custom series type.
    struct Probe;

    impl CustomSeries for Probe {
        fn kind(&self) -> &'static str {
            "test.probe"
        }
        fn axis_mode(&self) -> AxisMode {
            AxisMode::Slots
        }
        fn times(&self) -> &[Timestamp] {
            &[]
        }
        fn extremes(&self, _: usize, _: usize) -> Option<(f64, f64)> {
            None
        }
        fn build(&self, _: &SeriesCtx<'_>, _: usize, _: usize, _: &mut FrameBuilder<'_>) {}
    }

    /// An indicator that computes nothing.
    struct Nothing;

    impl Indicator for Nothing {
        fn name(&self) -> &'static str {
            "nothing"
        }
        fn outputs(&self) -> &[crate::plugin::OutputSpec] {
            const OUT: [crate::plugin::OutputSpec; 1] = [crate::plugin::OutputSpec::line("x")];
            &OUT
        }
        fn default_placement(&self) -> Placement {
            Placement::Overlay
        }
        fn warmup(&self) -> usize {
            0
        }
        fn update(&mut self, _: &IndicatorInput<'_>, _: DataChange, _: &mut IndicatorOutputs) {}
    }

    fn custom_chart(r: Ranges) -> (Chart, SeriesId) {
        let mut chart = Chart::new(ChartOptions::default());
        chart.resize(SizeCss { w: 400.0, h: 300.0 }, 1.0);
        let opts = SeriesOptions {
            color: Some(Rgba::hex(RED)),
            ..SeriesOptions::default()
        };
        let id = chart
            .add_custom_series(chart.main_pane(), Box::new(r), opts)
            .unwrap();
        (chart, id)
    }

    fn red_rects(chart: &mut Chart) -> Vec<RectPx> {
        chart.build_frame(0.0, &crate::testing::MonoMeasurer);
        // In the plot: the last-value label on the axis has the color too.
        let right = chart.layout().plot.right();
        rects_of(chart, LayerId::Main)
            .into_iter()
            .filter(|(c, r)| *c == Rgba::hex(RED) && r.x < right)
            .map(|(_, r)| r)
            .collect()
    }

    #[test]
    fn custom_series_take_slots_and_draw() {
        let mut r = Ranges::default();
        for (n, t) in [60_000, 120_000, 180_000].into_iter().enumerate() {
            r.push(t, n as f64, n as f64 + 1.0);
        }
        let (mut chart, id) = custom_chart(r);
        assert_eq!(chart.series_kind(id), Some(SeriesKind::Custom));
        assert_eq!(chart.time_index.times(), &[60_000, 120_000, 180_000]);
        chart.fit_content();
        let rects = red_rects(&mut chart);
        assert_eq!(rects.len(), 3);
        assert!(rects[0].x < rects[1].x && rects[1].x < rects[2].x);
        // Auto scaling covers 0..3: the first rect ends low, the last starts high.
        let plot = chart.layout().plot;
        assert!(rects[0].bottom() > plot.y + plot.h * 0.7);
        assert!(rects[2].y < plot.y + plot.h * 0.3);

        let view = chart.data_view();
        let g = view.generation(id).unwrap();
        let any: &dyn Any = view.custom(id).unwrap();
        assert_eq!(any.downcast_ref::<Ranges>().unwrap().time.len(), 3);

        // Real time: the last point changes, then a new one.
        chart
            .update_custom(id, |r: &mut Ranges| {
                r.range[2].1 = 9.0;
                DataChange::UpdateLast
            })
            .unwrap();
        assert_eq!(
            chart.data_view().changes_since(id, g),
            DataDelta::Range(IdxSpan { start: 2, end: 3 })
        );
        chart
            .update_custom(id, |r: &mut Ranges| r.push(240_000, 1.0, 2.0))
            .unwrap();
        assert_eq!(chart.time_index.len(), 4);
        assert_eq!(chart.custom_series::<Ranges>(id).unwrap().time.len(), 4);
        assert_eq!(chart.entry(id).unwrap().store.bar_at(2), Some([9.0; 4]));
        assert_eq!(chart.last_value(0).map(|v| v.0), Some(2.0));
        chart.fit_content();
        assert_eq!(red_rects(&mut chart).len(), 4);
    }

    #[test]
    fn custom_series_share_the_axis() {
        let mut chart = Chart::new(ChartOptions::default());
        chart.resize(SizeCss { w: 400.0, h: 300.0 }, 1.0);
        let pane = chart.main_pane();
        let candles = chart
            .add_series(pane, SeriesKind::Candlestick, SeriesOptions::default())
            .unwrap();
        chart.set_data(candles, ohlc(&[0, 20, 40], 10.0)).unwrap();
        let mut r = Ranges::default();
        r.push(20, 1.0, 2.0);
        r.push(40, 1.0, 2.0);
        let id = chart
            .add_custom_series(pane, Box::new(r), SeriesOptions::default())
            .unwrap();
        // On existing slots: nothing added.
        assert_eq!(chart.time_index.times(), &[0, 20, 40]);
        let c = |chart: &Chart| chart.entry(id).unwrap().store.span();
        assert_eq!(c(&chart), Some((1, 3)));

        // A time between slots rebuilds the axis; the candles keep their bars.
        chart
            .update_custom(id, |r: &mut Ranges| {
                r.time.insert(1, 30);
                r.range.insert(1, (0.0, 1.0));
                DataChange::Replace
            })
            .unwrap();
        assert_eq!(chart.time_index.times(), &[0, 20, 30, 40]);
        assert_eq!(at(&chart, candles, 40), Some(10.0));
        assert_eq!(merged_warnings(&mut chart), 1);
        assert_eq!(
            chart.entry(id).unwrap().store.custom().unwrap().point_at(3),
            Some(2)
        );

        // Candles rebuilding the axis re-map the custom series.
        chart
            .set_data(candles, ohlc(&[0, 10, 20, 40], 10.0))
            .unwrap();
        assert_eq!(chart.time_index.times(), &[0, 10, 20, 30, 40]);
        let cs = chart.entry(id).unwrap().store.custom().unwrap();
        assert_eq!(
            (cs.point_at(2), cs.point_at(3), cs.point_at(4)),
            (Some(0), Some(1), Some(2))
        );

        // Removing the candles keeps the custom series' slots.
        chart.remove_series(candles).unwrap();
        assert!(chart.time_index.len() >= 3);
        assert_eq!(c(&chart).map(|(a, b)| b - a), Some(3));
        assert!(
            chart
                .add_indicator(id, Box::new(Nothing), Placement::Overlay)
                .is_err()
        );
        assert!(chart.set_data(id, values(&[0], &[1.0])).is_err());
        assert!(
            chart
                .add_series(pane, SeriesKind::Custom, SeriesOptions::default())
                .is_err()
        );
    }

    #[test]
    fn overlay_custom_series_add_no_slots() {
        let (mut chart, id) = custom_chart(Ranges::overlay());
        let line = chart
            .add_series(
                chart.main_pane(),
                SeriesKind::Line,
                SeriesOptions::default(),
            )
            .unwrap();
        chart
            .set_data(line, values(&[0, 60, 120], &[0.0, 0.0, 0.0]))
            .unwrap();
        chart
            .update_custom(id, |r: &mut Ranges| {
                for t in [10, 30, 90] {
                    r.push(t, 1.0, 2.0);
                }
                DataChange::Replace
            })
            .unwrap();
        assert_eq!(chart.time_index.len(), 3);
        chart.fit_content();
        let rects = red_rects(&mut chart);
        assert_eq!(rects.len(), 3);
        // x by interpolation: 1/6, 1/2 and 3/2 of the first two slots' gap.
        let x = |i: f64| chart.layout().plot.x + chart.time_scale.index_to_x_f64(i) as f32 - 1.0;
        for (r, i) in rects.iter().zip([1.0 / 6.0, 0.5, 1.5]) {
            assert!((r.x - x(i)).abs() < 0.01, "{} {}", r.x, x(i));
        }
    }

    #[test]
    fn invalid_custom_data_is_an_error() {
        let (mut chart, id) = custom_chart(Ranges::default());
        chart
            .update_custom(id, |r: &mut Ranges| r.push(60, 1.0, 2.0))
            .unwrap();
        chart
            .update_custom(id, |r: &mut Ranges| r.push(120, 1.0, 2.0))
            .unwrap();
        // Not increasing: an error, and nothing is drawn.
        assert_eq!(
            chart.update_custom(id, |r: &mut Ranges| r.push(90, 1.0, 2.0)),
            Err(Error::TimeNotIncreasing { index: 2 })
        );
        assert!(red_rects(&mut chart).is_empty());
        // Another type: `f` is not called.
        assert!(
            chart
                .update_custom(id, |_: &mut Probe| unreachable!())
                .is_err()
        );
        assert!(chart.custom_series::<Probe>(id).is_none());
        // A valid change brings it back.
        chart
            .update_custom(id, |r: &mut Ranges| {
                r.time[2] = 180;
                DataChange::Replace
            })
            .unwrap();
        chart.fit_content();
        assert_eq!(red_rects(&mut chart).len(), 3);
        // Duplicate times are rejected when added.
        let mut dup = Ranges::default();
        dup.push(1, 0.0, 1.0);
        dup.push(1, 0.0, 1.0);
        let pane = chart.main_pane();
        assert!(
            chart
                .add_custom_series(pane, Box::new(dup), SeriesOptions::default())
                .is_err()
        );
        assert_eq!(chart.series.len(), 1);
        chart.remove_series(id).unwrap();
        assert!(chart.custom_series::<Ranges>(id).is_none());
    }

    #[test]
    fn native_kinds_get_a_custom_draw() {
        let mut r = Ranges::default();
        for (n, t) in [60_000, 120_000, 180_000].into_iter().enumerate() {
            r.push(t, n as f64, n as f64 + 1.0);
        }
        let (mut chart, id) = custom_chart(r);
        chart.fit_content();
        let portable = red_rects(&mut chart);
        assert_eq!(portable.len(), 3);

        chart.set_native_kinds(&["test.ranges"]);
        assert_eq!(chart.needs_redraw(), Invalidation::Full);
        assert!(red_rects(&mut chart).is_empty());
        let draws: Vec<CustomDraw> = chart.frame().layers[LayerId::Main as usize]
            .cmds
            .iter()
            .filter_map(|c| match c {
                DrawCmd::Custom(d) => Some(*d),
                _ => None,
            })
            .collect();
        assert_eq!(draws.len(), 1);
        let d = draws[0];
        assert_eq!((d.series, d.kind), (id, "test.ranges"));
        assert_eq!(d.points, IdxSpan { start: 0, end: 3 });
        assert_eq!(d.clip, chart.layout().plot);
        assert!((d.params[0] - 3.0).abs() < f32::EPSILON);
        assert_eq!(Some(d.generation), chart.data_view().generation(id));
        assert_eq!(chart.frame().styles[d.style.0 as usize].up, Rgba::hex(RED));
        // The same x as the portable path.
        let view = chart.data_view();
        for (i, r) in portable.iter().enumerate() {
            let idx = view.custom_index(id, i).unwrap();
            let x = ((idx - d.x_transform.base as f64) as f32) * d.x_transform.step
                + d.x_transform.offset;
            assert!((x - (r.x + 1.0)).abs() < 1e-3, "{x} {}", r.x + 1.0);
        }
        // Unchanged kinds: nothing to redraw.
        chart.set_native_kinds(&["test.ranges"]);
        assert_eq!(chart.needs_redraw(), Invalidation::None);
        chart.set_native_kinds(&[]);
        assert_eq!(red_rects(&mut chart).len(), 3);
    }

    use crate::testing::{MonoMeasurer as Mono, random_walk};

    /// The lines of the main layer.
    fn main_lines(chart: &Chart) -> Vec<LineInst> {
        let f = chart.frame();
        f.layers[LayerId::Main as usize]
            .cmds
            .iter()
            .filter_map(|c| match c {
                DrawCmd::Lines(s) => f.pools.lines.get(s.range()),
                _ => None,
            })
            .flatten()
            .copied()
            .collect()
    }

    fn transform_chart(t: Box<dyn SeriesTransform>, bars: usize) -> (Chart, SeriesId) {
        let mut chart = Chart::new(ChartOptions {
            min_bar_spacing: Css(0.01),
            ..ChartOptions::default()
        });
        chart.resize(SizeCss { w: 800.0, h: 450.0 }, 1.0);
        let id = chart
            .add_transform_series(chart.main_pane(), t, SeriesOptions::default())
            .unwrap();
        let data = random_walk(5, bars, 0, 60_000).unwrap();
        chart.set_data(id, SeriesData::Ohlc(data)).unwrap();
        chart.fit_content();
        (chart, id)
    }

    #[test]
    fn a_transform_series_owns_the_axis() {
        use crate::transform::{Kagi, Renko, Reversal};
        let (mut chart, renko) = transform_chart(Box::new(Renko::fixed(1.0).unwrap()), 2_000);
        let pane = chart.main_pane();
        let n = chart.time_index.len();
        assert!(n > 10 && n < 2_000, "{n} bricks");
        // Synthetic times: strictly increasing, within the input's.
        let times = chart.time_index.times();
        assert!(times.windows(2).all(|w| w[0] < w[1]));
        assert!(times[0] >= 0 && times[n - 1] <= 2_000 * 60_000);
        let opts = SeriesOptions::default;
        assert!(chart.add_series(pane, SeriesKind::Line, opts()).is_err());
        assert!(chart.add_series(pane, SeriesKind::Kagi, opts()).is_err());
        assert!(
            chart
                .add_transform_series(
                    pane,
                    Box::new(Kagi::new(Reversal::Fixed(1.0)).unwrap()),
                    opts()
                )
                .is_err()
        );
        assert!(
            chart
                .add_custom_series(pane, Box::new(Ranges::default()), opts())
                .is_err()
        );
        // Volume reads the bricks' volume.
        chart
            .add_series(pane, SeriesKind::Volume, SeriesOptions::volume_of(renko))
            .unwrap();
        chart.build_frame(0.0, &Mono);
        let kinds: Vec<SeriesKind> = chart.frame().layers[LayerId::Main as usize]
            .cmds
            .iter()
            .filter_map(|c| match c {
                DrawCmd::Series(d) => Some(d.kind),
                _ => None,
            })
            .collect();
        assert_eq!(kinds, [SeriesKind::Volume, SeriesKind::Candlestick]);
        // Without it, time-based series are welcome again, and the other
        // way round a transform series is refused.
        chart.remove_series(renko).unwrap();
        chart.add_series(pane, SeriesKind::Line, opts()).unwrap();
        assert!(
            chart
                .add_transform_series(pane, Box::new(Renko::fixed(1.0).unwrap()), opts())
                .is_err()
        );
    }

    #[test]
    fn indicators_follow_a_transform_series() {
        use crate::transform::{Kagi, Reversal};
        /// The close, doubled.
        struct Double;
        impl Indicator for Double {
            fn name(&self) -> &'static str {
                "double"
            }
            fn outputs(&self) -> &[crate::plugin::OutputSpec] {
                const OUT: [crate::plugin::OutputSpec; 1] = [crate::plugin::OutputSpec::line("d")];
                &OUT
            }
            fn default_placement(&self) -> Placement {
                Placement::Overlay
            }
            fn warmup(&self) -> usize {
                0
            }
            fn update(
                &mut self,
                input: &IndicatorInput<'_>,
                _: DataChange,
                out: &mut IndicatorOutputs,
            ) {
                let start = out.start();
                for (i, v) in out.column(0).iter_mut().enumerate() {
                    if let Some(b) = input.bar(start + i) {
                        *v = 2.0 * b.close;
                    }
                }
            }
        }
        let kagi = || Box::new(Kagi::new(Reversal::Fixed(1.5)).unwrap());
        let (mut chart, id) = transform_chart(kagi(), 300);
        let ind = chart
            .add_indicator(id, Box::new(Double), Placement::Overlay)
            .unwrap();
        let mut all = random_walk(5, 300, 0, 60_000).unwrap();
        let mut bar = BarUpdate {
            time: *all.time.last().unwrap(),
            open: *all.open.last().unwrap(),
            high: *all.high.last().unwrap(),
            low: *all.low.last().unwrap(),
            close: *all.close.last().unwrap(),
            volume: all.volume.as_ref().and_then(|v| v.last().copied()),
        };
        let mut x = 7_u64;
        for i in 0..400 {
            x = x.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1);
            let mv = ((x >> 33) as f64 / f64::from(1_u32 << 31) - 0.5) * 2.0;
            if i % 9 == 0 {
                bar = BarUpdate {
                    time: bar.time + 60_000,
                    open: bar.close,
                    high: bar.close,
                    low: bar.close,
                    close: bar.close,
                    volume: Some(0.0),
                };
                all.time.push(bar.time);
                all.open.push(bar.open);
                all.high.push(bar.high);
                all.low.push(bar.low);
                all.close.push(bar.close);
                all.volume.as_mut().unwrap().push(0.0);
            }
            bar.close += mv;
            bar.high = bar.high.max(bar.close);
            bar.low = bar.low.min(bar.close);
            bar.volume = bar.volume.map(|v| v + 1.0);
            let p = all.time.len() - 1;
            all.close[p] = bar.close;
            all.high[p] = bar.high;
            all.low[p] = bar.low;
            all.volume.as_mut().unwrap()[p] = bar.volume.unwrap();
            chart.update_last(id, bar).unwrap();
        }
        let (mut fresh, fid) = transform_chart(kagi(), 1);
        fresh.set_data(fid, SeriesData::Ohlc(all)).unwrap();
        let find = fresh
            .add_indicator(fid, Box::new(Double), Placement::Overlay)
            .unwrap();
        assert_eq!(chart.time_index.times(), fresh.time_index.times());
        let values = |c: &Chart, ind: IndicatorId| -> Vec<u64> {
            let s = c.indicator_series(ind).unwrap()[0];
            let view = c.data_view().ohlc(s, 0).unwrap();
            (0..view.close.len())
                .map(|i| view.close.get(i).unwrap().to_bits())
                .collect()
        };
        assert_eq!(values(&chart, ind), values(&fresh, find));
        assert_eq!(values(&chart, ind).len(), chart.time_index.len());
    }

    #[test]
    fn kagi_and_point_figure_are_primitives() {
        use crate::transform::{Kagi, PointFigure, Reversal};
        let theme = Theme::light();
        // Kagi: thick up-colored and thin down-colored lines.
        let (mut chart, _) =
            transform_chart(Box::new(Kagi::new(Reversal::Fixed(2.0)).unwrap()), 3_000);
        chart.time_scale().set_bar_spacing(8.0);
        chart.build_frame(0.0, &Mono);
        // The Kagi lines (the last-price line is dotted).
        let lines: Vec<LineInst> = main_lines(&chart)
            .into_iter()
            .filter(|l| l.stroke.dash == LineDash::Solid)
            .collect();
        let thick = lines.iter().filter(|l| l.stroke.color == theme.up).count();
        let thin = lines
            .iter()
            .filter(|l| l.stroke.color == theme.down)
            .count();
        assert!(thick > 10 && thin > 10, "{thick} thick, {thin} thin");
        assert!(
            lines
                .iter()
                .filter(|l| l.stroke.color == theme.up)
                .all(|l| l.stroke.width == Px(2.0))
        );
        assert!(
            lines
                .iter()
                .filter(|l| l.stroke.color == theme.down)
                .all(|l| l.stroke.width == Px(1.0))
        );
        // Zoomed far out: at most one line per pixel column.
        chart.time_scale().set_bar_spacing(0.05);
        chart.build_frame(0.0, &Mono);
        let n = main_lines(&chart).len();
        assert!(n > 10 && n <= 800, "{n} lines");
        // Point & Figure: X as crossed lines, O as closed polylines.
        let (mut chart, _) = transform_chart(Box::new(PointFigure::new(0.5, 3).unwrap()), 3_000);
        chart.time_scale().set_bar_spacing(12.0);
        chart.build_frame(0.0, &Mono);
        let xs = main_lines(&chart)
            .iter()
            .filter(|l| l.stroke.color == theme.up && l.stroke.dash == LineDash::Solid)
            .count();
        let os = chart.frame().layers[LayerId::Main as usize]
            .cmds
            .iter()
            .filter(|c| matches!(c, DrawCmd::Polyline { style, .. } if style.color == theme.down))
            .count();
        assert!(xs > 10 && xs % 2 == 0 && os > 5, "{xs} X lines, {os} Os");
        // Small boxes: one rect per column.
        chart.time_scale().set_bar_spacing(2.0);
        chart.build_frame(0.0, &Mono);
        let rects = rects_of(&chart, LayerId::Main)
            .iter()
            .filter(|(c, _)| *c == theme.up || *c == theme.down)
            .count();
        assert!(rects > 10, "{rects} columns");
    }

    fn series_draws(chart: &Chart) -> Vec<SeriesDraw> {
        chart.frame().layers[LayerId::Main as usize]
            .cmds
            .iter()
            .filter_map(|c| match c {
                DrawCmd::Series(s) => Some(*s),
                _ => None,
            })
            .collect()
    }

    /// A band is drawn before the series of its pane, with the lower
    /// series as `pair`, and goes away with either series.
    #[test]
    fn bands_follow_their_series() {
        use crate::testing::MonoMeasurer;
        let (mut chart, a, b) = two_series(SeriesKind::Line, SeriesKind::Line);
        chart.resize(SizeCss { w: 800.0, h: 450.0 }, 1.0);
        let t = [0, 60_000, 120_000];
        chart.set_data(a, values(&t, &[2.0, 3.0, 1.0])).unwrap();
        chart.set_data(b, values(&t, &[1.0, 1.0, 2.0])).unwrap();
        let candles = chart
            .add_series(
                chart.main_pane(),
                SeriesKind::Candlestick,
                SeriesOptions::default(),
            )
            .unwrap();
        assert!(chart.add_band(a, candles, BandOptions::default()).is_err());
        assert!(chart.add_band(a, a, BandOptions::default()).is_err());
        assert_eq!(
            chart.add_band(a, SeriesId(99), BandOptions::default()),
            Err(Error::UnknownId)
        );
        assert!(
            chart
                .add_series(
                    chart.main_pane(),
                    SeriesKind::Band,
                    SeriesOptions::default()
                )
                .is_err()
        );
        let other = chart.add_pane(PaneOptions::default());
        let c = chart
            .add_series(other, SeriesKind::Line, SeriesOptions::default())
            .unwrap();
        assert!(chart.add_band(a, c, BandOptions::default()).is_err());

        let fill = Rgba::hex(0x0011_2233);
        let band = chart
            .add_band(
                a,
                b,
                BandOptions {
                    fill: Some(fill),
                    fill_crossed: None,
                },
            )
            .unwrap();
        chart.build_frame(0.0, &MonoMeasurer);
        let draws = series_draws(&chart);
        let kinds: Vec<_> = draws.iter().map(|d| d.kind).collect();
        assert_eq!(kinds[0], SeriesKind::Band, "{kinds:?}");
        assert_eq!((draws[0].series, draws[0].pair), (a, Some(b)));
        let style = chart.frame().styles[draws[0].style.0 as usize];
        // `fill_crossed: None` is the same color.
        assert_eq!((style.fill_top, style.fill_bottom), (fill, fill));
        assert!(draws[1..].iter().all(|d| d.pair.is_none()));

        // Different shifts: not drawn.
        let opts = SeriesOptions {
            shift: 2,
            ..*chart.series_options(b).unwrap()
        };
        chart.set_series_options(b, opts).unwrap();
        chart.build_frame(0.0, &MonoMeasurer);
        assert!(
            series_draws(&chart)
                .iter()
                .all(|d| d.kind != SeriesKind::Band)
        );
        let opts = SeriesOptions { shift: 0, ..opts };
        chart.set_series_options(b, opts).unwrap();

        chart
            .set_band_options(band, BandOptions::default())
            .unwrap();
        assert_eq!(chart.band_options(band), Some(&BandOptions::default()));
        chart.remove_series(b).unwrap();
        assert_eq!(chart.band_options(band), None);
        assert_eq!(chart.remove_band(band), Err(Error::UnknownId));
        chart.build_frame(0.0, &MonoMeasurer);
        assert!(
            series_draws(&chart)
                .iter()
                .all(|d| d.kind != SeriesKind::Band)
        );
    }

    /// `OutputSpec::fill_to` must name another output.
    #[test]
    fn fill_to_is_checked() {
        use crate::plugin::{IndicatorInput, IndicatorOutputs, OutputSpec};
        struct Bad([OutputSpec; 2]);
        impl Indicator for Bad {
            fn name(&self) -> &'static str {
                "bad"
            }
            fn outputs(&self) -> &[OutputSpec] {
                &self.0
            }
            fn default_placement(&self) -> Placement {
                Placement::Overlay
            }
            fn warmup(&self) -> usize {
                0
            }
            fn update(&mut self, _: &IndicatorInput<'_>, _: DataChange, _: &mut IndicatorOutputs) {}
        }
        let mut chart = Chart::new(ChartOptions::default());
        let src = chart
            .add_series(
                chart.main_pane(),
                SeriesKind::Line,
                SeriesOptions::default(),
            )
            .unwrap();
        for to in [0, 2] {
            let a = OutputSpec {
                fill_to: Some((to, BandOptions::default())),
                ..OutputSpec::line("a")
            };
            let bad = Bad([a, OutputSpec::line("b")]);
            assert!(
                chart
                    .add_indicator(src, Box::new(bad), Placement::Overlay)
                    .is_err()
            );
        }
        assert_eq!(chart.series.len(), 1, "nothing was added");
    }
}