@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.
- package/LICENSE +202 -0
- package/README.md +2171 -2
- package/dist/src/builder/PlotPanel.d.ts +42 -0
- package/dist/src/builder/PlotPanel.js +433 -0
- package/dist/src/builder/Popover.d.ts +37 -0
- package/dist/src/builder/Popover.js +32 -0
- package/dist/src/builder/Stepper.d.ts +21 -0
- package/dist/src/builder/Stepper.js +41 -0
- package/dist/src/builder/index.d.ts +10 -0
- package/dist/src/builder/index.js +10 -0
- package/dist/src/core/adapters.d.ts +86 -0
- package/dist/src/core/adapters.js +219 -0
- package/dist/src/core/aggregate.d.ts +74 -0
- package/dist/src/core/aggregate.js +102 -0
- package/dist/src/core/annotations.d.ts +34 -0
- package/dist/src/core/annotations.js +71 -0
- package/dist/src/core/band-order.d.ts +86 -0
- package/dist/src/core/band-order.js +132 -0
- package/dist/src/core/binning.d.ts +123 -0
- package/dist/src/core/binning.js +193 -0
- package/dist/src/core/bounds.d.ts +40 -0
- package/dist/src/core/bounds.js +107 -0
- package/dist/src/core/brush.d.ts +73 -0
- package/dist/src/core/brush.js +102 -0
- package/dist/src/core/build.d.ts +322 -0
- package/dist/src/core/build.js +496 -0
- package/dist/src/core/camera.d.ts +152 -0
- package/dist/src/core/camera.js +303 -0
- package/dist/src/core/capabilities.d.ts +24 -0
- package/dist/src/core/capabilities.js +307 -0
- package/dist/src/core/chord.d.ts +49 -0
- package/dist/src/core/chord.js +57 -0
- package/dist/src/core/circular.d.ts +83 -0
- package/dist/src/core/circular.js +63 -0
- package/dist/src/core/cluster.d.ts +136 -0
- package/dist/src/core/cluster.js +607 -0
- package/dist/src/core/critique.d.ts +92 -0
- package/dist/src/core/critique.js +905 -0
- package/dist/src/core/density.d.ts +38 -0
- package/dist/src/core/density.js +82 -0
- package/dist/src/core/figure-set.d.ts +34 -0
- package/dist/src/core/figure-set.js +55 -0
- package/dist/src/core/finding-codes.d.ts +10 -0
- package/dist/src/core/finding-codes.js +1 -0
- package/dist/src/core/fit-types.d.ts +46 -0
- package/dist/src/core/fit-types.js +7 -0
- package/dist/src/core/fitter.d.ts +62 -0
- package/dist/src/core/fitter.js +102 -0
- package/dist/src/core/frame-geometry.d.ts +407 -0
- package/dist/src/core/frame-geometry.js +612 -0
- package/dist/src/core/frame.d.ts +329 -0
- package/dist/src/core/frame.js +1432 -0
- package/dist/src/core/geom-table.d.ts +503 -0
- package/dist/src/core/geom-table.js +484 -0
- package/dist/src/core/geom-types.d.ts +182 -0
- package/dist/src/core/geom-types.js +17 -0
- package/dist/src/core/grammar.d.ts +359 -0
- package/dist/src/core/grammar.js +397 -0
- package/dist/src/core/heat-reductions.d.ts +36 -0
- package/dist/src/core/heat-reductions.js +133 -0
- package/dist/src/core/hexbin.d.ts +84 -0
- package/dist/src/core/hexbin.js +168 -0
- package/dist/src/core/hierarchy.d.ts +59 -0
- package/dist/src/core/hierarchy.js +248 -0
- package/dist/src/core/hit.d.ts +75 -0
- package/dist/src/core/hit.js +37 -0
- package/dist/src/core/index.d.ts +33 -0
- package/dist/src/core/index.js +18 -0
- package/dist/src/core/internal.d.ts +58 -0
- package/dist/src/core/internal.js +58 -0
- package/dist/src/core/labels.d.ts +37 -0
- package/dist/src/core/labels.js +54 -0
- package/dist/src/core/layers.d.ts +594 -0
- package/dist/src/core/layers.js +2892 -0
- package/dist/src/core/load.d.ts +17 -0
- package/dist/src/core/load.js +356 -0
- package/dist/src/core/marginal.d.ts +65 -0
- package/dist/src/core/marginal.js +116 -0
- package/dist/src/core/mesh.d.ts +69 -0
- package/dist/src/core/mesh.js +237 -0
- package/dist/src/core/omission.d.ts +41 -0
- package/dist/src/core/omission.js +62 -0
- package/dist/src/core/palette-sets.d.ts +623 -0
- package/dist/src/core/palette-sets.js +1058 -0
- package/dist/src/core/palette.d.ts +871 -0
- package/dist/src/core/palette.js +511 -0
- package/dist/src/core/plot-data.d.ts +71 -0
- package/dist/src/core/plot-data.js +82 -0
- package/dist/src/core/predicate.d.ts +50 -0
- package/dist/src/core/predicate.js +20 -0
- package/dist/src/core/profile.d.ts +111 -0
- package/dist/src/core/profile.js +221 -0
- package/dist/src/core/readout.d.ts +108 -0
- package/dist/src/core/readout.js +215 -0
- package/dist/src/core/region.d.ts +56 -0
- package/dist/src/core/region.js +101 -0
- package/dist/src/core/rings.d.ts +96 -0
- package/dist/src/core/rings.js +171 -0
- package/dist/src/core/rules.d.ts +45 -0
- package/dist/src/core/rules.js +69 -0
- package/dist/src/core/sankey.d.ts +49 -0
- package/dist/src/core/sankey.js +96 -0
- package/dist/src/core/select.d.ts +52 -0
- package/dist/src/core/select.js +216 -0
- package/dist/src/core/side-track.d.ts +98 -0
- package/dist/src/core/side-track.js +197 -0
- package/dist/src/core/sizing.d.ts +54 -0
- package/dist/src/core/sizing.js +37 -0
- package/dist/src/core/spec-edit.d.ts +68 -0
- package/dist/src/core/spec-edit.js +105 -0
- package/dist/src/core/spec.d.ts +1111 -0
- package/dist/src/core/spec.js +39 -0
- package/dist/src/core/stats.d.ts +161 -0
- package/dist/src/core/stats.js +318 -0
- package/dist/src/core/tick-format.d.ts +21 -0
- package/dist/src/core/tick-format.js +87 -0
- package/dist/src/core/tokens.d.ts +47 -0
- package/dist/src/core/tokens.js +138 -0
- package/dist/src/core/transform.d.ts +21 -0
- package/dist/src/core/transform.js +19 -0
- package/dist/src/core/voxels.d.ts +53 -0
- package/dist/src/core/voxels.js +129 -0
- package/dist/src/core/zoom.d.ts +76 -0
- package/dist/src/core/zoom.js +103 -0
- package/dist/src/graph/GraphSurface.d.ts +127 -0
- package/dist/src/graph/GraphSurface.js +423 -0
- package/dist/src/graph/data.d.ts +103 -0
- package/dist/src/graph/data.js +140 -0
- package/dist/src/graph/index.d.ts +7 -0
- package/dist/src/graph/index.js +7 -0
- package/dist/src/mcp/data.d.ts +28 -0
- package/dist/src/mcp/data.js +131 -0
- package/dist/src/mcp/index.d.ts +7 -0
- package/dist/src/mcp/index.js +7 -0
- package/dist/src/mcp/paths.d.ts +12 -0
- package/dist/src/mcp/paths.js +30 -0
- package/dist/src/mcp/render.d.ts +67 -0
- package/dist/src/mcp/render.js +189 -0
- package/dist/src/mcp/server.d.ts +7 -0
- package/dist/src/mcp/server.js +25 -0
- package/dist/src/mcp/tools.d.ts +82 -0
- package/dist/src/mcp/tools.js +412 -0
- package/dist/src/react/BrushLayer.d.ts +20 -0
- package/dist/src/react/BrushLayer.js +98 -0
- package/dist/src/react/ColorLegend.d.ts +45 -0
- package/dist/src/react/ColorLegend.js +64 -0
- package/dist/src/react/Figure.d.ts +19 -0
- package/dist/src/react/Figure.js +6 -0
- package/dist/src/react/Frame.d.ts +87 -0
- package/dist/src/react/Frame.js +192 -0
- package/dist/src/react/MarkLegend.d.ts +32 -0
- package/dist/src/react/MarkLegend.js +24 -0
- package/dist/src/react/SeriesLegend.d.ts +38 -0
- package/dist/src/react/SeriesLegend.js +27 -0
- package/dist/src/react/SizeLegend.d.ts +24 -0
- package/dist/src/react/SizeLegend.js +25 -0
- package/dist/src/react/Toolbar.d.ts +109 -0
- package/dist/src/react/Toolbar.js +483 -0
- package/dist/src/react/Tooltip.d.ts +37 -0
- package/dist/src/react/Tooltip.js +59 -0
- package/dist/src/react/Viz.d.ts +112 -0
- package/dist/src/react/Viz.js +1311 -0
- package/dist/src/react/export.d.ts +32 -0
- package/dist/src/react/export.js +181 -0
- package/dist/src/react/geoms/Area.d.ts +38 -0
- package/dist/src/react/geoms/Area.js +48 -0
- package/dist/src/react/geoms/Bars.d.ts +130 -0
- package/dist/src/react/geoms/Bars.js +164 -0
- package/dist/src/react/geoms/Box.d.ts +50 -0
- package/dist/src/react/geoms/Box.js +32 -0
- package/dist/src/react/geoms/Chord.d.ts +60 -0
- package/dist/src/react/geoms/Chord.js +78 -0
- package/dist/src/react/geoms/Contour.d.ts +44 -0
- package/dist/src/react/geoms/Contour.js +78 -0
- package/dist/src/react/geoms/DensityCurve.d.ts +30 -0
- package/dist/src/react/geoms/DensityCurve.js +38 -0
- package/dist/src/react/geoms/FitCurve.d.ts +26 -0
- package/dist/src/react/geoms/FitCurve.js +52 -0
- package/dist/src/react/geoms/Freqpoly.d.ts +30 -0
- package/dist/src/react/geoms/Freqpoly.js +45 -0
- package/dist/src/react/geoms/Gauge.d.ts +42 -0
- package/dist/src/react/geoms/Gauge.js +90 -0
- package/dist/src/react/geoms/Heatmap.d.ts +79 -0
- package/dist/src/react/geoms/Heatmap.js +51 -0
- package/dist/src/react/geoms/Hexbin.d.ts +74 -0
- package/dist/src/react/geoms/Hexbin.js +89 -0
- package/dist/src/react/geoms/Icicle.d.ts +38 -0
- package/dist/src/react/geoms/Icicle.js +9 -0
- package/dist/src/react/geoms/Ideogram.d.ts +58 -0
- package/dist/src/react/geoms/Ideogram.js +77 -0
- package/dist/src/react/geoms/Interval.d.ts +30 -0
- package/dist/src/react/geoms/Interval.js +39 -0
- package/dist/src/react/geoms/Line.d.ts +39 -0
- package/dist/src/react/geoms/Line.js +52 -0
- package/dist/src/react/geoms/Parallel.d.ts +38 -0
- package/dist/src/react/geoms/Parallel.js +131 -0
- package/dist/src/react/geoms/Pie.d.ts +62 -0
- package/dist/src/react/geoms/Pie.js +59 -0
- package/dist/src/react/geoms/Points.d.ts +84 -0
- package/dist/src/react/geoms/Points.js +207 -0
- package/dist/src/react/geoms/Points3d.d.ts +49 -0
- package/dist/src/react/geoms/Points3d.js +58 -0
- package/dist/src/react/geoms/Radar.d.ts +39 -0
- package/dist/src/react/geoms/Radar.js +59 -0
- package/dist/src/react/geoms/Range.d.ts +31 -0
- package/dist/src/react/geoms/Range.js +20 -0
- package/dist/src/react/geoms/RingStack.d.ts +39 -0
- package/dist/src/react/geoms/RingStack.js +65 -0
- package/dist/src/react/geoms/Rule.d.ts +29 -0
- package/dist/src/react/geoms/Rule.js +51 -0
- package/dist/src/react/geoms/Sankey.d.ts +24 -0
- package/dist/src/react/geoms/Sankey.js +51 -0
- package/dist/src/react/geoms/Segment.d.ts +36 -0
- package/dist/src/react/geoms/Segment.js +39 -0
- package/dist/src/react/geoms/Sunburst.d.ts +46 -0
- package/dist/src/react/geoms/Sunburst.js +64 -0
- package/dist/src/react/geoms/Surface.d.ts +32 -0
- package/dist/src/react/geoms/Surface.js +49 -0
- package/dist/src/react/geoms/Text.d.ts +35 -0
- package/dist/src/react/geoms/Text.js +40 -0
- package/dist/src/react/geoms/Treemap.d.ts +39 -0
- package/dist/src/react/geoms/Treemap.js +16 -0
- package/dist/src/react/geoms/Violin.d.ts +56 -0
- package/dist/src/react/geoms/Violin.js +60 -0
- package/dist/src/react/geoms/Volume.d.ts +37 -0
- package/dist/src/react/geoms/Volume.js +40 -0
- package/dist/src/react/geoms/Waterfall.d.ts +43 -0
- package/dist/src/react/geoms/Waterfall.js +18 -0
- package/dist/src/react/geoms/cube-stage.d.ts +39 -0
- package/dist/src/react/geoms/cube-stage.js +85 -0
- package/dist/src/react/geoms/hierarchy-cells.d.ts +42 -0
- package/dist/src/react/geoms/hierarchy-cells.js +59 -0
- package/dist/src/react/geoms/sector-axis.d.ts +66 -0
- package/dist/src/react/geoms/sector-axis.js +99 -0
- package/dist/src/react/gl/GlMesh.d.ts +22 -0
- package/dist/src/react/gl/GlMesh.js +78 -0
- package/dist/src/react/gl/GlPoints.d.ts +45 -0
- package/dist/src/react/gl/GlPoints.js +117 -0
- package/dist/src/react/gl/GlVolume.d.ts +28 -0
- package/dist/src/react/gl/GlVolume.js +112 -0
- package/dist/src/react/gl/GlWalls.d.ts +24 -0
- package/dist/src/react/gl/GlWalls.js +55 -0
- package/dist/src/react/gl/context.d.ts +71 -0
- package/dist/src/react/gl/context.js +98 -0
- package/dist/src/react/gl/encode.d.ts +120 -0
- package/dist/src/react/gl/encode.js +215 -0
- package/dist/src/react/gl/index.d.ts +10 -0
- package/dist/src/react/gl/index.js +8 -0
- package/dist/src/react/gl/refresh.d.ts +7 -0
- package/dist/src/react/gl/refresh.js +19 -0
- package/dist/src/react/gl/renderer.d.ts +377 -0
- package/dist/src/react/gl/renderer.js +1081 -0
- package/dist/src/react/gl/shaders.d.ts +92 -0
- package/dist/src/react/gl/shaders.js +405 -0
- package/dist/src/react/gl/surface.d.ts +31 -0
- package/dist/src/react/gl/surface.js +73 -0
- package/dist/src/react/index.d.ts +12 -0
- package/dist/src/react/index.js +11 -0
- package/dist/src/react/orbit.d.ts +38 -0
- package/dist/src/react/orbit.js +81 -0
- package/dist/src/react/sides/DendrogramPanel.d.ts +32 -0
- package/dist/src/react/sides/DendrogramPanel.js +39 -0
- package/dist/src/react/sides/DensityPanel.d.ts +31 -0
- package/dist/src/react/sides/DensityPanel.js +37 -0
- package/dist/src/react/sides/HistogramPanel.d.ts +30 -0
- package/dist/src/react/sides/HistogramPanel.js +18 -0
- package/dist/src/react/sides/RugPanel.d.ts +28 -0
- package/dist/src/react/sides/RugPanel.js +21 -0
- package/dist/src/react/sides/TrackPanel.d.ts +37 -0
- package/dist/src/react/sides/TrackPanel.js +27 -0
- package/dist/src/react/theme.d.ts +48 -0
- package/dist/src/react/theme.js +148 -0
- package/dist/src/react/useFits.d.ts +26 -0
- package/dist/src/react/useFits.js +62 -0
- package/dist/src/react/useZoom.d.ts +50 -0
- package/dist/src/react/useZoom.js +141 -0
- package/package.json +164 -5
- package/theme/default.css +62 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Export handles file handling -- e.g png and svg export.
|
|
3
|
+
* During export, a file must maintain the same tokens as the rendered plot. png are rasterized from svg.
|
|
4
|
+
*/
|
|
5
|
+
import { type ThemeSnapshot } from "../core/internal.js";
|
|
6
|
+
/** Resolves theme tokens to literal values. The file then carries its own colors. */
|
|
7
|
+
export declare function resolveTokens(svg: string, snapshot: ThemeSnapshot): string;
|
|
8
|
+
/**
|
|
9
|
+
* Admits that a static copy of a cube loses depth. A file cannot be rotated.
|
|
10
|
+
*
|
|
11
|
+
* Under WebGL an SVG copy drops the geoms and back-wall chrome. A PNG keeps both.
|
|
12
|
+
*/
|
|
13
|
+
export declare const STATIC_CUBE_NOTE = "One view of a turned figure. Depth is not recoverable in this copy.";
|
|
14
|
+
export interface SvgOptions {
|
|
15
|
+
/** Whether the file paints its own paper. Off where the caller composites its own ground. */
|
|
16
|
+
ground?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The chart as a standalone SVG document. Colors and font resolve at export.
|
|
20
|
+
*/
|
|
21
|
+
export declare function exportSvg(root: Element, { ground }?: SvgOptions): string;
|
|
22
|
+
export interface PngOptions {
|
|
23
|
+
/** Device pixels per CSS pixel. Two, the default, draws a retina-density file. */
|
|
24
|
+
scale?: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The chart as a PNG, at `scale` device pixels per CSS pixel.
|
|
28
|
+
*
|
|
29
|
+
* Composites the page's density layer under the chart where a renderer already drew one. The file
|
|
30
|
+
* matches the screen, and the export builds no renderer of its own.
|
|
31
|
+
*/
|
|
32
|
+
export declare function exportPng(root: Element, { scale }?: PngOptions): Promise<Blob>;
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Export handles file handling -- e.g png and svg export.
|
|
3
|
+
* During export, a file must maintain the same tokens as the rendered plot. png are rasterized from svg.
|
|
4
|
+
*/
|
|
5
|
+
import { resolveTheme } from "./theme.js";
|
|
6
|
+
import { existingGlSurface } from "./gl/surface.js";
|
|
7
|
+
import { VIZ_TOKENS } from "../core/internal.js";
|
|
8
|
+
/** Resolves theme tokens to literal values. The file then carries its own colors. */
|
|
9
|
+
export function resolveTokens(svg, snapshot) {
|
|
10
|
+
let out = svg;
|
|
11
|
+
for (const token of VIZ_TOKENS) {
|
|
12
|
+
out = out.split(`var(${token})`).join(snapshot[token]);
|
|
13
|
+
}
|
|
14
|
+
return out;
|
|
15
|
+
}
|
|
16
|
+
const SVG_NS = "http://www.w3.org/2000/svg";
|
|
17
|
+
/** Finds the figure's panel SVGs, or the first SVG where none is marked. Throws where the element holds none. */
|
|
18
|
+
function panelsOf(root) {
|
|
19
|
+
if (root instanceof SVGSVGElement)
|
|
20
|
+
return [root];
|
|
21
|
+
const marked = [...root.querySelectorAll("svg[data-viz-panel]")];
|
|
22
|
+
if (marked.length > 0)
|
|
23
|
+
return marked;
|
|
24
|
+
const first = root.querySelector("svg");
|
|
25
|
+
if (!first)
|
|
26
|
+
throw new Error("exportSvg: no svg under the element handed in");
|
|
27
|
+
return [first];
|
|
28
|
+
}
|
|
29
|
+
/** The size an SVG draws at, off its box and then its attributes. */
|
|
30
|
+
const sizeOf = (svg) => ({
|
|
31
|
+
width: svg.clientWidth || Number(svg.getAttribute("width")) || 640,
|
|
32
|
+
height: svg.clientHeight || Number(svg.getAttribute("height")) || 320,
|
|
33
|
+
});
|
|
34
|
+
/**
|
|
35
|
+
* Builds the figure as one SVG element with each panel at its offset.
|
|
36
|
+
*
|
|
37
|
+
* A facet's strips are HTML, and each is written in as its card and its name.
|
|
38
|
+
*/
|
|
39
|
+
function figureOf(root) {
|
|
40
|
+
const panels = panelsOf(root);
|
|
41
|
+
if (panels.length === 1) {
|
|
42
|
+
const only = panels[0];
|
|
43
|
+
return { panels, figure: only.cloneNode(true), ...sizeOf(only) };
|
|
44
|
+
}
|
|
45
|
+
const doc = root.ownerDocument;
|
|
46
|
+
const origin = root.getBoundingClientRect();
|
|
47
|
+
const figure = doc.createElementNS(SVG_NS, "svg");
|
|
48
|
+
let width = 0;
|
|
49
|
+
let height = 0;
|
|
50
|
+
for (const panel of panels) {
|
|
51
|
+
const box = panel.getBoundingClientRect();
|
|
52
|
+
const [x, y] = [box.left - origin.left, box.top - origin.top];
|
|
53
|
+
const copy = panel.cloneNode(true);
|
|
54
|
+
copy.setAttribute("x", String(x));
|
|
55
|
+
copy.setAttribute("y", String(y));
|
|
56
|
+
figure.appendChild(copy);
|
|
57
|
+
const size = sizeOf(panel);
|
|
58
|
+
width = Math.max(width, x + size.width);
|
|
59
|
+
height = Math.max(height, y + size.height);
|
|
60
|
+
}
|
|
61
|
+
for (const strip of root.querySelectorAll("[data-viz-strip]")) {
|
|
62
|
+
const box = strip.getBoundingClientRect();
|
|
63
|
+
const [x, y] = [box.left - origin.left, box.top - origin.top];
|
|
64
|
+
const card = doc.createElementNS(SVG_NS, "rect");
|
|
65
|
+
card.setAttribute("x", String(x));
|
|
66
|
+
card.setAttribute("y", String(y));
|
|
67
|
+
card.setAttribute("width", String(box.width));
|
|
68
|
+
card.setAttribute("height", String(box.height));
|
|
69
|
+
card.setAttribute("fill", "var(--viz-rule)");
|
|
70
|
+
const name = doc.createElementNS(SVG_NS, "text");
|
|
71
|
+
name.setAttribute("x", String(x + box.width / 2));
|
|
72
|
+
name.setAttribute("y", String(y + box.height / 2));
|
|
73
|
+
name.setAttribute("text-anchor", "middle");
|
|
74
|
+
name.setAttribute("dominant-baseline", "central");
|
|
75
|
+
name.setAttribute("font-size", doc.defaultView?.getComputedStyle(strip).fontSize || "12px");
|
|
76
|
+
name.setAttribute("font-weight", "500");
|
|
77
|
+
name.setAttribute("fill", "var(--viz-ink)");
|
|
78
|
+
name.textContent = strip.textContent;
|
|
79
|
+
figure.append(card, name);
|
|
80
|
+
}
|
|
81
|
+
figure.setAttribute("width", String(width));
|
|
82
|
+
figure.setAttribute("height", String(height));
|
|
83
|
+
return { panels, figure, width, height };
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Admits that a static copy of a cube loses depth. A file cannot be rotated.
|
|
87
|
+
*
|
|
88
|
+
* Under WebGL an SVG copy drops the geoms and back-wall chrome. A PNG keeps both.
|
|
89
|
+
*/
|
|
90
|
+
export const STATIC_CUBE_NOTE = "One view of a turned figure. Depth is not recoverable in this copy.";
|
|
91
|
+
/** Whether the chart holds a cube. A cube export carries the depth note. */
|
|
92
|
+
const holdsCube = (svg) => svg.querySelector("[data-viz-cube]") != null;
|
|
93
|
+
/** Adds the depth note under the geoms. */
|
|
94
|
+
function noteInto(clone, height) {
|
|
95
|
+
const line = clone.ownerDocument.createElementNS("http://www.w3.org/2000/svg", "text");
|
|
96
|
+
line.setAttribute("x", "4");
|
|
97
|
+
line.setAttribute("y", String(Math.max(10, height - 4)));
|
|
98
|
+
line.setAttribute("font-size", "10");
|
|
99
|
+
line.setAttribute("fill", "var(--viz-ink-muted)");
|
|
100
|
+
line.textContent = STATIC_CUBE_NOTE;
|
|
101
|
+
clone.appendChild(line);
|
|
102
|
+
}
|
|
103
|
+
/** Sets the paper for the figure. Enables background resolution for both dark and light mode. */
|
|
104
|
+
function groundInto(clone, snapshot) {
|
|
105
|
+
const paper = clone.ownerDocument.createElementNS("http://www.w3.org/2000/svg", "rect");
|
|
106
|
+
paper.setAttribute("x", "0");
|
|
107
|
+
paper.setAttribute("y", "0");
|
|
108
|
+
paper.setAttribute("width", "100%");
|
|
109
|
+
paper.setAttribute("height", "100%");
|
|
110
|
+
paper.setAttribute("fill", snapshot["--viz-paper"]);
|
|
111
|
+
clone.insertBefore(paper, clone.firstChild);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* The chart as a standalone SVG document. Colors and font resolve at export.
|
|
115
|
+
*/
|
|
116
|
+
export function exportSvg(root, { ground = true } = {}) {
|
|
117
|
+
const { panels, figure: clone, height } = figureOf(root);
|
|
118
|
+
const svg = panels[0];
|
|
119
|
+
const snapshot = resolveTheme(svg);
|
|
120
|
+
clone.setAttribute("xmlns", SVG_NS);
|
|
121
|
+
if (ground)
|
|
122
|
+
groundInto(clone, snapshot);
|
|
123
|
+
const family = svg.ownerDocument.defaultView?.getComputedStyle(svg).fontFamily;
|
|
124
|
+
if (family)
|
|
125
|
+
clone.setAttribute("font-family", family);
|
|
126
|
+
if (panels.some(holdsCube))
|
|
127
|
+
noteInto(clone, height);
|
|
128
|
+
const text = new XMLSerializer().serializeToString(clone);
|
|
129
|
+
return `<?xml version="1.0" encoding="UTF-8"?>\n${resolveTokens(text, snapshot)}`;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* The chart as a PNG, at `scale` device pixels per CSS pixel.
|
|
133
|
+
*
|
|
134
|
+
* Composites the page's density layer under the chart where a renderer already drew one. The file
|
|
135
|
+
* matches the screen, and the export builds no renderer of its own.
|
|
136
|
+
*/
|
|
137
|
+
export async function exportPng(root, { scale = 2 } = {}) {
|
|
138
|
+
const { panels, width, height } = figureOf(root);
|
|
139
|
+
const svg = panels[0];
|
|
140
|
+
const snapshot = resolveTheme(svg);
|
|
141
|
+
const image = new Image();
|
|
142
|
+
/* The canvas paints the ground for this path. */
|
|
143
|
+
const held = new Blob([exportSvg(root, { ground: false })], { type: "image/svg+xml;charset=utf-8" });
|
|
144
|
+
const url = URL.createObjectURL(held);
|
|
145
|
+
try {
|
|
146
|
+
await new Promise((resolve, reject) => {
|
|
147
|
+
image.onload = () => resolve();
|
|
148
|
+
image.onerror = () => reject(new Error("exportPng: the SVG did not rasterise"));
|
|
149
|
+
image.src = url;
|
|
150
|
+
});
|
|
151
|
+
const canvas = svg.ownerDocument.createElement("canvas");
|
|
152
|
+
canvas.width = Math.round(width * scale);
|
|
153
|
+
canvas.height = Math.round(height * scale);
|
|
154
|
+
const ctx = canvas.getContext("2d");
|
|
155
|
+
if (!ctx)
|
|
156
|
+
throw new Error("exportPng: no 2d context to compose in");
|
|
157
|
+
ctx.fillStyle = snapshot["--viz-paper"];
|
|
158
|
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
159
|
+
/* The GL slice must be copied in the same task as the draw. A later read returns a cleared buffer. */
|
|
160
|
+
const surface = existingGlSurface();
|
|
161
|
+
const glCanvas = svg.ownerDocument.querySelector('canvas[data-ts-lib-viz="gl-surface"]');
|
|
162
|
+
if (surface && glCanvas && glCanvas.width > 0) {
|
|
163
|
+
const view = svg.ownerDocument.defaultView;
|
|
164
|
+
const dpr = view?.devicePixelRatio || 1;
|
|
165
|
+
/* Each panel's slice lands at the panel's own offset in the figure. */
|
|
166
|
+
const origin = (panels.length === 1 ? svg : root).getBoundingClientRect();
|
|
167
|
+
surface.drawNow();
|
|
168
|
+
for (const panel of panels) {
|
|
169
|
+
const rect = panel.getBoundingClientRect();
|
|
170
|
+
ctx.drawImage(glCanvas, rect.left * dpr, rect.top * dpr, rect.width * dpr, rect.height * dpr, (rect.left - origin.left) * scale, (rect.top - origin.top) * scale, rect.width * scale, rect.height * scale);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
|
|
174
|
+
return await new Promise((resolve, reject) => {
|
|
175
|
+
canvas.toBlob((blob) => (blob ? resolve(blob) : reject(new Error("exportPng: the canvas gave no blob"))), "image/png");
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
finally {
|
|
179
|
+
URL.revokeObjectURL(url);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The area geom: a line with the ground under it filled in.
|
|
3
|
+
*
|
|
4
|
+
* Same payload the line kind draws and the same two shapes of it: records along a value scale, or a
|
|
5
|
+
* reduction per group along a band. What the fill adds is quantity, which is why `GRAMMAR` marks the
|
|
6
|
+
* kind proportional and the frame grounds the vertical scale at zero for it. See rule 11 of
|
|
7
|
+
* `CHART-DESIGN.md`: ink standing for a length has to start where the length does.
|
|
8
|
+
*
|
|
9
|
+
* The edge is stroked over the fill. A wash with no boundary reads as a region and not as a series,
|
|
10
|
+
* and two of them overlaid are unreadable without one.
|
|
11
|
+
*/
|
|
12
|
+
import type { LinePoint, Scale } from "../../core/internal.js";
|
|
13
|
+
/** A reading of an area, with the height the fill closes on where one is stacked below it. */
|
|
14
|
+
export interface AreaPoint extends LinePoint {
|
|
15
|
+
/**
|
|
16
|
+
* Where the fill closes. Absent closes on `baseline`.
|
|
17
|
+
*
|
|
18
|
+
* Set by a position adjustment: a stacked area stands on the running total of the ones before it,
|
|
19
|
+
* the way a stacked bar stands on a floor. Held on the reading instead of as a second array, so a
|
|
20
|
+
* point and the ground under it cannot come apart in the walk.
|
|
21
|
+
*/
|
|
22
|
+
floor?: number;
|
|
23
|
+
}
|
|
24
|
+
export interface AreaProps {
|
|
25
|
+
/** In axis units, already ordered the way the series reads. */
|
|
26
|
+
points: AreaPoint[];
|
|
27
|
+
x: Scale;
|
|
28
|
+
y: Scale;
|
|
29
|
+
color: string;
|
|
30
|
+
/** Value the fill closes on. Zero is what the proportional rule grounds the scale at. */
|
|
31
|
+
baseline?: number;
|
|
32
|
+
/** Paint on the edge. Absent takes `color`. */
|
|
33
|
+
stroke?: string;
|
|
34
|
+
lineWidth?: number;
|
|
35
|
+
fillOpacity?: number;
|
|
36
|
+
opacity?: number;
|
|
37
|
+
}
|
|
38
|
+
export declare function Area({ points, x, y, color, baseline, stroke, lineWidth, fillOpacity, opacity, }: AreaProps): import("react").JSX.Element | null;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* The area geom: a line with the ground under it filled in.
|
|
4
|
+
*
|
|
5
|
+
* Same payload the line kind draws and the same two shapes of it: records along a value scale, or a
|
|
6
|
+
* reduction per group along a band. What the fill adds is quantity, which is why `GRAMMAR` marks the
|
|
7
|
+
* kind proportional and the frame grounds the vertical scale at zero for it. See rule 11 of
|
|
8
|
+
* `CHART-DESIGN.md`: ink standing for a length has to start where the length does.
|
|
9
|
+
*
|
|
10
|
+
* The edge is stroked over the fill. A wash with no boundary reads as a region and not as a series,
|
|
11
|
+
* and two of them overlaid are unreadable without one.
|
|
12
|
+
*/
|
|
13
|
+
import { useMemo } from "react";
|
|
14
|
+
/** Fill weight of a lone area, and of a stacked one. Stacked bands abut and none covers another. */
|
|
15
|
+
const WASH = 0.22;
|
|
16
|
+
const STACKED_WASH = 0.85;
|
|
17
|
+
export function Area({ points, x, y, color, baseline = 0, stroke = color, lineWidth = 1.5, fillOpacity, opacity = 1, }) {
|
|
18
|
+
const stacked = points.some((point) => point.floor != null);
|
|
19
|
+
const wash = fillOpacity ?? (stacked ? STACKED_WASH : WASH);
|
|
20
|
+
const { edge, ground } = useMemo(() => {
|
|
21
|
+
let edge = "";
|
|
22
|
+
/* Walked back to front, so the closing run reverses the outward one. */
|
|
23
|
+
let under = "";
|
|
24
|
+
for (const point of points) {
|
|
25
|
+
const at = x(point.x).toFixed(2);
|
|
26
|
+
edge += `${edge ? "L" : "M"}${at},${y(point.y).toFixed(2)}`;
|
|
27
|
+
if (point.floor != null)
|
|
28
|
+
under = `L${at},${y(point.floor).toFixed(2)}${under}`;
|
|
29
|
+
}
|
|
30
|
+
const first = points[0];
|
|
31
|
+
const last = points[points.length - 1];
|
|
32
|
+
if (!first || !last)
|
|
33
|
+
return { edge, ground: "" };
|
|
34
|
+
/*
|
|
35
|
+
A stacked area closes on its own floors and a grounded one on a flat baseline.
|
|
36
|
+
|
|
37
|
+
Two vertices are enough for the flat case, and a stack needs one per reading: the band below it
|
|
38
|
+
has a shape, and closing on a straight line would fill over it.
|
|
39
|
+
*/
|
|
40
|
+
if (under)
|
|
41
|
+
return { edge, ground: `${edge}${under}Z` };
|
|
42
|
+
const floor = y(baseline).toFixed(2);
|
|
43
|
+
return { edge, ground: `${edge}L${x(last.x).toFixed(2)},${floor}L${x(first.x).toFixed(2)},${floor}Z` };
|
|
44
|
+
}, [points, x, y, baseline]);
|
|
45
|
+
if (points.length < 2)
|
|
46
|
+
return null;
|
|
47
|
+
return (_jsxs("g", { children: [_jsx("path", { d: ground, fill: color, fillOpacity: wash * opacity, stroke: "none" }), _jsx("path", { d: edge, fill: "none", stroke: stroke, strokeWidth: lineWidth, strokeOpacity: opacity, strokeLinejoin: "round", strokeLinecap: "round" })] }));
|
|
48
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bars: banded, binned, stacked, dodged, with the interval and the readings that belong on them.
|
|
3
|
+
*
|
|
4
|
+
* One component, because a stack is a floor on the datum and not a payload of its own. There was a
|
|
5
|
+
* second, `StackedBars`, taking a category with its segments; nothing ever built that shape, since
|
|
6
|
+
* building it meant collapsing the trace per series that the legend, the color assignment and the
|
|
7
|
+
* hover all read. A floor keeps the traces whole and makes a stack and a dodge the same kind of
|
|
8
|
+
* change, so the second component went.
|
|
9
|
+
*
|
|
10
|
+
* Stacked segments are separate rounded blocks with a gap between them, instead of one contiguous
|
|
11
|
+
* bar divided by borders. The gap does the work a border would, and it does it without adding a line
|
|
12
|
+
* to read. Each segment keeps a floor height, so a one-percent share stays visible instead of
|
|
13
|
+
* collapsing to a sliver that reads as absent; that distorts the smallest shares by design, and the
|
|
14
|
+
* readout carries the figure. Fills are solid: a hatch or texture adds visual noise carrying no
|
|
15
|
+
* data, and it breaks down at the small segment sizes a stack produces.
|
|
16
|
+
*
|
|
17
|
+
* The radius is a fixed six pixels instead of half the segment height. Proportional rounding turns
|
|
18
|
+
* a short segment into a lozenge, and a column of lozenges reads as a row of tags instead of as a
|
|
19
|
+
* quantity stacked up.
|
|
20
|
+
*
|
|
21
|
+
* A muted state lets one category read as the selection while the rest stay legible as context,
|
|
22
|
+
* which is what makes a bar chart usable as a control instead of only as a readout.
|
|
23
|
+
*
|
|
24
|
+
* `Bars` draws its own interval and its own replicates where the data carries them. A reader meets a
|
|
25
|
+
* bar, its whisker and the readings behind it as one geom, and a caller composing the three by hand
|
|
26
|
+
* can put them out of step: the prototype's dose response did that, drawing whiskers beside bars it
|
|
27
|
+
* had positioned separately. Both stay exported for a caller drawing either with no bar under it.
|
|
28
|
+
*/
|
|
29
|
+
import type { BarDatum, GeomHit, Scale } from "../../core/internal.js";
|
|
30
|
+
/** Matches --chart-radius-geom. Fixed, so a short geom stays a block instead of a lozenge. */
|
|
31
|
+
export declare const MARK_RADIUS = 6;
|
|
32
|
+
/** Paper between two neighbors, so a row of bins reads as separate counts. */
|
|
33
|
+
export declare const MARK_GAP = 3;
|
|
34
|
+
/** Widest a bar draws where the series states no thickness, in pixels. */
|
|
35
|
+
export declare const BAR_CAP = 40;
|
|
36
|
+
export type { BarDatum };
|
|
37
|
+
export interface BarsProps {
|
|
38
|
+
data: BarDatum[];
|
|
39
|
+
x: Scale;
|
|
40
|
+
y: Scale;
|
|
41
|
+
band: number;
|
|
42
|
+
baseline?: number;
|
|
43
|
+
color?: string;
|
|
44
|
+
/** Widest a bar is drawn. The band bounds it where the band is narrower. */
|
|
45
|
+
maxWidth?: number;
|
|
46
|
+
/**
|
|
47
|
+
* Which slot of the band this series takes, where a dodge divided it. Absent takes the band whole.
|
|
48
|
+
*
|
|
49
|
+
* The core states which of how many and the arithmetic is here, since the band's width is the
|
|
50
|
+
* geom's own business, the way `maxWidth` is a cap the band bounds.
|
|
51
|
+
*/
|
|
52
|
+
slot?: {
|
|
53
|
+
index: number;
|
|
54
|
+
of: number;
|
|
55
|
+
width?: number;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Draws the geom across the frame instead of up it, the band running down the side.
|
|
59
|
+
*
|
|
60
|
+
* Read off the frame and not off the spec, since the frame refuses a transpose where the geoms
|
|
61
|
+
* read no shared pair. The arithmetic is the same either way round with the pair swapped, which is
|
|
62
|
+
* why this is a flag on the geom and not a second component.
|
|
63
|
+
*/
|
|
64
|
+
transposed?: boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Paper between two stacked segments, in pixels.
|
|
67
|
+
*
|
|
68
|
+
* A gap does the work a border between segments would and adds no line to read. Applied where a
|
|
69
|
+
* datum carries a floor, since a bar standing on the baseline has nothing to be set apart from.
|
|
70
|
+
*/
|
|
71
|
+
segmentGap?: number;
|
|
72
|
+
/** Shortest a stacked segment draws, so a one-percent share stays visible instead of vanishing. */
|
|
73
|
+
minSegment?: number;
|
|
74
|
+
opacity?: number;
|
|
75
|
+
/** Outline paint. Absent draws none, as ggplot's `color = NA` on a bar. */
|
|
76
|
+
stroke?: string;
|
|
77
|
+
/** Outline width in pixels, where `stroke` names a color. */
|
|
78
|
+
lineWidth?: number;
|
|
79
|
+
cap?: "full" | "top";
|
|
80
|
+
/** Category drawn at full strength. The rest mute to context. */
|
|
81
|
+
active?: string | null;
|
|
82
|
+
/** The bar under the pointer, placed at the middle of its head. */
|
|
83
|
+
onHover?: (hit: GeomHit<BarDatum> | null) => void;
|
|
84
|
+
/** A bar clicked. The group it stands for is what a linked view filters on. */
|
|
85
|
+
onSelect?: (hit: GeomHit<BarDatum>) => void;
|
|
86
|
+
}
|
|
87
|
+
export declare function Bars({ data, x, y, band, baseline, color, maxWidth, slot, transposed, segmentGap, minSegment, opacity, stroke, lineWidth, cap, active, onHover, onSelect, }: BarsProps): import("react").JSX.Element;
|
|
88
|
+
/**
|
|
89
|
+
* A block on a baseline, rounded at its top and square at its foot.
|
|
90
|
+
*
|
|
91
|
+
* The radius clamps to a quarter of the width and of the height.
|
|
92
|
+
*/
|
|
93
|
+
export declare function roundedTop({ x, y, width: w, height: h, radius: r, }: {
|
|
94
|
+
x: number;
|
|
95
|
+
y: number;
|
|
96
|
+
width: number;
|
|
97
|
+
height: number;
|
|
98
|
+
radius?: number;
|
|
99
|
+
}): string;
|
|
100
|
+
export interface ErrorBarsProps {
|
|
101
|
+
data: BarDatum[];
|
|
102
|
+
x: Scale;
|
|
103
|
+
y: Scale;
|
|
104
|
+
capWidth?: number;
|
|
105
|
+
color?: string;
|
|
106
|
+
/** The band runs down the frame and the measure runs across. Matches `Bars`. */
|
|
107
|
+
transposed?: boolean;
|
|
108
|
+
/** Pixels off the category center, per datum. A whisker follows a dodged bar into its slot. */
|
|
109
|
+
offsetOf?: (d: BarDatum) => number;
|
|
110
|
+
}
|
|
111
|
+
export declare function ErrorBars({ data, x, y, capWidth, color, transposed, offsetOf, }: ErrorBarsProps): import("react").JSX.Element;
|
|
112
|
+
/** Replicate values behind each bar, so the reader sees n instead of trusting a whisker. */
|
|
113
|
+
export interface ReplicateDotsProps {
|
|
114
|
+
/** A group's own `band` and `offset` win over the props. A dodge with uneven slots sets both per group. */
|
|
115
|
+
data: {
|
|
116
|
+
category: string;
|
|
117
|
+
values: number[];
|
|
118
|
+
band?: number;
|
|
119
|
+
offset?: number;
|
|
120
|
+
}[];
|
|
121
|
+
x: Scale;
|
|
122
|
+
y: Scale;
|
|
123
|
+
band: number;
|
|
124
|
+
/** Pixels off the category center. The dots follow a dodged geom into its slot. */
|
|
125
|
+
offset?: number;
|
|
126
|
+
color?: string;
|
|
127
|
+
/** The band runs down the frame and the measure runs across. Matches `Bars`. */
|
|
128
|
+
transposed?: boolean;
|
|
129
|
+
}
|
|
130
|
+
export declare function ReplicateDots({ data, x, y, band, offset: shift, color, transposed, }: ReplicateDotsProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/** Matches --chart-radius-geom. Fixed, so a short geom stays a block instead of a lozenge. */
|
|
3
|
+
export const MARK_RADIUS = 6;
|
|
4
|
+
/** Paper between two neighbors, so a row of bins reads as separate counts. */
|
|
5
|
+
export const MARK_GAP = 3;
|
|
6
|
+
/** Widest a bar draws where the series states no thickness, in pixels. */
|
|
7
|
+
export const BAR_CAP = 40;
|
|
8
|
+
/**
|
|
9
|
+
* Pixel distance between the closest pair of geoms.
|
|
10
|
+
*
|
|
11
|
+
* A banded scale hands its bandwidth in, and that is the whole slot. A binned bar sits on a value
|
|
12
|
+
* scale, where the slot is the gap the scale puts between bin centers, and that gap is narrower than
|
|
13
|
+
* the panel divided by the bin count whenever the domain runs past the data. Taking the closest pair
|
|
14
|
+
* is what stops the widest bin from covering its neighbor.
|
|
15
|
+
*/
|
|
16
|
+
function pitchOf(data, x, band) {
|
|
17
|
+
const centers = data
|
|
18
|
+
.filter((d) => d.at != null)
|
|
19
|
+
.map((d) => x(d.at))
|
|
20
|
+
.sort((a, b) => a - b);
|
|
21
|
+
if (centers.length < 2)
|
|
22
|
+
return band;
|
|
23
|
+
let closest = Infinity;
|
|
24
|
+
for (let i = 1; i < centers.length; i++)
|
|
25
|
+
closest = Math.min(closest, centers[i] - centers[i - 1]);
|
|
26
|
+
return closest > 0 && Number.isFinite(closest) ? closest : band;
|
|
27
|
+
}
|
|
28
|
+
export function Bars({ data, x, y, band, baseline = 0, color = "var(--viz-ink)", maxWidth = BAR_CAP, slot, transposed = false, segmentGap = MARK_GAP, minSegment = 5, opacity = 1, stroke, lineWidth = 1, cap = "full", active, onHover, onSelect, }) {
|
|
29
|
+
/* The band's scale is whichever one the measure does not read, so the pitch comes off that one. */
|
|
30
|
+
const along = transposed ? y : x;
|
|
31
|
+
const whole = Math.max(1, Math.min(pitchOf(data, along, band) - MARK_GAP, maxWidth));
|
|
32
|
+
/*
|
|
33
|
+
The slot divides the geom's own width, so a dodge inside a narrow band stays inside the band.
|
|
34
|
+
|
|
35
|
+
`slot.width` is how much of the band the set takes, which is what aligns a dodged geom with a
|
|
36
|
+
wider one beside it. A datum's own `slotOf` and `slotAt` override the set's count and index, which
|
|
37
|
+
is what `preserve: "total"` writes: the band divides by what it holds and the widths follow the count.
|
|
38
|
+
*/
|
|
39
|
+
const spread = slot?.width == null ? whole : whole * slot.width;
|
|
40
|
+
const slots = (d) => d.slotOf ?? slot?.of ?? 1;
|
|
41
|
+
const widthOf = (d) => (slot ? Math.max(1, spread / slots(d) - MARK_GAP / 2) : whole);
|
|
42
|
+
const offsetOf = (d) => (slot ? ((d.slotAt ?? slot.index) - (slots(d) - 1) / 2) * (spread / slots(d)) : 0);
|
|
43
|
+
const measure = transposed ? x : y;
|
|
44
|
+
const zero = measure(baseline);
|
|
45
|
+
const hasActive = active != null;
|
|
46
|
+
/* The dots spread across the bar's own width and not the whole band, so they sit over the geom. */
|
|
47
|
+
const replicated = data.flatMap((d) => d.values?.length ? [{ category: d.category, values: d.values, band: widthOf(d), offset: offsetOf(d) }] : []);
|
|
48
|
+
return (_jsxs("g", { children: [data.map((d, index) => {
|
|
49
|
+
/*
|
|
50
|
+
A segment runs from its floor to its own top, and a bar with no floor runs from the ground.
|
|
51
|
+
|
|
52
|
+
The gap comes off the segment and it keeps its middle, so a column of segments stays the
|
|
53
|
+
height the readings add up to instead of drifting down by a gap a segment.
|
|
54
|
+
*/
|
|
55
|
+
const stacked = d.floor != null;
|
|
56
|
+
const foot = stacked ? measure(d.floor) : zero;
|
|
57
|
+
const top = measure(d.value + (d.floor ?? 0));
|
|
58
|
+
const reach = Math.abs(foot - top);
|
|
59
|
+
const length = stacked ? Math.max(minSegment, reach - segmentGap) : Math.max(1, reach);
|
|
60
|
+
const width = widthOf(d);
|
|
61
|
+
const cx = along(d.at ?? d.category) + offsetOf(d);
|
|
62
|
+
/* A quarter of the smaller side at most. A narrow bar or a short segment keeps its corners square. */
|
|
63
|
+
const radius = Math.min(MARK_RADIUS, width / 4, length / 4);
|
|
64
|
+
const dim = hasActive && d.category !== active;
|
|
65
|
+
/*
|
|
66
|
+
One box, sized by role and then handed to the screen the way round the frame drew.
|
|
67
|
+
|
|
68
|
+
`width` is the band's extent and `length` the measure's, so the swap is one expression and
|
|
69
|
+
not a second branch through the rounding, the gap and the muting below.
|
|
70
|
+
*/
|
|
71
|
+
const start = Math.min(foot, top) + (stacked ? (reach - length) / 2 : 0);
|
|
72
|
+
const box = transposed
|
|
73
|
+
? { x: start, y: cx - width / 2, w: length, h: width }
|
|
74
|
+
: { x: cx - width / 2, y: start, w: width, h: length };
|
|
75
|
+
const head = transposed ? (top >= foot ? "right" : "left") : top <= foot ? "top" : "bottom";
|
|
76
|
+
const hit = { datum: d, index, at: transposed ? { x: top, y: cx } : { x: cx, y: top } };
|
|
77
|
+
return (_jsx("g", { "data-geom": "bar", onMouseEnter: () => onHover?.(hit), onMouseLeave: () => onHover?.(null), onClick: onSelect ? () => onSelect(hit) : undefined, style: onSelect ? { cursor: "pointer" } : undefined, children: cap === "top" ? (
|
|
78
|
+
// Rounded head, square foot: the bar sits on the baseline instead of floating.
|
|
79
|
+
_jsx("path", { d: roundedHead(box, radius, head), fill: dim ? "var(--viz-rule)" : (d.color ?? color), fillOpacity: opacity, ...(stroke ? { stroke, strokeWidth: lineWidth } : {}) })) : (_jsx("rect", { x: box.x, y: box.y, width: box.w, height: box.h, rx: radius, fill: dim ? "var(--viz-rule)" : (d.color ?? color), fillOpacity: opacity, ...(stroke ? { stroke, strokeWidth: lineWidth } : {}) })) }, d.category));
|
|
80
|
+
}), data.some((d) => d.error != null) && (_jsx(ErrorBars, { data: data, x: x, y: y, transposed: transposed, offsetOf: offsetOf })), replicated.length > 0 && _jsx(ReplicateDots, { data: replicated, x: x, y: y, band: whole, transposed: transposed })] }));
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* A block on a baseline, rounded at its top and square at its foot.
|
|
84
|
+
*
|
|
85
|
+
* The radius clamps to a quarter of the width and of the height.
|
|
86
|
+
*/
|
|
87
|
+
export function roundedTop({ x, y, width: w, height: h, radius: r = MARK_RADIUS, }) {
|
|
88
|
+
const radius = Math.min(r, w / 4, h / 4);
|
|
89
|
+
return [
|
|
90
|
+
`M${x},${y + h}`,
|
|
91
|
+
`L${x},${y + radius}`,
|
|
92
|
+
`Q${x},${y} ${x + radius},${y}`,
|
|
93
|
+
`L${x + w - radius},${y}`,
|
|
94
|
+
`Q${x + w},${y} ${x + w},${y + radius}`,
|
|
95
|
+
`L${x + w},${y + h}`,
|
|
96
|
+
"Z",
|
|
97
|
+
].join("");
|
|
98
|
+
}
|
|
99
|
+
/** A block rounded at its head, the end away from the baseline, and square at its foot. */
|
|
100
|
+
function roundedHead(box, r, head) {
|
|
101
|
+
const { x, y, w, h } = box;
|
|
102
|
+
if (head === "top")
|
|
103
|
+
return roundedTop({ x, y, width: w, height: h, radius: r });
|
|
104
|
+
const radius = head === "bottom" ? Math.min(r, w / 2, h) : Math.min(r, h / 2, w);
|
|
105
|
+
const path = head === "bottom"
|
|
106
|
+
? [
|
|
107
|
+
`M${x},${y}`,
|
|
108
|
+
`L${x},${y + h - radius}`,
|
|
109
|
+
`Q${x},${y + h} ${x + radius},${y + h}`,
|
|
110
|
+
`L${x + w - radius},${y + h}`,
|
|
111
|
+
`Q${x + w},${y + h} ${x + w},${y + h - radius}`,
|
|
112
|
+
`L${x + w},${y}`,
|
|
113
|
+
]
|
|
114
|
+
: head === "right"
|
|
115
|
+
? [
|
|
116
|
+
`M${x},${y}`,
|
|
117
|
+
`L${x + w - radius},${y}`,
|
|
118
|
+
`Q${x + w},${y} ${x + w},${y + radius}`,
|
|
119
|
+
`L${x + w},${y + h - radius}`,
|
|
120
|
+
`Q${x + w},${y + h} ${x + w - radius},${y + h}`,
|
|
121
|
+
`L${x},${y + h}`,
|
|
122
|
+
]
|
|
123
|
+
: [
|
|
124
|
+
`M${x + w},${y}`,
|
|
125
|
+
`L${x + radius},${y}`,
|
|
126
|
+
`Q${x},${y} ${x},${y + radius}`,
|
|
127
|
+
`L${x},${y + h - radius}`,
|
|
128
|
+
`Q${x},${y + h} ${x + radius},${y + h}`,
|
|
129
|
+
`L${x + w},${y + h}`,
|
|
130
|
+
];
|
|
131
|
+
return [...path, "Z"].join("");
|
|
132
|
+
}
|
|
133
|
+
export function ErrorBars({ data, x, y, capWidth = 7, color = "var(--viz-ink)", transposed = false, offsetOf = () => 0, }) {
|
|
134
|
+
const along = transposed ? y : x;
|
|
135
|
+
const measure = transposed ? x : y;
|
|
136
|
+
return (_jsx("g", { stroke: color, strokeWidth: 1.1, strokeLinecap: "round", fill: "none", children: data.map((d) => {
|
|
137
|
+
if (d.error == null || d.error <= 0)
|
|
138
|
+
return null;
|
|
139
|
+
const band = along(d.at ?? d.category) + offsetOf(d);
|
|
140
|
+
const hi = measure(d.value + d.error);
|
|
141
|
+
const lo = measure(d.value - d.error);
|
|
142
|
+
const half = capWidth / 2;
|
|
143
|
+
/* The whisker runs along the measure and each cap crosses the band. */
|
|
144
|
+
const whisker = transposed ? { x1: hi, x2: lo, y1: band, y2: band } : { x1: band, x2: band, y1: hi, y2: lo };
|
|
145
|
+
const capAt = (at) => transposed
|
|
146
|
+
? { x1: at, x2: at, y1: band - half, y2: band + half }
|
|
147
|
+
: { x1: band - half, x2: band + half, y1: at, y2: at };
|
|
148
|
+
return (_jsxs("g", { children: [_jsx("line", { ...whisker }), _jsx("line", { ...capAt(hi) }), _jsx("line", { ...capAt(lo) })] }, d.category));
|
|
149
|
+
}) }));
|
|
150
|
+
}
|
|
151
|
+
export function ReplicateDots({ data, x, y, band, offset: shift = 0, color = "var(--viz-ink)", transposed = false, }) {
|
|
152
|
+
const along = transposed ? y : x;
|
|
153
|
+
const measure = transposed ? x : y;
|
|
154
|
+
return (_jsx("g", { children: data.map((group) => {
|
|
155
|
+
const jitter = Math.min((group.band ?? band) * 0.32, 11);
|
|
156
|
+
return group.values.map((value, i) => {
|
|
157
|
+
// Deterministic spread. The dots hold still across renders.
|
|
158
|
+
const offset = group.values.length === 1 ? 0 : (i / (group.values.length - 1) - 0.5) * 2 * jitter;
|
|
159
|
+
const across = along(group.category) + (group.offset ?? shift) + offset;
|
|
160
|
+
const at = measure(value);
|
|
161
|
+
return (_jsx("circle", { cx: transposed ? at : across, cy: transposed ? across : at, r: 1.9, fill: color, fillOpacity: 0.5 }, `${group.category}-${i}`));
|
|
162
|
+
});
|
|
163
|
+
}) }));
|
|
164
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Box and whisker, drawn as a thick colored block.
|
|
3
|
+
*
|
|
4
|
+
* The interquartile range renders as a thick colored block carrying the same corner radius as the
|
|
5
|
+
* bars, so the whole set reads as one system. Thick and short, because that is what a box plot
|
|
6
|
+
* claims: the middle half of the distribution lives between these two points.
|
|
7
|
+
*
|
|
8
|
+
* The radius stays a fixed three pixels. Rounding proportional to the geom turns a short box into a
|
|
9
|
+
* lozenge and a tall one into a capsule, so a series of boxes stops reading as one kind of geom.
|
|
10
|
+
*
|
|
11
|
+
* The remaining geoms stay thin against it. Whiskers are hairlines, the median is a light tick that
|
|
12
|
+
* spans the block, and outliers are open circles so a dense tail stays countable.
|
|
13
|
+
*/
|
|
14
|
+
import type { BoxDatum, GeomHit, Scale, ValueRamp } from "../../core/internal.js";
|
|
15
|
+
export type { BoxDatum };
|
|
16
|
+
export interface BoxProps {
|
|
17
|
+
data: BoxDatum[];
|
|
18
|
+
x: Scale;
|
|
19
|
+
y: Scale;
|
|
20
|
+
band: number;
|
|
21
|
+
/** Maps a group's median, in the units it carries, to a color. */
|
|
22
|
+
ramp: ValueRamp;
|
|
23
|
+
/**
|
|
24
|
+
* Thickness of the interquartile block.
|
|
25
|
+
*
|
|
26
|
+
* Unset, the block takes most of its slot and stops at thirteen pixels, which is the proportion
|
|
27
|
+
* that leaves air between boxes on a crowded axis. Set, it is the width the author asked for, and
|
|
28
|
+
* the slot is the only thing that still bounds it: a box wider than its slot overlaps its neighbor.
|
|
29
|
+
*/
|
|
30
|
+
thickness?: number;
|
|
31
|
+
/** Opacity of the interquartile block. */
|
|
32
|
+
opacity?: number;
|
|
33
|
+
/** Outline paint on the block, and its width. Absent draws none. */
|
|
34
|
+
stroke?: string;
|
|
35
|
+
lineWidth?: number;
|
|
36
|
+
/**
|
|
37
|
+
* Draws the summary across the frame instead of up it, the band running down the side.
|
|
38
|
+
*
|
|
39
|
+
* Read off the frame, since it refuses a transpose where the geoms read no shared pair. The five
|
|
40
|
+
* numbers land the same way round either way: the same walk with the pair swapped.
|
|
41
|
+
*/
|
|
42
|
+
transposed?: boolean;
|
|
43
|
+
showOutliers?: boolean;
|
|
44
|
+
showMedian?: boolean;
|
|
45
|
+
/** The box under the pointer, placed at its median. */
|
|
46
|
+
onHover?: (hit: GeomHit<BoxDatum> | null) => void;
|
|
47
|
+
/** A box clicked. The group it summarises is what a linked view filters on. */
|
|
48
|
+
onSelect?: (hit: GeomHit<BoxDatum>) => void;
|
|
49
|
+
}
|
|
50
|
+
export declare function Box({ data, x, y, band, ramp, thickness, opacity, stroke, lineWidth, transposed, showOutliers, showMedian, onHover, onSelect, }: BoxProps): import("react").JSX.Element;
|