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

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 +28 -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,17 @@
1
+ /**
2
+ * Reading a saved spec, in this format or the one before it.
3
+ *
4
+ * A document holding `series` predates `SPEC_VERSION`, and each of its fields moves to today's name
5
+ * and shape. A field this reader does not know rides through, which keeps a host's own.
6
+ */
7
+ import { type ChartSpec } from "./spec.js";
8
+ /**
9
+ * A spec as this version reads it.
10
+ *
11
+ * Takes a current spec as it stands, and migrates one holding `series`. Throws on a version this
12
+ * package does not read, and on a document holding no layer list.
13
+ *
14
+ * A scale, style or ring field saved under an earlier name takes its name now, and an annotation of a
15
+ * kind that drew nothing drops.
16
+ */
17
+ export declare function loadSpec(input: unknown): ChartSpec;
@@ -0,0 +1,356 @@
1
+ /**
2
+ * Reading a saved spec, in this format or the one before it.
3
+ *
4
+ * A document holding `series` predates `SPEC_VERSION`, and each of its fields moves to today's name
5
+ * and shape. A field this reader does not know rides through, which keeps a host's own.
6
+ */
7
+ import { GEOMS, paintAesthetic } from "./geom-table.js";
8
+ import { SPEC_VERSION } from "./spec.js";
9
+ const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
10
+ /** The fields of `from` that hold a value. A migrated spec states nothing its source left out. */
11
+ const defined = (from) => Object.fromEntries(Object.entries(from).filter(([, value]) => value !== undefined));
12
+ const rowOf = (kind) => typeof kind === "string" ? GEOMS[kind] : undefined;
13
+ /** The kinds renamed to ggplot's names. `range` and `donut` take their own paths. */
14
+ const KIND_BEFORE = {
15
+ scatter: "point",
16
+ interval: "ribbon",
17
+ histogram2d: "hex",
18
+ box: "boxplot",
19
+ scatter3d: "point3d",
20
+ };
21
+ /** The aesthetics renamed to ggplot's names. */
22
+ const AES_BEFORE = {
23
+ lo: "ymin",
24
+ hi: "ymax",
25
+ mid: "y",
26
+ xEnd: "xend",
27
+ yEnd: "yend",
28
+ opacity: "alpha",
29
+ dash: "linetype",
30
+ };
31
+ /** The glyph a range kind drew unless its style named one. */
32
+ const RANGE_GLYPH = "pointrange";
33
+ /** The hole a donut drew unless its style named one, as a fraction of the outer radius. */
34
+ const DONUT_HOLE = 0.55;
35
+ /** The kinds that stack their groups where no position is stated. Before, they overlapped. */
36
+ const STACKED = ["bar", "histogram", "area"];
37
+ /** Position scale fields under their earlier names, each beside its name now. */
38
+ const SCALE_NAMES = { color: "ink", tickStep: "breakWidth" };
39
+ /** Style fields under their earlier names: a violin's `side` and a gauge's `bands`. */
40
+ const STYLE_NAMES = { side: "half", bands: "sections" };
41
+ /** Ring content kinds under their earlier names. A per-ribbon and a per-row point ring are one kind now. */
42
+ const RING_KINDS = { bars: "bar", points: "point", scatter: "point" };
43
+ /** Ring content fields under their earlier names: `reduce` for the aggregate. */
44
+ const RING_NAMES = { reduce: "aggregate" };
45
+ /** Annotation kinds that drew nothing, `series-label` under its name before the layer rename. */
46
+ const UNDRAWN = ["band", "callout", "layer-label", "series-label"];
47
+ /**
48
+ * A spec as this version reads it.
49
+ *
50
+ * Takes a current spec as it stands, and migrates one holding `series`. Throws on a version this
51
+ * package does not read, and on a document holding no layer list.
52
+ *
53
+ * A scale, style or ring field saved under an earlier name takes its name now, and an annotation of a
54
+ * kind that drew nothing drops.
55
+ */
56
+ export function loadSpec(input) {
57
+ if (!isObject(input))
58
+ throw new Error("ts-lib-viz: a spec is an object");
59
+ const spec = settled(Array.isArray(input.series) ? migrateChart(input) : input);
60
+ if (spec.version !== undefined && spec.version !== SPEC_VERSION) {
61
+ throw new Error(`ts-lib-viz: spec version ${String(spec.version)} is unknown, and this package reads ${SPEC_VERSION}`);
62
+ }
63
+ if (typeof spec.id !== "string")
64
+ throw new Error("ts-lib-viz: a spec states its id as a string");
65
+ if (!Array.isArray(spec.layers))
66
+ throw new Error("ts-lib-viz: a spec holds a list of layers");
67
+ spec.layers.forEach((layer, index) => {
68
+ if (!isObject(layer) || typeof layer.kind !== "string" || !isObject(layer.aes)) {
69
+ throw new Error(`ts-lib-viz: layers[${index}] states no kind and aes`);
70
+ }
71
+ });
72
+ return spec;
73
+ }
74
+ /**
75
+ * A spec in this layout under the field names the format settled on.
76
+ *
77
+ * Renames the `SCALE_NAMES`, `STYLE_NAMES` and `RING_NAMES` fields, a stated new name winning, and the
78
+ * `RING_KINDS`. Drops the `UNDRAWN` annotations. A spec holding none of these comes back as it stands.
79
+ */
80
+ function settled(spec) {
81
+ let moved = false;
82
+ const renamed = (from, names) => {
83
+ if (!isObject(from))
84
+ return from;
85
+ const next = { ...from };
86
+ for (const [old, now] of Object.entries(names)) {
87
+ if (!(old in next))
88
+ continue;
89
+ next[now] ??= next[old];
90
+ delete next[old];
91
+ moved = true;
92
+ }
93
+ return next;
94
+ };
95
+ const next = { ...spec };
96
+ if (isObject(spec.scales)) {
97
+ const scales = { ...spec.scales };
98
+ for (const axis of ["x", "y", "y2"])
99
+ if (axis in scales)
100
+ scales[axis] = renamed(scales[axis], SCALE_NAMES);
101
+ next.scales = scales;
102
+ }
103
+ const ring = (content) => {
104
+ const kind = typeof content.kind === "string" ? RING_KINDS[content.kind] : undefined;
105
+ if (kind)
106
+ moved = true;
107
+ return { ...renamed(content, RING_NAMES), ...(kind ? { kind } : {}) };
108
+ };
109
+ if (Array.isArray(spec.layers)) {
110
+ next.layers = spec.layers.map((layer) => {
111
+ if (!isObject(layer))
112
+ return layer;
113
+ const out = { ...layer };
114
+ if ("style" in layer)
115
+ out.style = renamed(layer.style, STYLE_NAMES);
116
+ if (Array.isArray(layer.rings)) {
117
+ out.rings = layer.rings.map((held) => isObject(held) && isObject(held.content) ? { ...held, content: ring(held.content) } : held);
118
+ }
119
+ return out;
120
+ });
121
+ }
122
+ if (Array.isArray(spec.annotations)) {
123
+ const drawn = spec.annotations.filter((note) => !(isObject(note) && UNDRAWN.includes(note.kind)));
124
+ if (drawn.length < spec.annotations.length)
125
+ moved = true;
126
+ next.annotations = drawn;
127
+ }
128
+ return moved ? next : spec;
129
+ }
130
+ /** A chart from before `SPEC_VERSION`, field by field. */
131
+ function migrateChart(old) {
132
+ const { series, version: _version, dek, facetBy, facetColumns, coord, axes, colorScale, legend, fit, aspect, annotations, sides, ...rest } = old;
133
+ const layers = series.filter(isObject).map(migrateLayer);
134
+ const notes = [];
135
+ for (const note of Array.isArray(annotations) ? annotations : []) {
136
+ if (isObject(note) && note.kind === "reference")
137
+ layers.push(referenceLayer(note, layers.length));
138
+ else
139
+ notes.push(note);
140
+ }
141
+ const scales = {};
142
+ const oldAxes = isObject(axes) ? axes : {};
143
+ for (const name of ["x", "y", "y2"]) {
144
+ if (isObject(oldAxes[name]))
145
+ scales[name] = migrateAxis(oldAxes[name]);
146
+ }
147
+ /* The old color scale and swatch title reached each layer, whichever paint it now maps. */
148
+ const paints = new Set(layers.flatMap((layer) => (rowOf(layer.kind) ? [paintAesthetic(layer)] : [])));
149
+ if (paints.size === 0)
150
+ paints.add("color");
151
+ const { titles, ...legendRest } = isObject(legend) ? legend : {};
152
+ const named = isObject(titles) ? titles : {};
153
+ const paintTitle = (isObject(colorScale) ? colorScale.title : undefined) ?? named.color ?? named.series;
154
+ for (const paint of paints) {
155
+ const scale = { ...(isObject(colorScale) ? migrateColorScale(colorScale) : {}), ...defined({ title: paintTitle }) };
156
+ if (Object.keys(scale).length > 0)
157
+ scales[paint] = scale;
158
+ }
159
+ for (const [was, aesthetic] of [
160
+ ["size", "size"],
161
+ ["shape", "shape"],
162
+ ["opacity", "alpha"],
163
+ ["dash", "linetype"],
164
+ ]) {
165
+ if (named[was] !== undefined)
166
+ scales[aesthetic] = { title: named[was] };
167
+ }
168
+ return {
169
+ version: SPEC_VERSION,
170
+ ...rest,
171
+ ...defined({
172
+ subtitle: dek,
173
+ layers,
174
+ facet: typeof facetBy === "string" ? defined({ by: facetBy, columns: facetColumns }) : undefined,
175
+ coord: coord === "transpose" ? "flip" : coord === "rect" ? "cartesian" : coord,
176
+ scales: Object.keys(scales).length > 0 ? scales : undefined,
177
+ annotations: Array.isArray(annotations) ? notes : undefined,
178
+ legend: isObject(legend) && Object.keys(legendRest).length > 0 ? legendRest : undefined,
179
+ sides: Array.isArray(sides) ? sides.map(migrateSide) : undefined,
180
+ sizing: fit,
181
+ /* Width over height before, height over width now, ggplot's aspect.ratio. */
182
+ aspect: typeof aspect === "number" && aspect > 0 ? 1 / aspect : aspect,
183
+ }),
184
+ };
185
+ }
186
+ /** One series from before `SPEC_VERSION` as a layer. */
187
+ function migrateLayer(old) {
188
+ const { channels, kind: was, style, label, splitBy, bin, axis, colorScale, fit, smooth, statParams, position, ...rest } = old;
189
+ const oldStyle = isObject(style) ? style : {};
190
+ let kind = was === "range" ? (oldStyle.range ?? RANGE_GLYPH) : was === "donut" ? "pie" : (KIND_BEFORE[String(was)] ?? was);
191
+ /* A stated bin cut a continuous x. A bar now draws one per distinct x, and the histogram bins. */
192
+ if (kind === "bar" && bin !== undefined)
193
+ kind = "histogram";
194
+ const row = rowOf(kind);
195
+ const aes = {};
196
+ for (const [key, value] of Object.entries(isObject(channels) ? channels : {})) {
197
+ if (value !== undefined)
198
+ aes[AES_BEFORE[key] ?? key] ??= value;
199
+ }
200
+ /* The color channel painted an interior before `fill` existed. */
201
+ if (row && row.accepts.includes("fill") && !row.accepts.includes("color") && aes.color !== undefined) {
202
+ aes.fill ??= aes.color;
203
+ delete aes.color;
204
+ }
205
+ /* A split handed each value its own color. It takes the paint where the layer binds none, and the group otherwise. */
206
+ if (typeof splitBy === "string" && row) {
207
+ const paint = paintAesthetic({ kind: kind, aes: aes });
208
+ const bound = [...row.requires, ...row.accepts];
209
+ if (aes[paint] === undefined && bound.includes(paint))
210
+ aes[paint] = splitBy;
211
+ else if (bound.includes("group"))
212
+ aes.group ??= splitBy;
213
+ }
214
+ /* A fit or a smooth ran one curve per value of its groupBy, which the group aesthetic now does. */
215
+ const oldFit = isObject(fit) ? fit : undefined;
216
+ const oldSmooth = isObject(smooth) ? smooth : undefined;
217
+ const groupBy = oldFit?.groupBy ?? oldSmooth?.groupBy;
218
+ if (typeof groupBy === "string")
219
+ aes.group ??= groupBy;
220
+ /* Each series held the chart's channels whole. An aesthetic the kind reads nowhere drops, as the builder leaves it behind now. */
221
+ if (row) {
222
+ const reads = new Set([...row.requires, ...row.accepts]);
223
+ for (const key of Object.keys(aes))
224
+ if (!reads.has(key))
225
+ delete aes[key];
226
+ }
227
+ const params = isObject(statParams) ? { ...statParams } : {};
228
+ if (typeof bin === "number")
229
+ params.binwidth ??= bin;
230
+ const { range: _range, jitter, ...styleRest } = oldStyle;
231
+ const nextStyle = migrateStyle(styleRest, row);
232
+ if (was === "donut")
233
+ nextStyle.innerRadius ??= DONUT_HOLE;
234
+ /* A jitter was a style fraction and is a position now, at ggplot's default reach. */
235
+ let nextPosition = position;
236
+ if (nextPosition === undefined && typeof jitter === "number" && jitter > 0)
237
+ nextPosition = { kind: "jitter" };
238
+ /* A jitter's reach was a share of the axis's resolution and is data units now. A stated share drops to the default. Zero reads the same in both. */
239
+ if (isObject(nextPosition) && (nextPosition.kind === "jitter" || nextPosition.kind === "jitterdodge")) {
240
+ const { width, height, ...kept } = nextPosition;
241
+ nextPosition = { ...kept, ...(width === 0 ? { width } : {}), ...(height === 0 ? { height } : {}) };
242
+ }
243
+ const grouped = ["fill", "color", "group"].some((aesthetic) => aes[aesthetic] !== undefined);
244
+ if (nextPosition === undefined && grouped && STACKED.includes(String(kind)))
245
+ nextPosition = "identity";
246
+ const layer = { ...rest, kind, aes };
247
+ const paint = row ? paintAesthetic(layer) : "color";
248
+ return {
249
+ ...layer,
250
+ ...defined({
251
+ axis: axis === "right" ? "y2" : axis === "left" ? "y" : axis,
252
+ style: Object.keys(nextStyle).length > 0 ? nextStyle : undefined,
253
+ position: nextPosition,
254
+ directLabels: isObject(label) ? migrateLabel(label) : undefined,
255
+ fit: oldFit ? withoutGroupBy(oldFit) : undefined,
256
+ smooth: oldSmooth ? withoutGroupBy(oldSmooth) : undefined,
257
+ statParams: Object.keys(params).length > 0 ? params : undefined,
258
+ scales: isObject(colorScale) ? { [paint]: migrateColorScale(colorScale) } : undefined,
259
+ }),
260
+ };
261
+ }
262
+ const withoutGroupBy = ({ groupBy: _groupBy, ...kept }) => kept;
263
+ /**
264
+ * A style from before `SPEC_VERSION`, key by key.
265
+ *
266
+ * `stroke` splits into `color`, `linewidth` and `linetype`. Its own opacity has no field and drops.
267
+ */
268
+ function migrateStyle(old, row) {
269
+ const { opacity, stroke, edgeWidth, level, parts, ...rest } = old;
270
+ const ink = typeof stroke === "string" ? { color: stroke } : isObject(stroke) ? stroke : {};
271
+ const next = {
272
+ ...rest,
273
+ ...defined({
274
+ alpha: opacity,
275
+ color: ink.color,
276
+ linewidth: ink.width,
277
+ linetype: ink.dash,
278
+ halo: edgeWidth,
279
+ isoLevel: level,
280
+ }),
281
+ };
282
+ /* A kind with no interior drew its line in its fill, ahead of the stroke's color. */
283
+ if (row && !row.paints.includes("fill") && next.fill !== undefined) {
284
+ next.color = next.fill;
285
+ delete next.fill;
286
+ }
287
+ if (isObject(parts)) {
288
+ next.parts = Object.fromEntries(Object.entries(parts).map(([part, paint]) => [part, isObject(paint) ? migrateStyle(paint, undefined) : paint]));
289
+ }
290
+ return next;
291
+ }
292
+ /** A top label rule ranked by an axis under `by`, which is `on` now, as the threshold rule reads. */
293
+ function migrateLabel(old) {
294
+ if (!isObject(old.when) || old.when.kind !== "top")
295
+ return old;
296
+ const { by, ...when } = old.when;
297
+ return { ...old, when: { ...when, on: when.on ?? by } };
298
+ }
299
+ function migrateAxis(old) {
300
+ const { range, lineWidth, log, order, ...rest } = old;
301
+ return {
302
+ ...rest,
303
+ ...defined({
304
+ limits: range,
305
+ linewidth: lineWidth,
306
+ transform: rest.transform ?? (log === true ? "log10" : undefined),
307
+ order: isObject(order) ? migrateOrder(order) : order,
308
+ }),
309
+ };
310
+ }
311
+ /** A band order tagged under `by`, which is `kind` now. A reorder callback cannot survive a save and drops. */
312
+ function migrateOrder(old) {
313
+ const { by, callback: _callback, ...rest } = old;
314
+ return defined({ kind: rest.kind ?? by, ...rest });
315
+ }
316
+ function migrateColorScale(old) {
317
+ const { range, scale, breaks, legendTicks, log, sqrt, ...rest } = old;
318
+ const ticks = Array.isArray(legendTicks) ? legendTicks.filter(isObject) : undefined;
319
+ const labels = ticks?.filter((tick) => tick.label != null).map((tick) => [String(tick.at), tick.label]);
320
+ return {
321
+ ...rest,
322
+ ...defined({
323
+ limits: range,
324
+ standardize: scale,
325
+ bins: breaks,
326
+ breaks: ticks?.map((tick) => tick.at),
327
+ labels: labels && labels.length > 0 ? Object.fromEntries(labels) : undefined,
328
+ transform: rest.transform ?? (log === true ? "log10" : sqrt === true ? "sqrt" : undefined),
329
+ }),
330
+ };
331
+ }
332
+ /** A side panel named its edge before. Its histogram took `bin`, which is `binwidth` now, and `auto` is the default. */
333
+ function migrateSide(old) {
334
+ if (!isObject(old))
335
+ return old;
336
+ const { edge, content, ...rest } = old;
337
+ let next = content;
338
+ if (isObject(content) && content.kind === "histogram") {
339
+ const { bin, ...kept } = content;
340
+ next = { ...kept, ...defined({ binwidth: typeof bin === "number" ? bin : undefined }) };
341
+ }
342
+ return { ...rest, ...defined({ position: rest.position ?? edge, content: next }) };
343
+ }
344
+ /** A reference annotation as the rule layer that replaced it, one line at its value. */
345
+ function referenceLayer(note, index) {
346
+ const kind = note.axis === "x" ? "vline" : "hline";
347
+ const at = kind === "vline" ? "xintercept" : "yintercept";
348
+ const labeled = typeof note.label === "string";
349
+ return {
350
+ id: `${kind}${index}`,
351
+ kind,
352
+ aes: labeled ? { [at]: at, label: "label" } : { [at]: at },
353
+ data: [labeled ? { [at]: note.value, label: note.label } : { [at]: note.value }],
354
+ showLegend: false,
355
+ };
356
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * The distribution along one of a plot's own axes.
3
+ *
4
+ * A marginal reads the points the geoms drew. `autoBinWidth` is Freedman-Diaconis and `BIN_CAP`
5
+ * widens it.
6
+ */
7
+ import { type Trace } from "./layers.js";
8
+ import { type AxisTransform } from "./transform.js";
9
+ /** One interval, in the measure's own units. */
10
+ export interface MarginalBin {
11
+ from: number;
12
+ to: number;
13
+ /** The interval's middle. A bar draws there against the shared scale. */
14
+ at: number;
15
+ count: number;
16
+ }
17
+ /** One density curve a marginal draws, named and colored like the geoms that made it. */
18
+ export interface MarginalCurve {
19
+ /** The level this curve came from, or empty where the readings were pooled. */
20
+ label: string;
21
+ color: string;
22
+ /** Evaluation points in the measure's units, and the density at each, peak scaled to one. */
23
+ grid: number[];
24
+ weight: number[];
25
+ }
26
+ /** What a side panel reads off the geoms along the axis it shares with them. */
27
+ export interface Marginal {
28
+ axis: "x" | "y";
29
+ kind: "histogram" | "rug" | "density";
30
+ /**
31
+ * The readings, in the measure's own units.
32
+ *
33
+ * A rug draws one tick per reading and a histogram counts them. Shared by reference between
34
+ * two panels on one axis.
35
+ */
36
+ values: number[];
37
+ /** Intervals a histogram cut. Absent on a rug. */
38
+ bins?: MarginalBin[];
39
+ /** The tallest count. The panel scales its own extent against it. */
40
+ most?: number;
41
+ /** Curves a density drew, one per level where it stratified. Absent on the other two. */
42
+ curves?: MarginalCurve[];
43
+ }
44
+ /**
45
+ * The readings along one axis, split by the color channel's level.
46
+ *
47
+ * A point takes its category's index as its weight and `colorLevels` names each group. Traces
48
+ * with no identity pool.
49
+ */
50
+ export declare function valuesByLevel(traces: Trace[], axis: "x" | "y"): MarginalCurve[];
51
+ /**
52
+ * The records' readings along one axis, as the geoms drew them.
53
+ *
54
+ * Point traces alone. A trace reduced to cells or bins has no records left to read.
55
+ */
56
+ export declare function valuesAlong(traces: Trace[], axis: "x" | "y"): number[];
57
+ /**
58
+ * The readings cut into intervals, at the width a histogram series would use.
59
+ *
60
+ * A transformed axis is cut in its own space. Even intervals over four decades fill one bin.
61
+ */
62
+ export declare function marginalBins(values: number[], bin?: "auto" | number, transform?: AxisTransform): {
63
+ bins: MarginalBin[];
64
+ most: number;
65
+ };
@@ -0,0 +1,116 @@
1
+ /**
2
+ * The distribution along one of a plot's own axes.
3
+ *
4
+ * A marginal reads the points the geoms drew. `autoBinWidth` is Freedman-Diaconis and `BIN_CAP`
5
+ * widens it.
6
+ */
7
+ import { boundsOf } from "./aggregate.js";
8
+ import { autoBinWidth, BIN_CAP } from "./layers.js";
9
+ import { kde } from "./stats.js";
10
+ import { TRANSFORMS } from "./transform.js";
11
+ /**
12
+ * The readings along one axis, split by the color channel's level.
13
+ *
14
+ * A point takes its category's index as its weight and `colorLevels` names each group. Traces
15
+ * with no identity pool.
16
+ */
17
+ export function valuesByLevel(traces, axis) {
18
+ const pooled = [];
19
+ const byLevel = new Map();
20
+ for (const trace of traces) {
21
+ if (trace.data.at !== "records" || trace.data.geom !== "point")
22
+ continue;
23
+ const levels = trace.colorLevels;
24
+ for (const point of trace.data.points) {
25
+ const value = point[axis];
26
+ if (!Number.isFinite(value))
27
+ continue;
28
+ const code = levels && point.weight != null ? Math.round(point.weight) : null;
29
+ const level = code == null ? null : levels?.find((held) => held.code === code);
30
+ if (!level) {
31
+ const held = (pooled[0] ??= { label: "", color: trace.color, values: [] });
32
+ held.values.push(value);
33
+ continue;
34
+ }
35
+ const held = byLevel.get(level.value) ?? { label: level.value, color: trace.ramp(level.code), values: [] };
36
+ held.values.push(value);
37
+ byLevel.set(level.value, held);
38
+ }
39
+ }
40
+ /* The unmatched readings stay. The panel's rug and its curves describe one set of rows. */
41
+ const groups = byLevel.size > 0 ? [...byLevel.values(), ...pooled] : pooled;
42
+ const out = [];
43
+ /*
44
+ The kernel's own defaults, which a violin overrides. The grid reaches three bandwidths past
45
+ the data and the curve comes down to nothing at each end.
46
+ */
47
+ for (const group of groups) {
48
+ const density = kde(group.values);
49
+ if (density)
50
+ out.push({ label: group.label, color: group.color, grid: density.grid, weight: density.weight });
51
+ }
52
+ /* One divisor over all of them. The tallest fills the panel and the rest keep proportion. */
53
+ let peak = 0;
54
+ for (const curve of out)
55
+ for (const value of curve.weight)
56
+ if (value > peak)
57
+ peak = value;
58
+ if (peak <= 0)
59
+ return [];
60
+ return out.map((curve) => ({ ...curve, weight: curve.weight.map((value) => value / peak) }));
61
+ }
62
+ /**
63
+ * The records' readings along one axis, as the geoms drew them.
64
+ *
65
+ * Point traces alone. A trace reduced to cells or bins has no records left to read.
66
+ */
67
+ export function valuesAlong(traces, axis) {
68
+ const out = [];
69
+ for (const trace of traces) {
70
+ if (trace.data.at !== "records" || trace.data.geom !== "point")
71
+ continue;
72
+ for (const point of trace.data.points) {
73
+ const value = point[axis];
74
+ if (Number.isFinite(value))
75
+ out.push(value);
76
+ }
77
+ }
78
+ return out;
79
+ }
80
+ /**
81
+ * The readings cut into intervals, at the width a histogram series would use.
82
+ *
83
+ * A transformed axis is cut in its own space. Even intervals over four decades fill one bin.
84
+ */
85
+ export function marginalBins(values, bin = "auto", transform = "identity") {
86
+ const { forward: cut, back, admits } = TRANSFORMS[transform];
87
+ const usable = values.filter(admits).map(cut);
88
+ if (usable.length === 0)
89
+ return { bins: [], most: 0 };
90
+ const asked = bin === "auto" ? autoBinWidth(usable) : bin;
91
+ if (!(asked > 0) || !Number.isFinite(asked))
92
+ return { bins: [], most: 0 };
93
+ const [lo] = boundsOf(usable);
94
+ const counted = (step) => {
95
+ const running = new Map();
96
+ for (const value of usable) {
97
+ const index = Math.floor((value - lo) / step);
98
+ running.set(index, (running.get(index) ?? 0) + 1);
99
+ }
100
+ return running;
101
+ };
102
+ let width = asked;
103
+ let running = counted(width);
104
+ /* An automatic width that would draw hundreds of bars is widened until it draws dozens. */
105
+ if (bin === "auto" && running.size > BIN_CAP) {
106
+ width = (asked * running.size) / BIN_CAP;
107
+ running = counted(width);
108
+ }
109
+ const bins = [...running]
110
+ .sort((a, b) => a[0] - b[0])
111
+ .map(([index, count]) => {
112
+ const from = lo + index * width;
113
+ return { from: back(from), to: back(from + width), at: back(from + width / 2), count };
114
+ });
115
+ return { bins, most: bins.reduce((high, held) => Math.max(high, held.count), 0) };
116
+ }
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Triangles for the two cube kinds drawing a surface.
3
+ *
4
+ * `surface` reduces a measure over a grid of two and triangulates it. `mesh3d` arrives as
5
+ * vertices and faces.
6
+ */
7
+ import { type Aggregate } from "./aggregate.js";
8
+ import type { PlotData } from "./plot-data.js";
9
+ /** What the GPU draws: positions, one normal each, and the triangles over them. */
10
+ export interface MeshData {
11
+ /** Interleaved x, y, z in the data's own units. */
12
+ positions: Float32Array;
13
+ /** Interleaved unit normals, one a vertex. */
14
+ normals: Float32Array;
15
+ /** Three indices a triangle. */
16
+ indices: Uint32Array;
17
+ /** Range of the third scale over the vertices, for the color ramp and the axis. */
18
+ zDomain: [number, number];
19
+ }
20
+ /**
21
+ * The most cells a relief takes across each dimension.
22
+ *
23
+ * Forty squares a side is 1,600 cells and 3,200 triangles, which draws in one call.
24
+ */
25
+ export declare const SURFACE_CELLS = 40;
26
+ /**
27
+ * Cells a table of this many rows supports.
28
+ *
29
+ * A cell with no reading is a hole and the quads either side go too. Two rows a cell.
30
+ */
31
+ export declare const cellsFor: (rowCount: number) => number;
32
+ /** The most cells a lattice takes across each dimension. */
33
+ export declare const LATTICE_CAP = 256;
34
+ /**
35
+ * The lattice under two columns, as cells across and down. Null for a scatter.
36
+ *
37
+ * One row per crossing. Cells run at the smallest step between two values.
38
+ */
39
+ export declare function latticeOf(data: PlotData, xId: string, yId: string): [number, number] | null;
40
+ /** A reduction of one measure over a grid of two, with the empty cells still holes. */
41
+ export interface Relief {
42
+ /** Cell values in row-major order, `NaN` where no row landed in the cell. */
43
+ values: Float64Array;
44
+ columns: number;
45
+ rows: number;
46
+ xDomain: [number, number];
47
+ yDomain: [number, number];
48
+ zDomain: [number, number];
49
+ }
50
+ /**
51
+ * A measure reduced over a grid of two others.
52
+ *
53
+ * The grid is regular in both dimensions. Rows on a lattice take its own cells. Cells nothing
54
+ * landed in stay holes -- interpolating them would draw terrain nobody measured.
55
+ */
56
+ export declare function relief(data: PlotData, xId: string, yId: string, zId: string, aggregate?: Aggregate, cells?: number | [number, number]): Relief | null;
57
+ /**
58
+ * A relief as triangles.
59
+ *
60
+ * Two a cell, and a cell missing any of its four corners is left out. The positions take the
61
+ * third scale in its own units.
62
+ */
63
+ export declare function meshOfRelief(grid: Relief): MeshData | null;
64
+ /**
65
+ * Vertices and faces as they arrive.
66
+ *
67
+ * The faces are triples of row numbers. A triple naming a row the table does not have is dropped.
68
+ */
69
+ export declare function meshOfRows(data: PlotData, xId: string, yId: string, zId: string, faces: [number, number, number][]): MeshData | null;