@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,168 @@
1
+ /**
2
+ * Binning a cloud into cells, on either tiling.
3
+ *
4
+ * The geom above 50,000 records. A hexagon has six neighbors at one distance. A square
5
+ * cross-references against the two axes.
6
+ */
7
+ import { latticeEdges } from "./binning.js";
8
+ const THIRD_PI = Math.PI / 3;
9
+ const ANGLES = [0, THIRD_PI, 2 * THIRD_PI, 3 * THIRD_PI, 4 * THIRD_PI, 5 * THIRD_PI];
10
+ /** Pixels across one hexagon. `rectbin` takes this width for a cell of about one area. */
11
+ const HEX_WIDTH = 2 * Math.sin(THIRD_PI);
12
+ /**
13
+ * A pixel lattice off stated bin parameters.
14
+ *
15
+ * Projecting the first two edges gives the size and anchor in pixels. A vertical scale anchors at
16
+ * the smaller pixel.
17
+ */
18
+ export function latticeFor(xRange, yRange, scales, options) {
19
+ const across = latticeEdges(xRange, options, 0);
20
+ const down = latticeEdges(yRange, options, 1);
21
+ if (!across || !down)
22
+ return null;
23
+ const width = Math.abs(scales.x(across.breaks[1]) - scales.x(across.breaks[0]));
24
+ const height = Math.abs(scales.y(down.breaks[1]) - scales.y(down.breaks[0]));
25
+ if (!(width > 0) || !(height > 0))
26
+ return null;
27
+ const top = Math.min(scales.y(down.breaks[0]), scales.y(down.breaks.at(-1)));
28
+ return {
29
+ width,
30
+ height,
31
+ origin: [scales.x(across.breaks[0]), top],
32
+ /* The high end of a cell. `stat_bin_2d` closes the same end by default. */
33
+ closed: options.closed ?? "right",
34
+ cells: [across.breaks.length - 1, down.breaks.length - 1],
35
+ ...(options.drop === false ? { whole: true } : {}),
36
+ };
37
+ }
38
+ /** The radius a hexagon takes to sit on a lattice of this width. */
39
+ export const hexRadiusFor = (lattice) => lattice.width / HEX_WIDTH;
40
+ /** The hexagon outline, centered on the origin. */
41
+ export function hexPath(radius) {
42
+ return (ANGLES.map((a) => {
43
+ const x = Math.sin(a) * radius;
44
+ const y = -Math.cos(a) * radius;
45
+ return `${x.toFixed(2)},${y.toFixed(2)}`;
46
+ }).join("L") + "Z").replace(/^/, "M");
47
+ }
48
+ /**
49
+ * Bins points already projected into pixel space.
50
+ *
51
+ * Binning after projection keeps the hexagons regular on screen. Binning in data space would
52
+ * skew them on a log axis.
53
+ */
54
+ export function hexbin(points, radius, origin = [0, 0]) {
55
+ const dx = radius * 2 * Math.sin(THIRD_PI);
56
+ const dy = radius * 1.5;
57
+ const bins = new Map();
58
+ for (const stated of points) {
59
+ /* The lattice anchors on the origin. A stated boundary lands where the author put it. */
60
+ const point = { x: stated.x - origin[0], y: stated.y - origin[1], weight: stated.weight };
61
+ const py = point.y / dy;
62
+ let pj = Math.round(py);
63
+ const px = point.x / dx - (pj & 1 ? 0.5 : 0);
64
+ let pi = Math.round(px);
65
+ const py1 = py - pj;
66
+ if (Math.abs(py1) * 3 > 1) {
67
+ const px1 = px - pi;
68
+ const pi2 = pi + (px < pi ? -1 : 1) / 2;
69
+ const pj2 = pj + (py < pj ? -1 : 1);
70
+ const px2 = px - pi2;
71
+ const py2 = py - pj2;
72
+ if (px1 * px1 + py1 * py1 > px2 * px2 + py2 * py2) {
73
+ pi = pi2 + (pj & 1 ? 1 : -1) / 2;
74
+ pj = pj2;
75
+ }
76
+ }
77
+ const key = `${pi},${pj}`;
78
+ const existing = bins.get(key);
79
+ if (existing) {
80
+ existing.count += 1;
81
+ if (point.weight != null) {
82
+ existing.sum += point.weight;
83
+ existing.weighted += 1;
84
+ }
85
+ }
86
+ else {
87
+ bins.set(key, {
88
+ key,
89
+ cx: (pi + (pj & 1 ? 0.5 : 0)) * dx + origin[0],
90
+ cy: pj * dy + origin[1],
91
+ count: 1,
92
+ sum: point.weight ?? 0,
93
+ weighted: point.weight != null ? 1 : 0,
94
+ });
95
+ }
96
+ }
97
+ return [...bins.values()].map(({ sum, weighted, ...bin }) => ({
98
+ ...bin,
99
+ mean: weighted > 0 ? sum / weighted : undefined,
100
+ }));
101
+ }
102
+ /**
103
+ * The cell outline, centered on the origin.
104
+ *
105
+ * Square where the height goes unstated. A stated width per axis makes a cell one interval of x
106
+ * by one of y.
107
+ */
108
+ export function rectPath(size, height = size) {
109
+ const halfWide = (size / 2).toFixed(2);
110
+ const halfTall = (height / 2).toFixed(2);
111
+ const across = size.toFixed(2);
112
+ const down = height.toFixed(2);
113
+ return `M-${halfWide},-${halfTall}h${across}v${down}h-${across}Z`;
114
+ }
115
+ /**
116
+ * Bins points already projected into pixel space, onto a square lattice.
117
+ *
118
+ * `Math.floor` over `Math.round`. A cell covers one half-open interval on each axis and the
119
+ * center falls in its middle.
120
+ */
121
+ export function rectbin(points, size) {
122
+ const lattice = typeof size === "number" ? { width: size } : size;
123
+ const across = lattice.width || 1;
124
+ const down = lattice.height || across;
125
+ const [ox, oy] = lattice.origin ?? [0, 0];
126
+ /* The closed end decides which cell a reading on an edge joins. */
127
+ const cellOf = (value, edge, count) => {
128
+ const cell = lattice.closed === "right" ? Math.ceil(value / edge) - 1 : Math.floor(value / edge);
129
+ // Clamped to the lattice. A reading on the outer edge joins the cell beside it.
130
+ return count == null ? cell : Math.min(Math.max(cell, 0), count - 1);
131
+ };
132
+ const bins = new Map();
133
+ const cell = (column, row) => ({
134
+ key: `${column},${row}`,
135
+ cx: (column + 0.5) * across + ox,
136
+ cy: (row + 0.5) * down + oy,
137
+ count: 0,
138
+ sum: 0,
139
+ weighted: 0,
140
+ });
141
+ if (lattice.whole && lattice.cells) {
142
+ for (let column = 0; column < lattice.cells[0]; column += 1) {
143
+ for (let row = 0; row < lattice.cells[1]; row += 1) {
144
+ const held = cell(column, row);
145
+ bins.set(held.key, held);
146
+ }
147
+ }
148
+ }
149
+ for (const point of points) {
150
+ const column = cellOf(point.x - ox, across, lattice.cells?.[0]);
151
+ const row = cellOf(point.y - oy, down, lattice.cells?.[1]);
152
+ const key = `${column},${row}`;
153
+ let held = bins.get(key);
154
+ if (!held) {
155
+ held = cell(column, row);
156
+ bins.set(key, held);
157
+ }
158
+ held.count += 1;
159
+ if (point.weight != null) {
160
+ held.sum += point.weight;
161
+ held.weighted += 1;
162
+ }
163
+ }
164
+ return [...bins.values()].map(({ sum, weighted, ...bin }) => ({
165
+ ...bin,
166
+ mean: weighted > 0 ? sum / weighted : undefined,
167
+ }));
168
+ }
@@ -0,0 +1,59 @@
1
+ /**
2
+ * One rectangle per node, in two arrangements.
3
+ *
4
+ * Rectangles come out in fractions of the drawing area. The tiling is the squarified algorithm of
5
+ * Bruls, Huizing and van Wijk.
6
+ */
7
+ /** A row a hierarchy is built from. `parent` absent, or naming no row, makes it a root. */
8
+ export interface HierarchyRow {
9
+ label: string;
10
+ value: number;
11
+ parent?: string;
12
+ }
13
+ /** One node's rectangle, in fractions of the drawing area from its top left corner. */
14
+ export interface HierarchyCell {
15
+ label: string;
16
+ /** The cell this one falls inside. Absent on a root. */
17
+ parent?: string;
18
+ /** Sum of the children drawn under it, or the row's own value where it has none. */
19
+ value: number;
20
+ /** Levels down from a root. A component inks one level or writes its labels alone. */
21
+ depth: number;
22
+ x0: number;
23
+ y0: number;
24
+ x1: number;
25
+ y1: number;
26
+ }
27
+ /**
28
+ * Children one parent draws.
29
+ *
30
+ * Past two dozen the smallest cells are slivers with labels that do not fit. A parent keeps its
31
+ * largest children and the count reaches the footer.
32
+ */
33
+ export declare const HIERARCHY_CAP = 24;
34
+ /** Cells to draw, and what the cap turned down. The footer owes a clause for each. */
35
+ export interface HierarchyLayout {
36
+ /** A parent stands before its children. Drawing in order paints a child over its parent. */
37
+ cells: HierarchyCell[];
38
+ /** Nodes the cap refused, counting the descendants that went with them. */
39
+ dropped: number;
40
+ }
41
+ /**
42
+ * Tiles the hierarchy in two dimensions. A cell's area stands for its value.
43
+ *
44
+ * A parent's cell contains its children. Chart-design rule 11 names the reading.
45
+ */
46
+ export declare function treemapLayout(rows: readonly HierarchyRow[], cap?: number): HierarchyLayout;
47
+ /**
48
+ * A row per level, with a cell's width standing for its value.
49
+ *
50
+ * Within one row the reading is a length on a shared baseline. The order is the row order.
51
+ */
52
+ export declare function icicleLayout(rows: readonly HierarchyRow[], cap?: number): HierarchyLayout;
53
+ /**
54
+ * The group each cell draws in, keyed by label.
55
+ *
56
+ * A forest groups by its roots. A single root groups by its children, and the root itself takes no
57
+ * group.
58
+ */
59
+ export declare function groupsOf(cells: readonly HierarchyCell[]): Map<string, string>;
@@ -0,0 +1,248 @@
1
+ /**
2
+ * One rectangle per node, in two arrangements.
3
+ *
4
+ * Rectangles come out in fractions of the drawing area. The tiling is the squarified algorithm of
5
+ * Bruls, Huizing and van Wijk.
6
+ */
7
+ import { boundsOf } from "./aggregate.js";
8
+ /**
9
+ * Children one parent draws.
10
+ *
11
+ * Past two dozen the smallest cells are slivers with labels that do not fit. A parent keeps its
12
+ * largest children and the count reaches the footer.
13
+ */
14
+ export const HIERARCHY_CAP = 24;
15
+ const WHOLE = { x0: 0, y0: 0, x1: 1, y1: 1 };
16
+ /** Totals a node set's values. */
17
+ const totalOf = (nodes) => nodes.reduce((sum, node) => sum + node.value, 0);
18
+ /** Counts what falls under a node, at any depth. */
19
+ const descendants = (node) => node.children.reduce((sum, child) => sum + 1 + descendants(child), 0);
20
+ /**
21
+ * Resolves the rows into a forest and sums each node off what it kept.
22
+ *
23
+ * A row naming a missing label becomes a root, as does one inside a cycle.
24
+ */
25
+ function forestOf(rows, cap) {
26
+ const nodes = new Map();
27
+ for (const row of rows) {
28
+ if (nodes.has(row.label))
29
+ continue;
30
+ nodes.set(row.label, { label: row.label, value: row.value, depth: 0, children: [] });
31
+ }
32
+ const parentOf = new Map();
33
+ for (const row of rows) {
34
+ if (parentOf.has(row.label))
35
+ continue;
36
+ if (row.parent != null && row.parent !== row.label && nodes.has(row.parent))
37
+ parentOf.set(row.label, row.parent);
38
+ }
39
+ const byParent = new Map();
40
+ for (const [label, parent] of parentOf) {
41
+ const held = byParent.get(parent) ?? [];
42
+ held.push(nodes.get(label));
43
+ byParent.set(parent, held);
44
+ }
45
+ /* Descended from the rows naming no parent. The walk fails to reach a cycle. */
46
+ const roots = [...nodes.values()].filter((node) => !parentOf.has(node.label));
47
+ const reached = new Set(roots.map((node) => node.label));
48
+ const queue = [...roots];
49
+ while (queue.length > 0) {
50
+ const node = queue.shift();
51
+ for (const child of byParent.get(node.label) ?? []) {
52
+ if (reached.has(child.label))
53
+ continue;
54
+ reached.add(child.label);
55
+ child.parent = node.label;
56
+ child.depth = node.depth + 1;
57
+ node.children.push(child);
58
+ queue.push(child);
59
+ }
60
+ }
61
+ for (const node of nodes.values()) {
62
+ if (reached.has(node.label))
63
+ continue;
64
+ reached.add(node.label);
65
+ roots.push(node);
66
+ }
67
+ let dropped = 0;
68
+ /** Takes the largest `cap` of a set, counting what went and all beneath it. */
69
+ const keep = (set) => {
70
+ const drawn = set.filter((node) => node.value > 0 || node.children.length > 0);
71
+ if (drawn.length <= cap)
72
+ return drawn;
73
+ const ranked = [...drawn].sort((one, other) => other.value - one.value);
74
+ for (const node of ranked.slice(cap))
75
+ dropped += 1 + descendants(node);
76
+ const held = new Set(ranked.slice(0, cap));
77
+ return drawn.filter((node) => held.has(node));
78
+ };
79
+ /* Post-order. A parent's value is the sum of the children the cap left it. */
80
+ const settle = (node) => {
81
+ node.children = keep(node.children);
82
+ for (const child of node.children)
83
+ settle(child);
84
+ if (node.children.length > 0)
85
+ node.value = totalOf(node.children);
86
+ };
87
+ const top = keep(roots);
88
+ for (const node of top)
89
+ settle(node);
90
+ return { roots: top.filter((node) => node.value > 0), dropped };
91
+ }
92
+ /**
93
+ * Reports the worst aspect ratio in a strip laid along a side of the given length.
94
+ *
95
+ * The measure the squarified tiling minimizes, taken over the strip's largest and smallest cell.
96
+ */
97
+ function worst(areas, side) {
98
+ let sum = 0;
99
+ let most = 0;
100
+ let least = Infinity;
101
+ for (const area of areas) {
102
+ sum += area;
103
+ if (area > most)
104
+ most = area;
105
+ if (area < least)
106
+ least = area;
107
+ }
108
+ if (!(sum > 0) || !(side > 0) || !(least > 0))
109
+ return Infinity;
110
+ const strip = sum * sum;
111
+ const along = side * side;
112
+ return Math.max((along * most) / strip, strip / (along * least));
113
+ }
114
+ /**
115
+ * Lays one strip of cells across a box and returns what is left.
116
+ *
117
+ * The strip runs along the shorter side. The last cell takes the box's own edge.
118
+ */
119
+ function layStrip(strip, areas, box, across, last, place) {
120
+ const total = areas.reduce((sum, area) => sum + area, 0);
121
+ if (across) {
122
+ const thick = last ? box.y1 - box.y0 : total / (box.x1 - box.x0);
123
+ let from = box.x0;
124
+ strip.forEach((node, index) => {
125
+ const to = index === strip.length - 1 ? box.x1 : from + areas[index] / thick;
126
+ place(node, { x0: from, y0: box.y0, x1: to, y1: box.y0 + thick });
127
+ from = to;
128
+ });
129
+ return { x0: box.x0, y0: box.y0 + thick, x1: box.x1, y1: box.y1 };
130
+ }
131
+ const thick = last ? box.x1 - box.x0 : total / (box.y1 - box.y0);
132
+ let from = box.y0;
133
+ strip.forEach((node, index) => {
134
+ const to = index === strip.length - 1 ? box.y1 : from + areas[index] / thick;
135
+ place(node, { x0: box.x0, y0: from, x1: box.x0 + thick, y1: to });
136
+ from = to;
137
+ });
138
+ return { x0: box.x0 + thick, y0: box.y0, x1: box.x1, y1: box.y1 };
139
+ }
140
+ /** Tiles one level of children into a box, largest first, one strip at a time. */
141
+ function squarify(children, box, place) {
142
+ let waiting = [...children].sort((one, other) => other.value - one.value);
143
+ let left = box;
144
+ while (waiting.length > 0) {
145
+ const width = left.x1 - left.x0;
146
+ const height = left.y1 - left.y0;
147
+ const total = totalOf(waiting);
148
+ if (!(width > 0) || !(height > 0) || !(total > 0))
149
+ return;
150
+ /* Rescaled per strip against what is left. The rounding of one strip stops there. */
151
+ const scale = (width * height) / total;
152
+ const areas = waiting.map((node) => node.value * scale);
153
+ const side = Math.min(width, height);
154
+ let count = 1;
155
+ while (count < areas.length && worst(areas.slice(0, count + 1), side) <= worst(areas.slice(0, count), side)) {
156
+ count += 1;
157
+ }
158
+ const strip = waiting.slice(0, count);
159
+ waiting = waiting.slice(count);
160
+ left = layStrip(strip, areas.slice(0, count), left, width <= height, waiting.length === 0, place);
161
+ }
162
+ }
163
+ /**
164
+ * Tiles the hierarchy in two dimensions. A cell's area stands for its value.
165
+ *
166
+ * A parent's cell contains its children. Chart-design rule 11 names the reading.
167
+ */
168
+ export function treemapLayout(rows, cap = HIERARCHY_CAP) {
169
+ const { roots, dropped } = forestOf(rows, cap);
170
+ const cells = [];
171
+ const tile = (nodes, box) => {
172
+ squarify(nodes, box, (node, rect) => {
173
+ cells.push({
174
+ label: node.label,
175
+ ...(node.parent == null ? {} : { parent: node.parent }),
176
+ value: node.value,
177
+ depth: node.depth,
178
+ ...rect,
179
+ });
180
+ if (node.children.length > 0)
181
+ tile(node.children, rect);
182
+ });
183
+ };
184
+ tile(roots, WHOLE);
185
+ return { cells, dropped };
186
+ }
187
+ /** Counts the levels a forest runs to. An icicle draws one row per level. */
188
+ const levelsOf = (nodes) => nodes.length === 0 ? 0 : 1 + boundsOf(nodes.map((node) => levelsOf(node.children)))[1];
189
+ /**
190
+ * A row per level, with a cell's width standing for its value.
191
+ *
192
+ * Within one row the reading is a length on a shared baseline. The order is the row order.
193
+ */
194
+ export function icicleLayout(rows, cap = HIERARCHY_CAP) {
195
+ const { roots, dropped } = forestOf(rows, cap);
196
+ const cells = [];
197
+ const levels = levelsOf(roots);
198
+ if (levels === 0)
199
+ return { cells, dropped };
200
+ const band = 1 / levels;
201
+ const spread = (nodes, x0, x1) => {
202
+ const total = totalOf(nodes);
203
+ if (!(total > 0))
204
+ return;
205
+ const span = x1 - x0;
206
+ let from = x0;
207
+ nodes.forEach((node, index) => {
208
+ const to = index === nodes.length - 1 ? x1 : from + (node.value / total) * span;
209
+ cells.push({
210
+ label: node.label,
211
+ ...(node.parent == null ? {} : { parent: node.parent }),
212
+ value: node.value,
213
+ depth: node.depth,
214
+ x0: from,
215
+ y0: node.depth * band,
216
+ x1: to,
217
+ y1: (node.depth + 1) * band,
218
+ });
219
+ if (node.children.length > 0)
220
+ spread(node.children, from, to);
221
+ from = to;
222
+ });
223
+ };
224
+ spread(roots, 0, 1);
225
+ return { cells, dropped };
226
+ }
227
+ /**
228
+ * The group each cell draws in, keyed by label.
229
+ *
230
+ * A forest groups by its roots. A single root groups by its children, and the root itself takes no
231
+ * group.
232
+ */
233
+ export function groupsOf(cells) {
234
+ const parentOf = new Map(cells.map((cell) => [cell.label, cell.parent]));
235
+ const depthOf = new Map(cells.map((cell) => [cell.label, cell.depth]));
236
+ const level = cells.filter((cell) => cell.depth === 0).length === 1 ? 1 : 0;
237
+ const groups = new Map();
238
+ for (const cell of cells) {
239
+ let at = cell.label;
240
+ /* Bounded by the cell count. A payload built elsewhere could carry a loop. */
241
+ for (let step = 0; at != null && (depthOf.get(at) ?? 0) > level && step < cells.length; step += 1) {
242
+ at = parentOf.get(at);
243
+ }
244
+ if (at != null && depthOf.get(at) === level)
245
+ groups.set(cell.label, at);
246
+ }
247
+ return groups;
248
+ }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Nearest-geom lookup for hover.
3
+ *
4
+ * A scatter at 15,000 records has no listener per geom. Geoms are indexed in pixel space and the
5
+ * frame asks one question per pointer move.
6
+ */
7
+ import type { PointShape } from "./geom-types.js";
8
+ export interface PlotPoint {
9
+ /**
10
+ * Position on a value scale. Where `category` is set this is the band's index.
11
+ *
12
+ * Numeric for the fits, the hexbin lattice, the label rules and the quadtree.
13
+ */
14
+ x: number;
15
+ /** Category for this geom, where the axis runs bands. Absent reads `x`. */
16
+ category?: string;
17
+ y: number;
18
+ /** Record this geom stands for. Absent where the points have no identity. */
19
+ key?: string;
20
+ /** Value behind a size or color channel. */
21
+ weight?: number;
22
+ /** Position on a third scale, where the geom draws in a cube. */
23
+ z?: number;
24
+ /**
25
+ * Radius in pixels, where the size channel is bound.
26
+ *
27
+ * Resolved in `buildLayers`. Both renderers draw one diameter from one scale. Absent draws at
28
+ * the layer's own radius.
29
+ */
30
+ size?: number;
31
+ /**
32
+ * Glyph this geom draws, where the shape channel is bound.
33
+ *
34
+ * Resolved in `buildLayers` beside the radius. A value keeps its level across a facet. Absent
35
+ * draws the layer's own glyph.
36
+ */
37
+ shape?: PointShape;
38
+ /**
39
+ * Opacity this geom draws at, where the opacity channel is bound.
40
+ *
41
+ * Resolved in `buildLayers`. Absent draws at the layer's own opacity.
42
+ */
43
+ opacity?: number;
44
+ /**
45
+ * Offset the geom is drawn at, in the axes' own units.
46
+ *
47
+ * ggplot's `geom_jitter`. The offset moves the geom and its hit target. A hover answers with
48
+ * the recorded value.
49
+ */
50
+ dx?: number;
51
+ dy?: number;
52
+ }
53
+ /** A point projected to pixels, with the datum it came from. */
54
+ export interface Placed {
55
+ x: number;
56
+ y: number;
57
+ point: PlotPoint;
58
+ /** Position in the input. A caller reaches parallel arrays with it. */
59
+ index: number;
60
+ }
61
+ /** The one question a hover asks of the placed geoms: the closest within a radius of a pixel. */
62
+ export interface HitIndex {
63
+ find(x: number, y: number, radius?: number): Placed | undefined;
64
+ }
65
+ /** Pointer distance that still counts as landing on a geom. */
66
+ export declare const SNAP_RADIUS = 16;
67
+ export declare function placePoints(points: PlotPoint[], x: (value: number | string) => number, y: (value: number | string) => number, { transposed, step, }?: {
68
+ /** Whether the pair of scales is flipped, as the band geoms take it. */
69
+ transposed?: boolean;
70
+ /** Pixels per data unit along the band a category reads on. A jitter's offset there reads in it. */
71
+ step?: number;
72
+ }): Placed[];
73
+ export declare function buildIndex(placed: Placed[]): HitIndex;
74
+ /** The closest geom within the snap radius, or nothing where the pointer is over empty ground. */
75
+ export declare function nearest(index: HitIndex, x: number, y: number, radius?: number): Placed | null;
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Nearest-geom lookup for hover.
3
+ *
4
+ * A scatter at 15,000 records has no listener per geom. Geoms are indexed in pixel space and the
5
+ * frame asks one question per pointer move.
6
+ */
7
+ import { quadtree } from "d3-quadtree";
8
+ /** Pointer distance that still counts as landing on a geom. */
9
+ export const SNAP_RADIUS = 16;
10
+ export function placePoints(points, x, y, { transposed = false, step = 0, } = {}) {
11
+ const placed = [];
12
+ for (let index = 0; index < points.length; index += 1) {
13
+ const point = points[index];
14
+ /* The nudge a jitter asked for, added at projection. The geom and its hit target move
15
+ together and `point` keeps the reading. */
16
+ /* The reading bound to X goes up the side once the pair is turned. The band is read by name, and its offset in steps. */
17
+ const along = point.category ?? point.x + (point.dx ?? 0);
18
+ const shift = point.category == null ? 0 : (point.dx ?? 0) * step;
19
+ const up = point.y + (point.dy ?? 0);
20
+ const px = transposed ? x(up) : x(along) + shift;
21
+ const py = transposed ? y(along) + shift : y(up);
22
+ // A projected NaN poisons the tree's bounds and takes the whole index down with it.
23
+ if (Number.isFinite(px) && Number.isFinite(py))
24
+ placed.push({ x: px, y: py, point, index });
25
+ }
26
+ return placed;
27
+ }
28
+ export function buildIndex(placed) {
29
+ return quadtree()
30
+ .x((d) => d.x)
31
+ .y((d) => d.y)
32
+ .addAll(placed);
33
+ }
34
+ /** The closest geom within the snap radius, or nothing where the pointer is over empty ground. */
35
+ export function nearest(index, x, y, radius = SNAP_RADIUS) {
36
+ return index.find(x, y, radius) ?? null;
37
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * The `/core` surface: author a chart, load a saved one, check it, and hand it to a drawing surface.
3
+ *
4
+ * No DOM and no React. Each name here is semver surface. The package's own entry points read the
5
+ * modules through `internal.ts`, which the exports map leaves out.
6
+ */
7
+ export { plot, type AnnotateValue, type AnnotateValues, type AnyLayerOptions, type FitOptions, type Labs, type LayerAes, type LayerArgs, type LayerCall, type LayerOptions, type PlotBuilder, type RuleOptions, type RuleValue, type SmoothOptions, } from "./build.js";
8
+ export { SPEC_VERSION, type Aes, type Anchor, type Annotation, type BandOrder, type CellNumbers, type ChartScales, type ChartSpec, type ColorScale, type FitSpec, type GuideScale, type LabelRule, type LabelWhen, type LayerSpec, type LayerStyle, type Leader, type LegendOptions, type Linetype, type PlotKind, type PlotSource, type Position, type PositionKind, type PositionScale, type SideContent, type SidePanel, type SmoothMethod, type SmoothSpec, type StatParams, type StoredSpec, type TickFormat, } from "./spec.js";
9
+ export { loadSpec } from "./load.js";
10
+ export { KINDS } from "./grammar.js";
11
+ export type { Aesthetic, Parts } from "./geom-table.js";
12
+ export type { Aggregate } from "./aggregate.js";
13
+ export type { AxisTransform } from "./transform.js";
14
+ export type { PointShape } from "./geom-types.js";
15
+ export type { PlotSourceOption } from "./spec-edit.js";
16
+ export type { BinClosed, BinDrop, PerAxis } from "./binning.js";
17
+ export type { Bandwidth, Kernel, QuantileType } from "./stats.js";
18
+ export type { ClusterDistance, DepthScale, Linkage } from "./cluster.js";
19
+ export type { RingContent, RingPanel } from "./rings.js";
20
+ export type { LinkFields } from "./circular.js";
21
+ export { fromArrow, fromColumns, fromRows, toPlotData, type AdapterOptions, type Arrayish, type ArrowLike, type FieldOverride, type VizData, } from "./adapters.js";
22
+ export type { PlotData, PlotField, PlotFieldRole } from "./plot-data.js";
23
+ export { blocking, critiqueSet, critiqueSpec as critique, type CritiqueOptions, type Finding, type FindingCode, type Severity, type UnwaivableCode, type WaivableCode, } from "./critique.js";
24
+ export { OPEN_PROFILE, validate, type CapabilityProfile, type Failure, type SettingGroup, type Verdict, } from "./profile.js";
25
+ export { fitSignature, type FitGroup, type FitRequest, type Fitter } from "./fitter.js";
26
+ export type { Fit, LayerFits } from "./fit-types.js";
27
+ export { categoricalCss, DEFAULT_CATEGORICAL, DEFAULT_DIVERGING, DEFAULT_PALETTES, DEFAULT_SEQUENTIAL, PALETTES, palettesFor, registerPalette, sequentialCss, stopsOf, type Palette, type PaletteChoice, type PaletteName, } from "./palette.js";
28
+ export type { Palettes } from "./layers.js";
29
+ export { DARK_THEME, LIGHT_THEME, TOKEN_MEANING, VIZ_TOKENS, type ThemeSnapshot, type VizToken } from "./tokens.js";
30
+ export { assign, type FigureSet } from "./figure-set.js";
31
+ export type { Selected } from "./select.js";
32
+ export type { BrushShape, Brushed } from "./region.js";
33
+ export { leaves, type Predicate } from "./predicate.js";
@@ -0,0 +1,18 @@
1
+ /**
2
+ * The `/core` surface: author a chart, load a saved one, check it, and hand it to a drawing surface.
3
+ *
4
+ * No DOM and no React. Each name here is semver surface. The package's own entry points read the
5
+ * modules through `internal.ts`, which the exports map leaves out.
6
+ */
7
+ export { plot, } from "./build.js";
8
+ export { SPEC_VERSION, } from "./spec.js";
9
+ export { loadSpec } from "./load.js";
10
+ export { KINDS } from "./grammar.js";
11
+ export { fromArrow, fromColumns, fromRows, toPlotData, } from "./adapters.js";
12
+ export { blocking, critiqueSet, critiqueSpec as critique, } from "./critique.js";
13
+ export { OPEN_PROFILE, validate, } from "./profile.js";
14
+ export { fitSignature } from "./fitter.js";
15
+ export { categoricalCss, DEFAULT_CATEGORICAL, DEFAULT_DIVERGING, DEFAULT_PALETTES, DEFAULT_SEQUENTIAL, PALETTES, palettesFor, registerPalette, sequentialCss, stopsOf, } from "./palette.js";
16
+ export { DARK_THEME, LIGHT_THEME, TOKEN_MEANING, VIZ_TOKENS } from "./tokens.js";
17
+ export { assign } from "./figure-set.js";
18
+ export { leaves } from "./predicate.js";