@tetrascience-npm/ts-lib-viz 0.0.0-stage → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (278) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +2171 -2
  3. package/dist/src/builder/PlotPanel.d.ts +42 -0
  4. package/dist/src/builder/PlotPanel.js +433 -0
  5. package/dist/src/builder/Popover.d.ts +37 -0
  6. package/dist/src/builder/Popover.js +32 -0
  7. package/dist/src/builder/Stepper.d.ts +21 -0
  8. package/dist/src/builder/Stepper.js +41 -0
  9. package/dist/src/builder/index.d.ts +10 -0
  10. package/dist/src/builder/index.js +10 -0
  11. package/dist/src/core/adapters.d.ts +86 -0
  12. package/dist/src/core/adapters.js +219 -0
  13. package/dist/src/core/aggregate.d.ts +74 -0
  14. package/dist/src/core/aggregate.js +102 -0
  15. package/dist/src/core/annotations.d.ts +34 -0
  16. package/dist/src/core/annotations.js +71 -0
  17. package/dist/src/core/band-order.d.ts +86 -0
  18. package/dist/src/core/band-order.js +132 -0
  19. package/dist/src/core/binning.d.ts +123 -0
  20. package/dist/src/core/binning.js +193 -0
  21. package/dist/src/core/bounds.d.ts +40 -0
  22. package/dist/src/core/bounds.js +107 -0
  23. package/dist/src/core/brush.d.ts +73 -0
  24. package/dist/src/core/brush.js +102 -0
  25. package/dist/src/core/build.d.ts +322 -0
  26. package/dist/src/core/build.js +496 -0
  27. package/dist/src/core/camera.d.ts +152 -0
  28. package/dist/src/core/camera.js +303 -0
  29. package/dist/src/core/capabilities.d.ts +24 -0
  30. package/dist/src/core/capabilities.js +307 -0
  31. package/dist/src/core/chord.d.ts +49 -0
  32. package/dist/src/core/chord.js +57 -0
  33. package/dist/src/core/circular.d.ts +83 -0
  34. package/dist/src/core/circular.js +63 -0
  35. package/dist/src/core/cluster.d.ts +136 -0
  36. package/dist/src/core/cluster.js +607 -0
  37. package/dist/src/core/critique.d.ts +92 -0
  38. package/dist/src/core/critique.js +905 -0
  39. package/dist/src/core/density.d.ts +38 -0
  40. package/dist/src/core/density.js +82 -0
  41. package/dist/src/core/figure-set.d.ts +34 -0
  42. package/dist/src/core/figure-set.js +55 -0
  43. package/dist/src/core/finding-codes.d.ts +10 -0
  44. package/dist/src/core/finding-codes.js +1 -0
  45. package/dist/src/core/fit-types.d.ts +46 -0
  46. package/dist/src/core/fit-types.js +7 -0
  47. package/dist/src/core/fitter.d.ts +62 -0
  48. package/dist/src/core/fitter.js +102 -0
  49. package/dist/src/core/frame-geometry.d.ts +407 -0
  50. package/dist/src/core/frame-geometry.js +612 -0
  51. package/dist/src/core/frame.d.ts +329 -0
  52. package/dist/src/core/frame.js +1432 -0
  53. package/dist/src/core/geom-table.d.ts +503 -0
  54. package/dist/src/core/geom-table.js +484 -0
  55. package/dist/src/core/geom-types.d.ts +182 -0
  56. package/dist/src/core/geom-types.js +17 -0
  57. package/dist/src/core/grammar.d.ts +359 -0
  58. package/dist/src/core/grammar.js +397 -0
  59. package/dist/src/core/heat-reductions.d.ts +36 -0
  60. package/dist/src/core/heat-reductions.js +133 -0
  61. package/dist/src/core/hexbin.d.ts +84 -0
  62. package/dist/src/core/hexbin.js +168 -0
  63. package/dist/src/core/hierarchy.d.ts +59 -0
  64. package/dist/src/core/hierarchy.js +248 -0
  65. package/dist/src/core/hit.d.ts +75 -0
  66. package/dist/src/core/hit.js +37 -0
  67. package/dist/src/core/index.d.ts +33 -0
  68. package/dist/src/core/index.js +18 -0
  69. package/dist/src/core/internal.d.ts +58 -0
  70. package/dist/src/core/internal.js +58 -0
  71. package/dist/src/core/labels.d.ts +37 -0
  72. package/dist/src/core/labels.js +54 -0
  73. package/dist/src/core/layers.d.ts +594 -0
  74. package/dist/src/core/layers.js +2892 -0
  75. package/dist/src/core/load.d.ts +17 -0
  76. package/dist/src/core/load.js +356 -0
  77. package/dist/src/core/marginal.d.ts +65 -0
  78. package/dist/src/core/marginal.js +116 -0
  79. package/dist/src/core/mesh.d.ts +69 -0
  80. package/dist/src/core/mesh.js +237 -0
  81. package/dist/src/core/omission.d.ts +41 -0
  82. package/dist/src/core/omission.js +62 -0
  83. package/dist/src/core/palette-sets.d.ts +623 -0
  84. package/dist/src/core/palette-sets.js +1058 -0
  85. package/dist/src/core/palette.d.ts +871 -0
  86. package/dist/src/core/palette.js +511 -0
  87. package/dist/src/core/plot-data.d.ts +71 -0
  88. package/dist/src/core/plot-data.js +82 -0
  89. package/dist/src/core/predicate.d.ts +50 -0
  90. package/dist/src/core/predicate.js +20 -0
  91. package/dist/src/core/profile.d.ts +111 -0
  92. package/dist/src/core/profile.js +221 -0
  93. package/dist/src/core/readout.d.ts +108 -0
  94. package/dist/src/core/readout.js +215 -0
  95. package/dist/src/core/region.d.ts +56 -0
  96. package/dist/src/core/region.js +101 -0
  97. package/dist/src/core/rings.d.ts +96 -0
  98. package/dist/src/core/rings.js +171 -0
  99. package/dist/src/core/rules.d.ts +45 -0
  100. package/dist/src/core/rules.js +69 -0
  101. package/dist/src/core/sankey.d.ts +49 -0
  102. package/dist/src/core/sankey.js +96 -0
  103. package/dist/src/core/select.d.ts +52 -0
  104. package/dist/src/core/select.js +216 -0
  105. package/dist/src/core/side-track.d.ts +98 -0
  106. package/dist/src/core/side-track.js +197 -0
  107. package/dist/src/core/sizing.d.ts +54 -0
  108. package/dist/src/core/sizing.js +37 -0
  109. package/dist/src/core/spec-edit.d.ts +68 -0
  110. package/dist/src/core/spec-edit.js +105 -0
  111. package/dist/src/core/spec.d.ts +1111 -0
  112. package/dist/src/core/spec.js +39 -0
  113. package/dist/src/core/stats.d.ts +161 -0
  114. package/dist/src/core/stats.js +318 -0
  115. package/dist/src/core/tick-format.d.ts +21 -0
  116. package/dist/src/core/tick-format.js +87 -0
  117. package/dist/src/core/tokens.d.ts +47 -0
  118. package/dist/src/core/tokens.js +138 -0
  119. package/dist/src/core/transform.d.ts +21 -0
  120. package/dist/src/core/transform.js +19 -0
  121. package/dist/src/core/voxels.d.ts +53 -0
  122. package/dist/src/core/voxels.js +129 -0
  123. package/dist/src/core/zoom.d.ts +76 -0
  124. package/dist/src/core/zoom.js +103 -0
  125. package/dist/src/graph/GraphSurface.d.ts +127 -0
  126. package/dist/src/graph/GraphSurface.js +423 -0
  127. package/dist/src/graph/data.d.ts +103 -0
  128. package/dist/src/graph/data.js +140 -0
  129. package/dist/src/graph/index.d.ts +7 -0
  130. package/dist/src/graph/index.js +7 -0
  131. package/dist/src/mcp/data.d.ts +28 -0
  132. package/dist/src/mcp/data.js +131 -0
  133. package/dist/src/mcp/index.d.ts +7 -0
  134. package/dist/src/mcp/index.js +7 -0
  135. package/dist/src/mcp/paths.d.ts +12 -0
  136. package/dist/src/mcp/paths.js +30 -0
  137. package/dist/src/mcp/render.d.ts +67 -0
  138. package/dist/src/mcp/render.js +189 -0
  139. package/dist/src/mcp/server.d.ts +7 -0
  140. package/dist/src/mcp/server.js +25 -0
  141. package/dist/src/mcp/tools.d.ts +82 -0
  142. package/dist/src/mcp/tools.js +412 -0
  143. package/dist/src/react/BrushLayer.d.ts +20 -0
  144. package/dist/src/react/BrushLayer.js +98 -0
  145. package/dist/src/react/ColorLegend.d.ts +45 -0
  146. package/dist/src/react/ColorLegend.js +64 -0
  147. package/dist/src/react/Figure.d.ts +19 -0
  148. package/dist/src/react/Figure.js +6 -0
  149. package/dist/src/react/Frame.d.ts +87 -0
  150. package/dist/src/react/Frame.js +192 -0
  151. package/dist/src/react/MarkLegend.d.ts +32 -0
  152. package/dist/src/react/MarkLegend.js +24 -0
  153. package/dist/src/react/SeriesLegend.d.ts +38 -0
  154. package/dist/src/react/SeriesLegend.js +27 -0
  155. package/dist/src/react/SizeLegend.d.ts +24 -0
  156. package/dist/src/react/SizeLegend.js +25 -0
  157. package/dist/src/react/Toolbar.d.ts +109 -0
  158. package/dist/src/react/Toolbar.js +483 -0
  159. package/dist/src/react/Tooltip.d.ts +37 -0
  160. package/dist/src/react/Tooltip.js +59 -0
  161. package/dist/src/react/Viz.d.ts +112 -0
  162. package/dist/src/react/Viz.js +1311 -0
  163. package/dist/src/react/export.d.ts +32 -0
  164. package/dist/src/react/export.js +181 -0
  165. package/dist/src/react/geoms/Area.d.ts +38 -0
  166. package/dist/src/react/geoms/Area.js +48 -0
  167. package/dist/src/react/geoms/Bars.d.ts +130 -0
  168. package/dist/src/react/geoms/Bars.js +164 -0
  169. package/dist/src/react/geoms/Box.d.ts +50 -0
  170. package/dist/src/react/geoms/Box.js +32 -0
  171. package/dist/src/react/geoms/Chord.d.ts +60 -0
  172. package/dist/src/react/geoms/Chord.js +78 -0
  173. package/dist/src/react/geoms/Contour.d.ts +44 -0
  174. package/dist/src/react/geoms/Contour.js +78 -0
  175. package/dist/src/react/geoms/DensityCurve.d.ts +30 -0
  176. package/dist/src/react/geoms/DensityCurve.js +38 -0
  177. package/dist/src/react/geoms/FitCurve.d.ts +26 -0
  178. package/dist/src/react/geoms/FitCurve.js +52 -0
  179. package/dist/src/react/geoms/Freqpoly.d.ts +30 -0
  180. package/dist/src/react/geoms/Freqpoly.js +45 -0
  181. package/dist/src/react/geoms/Gauge.d.ts +42 -0
  182. package/dist/src/react/geoms/Gauge.js +90 -0
  183. package/dist/src/react/geoms/Heatmap.d.ts +79 -0
  184. package/dist/src/react/geoms/Heatmap.js +51 -0
  185. package/dist/src/react/geoms/Hexbin.d.ts +74 -0
  186. package/dist/src/react/geoms/Hexbin.js +89 -0
  187. package/dist/src/react/geoms/Icicle.d.ts +38 -0
  188. package/dist/src/react/geoms/Icicle.js +9 -0
  189. package/dist/src/react/geoms/Ideogram.d.ts +58 -0
  190. package/dist/src/react/geoms/Ideogram.js +77 -0
  191. package/dist/src/react/geoms/Interval.d.ts +30 -0
  192. package/dist/src/react/geoms/Interval.js +39 -0
  193. package/dist/src/react/geoms/Line.d.ts +39 -0
  194. package/dist/src/react/geoms/Line.js +52 -0
  195. package/dist/src/react/geoms/Parallel.d.ts +38 -0
  196. package/dist/src/react/geoms/Parallel.js +131 -0
  197. package/dist/src/react/geoms/Pie.d.ts +62 -0
  198. package/dist/src/react/geoms/Pie.js +59 -0
  199. package/dist/src/react/geoms/Points.d.ts +84 -0
  200. package/dist/src/react/geoms/Points.js +207 -0
  201. package/dist/src/react/geoms/Points3d.d.ts +49 -0
  202. package/dist/src/react/geoms/Points3d.js +58 -0
  203. package/dist/src/react/geoms/Radar.d.ts +39 -0
  204. package/dist/src/react/geoms/Radar.js +59 -0
  205. package/dist/src/react/geoms/Range.d.ts +31 -0
  206. package/dist/src/react/geoms/Range.js +20 -0
  207. package/dist/src/react/geoms/RingStack.d.ts +39 -0
  208. package/dist/src/react/geoms/RingStack.js +65 -0
  209. package/dist/src/react/geoms/Rule.d.ts +29 -0
  210. package/dist/src/react/geoms/Rule.js +51 -0
  211. package/dist/src/react/geoms/Sankey.d.ts +24 -0
  212. package/dist/src/react/geoms/Sankey.js +51 -0
  213. package/dist/src/react/geoms/Segment.d.ts +36 -0
  214. package/dist/src/react/geoms/Segment.js +39 -0
  215. package/dist/src/react/geoms/Sunburst.d.ts +46 -0
  216. package/dist/src/react/geoms/Sunburst.js +64 -0
  217. package/dist/src/react/geoms/Surface.d.ts +32 -0
  218. package/dist/src/react/geoms/Surface.js +49 -0
  219. package/dist/src/react/geoms/Text.d.ts +35 -0
  220. package/dist/src/react/geoms/Text.js +40 -0
  221. package/dist/src/react/geoms/Treemap.d.ts +39 -0
  222. package/dist/src/react/geoms/Treemap.js +16 -0
  223. package/dist/src/react/geoms/Violin.d.ts +56 -0
  224. package/dist/src/react/geoms/Violin.js +60 -0
  225. package/dist/src/react/geoms/Volume.d.ts +37 -0
  226. package/dist/src/react/geoms/Volume.js +40 -0
  227. package/dist/src/react/geoms/Waterfall.d.ts +43 -0
  228. package/dist/src/react/geoms/Waterfall.js +18 -0
  229. package/dist/src/react/geoms/cube-stage.d.ts +39 -0
  230. package/dist/src/react/geoms/cube-stage.js +85 -0
  231. package/dist/src/react/geoms/hierarchy-cells.d.ts +42 -0
  232. package/dist/src/react/geoms/hierarchy-cells.js +59 -0
  233. package/dist/src/react/geoms/sector-axis.d.ts +66 -0
  234. package/dist/src/react/geoms/sector-axis.js +99 -0
  235. package/dist/src/react/gl/GlMesh.d.ts +22 -0
  236. package/dist/src/react/gl/GlMesh.js +78 -0
  237. package/dist/src/react/gl/GlPoints.d.ts +45 -0
  238. package/dist/src/react/gl/GlPoints.js +117 -0
  239. package/dist/src/react/gl/GlVolume.d.ts +28 -0
  240. package/dist/src/react/gl/GlVolume.js +112 -0
  241. package/dist/src/react/gl/GlWalls.d.ts +24 -0
  242. package/dist/src/react/gl/GlWalls.js +55 -0
  243. package/dist/src/react/gl/context.d.ts +71 -0
  244. package/dist/src/react/gl/context.js +98 -0
  245. package/dist/src/react/gl/encode.d.ts +120 -0
  246. package/dist/src/react/gl/encode.js +215 -0
  247. package/dist/src/react/gl/index.d.ts +10 -0
  248. package/dist/src/react/gl/index.js +8 -0
  249. package/dist/src/react/gl/refresh.d.ts +7 -0
  250. package/dist/src/react/gl/refresh.js +19 -0
  251. package/dist/src/react/gl/renderer.d.ts +377 -0
  252. package/dist/src/react/gl/renderer.js +1081 -0
  253. package/dist/src/react/gl/shaders.d.ts +92 -0
  254. package/dist/src/react/gl/shaders.js +405 -0
  255. package/dist/src/react/gl/surface.d.ts +31 -0
  256. package/dist/src/react/gl/surface.js +73 -0
  257. package/dist/src/react/index.d.ts +12 -0
  258. package/dist/src/react/index.js +11 -0
  259. package/dist/src/react/orbit.d.ts +38 -0
  260. package/dist/src/react/orbit.js +81 -0
  261. package/dist/src/react/sides/DendrogramPanel.d.ts +32 -0
  262. package/dist/src/react/sides/DendrogramPanel.js +39 -0
  263. package/dist/src/react/sides/DensityPanel.d.ts +31 -0
  264. package/dist/src/react/sides/DensityPanel.js +37 -0
  265. package/dist/src/react/sides/HistogramPanel.d.ts +30 -0
  266. package/dist/src/react/sides/HistogramPanel.js +18 -0
  267. package/dist/src/react/sides/RugPanel.d.ts +28 -0
  268. package/dist/src/react/sides/RugPanel.js +21 -0
  269. package/dist/src/react/sides/TrackPanel.d.ts +37 -0
  270. package/dist/src/react/sides/TrackPanel.js +27 -0
  271. package/dist/src/react/theme.d.ts +48 -0
  272. package/dist/src/react/theme.js +148 -0
  273. package/dist/src/react/useFits.d.ts +26 -0
  274. package/dist/src/react/useFits.js +62 -0
  275. package/dist/src/react/useZoom.d.ts +50 -0
  276. package/dist/src/react/useZoom.js +141 -0
  277. package/package.json +164 -5
  278. package/theme/default.css +62 -0
@@ -0,0 +1,197 @@
1
+ /**
2
+ * An annotation track resolved against its band.
3
+ *
4
+ * A track reads a column of the same table. A category takes its modal value per group and a
5
+ * measure takes the mean.
6
+ */
7
+ import { boundsOf } from "./aggregate.js";
8
+ import { groupRows } from "./plot-data.js";
9
+ import { pinnedOver } from "./figure-set.js";
10
+ import { rampPosition } from "./layers.js";
11
+ import { sequentialCss } from "./palette.js";
12
+ /**
13
+ * The sequential map a numeric track draws on.
14
+ *
15
+ * Separate from `DEFAULT_SEQUENTIAL`. One ramp with two meanings is the failure chart-design
16
+ * rule 8 names.
17
+ */
18
+ export const TRACK_SEQUENTIAL = "mako";
19
+ /** The value a group has most often, and whether the group had more than one. */
20
+ function modal(counts) {
21
+ let best = null;
22
+ let most = 0;
23
+ for (const [value, count] of counts) {
24
+ if (count > most) {
25
+ most = count;
26
+ best = value;
27
+ }
28
+ }
29
+ return best == null ? null : { value: best, alone: counts.size === 1 };
30
+ }
31
+ /**
32
+ * A track over a band's categories.
33
+ *
34
+ * `categories` is the band in drawn order and `keyField` is their column. A category with no
35
+ * reading is left out.
36
+ */
37
+ export function trackFor({ field, axis, categories, keyField, data, fieldAt, palette = TRACK_SEQUENTIAL, pinned, dropLevels = true, alias, }) {
38
+ const held = fieldAt.get(field);
39
+ if (!held || categories.length === 0)
40
+ return null;
41
+ const within = new Set(categories);
42
+ /* A reduced band draws centroids and the rows behind one centroid answer together. */
43
+ const drawnAs = (key) => alias?.get(key) ?? key;
44
+ if (held.numeric) {
45
+ const totals = new Map();
46
+ for (const group of groupRows(data, keyField, field, "mean")) {
47
+ const category = drawnAs(group.key);
48
+ if (!within.has(category))
49
+ continue;
50
+ const running = totals.get(category) ?? { sum: 0, n: 0 };
51
+ running.sum += group.value * group.n;
52
+ running.n += group.n;
53
+ totals.set(category, running);
54
+ }
55
+ const at = new Map();
56
+ for (const [category, { sum, n }] of totals)
57
+ if (n > 0)
58
+ at.set(category, sum / n);
59
+ if (at.size === 0)
60
+ return null;
61
+ return {
62
+ kind: "magnitude",
63
+ field,
64
+ label: held.unit ? `${held.label} (${held.unit})` : held.label,
65
+ axis,
66
+ at,
67
+ domain: boundsOf([...at.values()]),
68
+ palette,
69
+ };
70
+ }
71
+ const counted = new Map();
72
+ for (let row = 0; row < data.rowCount; row += 1) {
73
+ const read = data.text(row, keyField);
74
+ if (read == null)
75
+ continue;
76
+ const key = drawnAs(read);
77
+ if (!within.has(key))
78
+ continue;
79
+ const value = data.text(row, field);
80
+ if (value == null)
81
+ continue;
82
+ let counts = counted.get(key);
83
+ if (!counts) {
84
+ counts = new Map();
85
+ counted.set(key, counts);
86
+ }
87
+ counts.set(value, (counts.get(value) ?? 0) + 1);
88
+ }
89
+ const at = new Map();
90
+ const disagreed = [];
91
+ /* Walked in the band's order. The first-seen order behind the codes is the drawn order. */
92
+ for (const category of categories) {
93
+ const counts = counted.get(category);
94
+ if (!counts)
95
+ continue;
96
+ const picked = modal(counts);
97
+ if (!picked)
98
+ continue;
99
+ at.set(category, picked.value);
100
+ if (!picked.alone)
101
+ disagreed.push(category);
102
+ }
103
+ if (at.size === 0)
104
+ return null;
105
+ const seen = new Map();
106
+ for (const value of at.values())
107
+ if (!seen.has(value))
108
+ seen.set(value, seen.size);
109
+ const codes = pinnedOver(seen, pinned);
110
+ /* Drawn first either way. A legend keeping unused levels still opens on what is here. */
111
+ const levels = dropLevels ? [...seen.keys()] : [...seen.keys(), ...[...codes.keys()].filter((v) => !seen.has(v))];
112
+ return { kind: "identity", field, label: held.label, axis, at, codes, levels, disagreed };
113
+ }
114
+ /**
115
+ * A track over a band of measures.
116
+ *
117
+ * A track here binds a property of the field -- its unit, role, direction or cut. Keyed by the
118
+ * label the band drew.
119
+ */
120
+ export function trackOverFields({ field, axis, categories, measures, palette = TRACK_SEQUENTIAL, pinned, dropLevels = true, }) {
121
+ const within = new Set(categories);
122
+ const held = measures.filter((measure) => within.has(measure.label));
123
+ if (held.length === 0)
124
+ return null;
125
+ const read = (measure) => measure[field];
126
+ const numeric = held.some((measure) => typeof read(measure) === "number");
127
+ if (numeric) {
128
+ const at = new Map();
129
+ for (const measure of held) {
130
+ const value = read(measure);
131
+ if (typeof value === "number" && Number.isFinite(value))
132
+ at.set(measure.label, value);
133
+ }
134
+ if (at.size === 0)
135
+ return null;
136
+ const readings = [...at.values()];
137
+ return {
138
+ kind: "magnitude",
139
+ field,
140
+ label: field,
141
+ axis,
142
+ at,
143
+ domain: boundsOf(readings),
144
+ palette,
145
+ };
146
+ }
147
+ const byLabel = new Map(held.map((one) => [one.label, one]));
148
+ const at = new Map();
149
+ /* Walked in the band's order. The first-seen order behind the codes is the drawn order. */
150
+ for (const category of categories) {
151
+ const measure = byLabel.get(category);
152
+ const value = measure ? read(measure) : undefined;
153
+ if (typeof value === "string" && value.length > 0)
154
+ at.set(category, value);
155
+ }
156
+ if (at.size === 0)
157
+ return null;
158
+ const seen = new Map();
159
+ for (const value of at.values())
160
+ if (!seen.has(value))
161
+ seen.set(value, seen.size);
162
+ const codes = pinnedOver(seen, pinned);
163
+ const levels = dropLevels ? [...seen.keys()] : [...seen.keys(), ...[...codes.keys()].filter((v) => !seen.has(v))];
164
+ /* A declaration is one value per measure. No group disagrees with itself. */
165
+ return { kind: "identity", field, label: field, axis, at, codes, levels, disagreed: [] };
166
+ }
167
+ /**
168
+ * The map a numeric track's legend samples, taking a value in the track's own units.
169
+ *
170
+ * The same contract `Trace.ramp` states and `ColorLegend` reads. The figure's palettes resolve the
171
+ * track's map, as they resolve a layer's. Palettes holding no `sequentialFor` fall back to the registry.
172
+ */
173
+ export function trackRamp(track, palettes, dark = false) {
174
+ const [lo, hi] = track.domain;
175
+ const ramp = palettes.sequentialFor?.(track.palette) ?? ((t) => sequentialCss(t, track.palette, dark));
176
+ return (value) => ramp(rampPosition(value, lo, hi));
177
+ }
178
+ /**
179
+ * The fill each of a track's categories takes, or null with no reading.
180
+ *
181
+ * A category track reads the figure's categorical map. A measure track reads the sequential map
182
+ * it named.
183
+ */
184
+ export function trackFill(track, palettes, dark = false) {
185
+ if (track.kind === "identity") {
186
+ return (category) => {
187
+ const value = track.at.get(category);
188
+ const code = value == null ? undefined : track.codes.get(value);
189
+ return code == null ? null : palettes.categorical(code);
190
+ };
191
+ }
192
+ const ramp = trackRamp(track, palettes, dark);
193
+ return (category) => {
194
+ const value = track.at.get(category);
195
+ return value == null ? null : ramp(value);
196
+ };
197
+ }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Type and spacing, as a function of panel room.
3
+ *
4
+ * Three classes. `regular` is the package default, `compact` is a facet and `roomy` is a chart
5
+ * with its own page.
6
+ */
7
+ export type Sizing = "compact" | "regular" | "roomy";
8
+ /**
9
+ * What a sizing class sets.
10
+ *
11
+ * Seven values. Rule 10's other four type levels are set by classes in `Figure`.
12
+ */
13
+ export interface Metrics {
14
+ /** Tick labels and the one axis title, in pixels. */
15
+ axis: number;
16
+ /**
17
+ * Labeled ticks a value scale is asked for, as a hint.
18
+ *
19
+ * One number covers both axes. An axis stating `tickStep` or `tickCount` keeps what the author
20
+ * said.
21
+ */
22
+ ticks: number;
23
+ /** Annotation text written on the plot, in pixels. */
24
+ annotation: number;
25
+ /** Length of a tick dash, in pixels. */
26
+ tickLength: number;
27
+ /**
28
+ * Width of a break in a band, in pixels.
29
+ *
30
+ * Six pixels is a hairline on a full page and a third of a cell in a facet.
31
+ */
32
+ gap: number;
33
+ /** Radius of a point at full opacity, in pixels. */
34
+ pointRadius: number;
35
+ /** Radius under rule 7's faded treatment, in pixels. */
36
+ fadedRadius: number;
37
+ }
38
+ /**
39
+ * Nine pixels is the floor.
40
+ *
41
+ * Rule 10's smallest level is the source note at nine. A compact panel gives its room back by
42
+ * shortening ticks and shrinking geoms.
43
+ */
44
+ export declare const TYPE_FLOOR = 9;
45
+ export declare const METRICS: Record<Sizing, Metrics>;
46
+ /** The default class. What the package drew before the table existed. */
47
+ export declare const REGULAR: Metrics;
48
+ /**
49
+ * Which class a panel of this size falls into.
50
+ *
51
+ * Both dimensions decide it. Either one running short makes a panel compact. Roomy needs both.
52
+ */
53
+ export declare function sizingFor(width: number, height: number): Sizing;
54
+ export declare const metricsFor: (width: number, height: number) => Metrics;
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Type and spacing, as a function of panel room.
3
+ *
4
+ * Three classes. `regular` is the package default, `compact` is a facet and `roomy` is a chart
5
+ * with its own page.
6
+ */
7
+ /**
8
+ * Nine pixels is the floor.
9
+ *
10
+ * Rule 10's smallest level is the source note at nine. A compact panel gives its room back by
11
+ * shortening ticks and shrinking geoms.
12
+ */
13
+ export const TYPE_FLOOR = 9;
14
+ /*
15
+ Tick counts. Five suits an ordinary panel. A facet takes three -- five numbers across 300px
16
+ collide. A full page takes eight.
17
+ */
18
+ export const METRICS = {
19
+ compact: { axis: 10, ticks: 3, annotation: 10, tickLength: 3, gap: 4, pointRadius: 3, fadedRadius: 1.4 },
20
+ regular: { axis: 11, ticks: 5, annotation: 11, tickLength: 4, gap: 6, pointRadius: 4, fadedRadius: 1.6 },
21
+ roomy: { axis: 13, ticks: 8, annotation: 12, tickLength: 6, gap: 8, pointRadius: 5, fadedRadius: 1.8 },
22
+ };
23
+ /** The default class. What the package drew before the table existed. */
24
+ export const REGULAR = METRICS.regular;
25
+ /**
26
+ * Which class a panel of this size falls into.
27
+ *
28
+ * Both dimensions decide it. Either one running short makes a panel compact. Roomy needs both.
29
+ */
30
+ export function sizingFor(width, height) {
31
+ if (width < 360 || height < 220)
32
+ return "compact";
33
+ if (width > 720 && height > 420)
34
+ return "roomy";
35
+ return "regular";
36
+ }
37
+ export const metricsFor = (width, height) => METRICS[sizingFor(width, height)];
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Editing a spec, and which kinds a table can be asked for.
3
+ *
4
+ * Each edit returns a new spec of the caller's own type. The host stamps the time it saves, and
5
+ * nothing here reaches a store.
6
+ */
7
+ import type { Aesthetic } from "./geom-table.js";
8
+ import type { ChartSpec, PlotKind, PlotSource, StoredSpec, LayerSpec } from "./spec.js";
9
+ /** A chart type as the builder offers it. An unavailable kind names its reason. */
10
+ export interface KindOption {
11
+ kind: PlotKind;
12
+ label: string;
13
+ describe: string;
14
+ available: boolean;
15
+ /** Absent when available. */
16
+ reason?: string;
17
+ }
18
+ /**
19
+ * Which chart types this table can draw.
20
+ *
21
+ * Roles come from the field where it has one and from its type where it does not. An unavailable
22
+ * kind names the column it lacks.
23
+ */
24
+ export declare function plotKinds(fields: readonly {
25
+ role?: string;
26
+ numeric: boolean;
27
+ }[]): KindOption[];
28
+ /** A table a plot can be pointed at, offered by name. */
29
+ export interface PlotSourceOption {
30
+ source: PlotSource;
31
+ label: string;
32
+ hint?: string;
33
+ /**
34
+ * Columns this source offers, for a caller that has to name one.
35
+ *
36
+ * Read off the schema. Absent where the caller picks a column.
37
+ */
38
+ columns?: string[];
39
+ }
40
+ /**
41
+ * A layer of one kind with nothing bound or styled. The id is the kind and an index, as the builder
42
+ * names one.
43
+ */
44
+ export declare function newLayer(kind: PlotKind, index?: number): LayerSpec;
45
+ /** A new layer of one kind, at the first index no layer's id holds. */
46
+ export declare function addLayer<S extends ChartSpec>(spec: S, kind: PlotKind): S;
47
+ export declare function updateLayer<S extends ChartSpec>(spec: S, id: string, change: Partial<LayerSpec>): S;
48
+ /**
49
+ * Binds one aesthetic of one layer to a column, or clears it where the field is absent.
50
+ *
51
+ * `field` is a list where the geom reads several columns at once. An empty list clears it.
52
+ */
53
+ export declare function bindAes<S extends ChartSpec>(spec: S, { layerId, aesthetic, field }: {
54
+ layerId: string;
55
+ aesthetic: Aesthetic;
56
+ field?: string | readonly string[];
57
+ }): S;
58
+ /** A plot has at least one layer. Removing the last one is refused. */
59
+ export declare function removeLayer<S extends ChartSpec>(spec: S, id: string): S;
60
+ /**
61
+ * Changing the source clears the bindings.
62
+ *
63
+ * Field ids and category values belong to the table they came from. Channels, tracks and band
64
+ * orders drop them. A layer drawing its own rows keeps its own.
65
+ */
66
+ export declare function setSource<S extends StoredSpec>(spec: S, source: PlotSource): S;
67
+ /** Whether a visible layer reads the second vertical scale. A second axis draws where one does. */
68
+ export declare const usesY2: (spec: ChartSpec) => boolean;
@@ -0,0 +1,105 @@
1
+ /**
2
+ * Editing a spec, and which kinds a table can be asked for.
3
+ *
4
+ * Each edit returns a new spec of the caller's own type. The host stamps the time it saves, and
5
+ * nothing here reaches a store.
6
+ */
7
+ import { PLOTS } from "./capabilities.js";
8
+ /**
9
+ * Which chart types this table can draw.
10
+ *
11
+ * Roles come from the field where it has one and from its type where it does not. An unavailable
12
+ * kind names the column it lacks.
13
+ */
14
+ export function plotKinds(fields) {
15
+ /* Roles are tested for membership against what a channel accepts. An unknown role matches
16
+ nothing and offers the kind as unavailable. */
17
+ const roles = new Set(fields.map((field) => field.role ?? (field.numeric ? "measure" : "dimension")));
18
+ return PLOTS.map((capability) => {
19
+ const unmet = capability.aesthetics.filter((channel) => channel.required && !channel.roles.some((role) => roles.has(role)));
20
+ return {
21
+ kind: capability.kind,
22
+ label: capability.label,
23
+ describe: capability.describe,
24
+ available: unmet.length === 0,
25
+ reason: unmet.length === 0
26
+ ? undefined
27
+ : `Needs a ${unmet[0].roles.join(" or ")} column for ${unmet[0].label.toLowerCase()}`,
28
+ };
29
+ });
30
+ }
31
+ /**
32
+ * A layer of one kind with nothing bound or styled. The id is the kind and an index, as the builder
33
+ * names one.
34
+ */
35
+ export function newLayer(kind, index = 0) {
36
+ return { id: `${kind}${index}`, kind, aes: {} };
37
+ }
38
+ /** A new layer of one kind, at the first index no layer's id holds. */
39
+ export function addLayer(spec, kind) {
40
+ const taken = new Set(spec.layers.map((layer) => layer.id));
41
+ let index = spec.layers.length;
42
+ while (taken.has(`${kind}${index}`))
43
+ index += 1;
44
+ return { ...spec, layers: [...spec.layers, newLayer(kind, index)] };
45
+ }
46
+ export function updateLayer(spec, id, change) {
47
+ return { ...spec, layers: spec.layers.map((layer) => (layer.id === id ? { ...layer, ...change } : layer)) };
48
+ }
49
+ /**
50
+ * Binds one aesthetic of one layer to a column, or clears it where the field is absent.
51
+ *
52
+ * `field` is a list where the geom reads several columns at once. An empty list clears it.
53
+ */
54
+ export function bindAes(spec, { layerId, aesthetic, field }) {
55
+ const cleared = field === undefined || (typeof field !== "string" && field.length === 0);
56
+ return {
57
+ ...spec,
58
+ layers: spec.layers.map((layer) => {
59
+ if (layer.id !== layerId)
60
+ return layer;
61
+ const { [aesthetic]: _held, ...aes } = layer.aes;
62
+ return { ...layer, aes: cleared ? aes : { ...aes, [aesthetic]: field } };
63
+ }),
64
+ };
65
+ }
66
+ /** A plot has at least one layer. Removing the last one is refused. */
67
+ export function removeLayer(spec, id) {
68
+ if (spec.layers.length <= 1)
69
+ return spec;
70
+ return { ...spec, layers: spec.layers.filter((layer) => layer.id !== id) };
71
+ }
72
+ /** The scales that position a reading, as `ChartScales` keys them. */
73
+ const POSITIONS = new Set(["x", "y", "y2"]);
74
+ /** An axis with the old table's category values taken off. A cluster order recomputes and stays. */
75
+ const withoutValues = ({ labels: _labels, order, ...axis }) => order?.kind === "cluster" ? { ...axis, order } : axis;
76
+ /**
77
+ * Changing the source clears the bindings.
78
+ *
79
+ * Field ids and category values belong to the table they came from. Channels, tracks and band
80
+ * orders drop them. A layer drawing its own rows keeps its own.
81
+ */
82
+ export function setSource(spec, source) {
83
+ if (source.kind === spec.source?.kind && source.id === spec.source.id)
84
+ return spec;
85
+ return {
86
+ ...spec,
87
+ source,
88
+ layers: spec.layers.map((layer) => {
89
+ if (layer.data)
90
+ return layer;
91
+ const { directLabels: _label, ...rest } = layer;
92
+ return { ...rest, aes: {} };
93
+ }),
94
+ tooltip: [],
95
+ facet: undefined,
96
+ sides: spec.sides?.filter((side) => side.content.kind !== "track"),
97
+ scales: spec.scales &&
98
+ Object.fromEntries(Object.entries(spec.scales).map(([name, scale]) => [
99
+ name,
100
+ POSITIONS.has(name) && scale ? withoutValues(scale) : scale,
101
+ ])),
102
+ };
103
+ }
104
+ /** Whether a visible layer reads the second vertical scale. A second axis draws where one does. */
105
+ export const usesY2 = (spec) => spec.layers.some((layer) => layer.visible !== false && layer.axis === "y2");