@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,69 @@
1
+ /**
2
+ * Reference lines drawn as layers, and where each one crosses the plot area.
3
+ *
4
+ * ggplot's `geom_hline`, `geom_vline` and `geom_abline`. A sloped line reads transformed space.
5
+ */
6
+ import { TRANSFORMS, transformOf } from "./transform.js";
7
+ const ruleOf = (axis) => TRANSFORMS[transformOf(axis)];
8
+ const back = (axis) => ruleOf(axis).back;
9
+ /** A domain in the axis's own transformed space, low end first. Null where a log axis has no positive span. */
10
+ function spanOf(axis) {
11
+ const [a, b] = axis.domain;
12
+ const { forward } = ruleOf(axis);
13
+ const lo = forward(Math.min(a, b));
14
+ const hi = forward(Math.max(a, b));
15
+ return Number.isFinite(lo) && Number.isFinite(hi) ? [lo, hi] : null;
16
+ }
17
+ /** Whether a value lies on an axis, in data units. A band axis places each value at its category. */
18
+ function onAxis(value, axis) {
19
+ if (!axis)
20
+ return true;
21
+ if (!ruleOf(axis).admits(value))
22
+ return false;
23
+ const [a, b] = axis.domain;
24
+ return value >= Math.min(a, b) && value <= Math.max(a, b);
25
+ }
26
+ /**
27
+ * The part of a line inside the plot area, in data units, or null.
28
+ *
29
+ * A horizontal or vertical line on a band axis marks the category its value names. A sloped line
30
+ * needs continuous axes.
31
+ */
32
+ export function ruleSegment(rule, x, y) {
33
+ if (rule.kind === "h")
34
+ return onAxis(rule.y, y) ? { y: rule.y } : null;
35
+ if (rule.kind === "v")
36
+ return onAxis(rule.x, x) ? { x: rule.x } : null;
37
+ if (!x || !y)
38
+ return null;
39
+ const across = spanOf(x);
40
+ const down = spanOf(y);
41
+ if (!across || !down)
42
+ return null;
43
+ const [xa, xb] = across;
44
+ const [ya, yb] = down;
45
+ const { slope, intercept } = rule;
46
+ const at = (tx) => intercept + slope * tx;
47
+ /* The stretch of the transformed x run where the line stays between the two vertical ends. */
48
+ let lo = xa;
49
+ let hi = xb;
50
+ if (slope === 0) {
51
+ if (intercept < ya || intercept > yb)
52
+ return null;
53
+ }
54
+ else {
55
+ const one = (ya - intercept) / slope;
56
+ const two = (yb - intercept) / slope;
57
+ lo = Math.max(xa, Math.min(one, two));
58
+ hi = Math.min(xb, Math.max(one, two));
59
+ if (lo > hi)
60
+ return null;
61
+ }
62
+ /* An end on the frame takes the domain's own value. A round trip through the transform would drift it. */
63
+ const [xLo, xHi] = [Math.min(x.domain[0], x.domain[1]), Math.max(x.domain[0], x.domain[1])];
64
+ const [yLo, yHi] = [Math.min(y.domain[0], y.domain[1]), Math.max(y.domain[0], y.domain[1])];
65
+ const near = (a, b) => Math.abs(a - b) <= 1e-12 * (1 + Math.abs(b));
66
+ const toX = (t) => (near(t, xa) ? xLo : near(t, xb) ? xHi : back(x)(t));
67
+ const toY = (t) => (near(t, ya) ? yLo : near(t, yb) ? yHi : back(y)(t));
68
+ return { x0: toX(lo), y0: toY(at(lo)), x1: toX(hi), y1: toY(at(hi)) };
69
+ }
@@ -0,0 +1,49 @@
1
+ /**
2
+ * One rectangle per node and one ribbon per link, in pixels.
3
+ *
4
+ * Node width and node padding are pixel quantities. A fractional answer stretches both with the
5
+ * panel's aspect.
6
+ */
7
+ import type { SankeyLinkInput, SankeyNodeInput } from "./geom-types.js";
8
+ /** A node's rectangle, in pixels from the top left of the drawing area. */
9
+ export interface SankeyNode extends SankeyNodeInput {
10
+ x0: number;
11
+ x1: number;
12
+ y0: number;
13
+ y1: number;
14
+ value: number;
15
+ }
16
+ export interface SankeyLink extends SankeyLinkInput {
17
+ path: string;
18
+ /** Ribbon thickness at each end. The two differ under column normalization. */
19
+ sourceThickness: number;
20
+ targetThickness: number;
21
+ }
22
+ export interface SankeyLayout {
23
+ nodes: SankeyNode[];
24
+ links: SankeyLink[];
25
+ /** Left edge of each column, for a header written over it. */
26
+ columnX: Map<number, number>;
27
+ }
28
+ export interface SankeyOptions {
29
+ width: number;
30
+ height: number;
31
+ /** Thin enough that the ribbons read as the subject and a node as what they pass through. */
32
+ nodeWidth?: number;
33
+ /** Gap between two nodes of one column, in pixels. */
34
+ nodePadding?: number;
35
+ /**
36
+ * How a value becomes a thickness.
37
+ *
38
+ * `column` scales each column to its own population. `global` scales the figure once, which puts
39
+ * a long cascade's last columns under a pixel.
40
+ */
41
+ normalize?: "global" | "column";
42
+ }
43
+ /**
44
+ * Nodes placed and links routed.
45
+ *
46
+ * A node's value is the larger of what reaches it and what leaves it. A link naming an absent node
47
+ * is dropped.
48
+ */
49
+ export declare function sankeyLayout(nodes: readonly SankeyNodeInput[], links: readonly SankeyLinkInput[], { width, height, nodeWidth, nodePadding, normalize }: SankeyOptions): SankeyLayout;
@@ -0,0 +1,96 @@
1
+ /**
2
+ * One rectangle per node and one ribbon per link, in pixels.
3
+ *
4
+ * Node width and node padding are pixel quantities. A fractional answer stretches both with the
5
+ * panel's aspect.
6
+ */
7
+ /**
8
+ * Nodes placed and links routed.
9
+ *
10
+ * A node's value is the larger of what reaches it and what leaves it. A link naming an absent node
11
+ * is dropped.
12
+ */
13
+ export function sankeyLayout(nodes, links, { width, height, nodeWidth = 12, nodePadding = 8, normalize = "global" }) {
14
+ const value = new Map();
15
+ for (const node of nodes) {
16
+ let incoming = 0;
17
+ let outgoing = 0;
18
+ for (const link of links) {
19
+ if (link.target === node.id)
20
+ incoming += link.value;
21
+ if (link.source === node.id)
22
+ outgoing += link.value;
23
+ }
24
+ value.set(node.id, Math.max(incoming, outgoing));
25
+ }
26
+ const columns = [...new Set(nodes.map((node) => node.column))].sort((a, b) => a - b);
27
+ const lastColumn = columns[columns.length - 1] ?? 0;
28
+ const columnStep = columns.length > 1 ? (width - nodeWidth) / lastColumn : 0;
29
+ /* The tightest column sets the global scale. Every column fits under it. */
30
+ const columnScale = new Map();
31
+ let globalScale = Infinity;
32
+ for (const column of columns) {
33
+ const members = nodes.filter((node) => node.column === column);
34
+ const total = members.reduce((sum, node) => sum + (value.get(node.id) ?? 0), 0);
35
+ const available = height - nodePadding * Math.max(0, members.length - 1);
36
+ const scale = total > 0 ? available / total : 0;
37
+ columnScale.set(column, scale);
38
+ if (scale > 0)
39
+ globalScale = Math.min(globalScale, scale);
40
+ }
41
+ if (!Number.isFinite(globalScale))
42
+ globalScale = 0;
43
+ const scaleFor = (column) => (normalize === "column" ? (columnScale.get(column) ?? 0) : globalScale);
44
+ const placed = new Map();
45
+ const columnX = new Map();
46
+ for (const column of columns) {
47
+ const scale = scaleFor(column);
48
+ /* Exits sort under the survivors of their column, and the largest node leads. */
49
+ const members = nodes
50
+ .filter((node) => node.column === column)
51
+ .sort((a, b) => {
52
+ const rank = (node) => (node.kind === "exit" ? 1 : 0);
53
+ return rank(a) - rank(b) || (value.get(b.id) ?? 0) - (value.get(a.id) ?? 0);
54
+ });
55
+ const x0 = column * columnStep;
56
+ columnX.set(column, x0);
57
+ let y = 0;
58
+ for (const node of members) {
59
+ const reading = value.get(node.id) ?? 0;
60
+ const tall = Math.max(1, reading * scale);
61
+ placed.set(node.id, { ...node, value: reading, x0, x1: x0 + nodeWidth, y0: y, y1: y + tall });
62
+ y += tall + nodePadding;
63
+ }
64
+ }
65
+ const outOffset = new Map();
66
+ const inOffset = new Map();
67
+ const routed = [];
68
+ /* Survivors first. The main path hugs the top of each node and the exits hang under it. */
69
+ const ordered = [...links].sort((a, b) => {
70
+ const rank = (link) => (link.kind === "survivor" || !link.kind ? 0 : 1);
71
+ return rank(a) - rank(b);
72
+ });
73
+ for (const link of ordered) {
74
+ const source = placed.get(link.source);
75
+ const target = placed.get(link.target);
76
+ if (!source || !target)
77
+ continue;
78
+ const sourceThickness = Math.max(1, link.value * scaleFor(source.column));
79
+ const targetThickness = Math.max(1, link.value * scaleFor(target.column));
80
+ const sy = source.y0 + (outOffset.get(link.source) ?? 0);
81
+ const ty = target.y0 + (inOffset.get(link.target) ?? 0);
82
+ outOffset.set(link.source, (outOffset.get(link.source) ?? 0) + sourceThickness);
83
+ inOffset.set(link.target, (inOffset.get(link.target) ?? 0) + targetThickness);
84
+ /* Out along the top edge and back along the bottom, which closes the ribbon into one fill. */
85
+ const mid = (source.x1 + target.x0) / 2;
86
+ const path = [
87
+ `M${source.x1},${sy}`,
88
+ `C${mid},${sy} ${mid},${ty} ${target.x0},${ty}`,
89
+ `L${target.x0},${ty + targetThickness}`,
90
+ `C${mid},${ty + targetThickness} ${mid},${sy + sourceThickness} ${source.x1},${sy + sourceThickness}`,
91
+ "Z",
92
+ ].join("");
93
+ routed.push({ ...link, path, sourceThickness, targetThickness });
94
+ }
95
+ return { nodes: [...placed.values()], links: routed, columnX };
96
+ }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * What a click on a geom selects, as data.
3
+ *
4
+ * A click returns the rows, the keys and a `Predicate`, the three a brush returns. The host
5
+ * publishes the predicate into its own selection and the charts beside this one narrow.
6
+ */
7
+ import type { LayerHit } from "./readout.js";
8
+ import type { Predicate } from "./predicate.js";
9
+ import type { ChartSpec, PlotKind } from "./spec.js";
10
+ import type { PlotData } from "./plot-data.js";
11
+ /**
12
+ * A geom the reader clicked, and the records it stands for.
13
+ *
14
+ * `rows`, `keys` and `predicate` carry the names and types a brush's `Brushed` carries.
15
+ */
16
+ export interface Selected {
17
+ /** The layer the geom belongs to. */
18
+ layerId: string;
19
+ /** That layer's kind. A pie's slice reads `pie`, and a histogram's bar reads `histogram`. */
20
+ kind: PlotKind;
21
+ /**
22
+ * The record the geom stands for, field by field, as the layer's table reads it.
23
+ *
24
+ * Null where the geom stands for several records or none, such as a bar over a group.
25
+ */
26
+ datum: Readonly<Record<string, unknown>> | null;
27
+ /** Rows of the table the layer read, ascending, each once. A point names its own row. */
28
+ rows: number[];
29
+ /** Keys of the same records, where the key column names each record once. Empty otherwise. */
30
+ keys: string[];
31
+ /** A filter for the same records. Null where the geom stands for nothing a query can name. */
32
+ predicate: Predicate | null;
33
+ }
34
+ export interface SelectContext {
35
+ spec: ChartSpec;
36
+ /** The figure's table. A layer with rows of its own reads those, and its own key column. */
37
+ data: PlotData;
38
+ }
39
+ /**
40
+ * The filter a clicked geom means.
41
+ *
42
+ * The split and the facet fold in wherever a predicate came out. A geom in a split trace or a
43
+ * panel stands for its own subset.
44
+ */
45
+ export declare function selectionOf(hit: LayerHit, context: SelectContext): Predicate | null;
46
+ /**
47
+ * What a click selected, as the host reads it.
48
+ *
49
+ * A point, segment or line names its own row. A summary names the rows its predicate holds in the
50
+ * layer's table.
51
+ */
52
+ export declare function selectedOf(hit: LayerHit, context: SelectContext): Selected;
@@ -0,0 +1,216 @@
1
+ /**
2
+ * What a click on a geom selects, as data.
3
+ *
4
+ * A click returns the rows, the keys and a `Predicate`, the three a brush returns. The host
5
+ * publishes the predicate into its own selection and the charts beside this one narrow.
6
+ */
7
+ /**
8
+ * Whether a key column names each record once. A key naming two rows names a group, the brush's rule.
9
+ *
10
+ * Read on each click. A host's table can change under the figure.
11
+ */
12
+ function keyedOnce(table, field) {
13
+ const seen = new Set();
14
+ for (let row = 0; row < table.rowCount; row += 1) {
15
+ const key = table.text(row, field);
16
+ if (key == null)
17
+ continue;
18
+ if (seen.has(key))
19
+ return false;
20
+ seen.add(key);
21
+ }
22
+ return true;
23
+ }
24
+ /** The key column of the table a trace read, where it names each record once. */
25
+ const keyOf = (trace, data) => {
26
+ const table = trace.table ?? data;
27
+ return table.keyField && keyedOnce(table, table.keyField) ? table.keyField : undefined;
28
+ };
29
+ /** One field id off a channel. A channel may bind several. */
30
+ const oneField = (channel) => typeof channel === "string" ? channel : channel?.[0];
31
+ /** The trace's own values of the split field and the facet field. They narrow each predicate below it. */
32
+ function narrowingOf(trace) {
33
+ return [
34
+ ...(trace.splitField && trace.splitValue != null
35
+ ? [{ kind: "eq", field: trace.splitField.id, value: trace.splitValue }]
36
+ : []),
37
+ ...(trace.facetField && trace.facetValue != null
38
+ ? [{ kind: "eq", field: trace.facetField.id, value: trace.facetValue }]
39
+ : []),
40
+ ];
41
+ }
42
+ const allOf = (of) => of.length === 0 ? null : of.length === 1 ? of[0] : { kind: "and", of };
43
+ /**
44
+ * The filter a clicked geom means.
45
+ *
46
+ * The split and the facet fold in wherever a predicate came out. A geom in a split trace or a
47
+ * panel stands for its own subset.
48
+ */
49
+ export function selectionOf(hit, context) {
50
+ const series = context.spec.layers.find((one) => one.id === hit.trace.layerId);
51
+ const xField = oneField(series?.aes.x);
52
+ const yField = oneField(series?.aes.y);
53
+ const narrowing = narrowingOf(hit.trace);
54
+ const narrowed = (predicate) => (predicate ? allOf([predicate, ...narrowing]) : null);
55
+ const keyField = keyOf(hit.trace, context.data);
56
+ if (hit.kind === "point" || hit.kind === "cube") {
57
+ const { datum: point } = hit.hit;
58
+ if (keyField && point.key != null) {
59
+ return narrowed({ kind: "eq", field: keyField, value: point.key });
60
+ }
61
+ /* A geom in a cube was placed by three channels. Three name it back. */
62
+ const zField = hit.kind === "cube" ? oneField(series?.aes.z) : undefined;
63
+ return narrowed(allOf([
64
+ ...(xField ? [{ kind: "eq", field: xField, value: point.category ?? point.x }] : []),
65
+ ...(yField ? [{ kind: "eq", field: yField, value: point.y }] : []),
66
+ ...(zField && point.z != null ? [{ kind: "eq", field: zField, value: point.z }] : []),
67
+ ]));
68
+ }
69
+ /* A segment names its record by its key, else by both ends. A field bound to two ends is named once. */
70
+ if (hit.kind === "segment") {
71
+ const { datum } = hit.hit;
72
+ const { plotRow, row } = datum;
73
+ const at = plotRow ?? row;
74
+ const key = keyField && at != null ? (hit.trace.table ?? context.data).text(at, keyField) : null;
75
+ if (keyField && key != null)
76
+ return narrowed({ kind: "eq", field: keyField, value: key });
77
+ const ends = new Map([
78
+ [xField, datum.x],
79
+ [yField, datum.y],
80
+ ]);
81
+ /* A band axis draws both ends in the x category. The xend column drops out. */
82
+ if (typeof datum.xEnd === "number")
83
+ ends.set(oneField(series?.aes.xend), datum.xEnd);
84
+ ends.set(oneField(series?.aes.yend), datum.yEnd);
85
+ return narrowed(allOf([...ends].flatMap(([field, value]) => (field ? [{ kind: "eq", field, value }] : []))));
86
+ }
87
+ if (hit.kind === "bar" || hit.kind === "slice") {
88
+ if (!xField)
89
+ return null;
90
+ const { bin } = hit.datum;
91
+ /* An end that holds no reading on it is left out by value. */
92
+ if (bin) {
93
+ return narrowed(allOf([
94
+ { kind: "range", field: xField, lo: bin.from, hi: bin.to },
95
+ ...(bin.closed === "right" || bin.closed === "neither"
96
+ ? [{ kind: "eq", field: xField, value: bin.from, negate: true }]
97
+ : []),
98
+ ...(bin.closed === "left" || bin.closed === "neither"
99
+ ? [{ kind: "eq", field: xField, value: bin.to, negate: true }]
100
+ : []),
101
+ ]));
102
+ }
103
+ const width = "binWidth" in hit.trace.data ? hit.trace.data.binWidth : undefined;
104
+ const center = hit.datum.at;
105
+ if (width != null && center != null) {
106
+ if ((context.spec.scales?.x?.transform ?? "identity") !== "identity")
107
+ return null;
108
+ return narrowed({ kind: "range", field: xField, lo: center - width / 2, hi: center + width / 2 });
109
+ }
110
+ return narrowed({ kind: "eq", field: xField, value: hit.datum.category });
111
+ }
112
+ if (hit.kind === "box" || hit.kind === "violin") {
113
+ return xField ? narrowed({ kind: "eq", field: xField, value: hit.datum.category }) : null;
114
+ }
115
+ if (hit.kind === "cell") {
116
+ return narrowed(allOf([
117
+ ...(xField ? [{ kind: "eq", field: xField, value: hit.cell.x }] : []),
118
+ ...(yField ? [{ kind: "eq", field: yField, value: hit.cell.y }] : []),
119
+ ]));
120
+ }
121
+ /*
122
+ A parallel line is one record across several measures. It selects by the key column where the
123
+ table names one and by its reading on each trace axis otherwise.
124
+ */
125
+ if (hit.kind === "record") {
126
+ if (keyField && hit.line.key != null) {
127
+ return narrowed({ kind: "eq", field: keyField, value: hit.line.key });
128
+ }
129
+ const axes = hit.trace.data.geom === "parcoords" ? hit.trace.data.axes : [];
130
+ return narrowed(allOf(axes.flatMap((axis, index) => {
131
+ const value = hit.line.values[index];
132
+ return value == null ? [] : [{ kind: "eq", field: axis.id, value }];
133
+ })));
134
+ }
135
+ /* A node names its own row by label. A parent names the rows of its subtree too. */
136
+ if (hit.kind === "node") {
137
+ const label = oneField(series?.aes.label);
138
+ if (!label)
139
+ return null;
140
+ const data = hit.trace.data;
141
+ const cells = data.at === "groups" && (data.geom === "treemap" || data.geom === "icicle" || data.geom === "sunburst")
142
+ ? data.cells
143
+ : [];
144
+ /* One pass. A layout puts a parent before its children. */
145
+ const under = new Set([hit.cell.label]);
146
+ for (const cell of cells)
147
+ if (cell.parent != null && under.has(cell.parent))
148
+ under.add(cell.label);
149
+ return narrowed(under.size > 1
150
+ ? { kind: "in", field: label, values: [...under] }
151
+ : { kind: "eq", field: label, value: hit.cell.label });
152
+ }
153
+ /* A sankey ribbon is a geom the package does not draw yet. */
154
+ return null;
155
+ }
156
+ /** Whether a row meets a predicate of the kinds a click emits. */
157
+ function holds(predicate, table, row) {
158
+ switch (predicate.kind) {
159
+ case "and":
160
+ return predicate.of.every((one) => holds(one, table, row));
161
+ case "or":
162
+ return predicate.of.some((one) => holds(one, table, row));
163
+ case "eq": {
164
+ const equal = typeof predicate.value === "number"
165
+ ? table.number(row, predicate.field) === predicate.value
166
+ : table.text(row, predicate.field) === predicate.value;
167
+ return predicate.negate ? !equal : equal;
168
+ }
169
+ case "range": {
170
+ const value = table.number(row, predicate.field);
171
+ return (value != null &&
172
+ (predicate.lo == null || value >= predicate.lo) &&
173
+ (predicate.hi == null || value <= predicate.hi));
174
+ }
175
+ case "in": {
176
+ const value = table.text(row, predicate.field);
177
+ return value != null && predicate.values.includes(value);
178
+ }
179
+ default:
180
+ return false;
181
+ }
182
+ }
183
+ /** One row of a table as a record, each field read through the accessor its type takes. */
184
+ const recordAt = (table, row) => Object.fromEntries(table.fields.map(({ id, numeric }) => [id, numeric ? table.number(row, id) : table.text(row, id)]));
185
+ /**
186
+ * What a click selected, as the host reads it.
187
+ *
188
+ * A point, segment or line names its own row. A summary names the rows its predicate holds in the
189
+ * layer's table.
190
+ */
191
+ export function selectedOf(hit, context) {
192
+ const table = hit.trace.table ?? context.data;
193
+ const predicate = selectionOf(hit, context);
194
+ const own = hit.kind === "point" || hit.kind === "cube" || hit.kind === "segment"
195
+ ? (hit.hit.datum.plotRow ?? hit.hit.datum.row)
196
+ : hit.kind === "record"
197
+ ? (hit.line.plotRow ?? hit.line.row)
198
+ : undefined;
199
+ const rows = [];
200
+ if (typeof own === "number")
201
+ rows.push(own);
202
+ else if (predicate)
203
+ for (let row = 0; row < table.rowCount; row += 1)
204
+ if (holds(predicate, table, row))
205
+ rows.push(row);
206
+ const keyField = keyOf(hit.trace, context.data);
207
+ const keys = keyField
208
+ ? rows.flatMap((row) => {
209
+ const key = table.text(row, keyField);
210
+ return key == null ? [] : [key];
211
+ })
212
+ : [];
213
+ const { kind } = context.spec.layers.find((layer) => layer.id === hit.trace.layerId);
214
+ const datum = rows.length === 1 ? recordAt(table, rows[0]) : null;
215
+ return { layerId: hit.trace.layerId, kind, datum, rows, keys, predicate };
216
+ }
@@ -0,0 +1,98 @@
1
+ /**
2
+ * An annotation track resolved against its band.
3
+ *
4
+ * A track reads a column of the same table. A category takes its modal value per group and a
5
+ * measure takes the mean.
6
+ */
7
+ import { type PlotData, type PlotField } from "./plot-data.js";
8
+ import { type Palettes } from "./layers.js";
9
+ /**
10
+ * The sequential map a numeric track draws on.
11
+ *
12
+ * Separate from `DEFAULT_SEQUENTIAL`. One ramp with two meanings is the failure chart-design
13
+ * rule 8 names.
14
+ */
15
+ export declare const TRACK_SEQUENTIAL = "mako";
16
+ /** One track, resolved onto the categories the band drew. */
17
+ export type SideTrack = {
18
+ kind: "identity";
19
+ field: string;
20
+ /** The column's own name. The legend is labeled with it. */
21
+ label: string;
22
+ axis: "x" | "y";
23
+ /** The value for each category. A category with no reading is absent. */
24
+ at: Map<string, string>;
25
+ /** Position in the categorical palette per value, with a set's assignments in force. */
26
+ codes: Map<string, number>;
27
+ /**
28
+ * The values the legend names, in the order the strip drew them.
29
+ *
30
+ * Defaults to the drawn values. With `dropLevels` off, each value the set pinned on the
31
+ * field follows them.
32
+ */
33
+ levels: string[];
34
+ /** Categories with more than one value in the group. The modal one drew. */
35
+ disagreed: string[];
36
+ } | {
37
+ kind: "magnitude";
38
+ field: string;
39
+ label: string;
40
+ axis: "x" | "y";
41
+ at: Map<string, number>;
42
+ /** The two ends of what the track measured, in its own units. */
43
+ domain: [number, number];
44
+ /** Name of the sequential map, separate from the cells' map. The figure's palettes resolve it. */
45
+ palette: string;
46
+ };
47
+ /**
48
+ * A track over a band's categories.
49
+ *
50
+ * `categories` is the band in drawn order and `keyField` is their column. A category with no
51
+ * reading is left out.
52
+ */
53
+ export declare function trackFor({ field, axis, categories, keyField, data, fieldAt, palette, pinned, dropLevels, alias, }: {
54
+ field: string;
55
+ axis: "x" | "y";
56
+ categories: string[];
57
+ keyField: string;
58
+ data: PlotData;
59
+ fieldAt: Map<string, PlotField>;
60
+ palette?: string;
61
+ pinned?: Record<string, number>;
62
+ dropLevels?: boolean;
63
+ /** A row's own key to the category it draws under, where a kmeans order replaced the band. */
64
+ alias?: Map<string, string>;
65
+ }): SideTrack | null;
66
+ /**
67
+ * A track over a band of measures.
68
+ *
69
+ * A track here binds a property of the field -- its unit, role, direction or cut. Keyed by the
70
+ * label the band drew.
71
+ */
72
+ export declare function trackOverFields({ field, axis, categories, measures, palette, pinned, dropLevels, }: {
73
+ /** Name of the property read off each measure. The legend is labeled with it. */
74
+ field: string;
75
+ axis: "x" | "y";
76
+ /** The band in its drawn order. For this grid the measure labels. */
77
+ categories: string[];
78
+ measures: PlotField[];
79
+ palette?: string;
80
+ pinned?: Record<string, number>;
81
+ dropLevels?: boolean;
82
+ }): SideTrack | null;
83
+ /**
84
+ * The map a numeric track's legend samples, taking a value in the track's own units.
85
+ *
86
+ * The same contract `Trace.ramp` states and `ColorLegend` reads. The figure's palettes resolve the
87
+ * track's map, as they resolve a layer's. Palettes holding no `sequentialFor` fall back to the registry.
88
+ */
89
+ export declare function trackRamp(track: Extract<SideTrack, {
90
+ kind: "magnitude";
91
+ }>, palettes: Palettes, dark?: boolean): (value: number) => string;
92
+ /**
93
+ * The fill each of a track's categories takes, or null with no reading.
94
+ *
95
+ * A category track reads the figure's categorical map. A measure track reads the sequential map
96
+ * it named.
97
+ */
98
+ export declare function trackFill(track: SideTrack, palettes: Palettes, dark?: boolean): (category: string) => string | null;