@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,41 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /**
3
+ * A number somebody picks, nudged or typed.
4
+ *
5
+ * For a count: rows to add, decimals to show, an axis bound, a threshold.
6
+ *
7
+ * The field stays typeable. Clearing it means unset.
8
+ */
9
+ import { Minus, Plus } from "lucide-react";
10
+ import { useState } from "react";
11
+ export function Stepper({ value, onChange, min = -Infinity, max = Infinity, step = 1, suffix, placeholder, width, ariaLabel, }) {
12
+ /** What is being typed. A half-typed number clamps on commit. */
13
+ const [draft, setDraft] = useState(null);
14
+ const clamp = (next) => Math.min(max, Math.max(min, next));
15
+ /** Nudging an unset field starts from the floor, or from nought. */
16
+ const from = value ?? (Number.isFinite(min) ? min : 0);
17
+ return (_jsxs("span", { className: "viz-h-control inline-flex min-w-0 items-center rounded-md border border-border bg-background", children: [_jsx("button", { type: "button", onClick: () => onChange(clamp(from - step)), disabled: value != null && value <= min, "aria-label": "Less", className: "viz-press flex h-full items-center px-2 text-muted-foreground hover:text-foreground", children: _jsx(Minus, { size: 10 }) }), _jsxs("span", { className: "flex min-w-0 items-baseline gap-0.5", children: [_jsx("input", { value: draft ?? (value == null ? "" : String(value)), onChange: (event) => setDraft(event.target.value), onBlur: () => {
18
+ const text = (draft ?? "").trim();
19
+ setDraft(null);
20
+ if (draft == null)
21
+ return;
22
+ if (text === "")
23
+ return onChange(undefined);
24
+ const typed = Number(text);
25
+ if (Number.isFinite(typed))
26
+ onChange(clamp(typed));
27
+ }, onKeyDown: (event) => {
28
+ if (event.key === "Enter")
29
+ event.currentTarget.blur();
30
+ if (event.key === "ArrowUp") {
31
+ onChange(clamp(from + step));
32
+ setDraft(null);
33
+ event.preventDefault();
34
+ }
35
+ if (event.key === "ArrowDown") {
36
+ onChange(clamp(from - step));
37
+ setDraft(null);
38
+ event.preventDefault();
39
+ }
40
+ }, inputMode: "decimal", placeholder: placeholder, "aria-label": ariaLabel ?? placeholder, style: { width: width ?? 40 }, className: "text-sm viz-num min-w-0 bg-transparent text-center font-mono outline-none" }), suffix && _jsx("span", { className: "text-2xs pr-0.5 text-muted-foreground", children: suffix })] }), _jsx("button", { type: "button", onClick: () => onChange(clamp(from + step)), disabled: value != null && value >= max, "aria-label": "More", className: "viz-press flex h-full items-center px-2 text-muted-foreground hover:text-foreground", children: _jsx(Plus, { size: 10 }) })] }));
41
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * The property column an author edits a spec through.
3
+ *
4
+ * The one part of the package that requires the UI kit. The gate is `/core`'s capability profile.
5
+ *
6
+ * Styled by Tailwind utilities over the kit's theme, the `--viz-*` status tokens, and six classes
7
+ * the host defines: `viz-control-field`, `viz-h-control`, `viz-size-control`, `viz-press`,
8
+ * `viz-num` and `viz-popover`.
9
+ */
10
+ export { PlotPanel, type PlotPanelProps } from "./PlotPanel.js";
@@ -0,0 +1,10 @@
1
+ /**
2
+ * The property column an author edits a spec through.
3
+ *
4
+ * The one part of the package that requires the UI kit. The gate is `/core`'s capability profile.
5
+ *
6
+ * Styled by Tailwind utilities over the kit's theme, the `--viz-*` status tokens, and six classes
7
+ * the host defines: `viz-control-field`, `viz-h-control`, `viz-size-control`, `viz-press`,
8
+ * `viz-num` and `viz-popover`.
9
+ */
10
+ export { PlotPanel } from "./PlotPanel.js";
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Edge adapters -- what arrives becomes a PlotData.
3
+ *
4
+ * Three entry points reach one accessor surface. Field metadata comes from the caller and
5
+ * anything unstated is inferred from the column.
6
+ */
7
+ import type { PlotData, PlotField } from "./plot-data.js";
8
+ /** A column as it arrives: a plain array, or a typed array from the wire. */
9
+ export type Arrayish = unknown[] | Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array;
10
+ /** What a caller knows about a column and the table does not. */
11
+ export type FieldOverride = Partial<Omit<PlotField, "id">>;
12
+ export interface AdapterOptions {
13
+ /** Per-column metadata, keyed by column name. */
14
+ fieldOverrides?: Record<string, FieldOverride>;
15
+ /**
16
+ * Column naming a record, for the readout title and a selection's key. Defaults to the first text column.
17
+ *
18
+ * A `PlotData` of the caller's keeps its own key unless this names another.
19
+ */
20
+ keyField?: string;
21
+ }
22
+ /** A column name that says it holds times. */
23
+ export declare const TIME_NAME: RegExp;
24
+ /** An ISO-8601 date, with an optional time and an optional offset. */
25
+ export declare const ISO: RegExp;
26
+ /**
27
+ * Columns of values, however they arrived, as the surface a figure reads.
28
+ *
29
+ * The accessors index into the column arrays. A time-named column of ISO text or epoch seconds
30
+ * is copied once into epoch milliseconds, on a calendar scale. A caller's own field metadata wins.
31
+ */
32
+ export declare function fromColumns(given: Record<string, Arrayish>, numRows: number, options?: AdapterOptions): PlotData;
33
+ /** One column of an Arrow table, as both Arrow implementations answer it. */
34
+ interface ArrowColumn extends Iterable<unknown> {
35
+ nullCount: number;
36
+ toArray(): ArrayLike<unknown>;
37
+ }
38
+ /**
39
+ * The members of an Arrow table this module reads.
40
+ *
41
+ * A row count and a schema, with `toColumns`, `getChild` or both. flechette answers the first and
42
+ * apache-arrow the second. A table stating neither fails to compile.
43
+ */
44
+ export type ArrowLike = {
45
+ numRows: number;
46
+ schema: {
47
+ fields: readonly {
48
+ name: string;
49
+ }[];
50
+ };
51
+ } & ({
52
+ toColumns(): Record<string, ArrayLike<unknown>>;
53
+ getChild?(name: string): ArrowColumn | null;
54
+ } | {
55
+ toColumns?(): Record<string, ArrayLike<unknown>>;
56
+ getChild(name: string): ArrowColumn | null;
57
+ });
58
+ /**
59
+ * An Arrow table, as the service answers with.
60
+ *
61
+ * Read structurally through the row count and the columns. A numeric column with no nulls comes
62
+ * through uncopied. A text or dictionary column decodes into an array of strings.
63
+ */
64
+ export declare function fromArrow(table: ArrowLike, options?: AdapterOptions): PlotData;
65
+ /**
66
+ * Tabular data as a consumer has it, accepted by `toPlotData`.
67
+ *
68
+ * Row objects, columns of arrays or typed arrays, an Arrow table, or a `PlotData` of the
69
+ * caller's own.
70
+ */
71
+ export type VizData = readonly object[] | Record<string, Arrayish> | ArrowLike | PlotData;
72
+ /**
73
+ * One entry point over the three adapters.
74
+ *
75
+ * An array is rows. An Arrow table is detected by its schema accessor. A `PlotData` passes
76
+ * through with its own fields, and with its own key unless `keyField` names another. Anything else
77
+ * is columns, counted off the longest one.
78
+ */
79
+ export declare function toPlotData(data: VizData, options?: AdapterOptions): PlotData;
80
+ /**
81
+ * Row objects, as a lookup or a REST payload arrives in.
82
+ *
83
+ * Pivoted to columns once at the edge. The column set is the union of the keys present.
84
+ */
85
+ export declare function fromRows(rows: readonly Record<string, unknown>[], options?: AdapterOptions): PlotData;
86
+ export {};
@@ -0,0 +1,219 @@
1
+ /**
2
+ * Edge adapters -- what arrives becomes a PlotData.
3
+ *
4
+ * Three entry points reach one accessor surface. Field metadata comes from the caller and
5
+ * anything unstated is inferred from the column.
6
+ */
7
+ const isTypedArray = (values) => !Array.isArray(values);
8
+ /** First value that is not null, for deciding a plain array's type. */
9
+ function firstPresent(values) {
10
+ const length = values.length;
11
+ for (let index = 0; index < length; index += 1) {
12
+ const value = values[index];
13
+ if (value != null)
14
+ return value;
15
+ }
16
+ return null;
17
+ }
18
+ /**
19
+ * Whether a column reads as numeric.
20
+ *
21
+ * A typed array is numeric by construction. A plain array is judged on its first present value.
22
+ */
23
+ const numericColumn = (values) => isTypedArray(values) || typeof firstPresent(values) === "number";
24
+ const asNumber = (value) => typeof value === "number" && Number.isFinite(value) ? value : null;
25
+ /** Epoch milliseconds a lab table plausibly holds. Outside this range a large number is a measure. */
26
+ const EPOCH_LOW = Date.UTC(1990, 0, 1);
27
+ const EPOCH_HIGH = Date.UTC(2100, 0, 1);
28
+ /** A column name that says it holds times. */
29
+ export const TIME_NAME = /(^|[_\s-])(time|date|datetime|ts|timestamp|created)([_\s-]|$)|_at$/i;
30
+ /** An ISO-8601 date, with an optional time and an optional offset. */
31
+ export const ISO = /^\d{4}-\d{2}-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?)?(Z|[+-]\d{2}:?\d{2})?$/;
32
+ /** ISO-8601 text as epoch milliseconds. A time with no offset reads in UTC, the axis's own zone. */
33
+ function isoMillis(text) {
34
+ const stamp = text.replace(" ", "T");
35
+ return Date.parse(ISO.exec(stamp)?.[1] ? stamp : `${stamp}Z`);
36
+ }
37
+ /**
38
+ * A time-named column as epoch milliseconds, or null where it reads as something else.
39
+ *
40
+ * Each present value is ISO-8601 text, epoch seconds or epoch milliseconds, all of one kind.
41
+ * Text parses and seconds scale up. Milliseconds pass through uncopied.
42
+ */
43
+ function epochColumn(name, column) {
44
+ if (!TIME_NAME.test(name))
45
+ return null;
46
+ const values = column;
47
+ let present = 0;
48
+ let text = 0;
49
+ let seconds = 0;
50
+ let millis = 0;
51
+ for (let index = 0; index < values.length; index += 1) {
52
+ const value = values[index];
53
+ if (value == null || Number.isNaN(value))
54
+ continue;
55
+ present += 1;
56
+ if (typeof value === "string" && ISO.test(value))
57
+ text += 1;
58
+ else if (typeof value !== "number")
59
+ return null;
60
+ else if (value >= EPOCH_LOW && value <= EPOCH_HIGH)
61
+ millis += 1;
62
+ else if (value >= EPOCH_LOW / 1000 && value <= EPOCH_HIGH / 1000)
63
+ seconds += 1;
64
+ else
65
+ return null;
66
+ }
67
+ if (present === 0)
68
+ return null;
69
+ if (millis === present)
70
+ return column;
71
+ if (text === present)
72
+ return Array.from(values, (value) => (typeof value === "string" ? isoMillis(value) : null));
73
+ if (seconds === present)
74
+ return Array.from(values, (value) => (typeof value === "number" ? value * 1000 : null));
75
+ return null;
76
+ }
77
+ /**
78
+ * Columns of values, however they arrived, as the surface a figure reads.
79
+ *
80
+ * The accessors index into the column arrays. A time-named column of ISO text or epoch seconds
81
+ * is copied once into epoch milliseconds, on a calendar scale. A caller's own field metadata wins.
82
+ */
83
+ export function fromColumns(given, numRows, options = {}) {
84
+ const { fieldOverrides: overrides = {}, keyField } = options;
85
+ const names = Object.keys(given);
86
+ const columns = { ...given };
87
+ const fields = names.map((name) => {
88
+ const override = overrides[name] ?? {};
89
+ /*
90
+ Whatever the caller stated, with the keys they left alone dropped.
91
+ `exactOptionalPropertyTypes` reads a present-and-undefined key as an absence.
92
+ */
93
+ const stated = Object.fromEntries(Object.entries(override).filter(([, value]) => value !== undefined));
94
+ const epochs = override.timeScale === false || override.numeric === false ? null : epochColumn(name, given[name]);
95
+ if (epochs)
96
+ columns[name] = epochs;
97
+ return {
98
+ id: name,
99
+ label: override.label ?? name,
100
+ numeric: override.numeric ?? numericColumn(columns[name]),
101
+ ...(epochs ? { role: "timestamp", timeScale: true } : {}),
102
+ ...stated,
103
+ };
104
+ });
105
+ const at = (field, row) => {
106
+ const column = columns[field];
107
+ return column == null ? null : (column[row] ?? null);
108
+ };
109
+ const firstText = fields.find((field) => !field.numeric)?.id;
110
+ return {
111
+ fields,
112
+ rowCount: numRows,
113
+ number: (row, field) => asNumber(at(field, row)),
114
+ text: (row, field) => {
115
+ const value = at(field, row);
116
+ return value == null ? null : String(value);
117
+ },
118
+ // These tables have their own rows and the position is the index.
119
+ rowIndex: (row) => row,
120
+ ...((keyField ?? firstText) ? { keyField: keyField ?? firstText } : {}),
121
+ };
122
+ }
123
+ /**
124
+ * One column read whole off its Arrow vector.
125
+ *
126
+ * A vector holding nulls reads through its iterator, which answers null at each null slot.
127
+ * `toArray` answers the raw buffer value there.
128
+ */
129
+ const arrayOf = (column) => (column.nullCount > 0 ? Array.from(column) : column.toArray());
130
+ /**
131
+ * An Arrow table, as the service answers with.
132
+ *
133
+ * Read structurally through the row count and the columns. A numeric column with no nulls comes
134
+ * through uncopied. A text or dictionary column decodes into an array of strings.
135
+ */
136
+ export function fromArrow(table, options = {}) {
137
+ const arrow = table;
138
+ const numRows = arrow?.numRows;
139
+ if (typeof numRows === "number" && typeof arrow?.toColumns === "function") {
140
+ return fromColumns(arrow.toColumns(), numRows, options);
141
+ }
142
+ if (typeof numRows === "number" && typeof arrow?.getChild === "function" && arrow.schema) {
143
+ const columns = {};
144
+ for (const { name } of arrow.schema.fields) {
145
+ const column = arrow.getChild(name);
146
+ if (column)
147
+ columns[name] = arrayOf(column);
148
+ }
149
+ return fromColumns(columns, numRows, options);
150
+ }
151
+ throw new Error("fromArrow: the value carries no row count and no column accessor");
152
+ }
153
+ /** Whether a value quacks like an Arrow table, meaning a schema and a row count. */
154
+ const looksArrow = (data) => "schema" in data && typeof data.numRows === "number";
155
+ /** Whether a value is already the surface a figure reads. */
156
+ const isPlotData = (data) => typeof data.number === "function" && Array.isArray(data.fields);
157
+ /**
158
+ * A caller's table under another key column.
159
+ *
160
+ * Each accessor delegates, which keeps a class instance's prototype methods and its private state.
161
+ */
162
+ function keyedBy(data, keyField) {
163
+ const { numbers, texts } = data;
164
+ return {
165
+ fields: data.fields,
166
+ rowCount: data.rowCount,
167
+ keyField,
168
+ number: (row, field) => data.number(row, field),
169
+ text: (row, field) => data.text(row, field),
170
+ rowIndex: (row) => data.rowIndex(row),
171
+ ...(numbers ? { numbers: (field) => numbers.call(data, field) } : {}),
172
+ ...(texts ? { texts: (field) => texts.call(data, field) } : {}),
173
+ };
174
+ }
175
+ /**
176
+ * One entry point over the three adapters.
177
+ *
178
+ * An array is rows. An Arrow table is detected by its schema accessor. A `PlotData` passes
179
+ * through with its own fields, and with its own key unless `keyField` names another. Anything else
180
+ * is columns, counted off the longest one.
181
+ */
182
+ export function toPlotData(data, options = {}) {
183
+ if (data == null) {
184
+ throw new Error(`toPlotData: takes rows, columns or an Arrow table, and got ${data}. Pass [] while the data loads.`);
185
+ }
186
+ if (Array.isArray(data))
187
+ return fromRows(data, options);
188
+ if (isPlotData(data)) {
189
+ const { keyField } = options;
190
+ return keyField && keyField !== data.keyField ? keyedBy(data, keyField) : data;
191
+ }
192
+ if (looksArrow(data))
193
+ return fromArrow(data, options);
194
+ const columns = data;
195
+ const numRows = Object.values(columns).reduce((most, column) => Math.max(most, column.length), 0);
196
+ return fromColumns(columns, numRows, options);
197
+ }
198
+ /**
199
+ * Row objects, as a lookup or a REST payload arrives in.
200
+ *
201
+ * Pivoted to columns once at the edge. The column set is the union of the keys present.
202
+ */
203
+ export function fromRows(rows, options = {}) {
204
+ const names = [];
205
+ const seen = new Set();
206
+ for (const row of rows) {
207
+ for (const key of Object.keys(row)) {
208
+ if (seen.has(key))
209
+ continue;
210
+ seen.add(key);
211
+ names.push(key);
212
+ }
213
+ }
214
+ const columns = {};
215
+ for (const name of names) {
216
+ columns[name] = rows.map((row) => row[name] ?? null);
217
+ }
218
+ return fromColumns(columns, rows.length, options);
219
+ }
@@ -0,0 +1,74 @@
1
+ /**
2
+ * A group of records becomes one number.
3
+ *
4
+ * Bar heights, heat cell values and voxel measures share one accumulator. `reduceBucket` returns
5
+ * the aggregate asked for.
6
+ */
7
+ export type Aggregate = "count" | "mean" | "median" | "min" | "max" | "sum" | "p95";
8
+ /**
9
+ * Aggregates that read the values themselves.
10
+ *
11
+ * A reduction of 250k rows into 100 groups keeps nothing under `mean` and 100 arrays under
12
+ * `median`.
13
+ */
14
+ export declare const NEEDS_VALUES: Aggregate[];
15
+ export interface Bucket {
16
+ n: number;
17
+ sum: number;
18
+ /** Sum of squares. A deviation comes off the running totals with no array kept. */
19
+ sumSq: number;
20
+ min: number;
21
+ max: number;
22
+ }
23
+ /** Turns an accumulated bucket into the aggregate asked for. A group and a cell share it. */
24
+ export declare function reduceBucket(bucket: Bucket, aggregate: Aggregate, collected?: number[]): number;
25
+ /**
26
+ * Deviation and standard error off a bucket's running totals.
27
+ *
28
+ * One reading reports zero for both. A zero-width interval would read as a tight one.
29
+ */
30
+ export declare function spreadOf(bucket: Bucket): {
31
+ sd: number;
32
+ sem: number;
33
+ };
34
+ /**
35
+ * Lowest and highest of a list, walked.
36
+ *
37
+ * `Math.min(...values)` passes one argument per element and 250k of them overflow the stack.
38
+ */
39
+ export declare function boundsOf(values: number[]): [number, number];
40
+ /** Linear interpolation between order statistics. Matches the convention `boxStats` uses. */
41
+ export declare function quantile(collected: number[] | undefined, q: number): number;
42
+ /** One category and the number standing for it. A bar chart plots these. */
43
+ export interface Group {
44
+ key: string;
45
+ n: number;
46
+ value: number;
47
+ /** The group's own spread, for a geom drawing an interval around the reduction. */
48
+ sd: number;
49
+ sem: number;
50
+ /** The readings themselves, where the caller asked to keep them. */
51
+ values?: number[];
52
+ }
53
+ /**
54
+ * A series read as a running total, in the order given.
55
+ *
56
+ * Each datum adds the ones before it, `n` included. The error, the floor and the readings drop.
57
+ */
58
+ export declare function accumulate<T extends {
59
+ value: number;
60
+ error?: number;
61
+ floor?: number;
62
+ values?: number[];
63
+ n?: number;
64
+ }>(data: readonly T[]): T[];
65
+ /**
66
+ * Readings as a running total along the horizontal axis.
67
+ *
68
+ * The record form of `accumulate`. Points arrive in table order and a total means nothing in it, so
69
+ * they are put in x order first: the curve then answers how much had arrived by a date.
70
+ */
71
+ export declare function accumulateAlong<T extends {
72
+ x: number;
73
+ y: number;
74
+ }>(points: readonly T[]): T[];
@@ -0,0 +1,102 @@
1
+ /**
2
+ * A group of records becomes one number.
3
+ *
4
+ * Bar heights, heat cell values and voxel measures share one accumulator. `reduceBucket` returns
5
+ * the aggregate asked for.
6
+ */
7
+ /**
8
+ * Aggregates that read the values themselves.
9
+ *
10
+ * A reduction of 250k rows into 100 groups keeps nothing under `mean` and 100 arrays under
11
+ * `median`.
12
+ */
13
+ export const NEEDS_VALUES = ["median", "p95"];
14
+ /** Turns an accumulated bucket into the aggregate asked for. A group and a cell share it. */
15
+ export function reduceBucket(bucket, aggregate, collected) {
16
+ switch (aggregate) {
17
+ case "count":
18
+ return bucket.n;
19
+ case "sum":
20
+ return bucket.sum;
21
+ case "min":
22
+ return bucket.min;
23
+ case "max":
24
+ return bucket.max;
25
+ case "median":
26
+ return quantile(collected, 0.5);
27
+ case "p95":
28
+ return quantile(collected, 0.95);
29
+ default:
30
+ return bucket.sum / bucket.n;
31
+ }
32
+ }
33
+ /**
34
+ * Deviation and standard error off a bucket's running totals.
35
+ *
36
+ * One reading reports zero for both. A zero-width interval would read as a tight one.
37
+ */
38
+ export function spreadOf(bucket) {
39
+ if (bucket.n < 2)
40
+ return { sd: 0, sem: 0 };
41
+ const mean = bucket.sum / bucket.n;
42
+ // Clamped at zero. The one-pass form can land a hair below where the spread is tiny.
43
+ const variance = Math.max(0, (bucket.sumSq - bucket.n * mean * mean) / (bucket.n - 1));
44
+ const sd = Math.sqrt(variance);
45
+ return { sd, sem: sd / Math.sqrt(bucket.n) };
46
+ }
47
+ /**
48
+ * Lowest and highest of a list, walked.
49
+ *
50
+ * `Math.min(...values)` passes one argument per element and 250k of them overflow the stack.
51
+ */
52
+ export function boundsOf(values) {
53
+ let lo = Infinity;
54
+ let hi = -Infinity;
55
+ for (const value of values) {
56
+ if (value < lo)
57
+ lo = value;
58
+ if (value > hi)
59
+ hi = value;
60
+ }
61
+ return [lo, hi];
62
+ }
63
+ /** Linear interpolation between order statistics. Matches the convention `boxStats` uses. */
64
+ export function quantile(collected, q) {
65
+ const values = [...(collected ?? [])].sort((a, b) => a - b);
66
+ if (values.length === 0)
67
+ return 0;
68
+ const pos = q * (values.length - 1);
69
+ const base = Math.floor(pos);
70
+ const next = values[Math.min(values.length - 1, base + 1)];
71
+ return values[base] + (pos - base) * (next - values[base]);
72
+ }
73
+ /**
74
+ * A series read as a running total, in the order given.
75
+ *
76
+ * Each datum adds the ones before it, `n` included. The error, the floor and the readings drop.
77
+ */
78
+ export function accumulate(data) {
79
+ let running = 0;
80
+ let rows = 0;
81
+ return data.map((datum) => {
82
+ running += datum.value;
83
+ rows += datum.n ?? 0;
84
+ const { error: _error, floor: _floor, values: _values, ...rest } = datum;
85
+ return { ...rest, value: running, ...(datum.n == null ? {} : { n: rows }) };
86
+ });
87
+ }
88
+ /**
89
+ * Readings as a running total along the horizontal axis.
90
+ *
91
+ * The record form of `accumulate`. Points arrive in table order and a total means nothing in it, so
92
+ * they are put in x order first: the curve then answers how much had arrived by a date.
93
+ */
94
+ export function accumulateAlong(points) {
95
+ let running = 0;
96
+ return [...points]
97
+ .sort((a, b) => a.x - b.x)
98
+ .map((point) => {
99
+ running += point.y;
100
+ return { ...point, y: running };
101
+ });
102
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * What a reader wrote on the chart, and what the chart offers to write.
3
+ *
4
+ * Annotations are saved with the spec. Accepting a suggestion appends it and stops the offer.
5
+ */
6
+ import type { Anchor, Annotation } from "./spec.js";
7
+ /** Fields with enough to seed an annotation without anybody typing. */
8
+ export interface Annotatable {
9
+ id: string;
10
+ label: string;
11
+ unit?: string;
12
+ /** Which way counts as better. Absent where the source declined to guess. */
13
+ direction?: "higher" | "lower";
14
+ }
15
+ /** Same annotation, ignoring the label somebody may have edited. */
16
+ export declare function sameAnnotation(a: Annotation, b: Annotation): boolean;
17
+ /**
18
+ * Annotations the plot could write for itself, minus the ones already on it.
19
+ *
20
+ * `axis-end` comes from a measure's direction. The author edits the text.
21
+ */
22
+ export declare function suggest(bound: {
23
+ axis: "x" | "y";
24
+ field: Annotatable | undefined;
25
+ }[], existing: readonly Annotation[]): Annotation[];
26
+ /** A short description for a list row. An annotation has no name of its own. */
27
+ export declare function describeAnnotation(annotation: Annotation): string;
28
+ /**
29
+ * A note moved on the chart.
30
+ *
31
+ * The anchor is in data coordinates and a dragged note stays on its point through a zoom. A
32
+ * leader that was there stays there.
33
+ */
34
+ export declare function moveNote(annotations: Annotation[], index: number, at: Anchor, toward?: Anchor): Annotation[];
@@ -0,0 +1,71 @@
1
+ /**
2
+ * What a reader wrote on the chart, and what the chart offers to write.
3
+ *
4
+ * Annotations are saved with the spec. Accepting a suggestion appends it and stops the offer.
5
+ */
6
+ /** Same annotation, ignoring the label somebody may have edited. */
7
+ export function sameAnnotation(a, b) {
8
+ if (a.kind !== b.kind)
9
+ return false;
10
+ if (a.kind === "axis-end" && b.kind === "axis-end")
11
+ return a.axis === b.axis && a.end === b.end;
12
+ if (a.kind === "note" && b.kind === "note")
13
+ return a.text === b.text && a.at.x === b.at.x && a.at.y === b.at.y;
14
+ return false;
15
+ }
16
+ /**
17
+ * Annotations the plot could write for itself, minus the ones already on it.
18
+ *
19
+ * `axis-end` comes from a measure's direction. The author edits the text.
20
+ */
21
+ export function suggest(bound, existing) {
22
+ const out = [];
23
+ for (const { axis, field } of bound) {
24
+ if (!field)
25
+ continue;
26
+ if (field.direction) {
27
+ const betterEnd = field.direction === "lower" ? "low" : "high";
28
+ out.push({ kind: "axis-end", axis, end: betterEnd, text: "better" });
29
+ out.push({ kind: "axis-end", axis, end: betterEnd === "low" ? "high" : "low", text: "worse" });
30
+ }
31
+ }
32
+ /*
33
+ Deduplicated against the plot, and against itself where two candidates agree whole. Two series
34
+ may bind one measure. Two measures disagreeing about an end offer both captions.
35
+ */
36
+ const kept = [];
37
+ for (const candidate of out) {
38
+ if (existing.some((held) => sameAnnotation(held, candidate)))
39
+ continue;
40
+ if (kept.some((held) => JSON.stringify(held) === JSON.stringify(candidate)))
41
+ continue;
42
+ kept.push(candidate);
43
+ }
44
+ return kept;
45
+ }
46
+ /** A short description for a list row. An annotation has no name of its own. */
47
+ export function describeAnnotation(annotation) {
48
+ switch (annotation.kind) {
49
+ case "note":
50
+ return annotation.text || "Note";
51
+ case "axis-end":
52
+ return `${annotation.axis.toUpperCase()} ${annotation.end}: ${annotation.text}`;
53
+ }
54
+ }
55
+ /**
56
+ * A note moved on the chart.
57
+ *
58
+ * The anchor is in data coordinates and a dragged note stays on its point through a zoom. A
59
+ * leader that was there stays there.
60
+ */
61
+ export function moveNote(annotations, index, at, toward) {
62
+ return annotations.map((annotation, position) => {
63
+ if (position !== index || annotation.kind !== "note")
64
+ return annotation;
65
+ return {
66
+ ...annotation,
67
+ at,
68
+ leader: annotation.leader && toward ? { ...annotation.leader, toward } : annotation.leader,
69
+ };
70
+ });
71
+ }