datui-lib 0.4.4

Data Exploration in the Terminal (library)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
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
//! Chart view state: the chart's spec and the panel that edits it.
//!
//! The spec is a set of shelves, named as Vega-Lite names them so a later version
//! can save it: `mark` (the Type shelf), `encoding.x.field` with its `timeUnit`,
//! `encoding.y.field` with its `aggregate`, and `encoding.color.field`. Every chart
//! type shows the same shelves; a shelf a type does not use is dimmed, never
//! hidden (`ChartModal::shelf`). Options that are not part of what is charted
//! (bins, ranges, the grid, the sample size) sit beside the spec.
//!
//! The panel is one form of the shared focus model (`crate::app::form`); column and
//! value rows are edited through the one shared Picker.

use crate::chart::chart_data::{BarOrder, ValueRange};
use crate::widgets::ui::PickerState;
use polars::prelude::DataType;
use serde::{Deserialize, Serialize};

/// The Type shelf: what marks the chart draws.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Mark {
    #[default]
    Line,
    Scatter,
    Bar,
    Histogram,
    Box,
    Kde,
    Heatmap,
}

impl Mark {
    pub const ALL: [Self; 7] = [
        Self::Line,
        Self::Scatter,
        Self::Bar,
        Self::Histogram,
        Self::Box,
        Self::Kde,
        Self::Heatmap,
    ];

    pub fn label(self) -> &'static str {
        match self {
            Self::Line => "Line",
            Self::Scatter => "Scatter",
            Self::Bar => "Bar",
            Self::Histogram => "Histogram",
            Self::Box => "Box",
            Self::Kde => "KDE",
            Self::Heatmap => "Heatmap",
        }
    }

    /// Line and scatter: X against one or more Y columns.
    pub fn is_xy(self) -> bool {
        matches!(self, Self::Line | Self::Scatter)
    }
}

/// How a temporal X is bucketed before Y is aggregated.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TimeUnit {
    #[default]
    None,
    Day,
    Week,
    Month,
    Quarter,
    Year,
}

impl TimeUnit {
    pub const ALL: [Self; 6] = [
        Self::None,
        Self::Day,
        Self::Week,
        Self::Month,
        Self::Quarter,
        Self::Year,
    ];

    pub fn label(self) -> &'static str {
        match self {
            Self::None => "none",
            Self::Day => "day",
            Self::Week => "week",
            Self::Month => "month",
            Self::Quarter => "quarter",
            Self::Year => "year",
        }
    }

    /// The Polars duration a date is truncated to.
    pub fn every(self) -> Option<&'static str> {
        match self {
            Self::None => None,
            Self::Day => Some("1d"),
            Self::Week => Some("1w"),
            Self::Month => Some("1mo"),
            Self::Quarter => Some("1q"),
            Self::Year => Some("1y"),
        }
    }
}

/// What a Y value is, per X (and color): the column as it is, or an aggregate of
/// the rows that share it.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Aggregate {
    #[default]
    None,
    Count,
    /// The distinct values of Y: `nunique` of the query language, nulls left out.
    Distinct,
    Sum,
    Mean,
    Median,
    /// The sample standard deviation (one degree of freedom); a group of one row
    /// has none.
    Stdev,
    /// A percentile of Y, linearly interpolated: [`YEncoding::quantile`].
    Quantile,
    Min,
    Max,
    /// The first and last Y of each group in the view's row order: its sort, or the
    /// order the rows were read in.
    First,
    Last,
}

impl Aggregate {
    pub const ALL: [Self; 12] = [
        Self::None,
        Self::Count,
        Self::Distinct,
        Self::Sum,
        Self::Mean,
        Self::Median,
        Self::Stdev,
        Self::Quantile,
        Self::Min,
        Self::Max,
        Self::First,
        Self::Last,
    ];

    pub fn label(self) -> &'static str {
        match self {
            Self::None => "none",
            Self::Count => "count",
            Self::Distinct => "distinct",
            Self::Sum => "sum",
            Self::Mean => "mean",
            Self::Median => "median",
            Self::Stdev => "stdev",
            Self::Quantile => "quantile",
            Self::Min => "min",
            Self::Max => "max",
            Self::First => "first",
            Self::Last => "last",
        }
    }

    /// How it reads in a title or a column name: a quantile as its percentile,
    /// `p90`.
    pub fn named(self, quantile: u8) -> String {
        match self {
            Self::Quantile => format!("p{quantile}"),
            other => other.label().to_string(),
        }
    }

    /// Whether it reads the rows in the view's order.
    pub fn follows_row_order(self) -> bool {
        matches!(self, Self::First | Self::Last)
    }

    /// Whether every value it makes may have a fraction, whatever Y is.
    pub fn is_fractional(self) -> bool {
        matches!(
            self,
            Self::Mean | Self::Median | Self::Stdev | Self::Quantile
        )
    }

    /// Whether Y may be any column, not only a number: a count of its distinct
    /// values is a number whatever they are.
    pub fn takes_any_y(self) -> bool {
        self == Self::Distinct
    }

    /// Whether cumulative can run with it: the rows run as a total, which for a
    /// count, sum, mean, median, min or max is what was asked. A running sum of
    /// distinct counts is not the distinct count so far, nor one of deviations,
    /// percentiles or first and last values anything: those take none.
    pub fn runs_cumulative(self) -> bool {
        matches!(
            self,
            Self::Count | Self::Sum | Self::Mean | Self::Median | Self::Min | Self::Max
        )
    }

    /// Whether every value it makes is a whole number, whatever Y is.
    pub fn is_count(self) -> bool {
        matches!(self, Self::Count | Self::Distinct)
    }
}

/// A running total of the aggregated Y, along X.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Cumulative {
    #[default]
    Off,
    /// The values added up.
    Sum,
    /// Returns compounded: each value is a rate, and the line is what 1 grew to,
    /// less 1 (`(1 + a)(1 + b)... - 1`).
    Compound,
}

impl Cumulative {
    pub const ALL: [Self; 3] = [Self::Off, Self::Sum, Self::Compound];

    pub fn label(self) -> &'static str {
        match self {
            Self::Off => "off",
            Self::Sum => "running sum",
            Self::Compound => "compound",
        }
    }
}

#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct XEncoding {
    pub field: Option<String>,
    pub time_unit: TimeUnit,
}

#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct YEncoding {
    /// One column, or several on a line or scatter chart (one series each).
    pub field: Vec<String>,
    pub aggregate: Aggregate,
    pub cumulative: Cumulative,
    /// The percentile a quantile takes; unset, [`QUANTILE_DEFAULT`].
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub percentile: Option<u8>,
}

/// The percentiles the quantile steps through.
pub const QUANTILES: [u8; 8] = [1, 5, 10, 25, 75, 90, 95, 99];

/// The percentile a quantile starts at.
pub const QUANTILE_DEFAULT: u8 = 90;

impl YEncoding {
    /// The percentile a quantile takes.
    pub fn quantile(&self) -> u8 {
        self.percentile.unwrap_or(QUANTILE_DEFAULT)
    }

    /// The aggregate as a title or a column name says it: `mean`, `p90`.
    pub fn aggregate_name(&self) -> String {
        self.aggregate.named(self.quantile())
    }
}

#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ColorEncoding {
    pub field: Option<String>,
    /// The values given a series each, in color order. Empty: the largest
    /// [`COLOR_MAX`] by rows. `None` is the rows with no value.
    pub values: Vec<Option<String>>,
    /// Whether every other value's rows make one more series, Other. Unset: on for a
    /// scatter, off otherwise.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub other: Option<bool>,
}

#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
pub struct Encoding {
    pub x: XEncoding,
    pub y: YEncoding,
    pub color: ColorEncoding,
}

/// What is charted.
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
pub struct ChartSpec {
    pub mark: Mark,
    pub encoding: Encoding,
}

/// Series a color splits a chart into, at most: one per palette color
/// (`chart_1` to `chart_10`). A terminal of fewer colors draws fewer:
/// [`ChartModal::series_max`].
pub const COLOR_MAX: usize = 10;

/// Most Y columns a line or scatter chart draws at once.
pub const Y_SERIES_MAX: usize = COLOR_MAX;

/// Default histogram bin count.
pub const HISTOGRAM_DEFAULT_BINS: usize = 40;
pub const HISTOGRAM_MIN_BINS: usize = 5;
pub const HISTOGRAM_MAX_BINS: usize = 100;

/// Default heatmap bin count (applies to both axes).
pub const HEATMAP_DEFAULT_BINS: usize = 20;
pub const HEATMAP_MIN_BINS: usize = 5;
pub const HEATMAP_MAX_BINS: usize = 60;

/// KDE bandwidth multiplier bounds and step.
pub const KDE_BANDWIDTH_MIN: f64 = 0.2;
pub const KDE_BANDWIDTH_MAX: f64 = 5.0;
pub const KDE_BANDWIDTH_STEP: f64 = 0.1;

/// The largest sample size (Polars slice takes u32).
pub const CHART_ROW_LIMIT_MAX: usize = u32::MAX as usize;

/// A change to the Rows row not yet read: ←/→ or a typed size waits for Enter, or
/// for focus to leave the row, so the chart reads once rather than per key.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RowsDraft {
    /// Every row rather than a sample.
    pub every: bool,
    /// The sample size being typed, as typed (`50k`).
    pub typed: Option<String>,
    /// Why the typed size cannot be read, until it is edited.
    pub error: Option<&'static str>,
}

/// One row of the panel: a shelf, the line under it, or an option.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum ChartFocus {
    /// The Type shelf: ←/→ step the chart type.
    #[default]
    Type,
    /// The X shelf's column.
    X,
    /// Under X: the time bucket of a temporal X.
    TimeUnit,
    /// Under X on a bar chart: bars by value or by label.
    Order,
    /// Under X on a histogram; an option on a heatmap.
    Bins,
    /// The Y shelf's column (or columns); on a histogram, count or share.
    Y,
    /// Under Y: the aggregate.
    Aggregate,
    /// Under the aggregate, a quantile's percentile.
    Quantile,
    /// The Color shelf's column.
    Color,
    /// Under Color: which values get a series.
    ColorValues,
    /// Options.
    Cumulative,
    Bandwidth,
    Range,
    YStartsAtZero,
    LogScale,
    ShowLegend,
    Grid,
    /// Sample size, for charts that sample.
    LimitRows,
}

/// Whether a shelf takes part in the chart on screen.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShelfUse {
    /// Edited and charted.
    Used,
    /// Shown dimmed with why: `density`, `same as X`.
    Dimmed(&'static str),
}

/// The view's columns a chart can take, by role.
#[derive(Clone, Copy, Default)]
pub struct ChartColumns<'a> {
    pub numeric: &'a [String],
    pub datetime: &'a [String],
    /// Dates and datetimes, which a time bucket truncates (not times of day).
    pub bucketable: &'a [String],
    /// Categories: text, categorical, boolean and integer columns
    /// (see `chart_data::is_category_dtype`).
    pub category: &'a [String],
}

/// The values of the Color column, most rows first, as the chart's last count
/// found them: what the value picker lists.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ColorCounts {
    pub column: String,
    pub values: Vec<(Option<String>, u64)>,
}

/// What the open Picker edits.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PickerFor {
    X,
    Y,
    Color,
    ColorValues,
}

/// The item a Picker offers for "nothing": no Color, no category on a box plot.
pub const NONE_ITEM: &str = "none";

/// Chart view state: the spec, the options, and the panel's focus.
#[derive(Default)]
pub struct ChartModal {
    pub spec: ChartSpec,
    /// The distinct colors the series slots come out as on this terminal
    /// (`Theme::series_colors`); `None` before the app says, read as [`COLOR_MAX`].
    pub series_cap: Option<usize>,
    /// The view's sort as the footer writes it (`time ▲`), which first and last
    /// read the rows in; `None` for the order they were read in.
    pub row_order: Option<String>,
    /// The cursor column's type when `c` chose the chart (`f64`), shown under Type
    /// until the type is changed.
    pub suggested: Option<String>,
    pub y_starts_at_zero: bool,
    pub log_scale: bool,
    pub show_legend: bool,
    /// The grid at the major ticks, on the kinds with axes (`g`).
    pub grid: bool,
    /// Histogram: each bin's share of its group's rows rather than its count.
    pub share: bool,
    pub hist_bins: usize,
    pub heatmap_bins: usize,
    pub kde_bandwidth_factor: f64,
    /// Histogram, Box and KDE: which values are drawn.
    pub value_range: ValueRange,
    pub bar_order: BarOrder,
    /// Rows a chart that samples reads: up to this many, spread across the table.
    /// None = every row. What the chart reads; the Rows row edits `rows_draft`.
    pub row_limit: Option<usize>,
    /// The sample size Sample returns to, kept while Every row is chosen.
    pub sample_rows: usize,
    /// The Rows row's pending change, if any.
    pub rows_draft: Option<RowsDraft>,
    /// The view's row count when the table knows it, for Every row's cost.
    pub view_rows: Option<usize>,
    /// The view is a sample, held in memory: the chart reads all of it, and has no
    /// Rows row of its own.
    pub view_sampled: bool,
    /// A saved view put its chart here: `c` brings it back, whichever column the
    /// cursor is on.
    pub restored: bool,
    pub focus: ChartFocus,
    /// The one Picker, open for the focused row; None while the form has the keys.
    pub picker: Option<PickerState>,
    pub picker_for: Option<PickerFor>,
    /// Per item of the value picker: its rows, shown beside it.
    pub picker_details: Vec<String>,
    pub numeric_candidates: Vec<String>,
    pub temporal_candidates: Vec<String>,
    /// Dates and datetimes: the X columns a time bucket truncates.
    pub bucketable_candidates: Vec<String>,
    pub category_candidates: Vec<String>,
    /// The Color column's values by rows, once a chart counted them.
    pub color_counts: Option<ColorCounts>,
    /// The dataset the choices were made on (`App::dataset_generation`), and the
    /// column the table's cursor was on: `c` again from the same column on the
    /// same dataset brings the chart back as it was left.
    pub dataset: Option<u64>,
    pub opened_on: Option<String>,
    /// Each column's unit, from a delimited spec's unit row: the axis titles name
    /// them. Set as the chart is drawn.
    pub units: Vec<(String, String)>,
    /// The plot has the keys rather than the panel (`x`): ←→ move the crosshair.
    pub plot_focus: bool,
    /// The x of the point the crosshair stands on, kept while the panel has the
    /// keys so it comes back where it was.
    pub cursor_x: Option<f64>,
    /// Where the line or scatter plot was last drawn, for the crosshair and a
    /// click; `None` when it has no points on screen.
    pub plot: Option<crate::widgets::crosshair::PlotPlace>,
}

impl ChartModal {
    pub fn new() -> Self {
        Self::default()
    }

    /// Most series a chart draws: one per distinct series color, up to [`COLOR_MAX`].
    /// A 16-color terminal draws fewer rather than two in one color.
    pub fn series_max(&self) -> usize {
        self.series_cap.unwrap_or(COLOR_MAX).clamp(1, COLOR_MAX)
    }

    /// An axis title for `column`: its name, and its unit when it has one.
    pub fn axis_title(&self, column: &str) -> String {
        match self.units.iter().find(|(name, _)| name == column) {
            Some((_, unit)) => format!("{column} ({unit})"),
            None => column.to_string(),
        }
    }

    pub fn mark(&self) -> Mark {
        self.spec.mark
    }

    pub fn x(&self) -> Option<&String> {
        self.spec.encoding.x.field.as_ref()
    }

    pub fn y(&self) -> &[String] {
        &self.spec.encoding.y.field
    }

    pub fn aggregate(&self) -> Aggregate {
        self.spec.encoding.y.aggregate
    }

    pub fn color(&self) -> Option<&String> {
        self.spec.encoding.color.field.as_ref()
    }

    /// Open the chart view. From the column it was left on, on the same dataset,
    /// the chart comes back as it was, less any column the view no longer has.
    /// Otherwise the chart is chosen from the cursor column's type (`cursor`): a
    /// histogram of a number, a bar of a category's counts, a line over a date.
    pub fn open(
        &mut self,
        columns: ChartColumns<'_>,
        cursor: Option<(&str, &DataType)>,
        default_row_limit: Option<usize>,
        grid: bool,
        dataset: u64,
    ) {
        self.close_picker();
        self.temporal_candidates = columns.datetime.to_vec();
        self.bucketable_candidates = columns.bucketable.to_vec();
        self.numeric_candidates = columns.numeric.to_vec();
        self.category_candidates = columns.category.to_vec();
        self.plot_focus = false;
        self.rows_draft = None;
        let opened_on = cursor.map(|(name, _)| name.to_string());
        let restored = std::mem::take(&mut self.restored);
        if self.dataset == Some(dataset) && (self.opened_on == opened_on || restored) {
            self.keep_existing_choices();
            self.settle();
            self.focus = ChartFocus::Type;
            return;
        }
        self.dataset = Some(dataset);
        self.opened_on = opened_on;
        self.cursor_x = None;
        self.color_counts = None;
        self.spec = ChartSpec::default();
        self.suggested = None;
        self.y_starts_at_zero = false;
        self.log_scale = false;
        self.show_legend = true;
        self.grid = grid;
        self.share = false;
        self.value_range = ValueRange::All;
        self.row_limit = default_row_limit.and_then(|n| {
            if n == 0 {
                None
            } else {
                Some(n.clamp(1, CHART_ROW_LIMIT_MAX))
            }
        });
        self.sample_rows = self
            .row_limit
            .unwrap_or(crate::config::DEFAULT_CHART_ROW_LIMIT);
        self.hist_bins = HISTOGRAM_DEFAULT_BINS;
        self.kde_bandwidth_factor = 1.0;
        self.heatmap_bins = HEATMAP_DEFAULT_BINS;
        self.bar_order = BarOrder::Value;
        if let Some((name, dtype)) = cursor {
            self.suggest(name, dtype);
        }
        self.focus = ChartFocus::Type;
    }

    /// Put a saved view's chart in place for `dataset`: the next `c` draws it.
    pub fn restore(&mut self, saved: &crate::view::SavedChart, dataset: u64) {
        self.spec = saved.spec.clone();
        self.hist_bins = saved.histogram_bins.max(1);
        self.heatmap_bins = saved.heatmap_bins.max(1);
        self.kde_bandwidth_factor = saved.bandwidth;
        self.value_range = saved.range;
        self.bar_order = saved.bar_order;
        self.share = saved.share;
        self.y_starts_at_zero = saved.y_starts_at_zero;
        self.log_scale = saved.log_scale;
        self.show_legend = saved.legend;
        self.grid = saved.grid;
        self.row_limit = saved.rows;
        if let Some(rows) = saved.rows {
            self.sample_rows = rows;
        }
        self.suggested = None;
        self.color_counts = None;
        self.cursor_x = None;
        self.dataset = Some(dataset);
        self.restored = true;
    }

    /// The chart as a view keeps it, with `seed`, the one its own sample is drawn
    /// with, and `export`, how it was last exported.
    pub fn saved(
        &self,
        seed: u64,
        export: Option<crate::view::SavedChartExport>,
    ) -> crate::view::SavedChart {
        crate::view::SavedChart {
            spec: self.spec.clone(),
            histogram_bins: self.hist_bins,
            heatmap_bins: self.heatmap_bins,
            bandwidth: self.kde_bandwidth_factor,
            range: self.value_range,
            bar_order: self.bar_order,
            share: self.share,
            y_starts_at_zero: self.y_starts_at_zero,
            log_scale: self.log_scale,
            legend: self.show_legend,
            grid: self.grid,
            rows: if self.view_sampled {
                None
            } else {
                self.row_limit
            },
            seed: (!self.view_sampled && self.row_limit.is_some()).then_some(seed),
            export,
        }
    }

    /// Show Me: the chart a column's type suggests. A number: its histogram. A
    /// category: a bar of its counts. A date or time: a line over it, of the
    /// first numeric column, with Y left to pick when there is none.
    fn suggest(&mut self, name: &str, dtype: &DataType) {
        let name = name.to_string();
        let encoding = &mut self.spec.encoding;
        if self.temporal_candidates.contains(&name) {
            self.spec.mark = Mark::Line;
            encoding.x.field = Some(name);
            if let Some(y) = self.numeric_candidates.first() {
                encoding.y.field = vec![y.clone()];
            }
        } else if dtype.is_float() || (dtype.is_numeric() && !dtype.is_integer()) {
            self.spec.mark = Mark::Histogram;
            encoding.x.field = Some(name);
        } else if self.category_candidates.contains(&name) && !dtype.is_integer() {
            self.spec.mark = Mark::Bar;
            encoding.x.field = Some(name);
            encoding.y.aggregate = Aggregate::Count;
        } else if self.numeric_candidates.contains(&name) {
            // An integer: a measure more often than a code.
            self.spec.mark = Mark::Histogram;
            encoding.x.field = Some(name);
        } else {
            return;
        }
        self.suggested = Some(crate::formats::column_types::dtype_label(dtype));
    }

    /// Close the chart view. The choices stay for the next open on this dataset.
    pub fn close(&mut self) {
        self.close_picker();
        self.plot_focus = false;
        self.rows_draft = None;
    }

    pub fn close_picker(&mut self) {
        self.picker = None;
        self.picker_for = None;
        self.picker_details.clear();
    }

    /// Whether the crosshair can take the keys: a line or scatter plot with points
    /// on screen.
    pub fn has_crosshair(&self) -> bool {
        self.spec.mark.is_xy() && self.plot.is_some()
    }

    /// Drop the choices whose columns the view no longer offers.
    fn keep_existing_choices(&mut self) {
        let mut all: Vec<&String> = self.numeric_candidates.iter().collect();
        all.extend(self.temporal_candidates.iter());
        all.extend(self.category_candidates.iter());
        let has = |c: &String| all.contains(&c);
        let encoding = &mut self.spec.encoding;
        if encoding.x.field.as_ref().is_some_and(|c| !has(c)) {
            encoding.x.field = None;
        }
        encoding.y.field.retain(|c| has(c));
        if encoding.color.field.as_ref().is_some_and(|c| !has(c)) {
            encoding.color.field = None;
            encoding.color.values.clear();
        }
    }

    // ----- What applies -----

    /// Whether X is a date or a datetime, which a time bucket truncates; a time of
    /// day is not.
    pub fn x_is_bucketable(&self) -> bool {
        self.x()
            .is_some_and(|x| self.bucketable_candidates.iter().any(|c| c == x))
    }

    /// The columns a shelf takes on the chart on screen.
    fn pool(&self, shelf: PickerFor) -> Vec<String> {
        let mark = self.spec.mark;
        let x = self.x();
        match shelf {
            PickerFor::X => match mark {
                Mark::Line | Mark::Scatter => {
                    let mut out = self.temporal_candidates.clone();
                    for c in &self.numeric_candidates {
                        if !out.contains(c) {
                            out.push(c.clone());
                        }
                    }
                    out
                }
                Mark::Bar | Mark::Box => self.category_candidates.clone(),
                Mark::Histogram | Mark::Kde | Mark::Heatmap => self.numeric_candidates.clone(),
            },
            // A distinct count takes any column; the rest a number.
            PickerFor::Y
                if self.spec.encoding.y.aggregate.takes_any_y() && self.takes_aggregate() =>
            {
                let mut all = self.numeric_candidates.clone();
                for c in self
                    .temporal_candidates
                    .iter()
                    .chain(&self.category_candidates)
                {
                    if !all.contains(c) {
                        all.push(c.clone());
                    }
                }
                all.retain(|c| Some(c) != x);
                all
            }
            // The X column against itself is only a diagonal.
            PickerFor::Y => self
                .numeric_candidates
                .iter()
                .filter(|c| Some(*c) != x || mark == Mark::Box)
                .cloned()
                .collect(),
            PickerFor::Color => self
                .category_candidates
                .iter()
                .filter(|c| Some(*c) != x)
                .cloned()
                .collect(),
            PickerFor::ColorValues => Vec::new(),
        }
    }

    /// Whether the Y shelf takes several columns: a line or scatter chart draws one
    /// series each.
    pub fn y_is_multi(&self) -> bool {
        self.spec.mark.is_xy()
    }

    /// How the Y shelf is used on the chart on screen.
    pub fn y_use(&self) -> ShelfUse {
        match self.spec.mark {
            Mark::Kde => ShelfUse::Dimmed("density"),
            _ => ShelfUse::Used,
        }
    }

    /// How the Color shelf is used on the chart on screen.
    pub fn color_use(&self) -> ShelfUse {
        Self::color_use_in(&self.spec)
    }

    /// How `spec` uses its Color shelf.
    pub fn color_use_in(spec: &ChartSpec) -> ShelfUse {
        let y = &spec.encoding.y;
        match spec.mark {
            Mark::Box => ShelfUse::Dimmed("same as X"),
            Mark::Heatmap => ShelfUse::Dimmed("density"),
            Mark::Line | Mark::Scatter if y.field.len() > 1 => ShelfUse::Dimmed("one per Y column"),
            Mark::Bar if y.aggregate == Aggregate::None => ShelfUse::Dimmed("needs an aggregate"),
            _ => ShelfUse::Used,
        }
    }

    /// Whether `spec`'s color splits its chart into series.
    pub fn colored_in(spec: &ChartSpec) -> bool {
        spec.encoding.color.field.is_some() && Self::color_use_in(spec) == ShelfUse::Used
    }

    /// Whether the chart takes an aggregate: a line, a scatter, a bar.
    pub fn takes_aggregate(&self) -> bool {
        matches!(self.spec.mark, Mark::Line | Mark::Scatter | Mark::Bar)
    }

    /// Whether the chart reads every row (an aggregate over the whole view) rather
    /// than a sample.
    pub fn aggregates(&self) -> bool {
        self.takes_aggregate() && self.aggregate() != Aggregate::None
    }

    /// Whether the color splits the chart into series.
    pub fn colored(&self) -> bool {
        Self::colored_in(&self.spec)
    }

    /// Whether a colored `spec` draws Other: as set, or on for a scatter, whose
    /// cloud keeps its shape with every point drawn.
    pub fn shows_other_in(spec: &ChartSpec) -> bool {
        Self::colored_in(spec)
            && spec
                .encoding
                .color
                .other
                .unwrap_or(spec.mark == Mark::Scatter)
    }

    pub fn shows_other(&self) -> bool {
        Self::shows_other_in(&self.spec)
    }

    /// The panel's rows for the chart on screen, in Tab order. A dimmed shelf is
    /// left out: focus passes over it.
    pub fn row_order(&self) -> Vec<ChartFocus> {
        use ChartFocus::*;
        let mark = self.spec.mark;
        let mut rows = vec![Type, X];
        match mark {
            Mark::Line | Mark::Scatter if self.x_is_bucketable() => rows.push(TimeUnit),
            Mark::Bar => rows.push(Order),
            Mark::Histogram => rows.push(Bins),
            _ => {}
        }
        if self.y_use() == ShelfUse::Used {
            rows.push(Y);
        }
        if self.takes_aggregate() {
            rows.push(Aggregate);
            if self.aggregate() == self::Aggregate::Quantile {
                rows.push(Quantile);
            }
        }
        if self.color_use() == ShelfUse::Used {
            rows.push(Color);
            if self.color().is_some() {
                rows.push(ColorValues);
            }
        }
        // Options.
        match mark {
            Mark::Line | Mark::Scatter => {
                if self.aggregates() && self.aggregate().runs_cumulative() {
                    rows.push(Cumulative);
                }
                rows.extend([YStartsAtZero, LogScale, ShowLegend, Grid]);
            }
            Mark::Bar => rows.push(ShowLegend),
            Mark::Histogram => rows.extend([Range, ShowLegend, Grid]),
            Mark::Kde => rows.extend([Bandwidth, Range, ShowLegend, Grid]),
            Mark::Box => rows.extend([Range, Grid]),
            Mark::Heatmap => rows.push(Bins),
        }
        if !self.aggregates() && !self.view_sampled {
            rows.push(LimitRows);
        }
        rows
    }

    /// Whether the chart on screen has axes to draw a grid on: the heatmap's cells
    /// and the bar chart's rows have none.
    pub fn has_grid(&self) -> bool {
        self.row_order().contains(&ChartFocus::Grid)
    }

    // ----- Type -----

    /// Switch the chart type. The shelves carry over where the new type takes
    /// what they hold; a column it cannot take moves to where it can, or goes.
    pub fn set_mark(&mut self, mark: Mark) {
        if mark == self.spec.mark {
            return;
        }
        let old = self.spec.mark;
        self.spec.mark = mark;
        self.suggested = None;
        self.close_picker();
        if !mark.is_xy() {
            self.plot_focus = false;
        }
        let encoding = &mut self.spec.encoding;
        // A number charted against something becomes the value of a one-column
        // chart, and the other way round.
        let numeric = |c: &String| self.numeric_candidates.contains(c);
        let category = |c: &String| self.category_candidates.contains(c);
        match mark {
            Mark::Histogram | Mark::Kde => {
                if !encoding.x.field.as_ref().is_some_and(numeric) {
                    encoding.x.field = encoding.y.field.first().cloned();
                }
            }
            Mark::Box => {
                if encoding.y.field.is_empty()
                    && let Some(x) = encoding.x.field.clone().filter(numeric)
                {
                    encoding.y.field = vec![x];
                    encoding.x.field = None;
                }
                if !encoding.x.field.as_ref().is_some_and(category) {
                    encoding.x.field = None;
                }
            }
            Mark::Bar => {
                if !encoding.x.field.as_ref().is_some_and(category) {
                    encoding.x.field = encoding.color.field.take();
                    encoding.color.values.clear();
                }
                if encoding.y.field.is_empty() && encoding.y.aggregate == Aggregate::None {
                    encoding.y.aggregate = Aggregate::Count;
                }
            }
            Mark::Heatmap => {
                if !encoding.x.field.as_ref().is_some_and(numeric) {
                    encoding.x.field = None;
                }
            }
            Mark::Line | Mark::Scatter => {
                if matches!(old, Mark::Histogram | Mark::Kde | Mark::Box)
                    && encoding.y.field.is_empty()
                    && let Some(x) = encoding.x.field.take()
                {
                    encoding.y.field = vec![x];
                }
            }
        }
        self.settle();
        if !self.row_order().contains(&self.focus) {
            self.focus = ChartFocus::Type;
        }
    }

    /// Keep the spec within what the chart on screen takes.
    fn settle(&mut self) {
        let x_pool = self.pool(PickerFor::X);
        let y_pool = self.pool(PickerFor::Y);
        let color_pool = self.pool(PickerFor::Color);
        let mark = self.spec.mark;
        let x_temporal = self.x_is_bucketable();
        let encoding = &mut self.spec.encoding;
        if encoding
            .x
            .field
            .as_ref()
            .is_some_and(|x| !x_pool.contains(x))
        {
            encoding.x.field = None;
        }
        encoding.y.field.retain(|y| y_pool.contains(y));
        if !mark.is_xy() {
            encoding.y.field.truncate(1);
        }
        // A bucket holds many rows: without an aggregate there is nothing to bucket.
        if !x_temporal || !mark.is_xy() || encoding.y.aggregate == Aggregate::None {
            encoding.x.time_unit = TimeUnit::None;
        }
        if encoding
            .color
            .field
            .as_ref()
            .is_some_and(|c| !color_pool.contains(c))
        {
            encoding.color.field = None;
            encoding.color.values.clear();
        }
        if !matches!(mark, Mark::Line | Mark::Scatter | Mark::Bar) {
            encoding.y.aggregate = Aggregate::None;
        }
        if !encoding.y.aggregate.runs_cumulative() || !mark.is_xy() {
            encoding.y.cumulative = Cumulative::Off;
        }
    }

    pub fn step_mark(&mut self, delta: i8) {
        let mark = crate::app::form::step_value(&Mark::ALL, self.spec.mark, delta);
        self.set_mark(mark);
    }

    // ----- Picker -----

    /// Which picker the focused row opens, if it opens one.
    pub fn picker_for(&self, focus: ChartFocus) -> Option<PickerFor> {
        match focus {
            ChartFocus::X => Some(PickerFor::X),
            ChartFocus::Y if self.spec.mark != Mark::Histogram => Some(PickerFor::Y),
            ChartFocus::Color => Some(PickerFor::Color),
            ChartFocus::ColorValues => Some(PickerFor::ColorValues),
            _ => None,
        }
    }

    /// Whether the focused row's picker toggles several items.
    pub fn picker_is_multi(&self, which: PickerFor) -> bool {
        match which {
            PickerFor::Y => self.y_is_multi(),
            PickerFor::ColorValues => true,
            PickerFor::X | PickerFor::Color => false,
        }
    }

    /// Whether a shelf's picker offers "none" first.
    fn offers_none(&self, which: PickerFor) -> bool {
        which == PickerFor::Color || (which == PickerFor::X && self.spec.mark == Mark::Box)
    }

    /// What a picker offers, and what goes beside each item.
    fn picker_items(&self, which: PickerFor) -> (Vec<String>, Vec<String>) {
        if which == PickerFor::ColorValues {
            let null = crate::glyphs::get().null;
            let Some(counts) = &self.color_counts else {
                return (Vec::new(), Vec::new());
            };
            return counts
                .values
                .iter()
                .map(|(value, rows)| {
                    (
                        value.clone().unwrap_or_else(|| null.to_string()),
                        crate::numfmt::group_chrome(*rows as usize),
                    )
                })
                .unzip();
        }
        let mut items = Vec::new();
        if self.offers_none(which) {
            items.push(NONE_ITEM.to_string());
        }
        items.extend(self.pool(which));
        (items, Vec::new())
    }

    /// The value at `i` of the value picker.
    fn color_value_at(&self, i: usize) -> Option<Option<String>> {
        self.color_counts
            .as_ref()?
            .values
            .get(i)
            .map(|(value, _)| value.clone())
    }

    /// Where the row's current choice sits among `items`.
    fn current_index(&self, which: PickerFor, items: &[String]) -> Option<usize> {
        let encoding = &self.spec.encoding;
        let current = match which {
            PickerFor::X => encoding.x.field.as_deref(),
            PickerFor::Y => encoding.y.field.first().map(String::as_str),
            PickerFor::Color => encoding.color.field.as_deref(),
            PickerFor::ColorValues => return Some(0),
        };
        match current {
            Some(current) => items.iter().position(|i| i == current),
            None if self.offers_none(which) => Some(0),
            None => None,
        }
    }

    /// Open the Picker for the focused row, cursor on the current choice. The value
    /// picker opens only once the chart has counted the values.
    pub fn open_picker(&mut self) {
        let Some(which) = self.picker_for(self.focus) else {
            return;
        };
        if which == PickerFor::ColorValues && !self.has_color_counts() {
            return;
        }
        let (items, details) = self.picker_items(which);
        let mut state = PickerState::new(items.clone());
        if let Some(i) = self.current_index(which, &items) {
            state.select_original(i);
        }
        self.picker = Some(state);
        self.picker_for = Some(which);
        self.picker_details = details;
    }

    /// Whether the value picker has values to list: the chart has counted the
    /// Color column on screen.
    pub fn has_color_counts(&self) -> bool {
        self.color_counts
            .as_ref()
            .is_some_and(|c| Some(&c.column) == self.color())
    }

    /// ←/→ on a pick-one column row: the next or previous column, chosen at once.
    pub fn step_picker_row(&mut self, delta: i8) {
        let Some(which) = self.picker_for(self.focus) else {
            return;
        };
        if self.picker_is_multi(which) {
            return;
        }
        let (items, _) = self.picker_items(which);
        if items.is_empty() {
            return;
        }
        let next = match self.current_index(which, &items) {
            Some(at) => crate::app::form::step_index(at, items.len(), delta),
            None if delta < 0 => items.len() - 1,
            None => 0,
        };
        self.choose(which, &items[next], next);
    }

    /// The original index under the open Picker's cursor.
    fn picker_cursor(&self) -> Option<usize> {
        self.picker.as_ref()?.selected_original()
    }

    /// Enter in the Picker: a pick-one row takes the cursor's item; a row of
    /// several keeps its toggles, or adopts the cursor's item when none are
    /// toggled, so Enter on a fresh list still charts something. The Picker closes.
    pub fn picker_choose(&mut self) {
        let (Some(which), Some(i)) = (self.picker_for, self.picker_cursor()) else {
            self.close_picker();
            return;
        };
        let item = self.picker.as_ref().and_then(|p| p.items().get(i).cloned());
        self.close_picker();
        let Some(item) = item else {
            return;
        };
        match which {
            PickerFor::Y if self.y_is_multi() => {
                if self.spec.encoding.y.field.is_empty() {
                    self.spec.encoding.y.field.push(item);
                }
            }
            PickerFor::ColorValues => {
                if self.spec.encoding.color.values.is_empty()
                    && let Some(value) = self.color_value_at(i)
                {
                    self.spec.encoding.color.values.push(value);
                }
            }
            _ => self.choose(which, &item, i),
        }
        self.settle();
    }

    /// Take `item` (at `i` in the row's items) as the row's one choice.
    fn choose(&mut self, which: PickerFor, item: &str, i: usize) {
        let none = self.offers_none(which) && i == 0;
        let encoding = &mut self.spec.encoding;
        match which {
            PickerFor::X => {
                encoding.x.field = (!none).then(|| item.to_string());
                // A series cannot be the X axis too, nor a color.
                encoding.y.field.retain(|y| y != item);
                if encoding.color.field.as_deref() == Some(item) {
                    encoding.color.field = None;
                    encoding.color.values.clear();
                }
                encoding.x.time_unit = TimeUnit::None;
            }
            PickerFor::Y => encoding.y.field = vec![item.to_string()],
            PickerFor::Color => {
                let field = (!none).then(|| item.to_string());
                if field != encoding.color.field {
                    encoding.color.values.clear();
                }
                encoding.color.field = field;
            }
            PickerFor::ColorValues => {}
        }
        self.settle();
        if !self.row_order().contains(&self.focus) {
            self.focus = ChartFocus::Type;
        }
    }

    /// Space in a picker of several: flip the cursor's item in or out, up to the
    /// palette's colors.
    pub fn picker_toggle(&mut self) {
        let (Some(which), Some(i)) = (self.picker_for, self.picker_cursor()) else {
            return;
        };
        match which {
            PickerFor::Y if self.y_is_multi() => {
                let Some(item) = self.picker.as_ref().and_then(|p| p.items().get(i).cloned())
                else {
                    return;
                };
                let most = Y_SERIES_MAX.min(self.series_max());
                let field = &mut self.spec.encoding.y.field;
                if let Some(pos) = field.iter().position(|c| *c == item) {
                    field.remove(pos);
                } else if field.len() < most {
                    field.push(item);
                }
            }
            PickerFor::ColorValues => {
                let Some(value) = self.color_value_at(i) else {
                    return;
                };
                let most = self.series_max();
                let values = &mut self.spec.encoding.color.values;
                if let Some(pos) = values.iter().position(|v| *v == value) {
                    values.remove(pos);
                } else if values.len() < most {
                    values.push(value);
                }
            }
            _ => {}
        }
    }

    /// Whether the item at `i` of the open picker is toggled on.
    pub fn is_marked(&self, i: usize) -> bool {
        match self.picker_for {
            Some(PickerFor::Y) => self
                .picker
                .as_ref()
                .and_then(|p| p.items().get(i))
                .is_some_and(|item| self.y().contains(item)),
            Some(PickerFor::ColorValues) => self
                .color_value_at(i)
                .is_some_and(|v| self.spec.encoding.color.values.contains(&v)),
            _ => false,
        }
    }

    /// Whether the open picker toggles several items.
    pub fn picker_multi(&self) -> bool {
        self.picker_for.is_some_and(|w| self.picker_is_multi(w))
    }

    // ----- What is charted right now -----

    /// The spec to chart: the one chosen, with an open Picker's cursor previewed on
    /// the Y shelf (and on X, except for a line or scatter, whose X re-reads
    /// everything).
    pub fn effective_spec(&self) -> ChartSpec {
        let mut spec = self.spec.clone();
        let (Some(which), Some(i)) = (self.picker_for, self.picker_cursor()) else {
            return spec;
        };
        let Some(item) = self.picker.as_ref().and_then(|p| p.items().get(i).cloned()) else {
            return spec;
        };
        match which {
            PickerFor::Y if self.y_is_multi() => {
                if !spec.encoding.y.field.contains(&item) {
                    spec.encoding.y.field.push(item);
                }
            }
            PickerFor::Y => spec.encoding.y.field = vec![item],
            PickerFor::X if !spec.mark.is_xy() => {
                spec.encoding.x.field = (!(self.offers_none(which) && i == 0)).then_some(item);
            }
            _ => {}
        }
        spec
    }

    // ----- Stepping -----

    /// ←/→ (and Space, on a choice) on the focused row.
    pub fn step(&mut self, focus: ChartFocus, delta: i8) {
        match focus {
            ChartFocus::Type => self.step_mark(delta),
            ChartFocus::TimeUnit => {
                let encoding = &mut self.spec.encoding;
                encoding.x.time_unit =
                    crate::app::form::step_value(&TimeUnit::ALL, encoding.x.time_unit, delta);
                // A bucket holds many rows, so it needs something to make of them.
                if encoding.x.time_unit != TimeUnit::None && encoding.y.aggregate == Aggregate::None
                {
                    encoding.y.aggregate = Aggregate::Mean;
                }
            }
            ChartFocus::Aggregate => {
                let was = self.aggregate();
                let bucketable = self.x_is_bucketable() && self.spec.mark.is_xy();
                let encoding = &mut self.spec.encoding;
                encoding.y.aggregate =
                    crate::app::form::step_value(&Aggregate::ALL, encoding.y.aggregate, delta);
                // A date X with a group per raw value is close to a group per row:
                // an aggregate starts by the day.
                if was == Aggregate::None
                    && bucketable
                    && encoding.y.aggregate != Aggregate::None
                    && encoding.x.time_unit == TimeUnit::None
                {
                    encoding.x.time_unit = TimeUnit::Day;
                }
                self.settle();
            }
            ChartFocus::Cumulative => {
                let y = &mut self.spec.encoding.y;
                y.cumulative = crate::app::form::step_value(&Cumulative::ALL, y.cumulative, delta);
            }
            ChartFocus::Quantile => {
                let y = &mut self.spec.encoding.y;
                y.percentile = Some(crate::app::form::step_value(
                    &QUANTILES,
                    y.quantile(),
                    delta,
                ));
            }
            ChartFocus::Y if self.spec.mark == Mark::Histogram => self.share = !self.share,
            ChartFocus::Order => {
                self.bar_order =
                    crate::app::form::step_value(&BarOrder::ALL, self.bar_order, delta);
            }
            ChartFocus::Range => {
                self.value_range =
                    crate::app::form::step_value(&ValueRange::ALL, self.value_range, delta);
            }
            ChartFocus::Bins => self.adjust_bins(delta.into()),
            ChartFocus::Bandwidth => {
                self.adjust_kde_bandwidth_factor(f64::from(delta) * KDE_BANDWIDTH_STEP)
            }
            ChartFocus::LimitRows => self.toggle_rows(),
            ChartFocus::YStartsAtZero => self.y_starts_at_zero = !self.y_starts_at_zero,
            ChartFocus::LogScale => self.log_scale = !self.log_scale,
            ChartFocus::ShowLegend => self.show_legend = !self.show_legend,
            ChartFocus::Grid => self.grid = !self.grid,
            // The values line: Space picks them, ←/→ turn Other on or off.
            ChartFocus::ColorValues => {
                self.spec.encoding.color.other = Some(!self.shows_other());
            }
            focus => {
                if self.picker_for(focus).is_some() {
                    self.step_picker_row(delta);
                }
            }
        }
        if !self.row_order().contains(&self.focus) {
            self.focus = ChartFocus::Type;
            self.leave_rows();
        }
    }

    pub fn is_toggle_row(&self, focus: ChartFocus) -> bool {
        matches!(
            focus,
            ChartFocus::YStartsAtZero
                | ChartFocus::LogScale
                | ChartFocus::ShowLegend
                | ChartFocus::Grid
        )
    }

    pub fn toggle_grid(&mut self) {
        self.grid = !self.grid;
    }

    fn adjust_bins(&mut self, delta: i32) {
        if self.spec.mark == Mark::Heatmap {
            self.heatmap_bins = (self.heatmap_bins as i32 + delta)
                .clamp(HEATMAP_MIN_BINS as i32, HEATMAP_MAX_BINS as i32)
                as usize;
        } else {
            self.hist_bins = (self.hist_bins as i32 + delta)
                .clamp(HISTOGRAM_MIN_BINS as i32, HISTOGRAM_MAX_BINS as i32)
                as usize;
        }
    }

    pub fn adjust_kde_bandwidth_factor(&mut self, delta: f64) {
        let next = (self.kde_bandwidth_factor + delta).clamp(KDE_BANDWIDTH_MIN, KDE_BANDWIDTH_MAX);
        self.kde_bandwidth_factor = (next * 10.0).round() / 10.0;
    }

    /// `+`/`-`: step the focused number row.
    pub fn adjust_number_row(&mut self, delta: i32) {
        match self.focus {
            ChartFocus::Bins => self.adjust_bins(delta),
            ChartFocus::Bandwidth => {
                self.adjust_kde_bandwidth_factor(delta as f64 * KDE_BANDWIDTH_STEP)
            }
            _ => {}
        }
    }

    // ----- The Rows row -----

    /// What the Rows row shows: the pending change, or what the chart reads.
    pub fn rows_shown(&self) -> RowsDraft {
        self.rows_draft.clone().unwrap_or(RowsDraft {
            every: self.row_limit.is_none(),
            ..RowsDraft::default()
        })
    }

    /// Whether the Rows row holds a change the chart has not read.
    pub fn rows_pending(&self) -> bool {
        self.rows_draft
            .as_ref()
            .is_some_and(|draft| draft.typed.is_some() || draft.every != self.row_limit.is_none())
    }

    /// ←/→ or Space on Rows: Sample or Every row, pending until Enter. A size being
    /// typed is taken first; one that cannot be read stays to be fixed.
    pub fn toggle_rows(&mut self) {
        if !self.take_typed_size() {
            return;
        }
        let mut draft = self.rows_shown();
        draft.every = !draft.every;
        self.rows_draft = Some(draft);
    }

    /// A key typed on Rows: part of a sample size (`50k`). Typing chooses Sample.
    pub fn type_rows(&mut self, c: char) {
        let mut draft = self.rows_shown();
        draft.every = false;
        draft.error = None;
        draft.typed.get_or_insert_with(String::new).push(c);
        self.rows_draft = Some(draft);
    }

    /// Whether a size is being typed on Rows.
    pub fn typing_rows(&self) -> bool {
        self.rows_draft.as_ref().is_some_and(|d| d.typed.is_some())
    }

    /// Backspace on Rows: one character of the typed size off.
    pub fn backspace_rows(&mut self) {
        if let Some(draft) = self.rows_draft.as_mut()
            && let Some(typed) = draft.typed.as_mut()
        {
            typed.pop();
            draft.error = None;
            if typed.is_empty() {
                draft.typed = None;
            }
        }
    }

    /// Esc on Rows with a change pending: put back what the chart reads.
    pub fn discard_rows(&mut self) {
        self.rows_draft = None;
    }

    /// Resolve a typed size into the draft's sample size. False, with the reason on
    /// the row, when it cannot be read.
    fn take_typed_size(&mut self) -> bool {
        let Some(draft) = self.rows_draft.as_mut() else {
            return true;
        };
        let Some(typed) = draft.typed.take() else {
            return true;
        };
        match crate::analysis::sampling::parse_size(&typed) {
            Ok(rows) => {
                let rows = rows.min(CHART_ROW_LIMIT_MAX);
                // A sample of at least every row is every row.
                if self.view_rows.is_some_and(|total| rows >= total) {
                    draft.every = true;
                } else {
                    draft.every = false;
                    self.sample_rows = rows;
                }
                true
            }
            Err(e) => {
                draft.error = Some(e.short());
                draft.typed = Some(typed);
                false
            }
        }
    }

    /// Enter on Rows: read what it says. False when a typed size cannot be read,
    /// which stays on the row with why.
    pub fn commit_rows(&mut self) -> bool {
        if !self.take_typed_size() {
            return false;
        }
        if let Some(draft) = self.rows_draft.take() {
            self.row_limit = (!draft.every).then_some(self.sample_rows);
        }
        true
    }

    /// Focus left the Rows row: what it says is read, and a size that cannot be
    /// is dropped.
    pub fn leave_rows(&mut self) {
        if !self.commit_rows() {
            self.rows_draft = None;
        }
    }

    /// Whether the spec names everything its chart needs.
    pub fn is_complete(spec: &ChartSpec) -> bool {
        let encoding = &spec.encoding;
        let x = encoding.x.field.is_some();
        let y = !encoding.y.field.is_empty();
        match spec.mark {
            Mark::Line | Mark::Scatter => x && (y || encoding.y.aggregate == Aggregate::Count),
            Mark::Bar => x && (y || encoding.y.aggregate == Aggregate::Count),
            Mark::Histogram | Mark::Kde => x,
            Mark::Box => y,
            Mark::Heatmap => x && y,
        }
    }

    pub fn can_export(&self) -> bool {
        Self::is_complete(&self.effective_spec())
    }

    /// How the chart was made of the rows, as a phrase that stands alone: `mean by
    /// month, running sum, colored by carrier`; empty when there is nothing to say.
    /// The columns charted are named at their axes, not here.
    pub fn how(&self) -> String {
        let spec = self.effective_spec();
        let encoding = &spec.encoding;
        let x = encoding.x.field.clone().unwrap_or_default();
        let mut parts: Vec<String> = Vec::new();
        match spec.mark {
            Mark::Histogram => parts.push(if self.share {
                "share per bin".to_string()
            } else {
                "count per bin".to_string()
            }),
            // The y axis already says density.
            Mark::Kde => {}
            Mark::Box if !x.is_empty() => parts.push(format!("one box per {x}")),
            Mark::Box => {}
            Mark::Heatmap => parts.push("rows per cell".to_string()),
            Mark::Line | Mark::Scatter | Mark::Bar => {
                let aggregate = encoding.y.aggregate;
                let mut how = String::new();
                // Cumulative runs over the rows, not the aggregate.
                if aggregate != Aggregate::None && encoding.y.cumulative == Cumulative::Off {
                    how.push_str(&encoding.y.aggregate_name());
                    how.push(' ');
                }
                let unit = encoding.x.time_unit;
                if unit != TimeUnit::None {
                    how.push_str(&format!("by {}", unit.label()));
                } else if aggregate != Aggregate::None {
                    how.push_str(&format!("by {x}"));
                }
                if !how.trim().is_empty() {
                    parts.push(how.trim().to_string());
                }
                match encoding.y.cumulative {
                    Cumulative::Off => {}
                    Cumulative::Sum => parts.push("running sum".to_string()),
                    Cumulative::Compound => parts.push("compounded".to_string()),
                }
            }
        }
        if self.colored() {
            parts.push(format!("colored by {}", self.color().unwrap()));
        }
        parts.join(", ")
    }
}

impl crate::app::form::Form for ChartModal {
    type Field = ChartFocus;

    fn shown_picker(&mut self) -> Option<(&mut crate::widgets::ui::PickerState, bool)> {
        let multi = self.picker_multi();
        self.picker.as_mut().map(|p| (p, multi))
    }

    fn dismiss_picker(&mut self) {
        ChartModal::close_picker(self);
    }

    fn pick(&mut self, toggle: bool) {
        if toggle {
            self.picker_toggle();
        } else {
            self.picker_choose();
        }
    }

    fn fields(&self) -> Vec<(ChartFocus, crate::app::form::FieldKind)> {
        use crate::app::form::FieldKind;
        self.row_order()
            .into_iter()
            .map(|row| {
                let kind = match self.picker_for(row) {
                    Some(which) => FieldKind::Picker {
                        multi: self.picker_is_multi(which),
                    },
                    None if self.is_toggle_row(row) => FieldKind::Checkbox,
                    // Type, the buckets, the aggregates and the numbers step.
                    None => FieldKind::Choice,
                };
                (row, kind)
            })
            .collect()
    }

    fn focused(&self) -> ChartFocus {
        self.focus
    }

    fn set_focused(&mut self, field: ChartFocus) {
        if field != ChartFocus::LimitRows {
            self.leave_rows();
        }
        self.focus = field;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::app::form::Form;

    fn s(v: &[&str]) -> Vec<String> {
        v.iter().map(|s| s.to_string()).collect()
    }

    struct Cols {
        numeric: Vec<String>,
        datetime: Vec<String>,
        category: Vec<String>,
    }

    fn cols() -> Cols {
        Cols {
            numeric: s(&["delay", "distance", "year"]),
            datetime: s(&["date"]),
            category: s(&["carrier", "origin", "year"]),
        }
    }

    fn open_on(cursor: Option<(&str, &DataType)>) -> ChartModal {
        let c = cols();
        let mut modal = ChartModal::new();
        modal.open(
            ChartColumns {
                numeric: &c.numeric,
                datetime: &c.datetime,
                bucketable: &c.datetime,
                category: &c.category,
            },
            cursor,
            Some(10_000),
            false,
            1,
        );
        modal
    }

    #[test]
    fn an_axis_title_names_the_unit() {
        let mut modal = ChartModal::default();
        assert_eq!(modal.axis_title("cht1"), "cht1");
        modal.units = vec![("cht1".to_string(), "deg F".to_string())];
        assert_eq!(modal.axis_title("cht1"), "cht1 (deg F)");
    }

    /// `c` picks the chart from the cursor column's type and says so under Type.
    #[test]
    fn quick_chart_follows_the_cursor_column_type() {
        let modal = open_on(Some(("delay", &DataType::Float64)));
        assert_eq!(modal.mark(), Mark::Histogram);
        assert_eq!(modal.x().map(String::as_str), Some("delay"));
        assert_eq!(modal.suggested.as_deref(), Some("f64"));

        let modal = open_on(Some(("carrier", &DataType::String)));
        assert_eq!(modal.mark(), Mark::Bar);
        assert_eq!(modal.aggregate(), Aggregate::Count);
        assert!(ChartModal::is_complete(&modal.spec), "counts need no Y");

        let modal = open_on(Some(("date", &DataType::Date)));
        assert_eq!(modal.mark(), Mark::Line);
        assert_eq!(modal.x().map(String::as_str), Some("date"));
        assert_eq!(modal.y(), ["delay"], "the first numeric column");

        let modal = open_on(Some(("year", &DataType::Int64)));
        assert_eq!(modal.mark(), Mark::Histogram, "an integer is a measure");

        let modal = open_on(None);
        assert_eq!(modal.mark(), Mark::Line);
        assert!(modal.x().is_none() && modal.suggested.is_none());
    }

    #[test]
    fn changing_the_type_clears_the_suggestion() {
        let mut modal = open_on(Some(("delay", &DataType::Float64)));
        modal.step(ChartFocus::Type, 1);
        assert_eq!(modal.mark(), Mark::Box);
        assert!(modal.suggested.is_none());
        assert_eq!(modal.y(), ["delay"], "the histogram's value is the box's");
        assert!(modal.x().is_none(), "a box's X is a category or none");
    }

    /// Every type shows the same shelves; one it does not use is dimmed and
    /// focus passes over it.
    #[test]
    fn shelves_dim_by_type() {
        let mut modal = open_on(None);
        let rows = |m: &ChartModal| m.row_order();
        assert!(rows(&modal).contains(&ChartFocus::Color));
        modal.set_mark(Mark::Kde);
        assert_eq!(modal.y_use(), ShelfUse::Dimmed("density"));
        assert!(!rows(&modal).contains(&ChartFocus::Y));
        modal.set_mark(Mark::Box);
        assert_eq!(modal.color_use(), ShelfUse::Dimmed("same as X"));
        assert!(!rows(&modal).contains(&ChartFocus::Color));
        modal.set_mark(Mark::Heatmap);
        assert_eq!(modal.color_use(), ShelfUse::Dimmed("density"));
        modal.set_mark(Mark::Line);
        modal.spec.encoding.y.field = s(&["delay", "distance"]);
        assert_eq!(modal.color_use(), ShelfUse::Dimmed("one per Y column"));
        modal.set_mark(Mark::Bar);
        modal.spec.encoding.y.aggregate = Aggregate::None;
        assert_eq!(modal.color_use(), ShelfUse::Dimmed("needs an aggregate"));
    }

    /// The panel's rows follow the type: a bucket under a date X, the order under
    /// a bar's X, the bins under a histogram's; the sample size only where the
    /// chart samples.
    #[test]
    fn rows_follow_the_type() {
        use ChartFocus::*;
        let mut modal = open_on(Some(("date", &DataType::Date)));
        assert_eq!(
            modal.fields().iter().map(|(f, _)| *f).collect::<Vec<_>>(),
            [
                Type,
                X,
                TimeUnit,
                Y,
                Aggregate,
                Color,
                YStartsAtZero,
                LogScale,
                ShowLegend,
                Grid,
                LimitRows
            ]
        );
        modal.step(TimeUnit, 1);
        assert_eq!(modal.spec.encoding.x.time_unit, super::TimeUnit::Day);
        assert_eq!(
            modal.aggregate(),
            super::Aggregate::Mean,
            "a bucket needs one"
        );
        let rows = modal.row_order();
        assert!(rows.contains(&Cumulative) && !rows.contains(&LimitRows));

        modal.set_mark(Mark::Bar);
        assert_eq!(modal.spec.encoding.x.time_unit, super::TimeUnit::None);
        assert!(modal.row_order().contains(&Order));
        modal.set_mark(Mark::Histogram);
        assert!(modal.row_order().contains(&Bins));
    }

    /// Step the aggregate row by one until it reads `to`.
    fn step_to(modal: &mut ChartModal, to: Aggregate, delta: i8) {
        for _ in 0..Aggregate::ALL.len() {
            if modal.aggregate() == to {
                return;
            }
            modal.step(ChartFocus::Aggregate, delta);
        }
        assert_eq!(modal.aggregate(), to);
    }

    #[test]
    fn the_aggregate_steps_through_every_one() {
        let mut modal = open_on(Some(("date", &DataType::Date)));
        let labels: Vec<&str> = (0..Aggregate::ALL.len())
            .map(|_| {
                modal.step(ChartFocus::Aggregate, 1);
                modal.aggregate().label()
            })
            .collect();
        assert_eq!(
            labels,
            [
                "count", "distinct", "sum", "mean", "median", "stdev", "quantile", "min", "max",
                "first", "last", "none"
            ]
        );
        modal.step(ChartFocus::Aggregate, -1);
        assert_eq!(modal.aggregate(), Aggregate::Last);
    }

    #[test]
    fn cumulative_goes_with_the_aggregate() {
        let mut modal = open_on(Some(("date", &DataType::Date)));
        modal.spec.encoding.y.aggregate = Aggregate::Sum;
        modal.step(ChartFocus::Cumulative, 1);
        assert_eq!(modal.spec.encoding.y.cumulative, Cumulative::Sum);
        modal.step(ChartFocus::Cumulative, 1);
        assert_eq!(modal.spec.encoding.y.cumulative, Cumulative::Compound);
        step_to(&mut modal, Aggregate::None, -1);
        assert_eq!(modal.spec.encoding.y.cumulative, Cumulative::Off);
    }

    #[test]
    fn color_picks_a_category_then_values_by_count() {
        let mut modal = open_on(Some(("delay", &DataType::Float64)));
        modal.focus = ChartFocus::Color;
        modal.open_picker();
        let items = modal.picker.as_ref().unwrap().items().to_vec();
        assert_eq!(items, ["none", "carrier", "origin", "year"]);
        modal.picker.as_mut().unwrap().select_original(1);
        modal.picker_choose();
        assert_eq!(modal.color().map(String::as_str), Some("carrier"));
        assert!(modal.row_order().contains(&ChartFocus::ColorValues));

        // Nothing to pick until a chart has counted the values.
        modal.focus = ChartFocus::ColorValues;
        modal.open_picker();
        assert!(modal.picker.is_none());
        modal.color_counts = Some(ColorCounts {
            column: "carrier".to_string(),
            values: (0..COLOR_MAX + 2)
                .map(|i| (Some(format!("C{i}")), 100 - i as u64))
                .chain([(None, 1)])
                .collect(),
        });
        modal.open_picker();
        assert_eq!(modal.picker_details[0], "100");
        for _ in 0..COLOR_MAX + 2 {
            modal.picker_toggle();
            modal.picker.as_mut().unwrap().move_down();
        }
        assert_eq!(modal.spec.encoding.color.values.len(), COLOR_MAX, "capped");
        assert!(modal.is_marked(0));
        modal.picker.as_mut().unwrap().select_original(0);
        modal.picker_toggle();
        assert!(!modal.is_marked(0));
        modal.picker_choose();
        assert_eq!(modal.spec.encoding.color.values.len(), COLOR_MAX - 1);

        // Another column starts from the top values again.
        modal.focus = ChartFocus::Color;
        modal.step(ChartFocus::Color, 1);
        assert_eq!(modal.color().map(String::as_str), Some("origin"));
        assert!(modal.spec.encoding.color.values.is_empty());
        modal.step(ChartFocus::Color, -1);
        modal.step(ChartFocus::Color, -1);
        assert!(modal.color().is_none(), "none is the first choice");
    }

    #[test]
    fn the_spec_saves_its_time_unit_and_aggregate() {
        let mut modal = open_on(Some(("date", &DataType::Date)));
        modal.step(ChartFocus::TimeUnit, 3);
        let saved = serde_json::to_value(&modal.spec).unwrap();
        assert_eq!(saved["encoding"]["x"]["timeUnit"], "month");
        assert_eq!(saved["encoding"]["y"]["aggregate"], "mean");
    }

    #[test]
    fn the_title_says_how_the_rows_were_made() {
        let mut modal = open_on(Some(("date", &DataType::Date)));
        modal.step(ChartFocus::TimeUnit, 3);
        modal.spec.encoding.y.cumulative = Cumulative::Sum;
        modal.spec.encoding.color.field = Some("carrier".to_string());
        assert_eq!(modal.how(), "by month, running sum, colored by carrier");
    }

    /// Distinct takes any Y, strings too, and reads `distinct by month`; leaving it
    /// for an aggregate of numbers lets a string Y go, as a sum never had one. It
    /// takes no cumulative.
    #[test]
    fn distinct_takes_any_y_and_no_cumulative() {
        let mut modal = open_on(Some(("date", &DataType::Date)));
        step_to(&mut modal, Aggregate::Distinct, 1);
        modal.focus = ChartFocus::Y;
        modal.open_picker();
        let items = modal.picker.as_ref().unwrap().items().to_vec();
        assert!(items.contains(&"carrier".to_string()), "{items:?}");
        modal.close_picker();
        modal.spec.encoding.y.field = vec!["carrier".to_string()];
        modal.step(ChartFocus::TimeUnit, 2);
        assert_eq!(modal.how(), "distinct by month");
        assert!(!modal.row_order().contains(&ChartFocus::Cumulative));
        modal.spec.encoding.y.cumulative = Cumulative::Sum;
        modal.step(ChartFocus::Aggregate, 0);
        assert_eq!(modal.spec.encoding.y.cumulative, Cumulative::Off);
        // Over to sum: a string is no number to add.
        modal.step(ChartFocus::Aggregate, 1);
        assert_eq!(modal.aggregate(), Aggregate::Sum);
        assert!(modal.spec.encoding.y.field.is_empty());
    }

    /// A quantile's percentile is the line under Aggregate: ←/→ step it, the title
    /// says `p95 by month`. Stdev, quantile, first and last take no cumulative.
    #[test]
    fn a_quantile_steps_its_percentile() {
        let mut modal = open_on(Some(("date", &DataType::Date)));
        step_to(&mut modal, Aggregate::Quantile, 1);
        modal.step(ChartFocus::TimeUnit, 2);
        assert_eq!(modal.spec.encoding.x.time_unit, TimeUnit::Month);
        let rows = modal.row_order();
        let at = rows
            .iter()
            .position(|r| *r == ChartFocus::Aggregate)
            .unwrap();
        assert_eq!(rows[at + 1], ChartFocus::Quantile);
        assert_eq!(modal.how(), "p90 by month");
        modal.step(ChartFocus::Quantile, 1);
        assert_eq!(modal.spec.encoding.y.quantile(), 95);
        assert_eq!(modal.how(), "p95 by month");
        modal.step(ChartFocus::Quantile, 1);
        modal.step(ChartFocus::Quantile, 1);
        assert_eq!(modal.spec.encoding.y.quantile(), 1, "wraps");
        for aggregate in [
            Aggregate::Stdev,
            Aggregate::Quantile,
            Aggregate::First,
            Aggregate::Last,
        ] {
            modal.spec.encoding.y.aggregate = aggregate;
            assert!(!modal.row_order().contains(&ChartFocus::Cumulative));
        }
        modal.spec.encoding.y.aggregate = Aggregate::Last;
        assert!(!modal.row_order().contains(&ChartFocus::Quantile));
        assert_eq!(modal.how(), "last by month");
    }

    /// Each type's phrase reads alone and never names the Y column, which its axis
    /// names.
    #[test]
    fn the_how_names_no_y_column() {
        let mut modal = open_on(Some(("date", &DataType::Date)));
        modal.step(ChartFocus::TimeUnit, 3);
        assert_eq!(modal.how(), "mean by month");
        modal.set_mark(Mark::Scatter);
        modal.spec.encoding.x.time_unit = TimeUnit::None;
        modal.spec.encoding.y.aggregate = Aggregate::None;
        assert_eq!(modal.how(), "");
        modal.spec.encoding.color.field = Some("carrier".to_string());
        assert_eq!(modal.how(), "colored by carrier");
        modal.spec.encoding.color.field = None;
        modal.spec.encoding.y.aggregate = Aggregate::Mean;
        assert_eq!(modal.how(), "mean by date");
        modal.set_mark(Mark::Histogram);
        assert_eq!(modal.how(), "count per bin");
        modal.set_mark(Mark::Kde);
        assert_eq!(modal.how(), "");
        modal.set_mark(Mark::Heatmap);
        assert_eq!(modal.how(), "rows per cell");
        for mark in Mark::ALL {
            modal.set_mark(mark);
            assert!(!modal.how().contains("delay"), "{mark:?}: {}", modal.how());
        }
    }

    /// Reopening from the same column on the same dataset keeps the chart; another
    /// column suggests again.
    #[test]
    fn reopening_from_the_same_column_keeps_the_chart() {
        let mut modal = open_on(Some(("delay", &DataType::Float64)));
        modal.set_mark(Mark::Kde);
        modal.toggle_grid();
        modal.close();
        let c = cols();
        let columns = ChartColumns {
            numeric: &c.numeric,
            datetime: &c.datetime,
            bucketable: &c.datetime,
            category: &c.category,
        };
        modal.open(columns, Some(("delay", &DataType::Float64)), None, false, 1);
        assert_eq!(modal.mark(), Mark::Kde);
        assert!(modal.grid);
        modal.open(
            columns,
            Some(("carrier", &DataType::String)),
            None,
            false,
            1,
        );
        assert_eq!(modal.mark(), Mark::Bar);
        assert!(!modal.grid);
    }

    #[test]
    fn the_y_picker_leaves_out_x_and_caps_its_series() {
        let mut modal = open_on(Some(("date", &DataType::Date)));
        modal.spec.encoding.x.field = Some("delay".to_string());
        modal.spec.encoding.y.field.clear();
        modal.focus = ChartFocus::Y;
        modal.open_picker();
        assert_eq!(modal.picker.as_ref().unwrap().items(), ["distance", "year"]);
        assert!(modal.picker_multi());
        modal.picker_choose();
        assert_eq!(
            modal.y(),
            ["distance"],
            "Enter on a fresh list takes the cursor"
        );
    }

    #[test]
    fn number_rows_route_by_the_type() {
        let mut modal = open_on(Some(("delay", &DataType::Float64)));
        modal.focus = ChartFocus::Bins;
        modal.adjust_number_row(1);
        assert_eq!(modal.hist_bins, HISTOGRAM_DEFAULT_BINS + 1);
        modal.set_mark(Mark::Heatmap);
        modal.focus = ChartFocus::Bins;
        modal.adjust_number_row(-1);
        assert_eq!(modal.heatmap_bins, HEATMAP_DEFAULT_BINS - 1);
    }

    /// Rows: ←/→ switch between a sample and every row, a typed size edits the
    /// sample, and nothing reaches what the chart reads until Enter or focus leaves.
    #[test]
    fn rows_change_is_read_on_enter() {
        let mut modal = open_on(Some(("delay", &DataType::Float64)));
        modal.view_rows = Some(36_800_000);
        modal.focus = ChartFocus::LimitRows;
        assert_eq!(modal.row_limit, Some(10_000));
        modal.step(ChartFocus::LimitRows, 1);
        assert!(modal.rows_shown().every && modal.rows_pending());
        assert_eq!(modal.row_limit, Some(10_000), "pending until Enter");
        modal.step(ChartFocus::LimitRows, -1);
        assert!(!modal.rows_pending(), "back where it was");
        modal.step(ChartFocus::LimitRows, 1);
        assert!(modal.commit_rows());
        assert_eq!(modal.row_limit, None);

        // Typing a size chooses Sample; Backspace edits; Esc puts it back.
        for c in "250kx".chars() {
            modal.type_rows(c);
        }
        modal.backspace_rows();
        assert_eq!(modal.rows_shown().typed.as_deref(), Some("250k"));
        assert!(!modal.rows_shown().every);
        modal.discard_rows();
        assert_eq!(modal.row_limit, None);
        assert!(modal.rows_shown().every);

        for c in "250k".chars() {
            modal.type_rows(c);
        }
        assert!(modal.commit_rows());
        assert_eq!(modal.row_limit, Some(250_000));
        assert_eq!(modal.rows_draft, None);

        // A size it cannot read stays on the row with why, and changes nothing.
        modal.type_rows('0');
        assert!(!modal.commit_rows());
        assert!(modal.rows_shown().error.is_some());
        assert_eq!(modal.row_limit, Some(250_000));
        modal.backspace_rows();
        modal.type_rows('2');
        modal.type_rows('m');
        // Leaving the row reads it.
        crate::app::form::Form::focus(&mut modal, ChartFocus::Type);
        assert_eq!(modal.row_limit, Some(2_000_000));

        // At least every row is Every row; Sample remembers its size.
        modal.focus = ChartFocus::LimitRows;
        for c in "40m".chars() {
            modal.type_rows(c);
        }
        assert!(modal.commit_rows());
        assert_eq!(modal.row_limit, None);
        modal.step(ChartFocus::LimitRows, 1);
        assert!(modal.commit_rows());
        assert_eq!(modal.row_limit, Some(2_000_000));
    }

    /// A bucket goes with its aggregate: none takes the bucket away. A time of day
    /// has no bucket to pick.
    #[test]
    fn a_bucket_needs_an_aggregate_and_a_date() {
        let mut modal = open_on(Some(("date", &DataType::Date)));
        modal.step(ChartFocus::TimeUnit, 3);
        assert_eq!(modal.spec.encoding.x.time_unit, TimeUnit::Month);
        modal.spec.encoding.y.aggregate = Aggregate::Sum;
        step_to(&mut modal, Aggregate::None, -1);
        assert_eq!(modal.spec.encoding.x.time_unit, TimeUnit::None);

        let numeric = s(&["delay"]);
        let datetime = s(&["date", "clock"]);
        let mut modal = ChartModal::new();
        modal.open(
            ChartColumns {
                numeric: &numeric,
                datetime: &datetime,
                bucketable: &datetime[..1],
                category: &[],
            },
            Some(("clock", &DataType::Time)),
            None,
            false,
            1,
        );
        assert_eq!(modal.mark(), Mark::Line);
        assert!(!modal.row_order().contains(&ChartFocus::TimeUnit));
    }

    /// An aggregate over a date X starts by the day: a group per raw value would be
    /// close to a group per row.
    #[test]
    fn an_aggregate_on_a_date_starts_by_the_day() {
        let mut modal = open_on(Some(("date", &DataType::Date)));
        assert_eq!(modal.spec.encoding.x.time_unit, TimeUnit::None);
        modal.step(ChartFocus::Aggregate, 1);
        assert_eq!(modal.spec.encoding.x.time_unit, TimeUnit::Day);
        step_to(&mut modal, Aggregate::Mean, 1);
        assert_eq!(modal.spec.encoding.x.time_unit, TimeUnit::Day);
        modal.step(ChartFocus::TimeUnit, -1);
        assert_eq!(
            modal.spec.encoding.x.time_unit,
            TimeUnit::None,
            "still the user's call"
        );
    }

    /// An integer histogram turned into a box: its column is the box's value, and
    /// not also its category.
    #[test]
    fn a_box_from_an_integer_histogram_has_no_category() {
        let mut modal = open_on(Some(("year", &DataType::Int64)));
        modal.set_mark(Mark::Box);
        assert_eq!(modal.y(), ["year"]);
        assert!(modal.x().is_none());
    }

    /// Whether color splits a chart reads the spec charted: a second Y previewed in
    /// the picker dims the color.
    #[test]
    fn color_use_reads_the_spec_charted() {
        let mut modal = open_on(Some(("date", &DataType::Date)));
        modal.spec.encoding.color.field = Some("carrier".to_string());
        assert!(modal.colored());
        let mut spec = modal.spec.clone();
        spec.encoding.y.field.push("distance".to_string());
        assert!(!ChartModal::colored_in(&spec));
    }
}