@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,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An author's order resolved onto a band.
|
|
3
|
+
*
|
|
4
|
+
* The permutation makes a clustered heatmap's blocks visible. The matrix arrives as a thunk and
|
|
5
|
+
* most bands resolve without the cells.
|
|
6
|
+
*/
|
|
7
|
+
import { type Dendrogram, type HeatMatrix } from "./cluster.js";
|
|
8
|
+
import type { HeatCell } from "./geom-types.js";
|
|
9
|
+
import type { BandOrder } from "./spec.js";
|
|
10
|
+
/**
|
|
11
|
+
* Rows a clustered band draws.
|
|
12
|
+
*
|
|
13
|
+
* At four pixels a row, 256 rows is a figure a thousand pixels tall. `kmeans` is the way past
|
|
14
|
+
* that.
|
|
15
|
+
*/
|
|
16
|
+
export declare const CLUSTER_CAP = 256;
|
|
17
|
+
/**
|
|
18
|
+
* A tree and the labels its leaf ids point at.
|
|
19
|
+
*
|
|
20
|
+
* A leaf id indexes the labels the clustering saw, which is the build order.
|
|
21
|
+
*/
|
|
22
|
+
export interface ClusterTree {
|
|
23
|
+
tree: Dendrogram;
|
|
24
|
+
labels: string[];
|
|
25
|
+
}
|
|
26
|
+
/** What the band draws, and what the cap refused on the way. */
|
|
27
|
+
export interface OrderedBand {
|
|
28
|
+
values: string[];
|
|
29
|
+
/** Break positions, each the index in `values` the break falls before. */
|
|
30
|
+
gaps: number[];
|
|
31
|
+
/**
|
|
32
|
+
* The tree the order came out of, where one was built.
|
|
33
|
+
*
|
|
34
|
+
* A dendrogram is a picture of these merges. Absent on a stated list or a refused cap.
|
|
35
|
+
*/
|
|
36
|
+
clustered?: ClusterTree;
|
|
37
|
+
/** Set where a cluster order was asked for and the cap refused it. */
|
|
38
|
+
refused?: {
|
|
39
|
+
rows: number;
|
|
40
|
+
cap: number;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Set where `kmeans` replaced the rows with centroids.
|
|
44
|
+
*
|
|
45
|
+
* The caller rewrites the cells to match. The band names centroids the table has no rows for.
|
|
46
|
+
*/
|
|
47
|
+
reduced?: {
|
|
48
|
+
of: string;
|
|
49
|
+
to: string;
|
|
50
|
+
}[];
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The order the band takes, with its breaks.
|
|
54
|
+
*
|
|
55
|
+
* `values` is the build order and the result permutes it. A stated list naming a category the
|
|
56
|
+
* data lacks skips that name.
|
|
57
|
+
*/
|
|
58
|
+
export declare function orderBand(order: BandOrder | undefined, values: string[], matrixOf: () => HeatMatrix | null): OrderedBand;
|
|
59
|
+
/**
|
|
60
|
+
* Bands an order leaves the axis with.
|
|
61
|
+
*
|
|
62
|
+
* A kmeans order names its centroids. Eight centroids over four hundred rows is eight bands.
|
|
63
|
+
*/
|
|
64
|
+
export declare const bandsUnder: (order: BandOrder | undefined, count: number) => number;
|
|
65
|
+
/**
|
|
66
|
+
* Breaks an order asks for, answerable without building anything.
|
|
67
|
+
*
|
|
68
|
+
* The content-fit sum runs before the frame exists. k clusters make k-1 boundaries. A refused
|
|
69
|
+
* band asks for no room.
|
|
70
|
+
*/
|
|
71
|
+
export declare function breakCount(order: BandOrder | undefined, bands: number): number;
|
|
72
|
+
/**
|
|
73
|
+
* The cells rewritten onto a kmeans order's centroids.
|
|
74
|
+
*
|
|
75
|
+
* The band names clusters the table has no rows for and a cell is the cluster mean.
|
|
76
|
+
*/
|
|
77
|
+
export declare function reducedCells(cells: HeatCell[], reduced: {
|
|
78
|
+
of: string;
|
|
79
|
+
to: string;
|
|
80
|
+
}[], along: "y" | "x"): HeatCell[];
|
|
81
|
+
/**
|
|
82
|
+
* The matrix a band clusters over, built from the traces' own cells.
|
|
83
|
+
*
|
|
84
|
+
* One matrix per axis. The caller states both label orders.
|
|
85
|
+
*/
|
|
86
|
+
export declare function matrixFor(cells: HeatCell[], rows: string[], columns: string[]): HeatMatrix | null;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An author's order resolved onto a band.
|
|
3
|
+
*
|
|
4
|
+
* The permutation makes a clustered heatmap's blocks visible. The matrix arrives as a thunk and
|
|
5
|
+
* most bands resolve without the cells.
|
|
6
|
+
*/
|
|
7
|
+
import { cluster, condensedDistance, cutBoundaries, heatMatrix, kmeansRows, leafOrder, } from "./cluster.js";
|
|
8
|
+
/**
|
|
9
|
+
* Rows a clustered band draws.
|
|
10
|
+
*
|
|
11
|
+
* At four pixels a row, 256 rows is a figure a thousand pixels tall. `kmeans` is the way past
|
|
12
|
+
* that.
|
|
13
|
+
*/
|
|
14
|
+
export const CLUSTER_CAP = 256;
|
|
15
|
+
const held = (values, within) => values.filter((value) => within.has(value));
|
|
16
|
+
/**
|
|
17
|
+
* The order the band takes, with its breaks.
|
|
18
|
+
*
|
|
19
|
+
* `values` is the build order and the result permutes it. A stated list naming a category the
|
|
20
|
+
* data lacks skips that name.
|
|
21
|
+
*/
|
|
22
|
+
export function orderBand(order, values, matrixOf) {
|
|
23
|
+
if (!order)
|
|
24
|
+
return { values, gaps: [] };
|
|
25
|
+
if (order.kind === "list") {
|
|
26
|
+
const asked = new Set(values);
|
|
27
|
+
/* A name written twice takes one slot. A second slot would draw the band's row twice. */
|
|
28
|
+
const taken = held([...new Set(order.values)], asked);
|
|
29
|
+
const seen = new Set(taken);
|
|
30
|
+
/* A category the list left out keeps its slot at the end. A partial list is still an order. */
|
|
31
|
+
const out = [...taken, ...values.filter((value) => !seen.has(value))];
|
|
32
|
+
return {
|
|
33
|
+
values: out,
|
|
34
|
+
/* A break past the last slot draws at no boundary. One at zero draws outside the band. */
|
|
35
|
+
gaps: [...new Set(order.gaps ?? [])].filter((at) => at > 0 && at < out.length).sort((a, b) => a - b),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
if (values.length < 2)
|
|
39
|
+
return { values, gaps: [] };
|
|
40
|
+
if (values.length > CLUSTER_CAP && !order.kmeans) {
|
|
41
|
+
return { values, gaps: [], refused: { rows: values.length, cap: CLUSTER_CAP } };
|
|
42
|
+
}
|
|
43
|
+
const matrix = matrixOf();
|
|
44
|
+
if (!matrix || matrix.rows.length < 2 || matrix.columns.length === 0)
|
|
45
|
+
return { values, gaps: [] };
|
|
46
|
+
const reduced = order.kmeans ? kmeansRows(matrix, order.kmeans) : null;
|
|
47
|
+
const clustered = reduced ? reduced.centroids : matrix;
|
|
48
|
+
if (clustered.rows.length > CLUSTER_CAP) {
|
|
49
|
+
return { values, gaps: [], refused: { rows: clustered.rows.length, cap: CLUSTER_CAP } };
|
|
50
|
+
}
|
|
51
|
+
const distances = condensedDistance(clustered, order.distance ?? "euclidean");
|
|
52
|
+
const tree = cluster(distances, clustered.rows.length, order.method ?? "complete");
|
|
53
|
+
return {
|
|
54
|
+
values: leafOrder(tree, clustered.rows),
|
|
55
|
+
gaps: order.cut ? cutBoundaries(tree, clustered.rows.length, order.cut) : [],
|
|
56
|
+
clustered: { tree, labels: clustered.rows },
|
|
57
|
+
reduced: reduced
|
|
58
|
+
? matrix.rows.map((row, at) => ({ of: row, to: reduced.centroids.rows[reduced.assignment[at]] }))
|
|
59
|
+
: undefined,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Bands an order leaves the axis with.
|
|
64
|
+
*
|
|
65
|
+
* A kmeans order names its centroids. Eight centroids over four hundred rows is eight bands.
|
|
66
|
+
*/
|
|
67
|
+
export const bandsUnder = (order, count) => order?.kind === "cluster" && order.kmeans ? Math.min(order.kmeans, count) : count;
|
|
68
|
+
/**
|
|
69
|
+
* Breaks an order asks for, answerable without building anything.
|
|
70
|
+
*
|
|
71
|
+
* The content-fit sum runs before the frame exists. k clusters make k-1 boundaries. A refused
|
|
72
|
+
* band asks for no room.
|
|
73
|
+
*/
|
|
74
|
+
export function breakCount(order, bands) {
|
|
75
|
+
if (!order)
|
|
76
|
+
return 0;
|
|
77
|
+
if (order.kind === "list") {
|
|
78
|
+
return [...new Set(order.gaps ?? [])].filter((at) => at > 0 && at < bands).length;
|
|
79
|
+
}
|
|
80
|
+
/* A kmeans order clusters its centroids and the cap applies to those. `orderBand` reads it
|
|
81
|
+
the same way. The count comes out of `bandsUnder` here too. */
|
|
82
|
+
const under = bandsUnder(order, bands);
|
|
83
|
+
if (under > CLUSTER_CAP)
|
|
84
|
+
return 0;
|
|
85
|
+
return order.cut ? Math.max(0, Math.min(order.cut, under) - 1) : 0;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The cells rewritten onto a kmeans order's centroids.
|
|
89
|
+
*
|
|
90
|
+
* The band names clusters the table has no rows for and a cell is the cluster mean.
|
|
91
|
+
*/
|
|
92
|
+
export function reducedCells(cells, reduced, along) {
|
|
93
|
+
const to = new Map(reduced.map((entry) => [entry.of, entry.to]));
|
|
94
|
+
/*
|
|
95
|
+
Nested, the way `gateCells` does it. A category is free text and any separator is a character
|
|
96
|
+
a cluster name can hold.
|
|
97
|
+
*/
|
|
98
|
+
const running = new Map();
|
|
99
|
+
for (const cell of cells) {
|
|
100
|
+
const moved = to.get(cell[along]);
|
|
101
|
+
if (moved == null)
|
|
102
|
+
continue;
|
|
103
|
+
const x = along === "y" ? cell.x : moved;
|
|
104
|
+
const y = along === "y" ? moved : cell.y;
|
|
105
|
+
let row = running.get(y);
|
|
106
|
+
if (!row) {
|
|
107
|
+
row = new Map();
|
|
108
|
+
running.set(y, row);
|
|
109
|
+
}
|
|
110
|
+
const entry = row.get(x) ?? { sum: 0, count: 0, n: 0 };
|
|
111
|
+
entry.sum += cell.value;
|
|
112
|
+
entry.count += 1;
|
|
113
|
+
entry.n += cell.n;
|
|
114
|
+
row.set(x, entry);
|
|
115
|
+
}
|
|
116
|
+
const out = [];
|
|
117
|
+
for (const [y, row] of running) {
|
|
118
|
+
for (const [x, entry] of row)
|
|
119
|
+
out.push({ x, y, value: entry.sum / entry.count, n: entry.n });
|
|
120
|
+
}
|
|
121
|
+
return out;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* The matrix a band clusters over, built from the traces' own cells.
|
|
125
|
+
*
|
|
126
|
+
* One matrix per axis. The caller states both label orders.
|
|
127
|
+
*/
|
|
128
|
+
export function matrixFor(cells, rows, columns) {
|
|
129
|
+
if (cells.length === 0 || rows.length === 0 || columns.length === 0)
|
|
130
|
+
return null;
|
|
131
|
+
return heatMatrix(cells, rows, columns);
|
|
132
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a binning puts its edges.
|
|
3
|
+
*
|
|
4
|
+
* A width or a count sets the resolution. An edge or a center aligns them. Ported from ggplot's
|
|
5
|
+
* `stat_bin`.
|
|
6
|
+
*/
|
|
7
|
+
/** Which end of an interval takes a reading that lands on its edge. */
|
|
8
|
+
export type BinClosed = "right" | "left";
|
|
9
|
+
/**
|
|
10
|
+
* Empty intervals a binning keeps.
|
|
11
|
+
*
|
|
12
|
+
* `none` keeps them all. `all` keeps the intervals with readings. `extremes` keeps the interior
|
|
13
|
+
* and cuts the empty runs off both ends.
|
|
14
|
+
*/
|
|
15
|
+
export type BinDrop = "none" | "all" | "extremes";
|
|
16
|
+
/** A number per axis, or one number for both. ggplot's `dual_param`. */
|
|
17
|
+
export type PerAxis = number | readonly [number, number];
|
|
18
|
+
/**
|
|
19
|
+
* What a one-dimensional binning was told. ggplot's `stat_bin` arguments.
|
|
20
|
+
*
|
|
21
|
+
* The four numeric parameters take a pair as well as a number, and a bin reads the first entry.
|
|
22
|
+
*/
|
|
23
|
+
export interface BinOptions {
|
|
24
|
+
/** Width of one interval, in the data's own units. */
|
|
25
|
+
binwidth?: PerAxis;
|
|
26
|
+
/** Intervals across the range. Read where no width is stated. */
|
|
27
|
+
bins?: PerAxis;
|
|
28
|
+
/** Center of one interval, which fixes where all of them fall. */
|
|
29
|
+
center?: PerAxis;
|
|
30
|
+
/** Edge of one interval, which fixes where all of them fall. */
|
|
31
|
+
boundary?: PerAxis;
|
|
32
|
+
/** Which end of an interval is inclusive. The right end unless stated. */
|
|
33
|
+
closed?: BinClosed;
|
|
34
|
+
/** Adds an empty interval at each end. A filled shape closes on the axis. */
|
|
35
|
+
pad?: boolean;
|
|
36
|
+
/** The edges themselves, which override the width, the count and the alignment. */
|
|
37
|
+
breaks?: readonly number[];
|
|
38
|
+
/** Which empty intervals survive. All of them unless stated. ggplot's `drop`. */
|
|
39
|
+
dropEmpty?: BinDrop;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* What a two-dimensional binning was told. ggplot's `stat_bin_2d` and `stat_bin_hex`.
|
|
43
|
+
*
|
|
44
|
+
* The lattice parameters take a number per axis. The hexagonal tiling reads width and count
|
|
45
|
+
* alone. `breaks` is refused here.
|
|
46
|
+
*/
|
|
47
|
+
export interface Bin2dOptions {
|
|
48
|
+
/** Width of one cell, in the data's own units. */
|
|
49
|
+
binwidth?: PerAxis;
|
|
50
|
+
/** Cells across each axis. Thirty unless stated. */
|
|
51
|
+
bins?: PerAxis;
|
|
52
|
+
center?: PerAxis;
|
|
53
|
+
boundary?: PerAxis;
|
|
54
|
+
closed?: BinClosed;
|
|
55
|
+
/** Keeps a cell nothing landed in and draws the lattice whole. Occupied cells alone unless stated. */
|
|
56
|
+
drop?: boolean;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* A one-dimensional binning as a caller states it.
|
|
60
|
+
*
|
|
61
|
+
* A bare number states the width. `auto` reads a width off the data. The object form is the whole
|
|
62
|
+
* parameter set.
|
|
63
|
+
*/
|
|
64
|
+
export type BinSpec = "auto" | number | BinOptions;
|
|
65
|
+
/** Edges of a binning, with the copy the cut runs against. */
|
|
66
|
+
export interface BinEdges {
|
|
67
|
+
/** The edges themselves, which the reported center and width come off. */
|
|
68
|
+
breaks: number[];
|
|
69
|
+
/**
|
|
70
|
+
* The same edges nudged outward at the closed end.
|
|
71
|
+
*
|
|
72
|
+
* A reading recorded as 45 lands a few ulps off an edge at 45. The nudge matches R's `hist`.
|
|
73
|
+
*/
|
|
74
|
+
fuzzy: number[];
|
|
75
|
+
/** Whether the right end of an interval is the inclusive one. */
|
|
76
|
+
rightClosed: boolean;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Edges for a range, given what the author stated.
|
|
80
|
+
*
|
|
81
|
+
* `null` where the options contradict. Stated edges win, then a width, then a count, and thirty
|
|
82
|
+
* where nothing was said.
|
|
83
|
+
*/
|
|
84
|
+
export declare function binBreaks(range: readonly [number, number], options?: BinOptions): BinEdges | null;
|
|
85
|
+
/**
|
|
86
|
+
* Which interval a reading lands in, or `-1` where it lands outside them all.
|
|
87
|
+
*
|
|
88
|
+
* Read against the fuzzed edges. A reading on an edge lands on the side `closed` names.
|
|
89
|
+
*/
|
|
90
|
+
export declare function binIndex(value: number, edges: BinEdges): number;
|
|
91
|
+
/** One interval of a binning, with what landed in it. */
|
|
92
|
+
export interface BinInterval {
|
|
93
|
+
from: number;
|
|
94
|
+
to: number;
|
|
95
|
+
/** Middle of the interval, off the stated edges. */
|
|
96
|
+
at: number;
|
|
97
|
+
width: number;
|
|
98
|
+
count: number;
|
|
99
|
+
}
|
|
100
|
+
/** The intervals a set of edges describes, in order, before any reading is counted into them. */
|
|
101
|
+
export declare function intervalsOf(edges: BinEdges): BinInterval[];
|
|
102
|
+
/**
|
|
103
|
+
* The same intervals with an empty one at each end. ggplot's `pad`.
|
|
104
|
+
*
|
|
105
|
+
* The added interval takes the width of the one beside it.
|
|
106
|
+
*/
|
|
107
|
+
export declare function padded(intervals: readonly BinInterval[]): BinInterval[];
|
|
108
|
+
/** The entries one of the three drops keeps, read off what each contains. */
|
|
109
|
+
export declare function keptUnder<T>(entries: readonly T[], holds: (of: T) => boolean, drop: BinDrop): T[];
|
|
110
|
+
/**
|
|
111
|
+
* The options a stated binning amounts to.
|
|
112
|
+
*
|
|
113
|
+
* A bare number states the width. `auto` reads a width off the data, and an options object naming
|
|
114
|
+
* no resolution keeps that reading.
|
|
115
|
+
*/
|
|
116
|
+
export declare function binOptionsOf(bin: BinSpec | undefined, autoWidth: () => number): BinOptions;
|
|
117
|
+
/**
|
|
118
|
+
* Edges for one axis of a lattice.
|
|
119
|
+
*
|
|
120
|
+
* Zero is the edge a lattice aligns on, where a one-dimensional bin takes half a width.
|
|
121
|
+
* `stat_bin_2d` does the same.
|
|
122
|
+
*/
|
|
123
|
+
export declare function latticeEdges(range: readonly [number, number], options: Bin2dOptions, axis: 0 | 1): BinEdges | null;
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a binning puts its edges.
|
|
3
|
+
*
|
|
4
|
+
* A width or a count sets the resolution. An edge or a center aligns them. Ported from ggplot's
|
|
5
|
+
* `stat_bin`.
|
|
6
|
+
*/
|
|
7
|
+
/** Intervals ggplot's default takes where nothing is stated. */
|
|
8
|
+
const DEFAULT_BINS = 30;
|
|
9
|
+
/** R's modulo. The result takes the sign of the divisor. */
|
|
10
|
+
const mod = (value, by) => value - Math.floor(value / by) * by;
|
|
11
|
+
/** One entry of a per-axis parameter, which ggplot spells `dual_param`. */
|
|
12
|
+
const perAxis = (stated, axis) => stated == null ? undefined : typeof stated === "number" ? stated : stated[axis];
|
|
13
|
+
/** Whether a range is one point, or near enough to it that a width off it means nothing. */
|
|
14
|
+
const zeroRange = ([lo, hi]) => {
|
|
15
|
+
if (lo === hi)
|
|
16
|
+
return true;
|
|
17
|
+
const smallest = Math.min(Math.abs(lo), Math.abs(hi));
|
|
18
|
+
if (smallest === 0)
|
|
19
|
+
return false;
|
|
20
|
+
return Math.abs((lo - hi) / smallest) < 1000 * Number.EPSILON;
|
|
21
|
+
};
|
|
22
|
+
const median = (values) => {
|
|
23
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
24
|
+
if (sorted.length === 0)
|
|
25
|
+
return NaN;
|
|
26
|
+
const middle = sorted.length / 2;
|
|
27
|
+
return sorted.length % 2 === 1 ? sorted[Math.floor(middle)] : (sorted[middle - 1] + sorted[middle]) / 2;
|
|
28
|
+
};
|
|
29
|
+
/** Sorted edges with the fuzz applied. A cut reads this shape. */
|
|
30
|
+
function edgesOf(stated, closed = "right") {
|
|
31
|
+
const breaks = [...stated].sort((a, b) => a - b);
|
|
32
|
+
const widths = breaks.slice(1).map((edge, at) => edge - breaks[at]);
|
|
33
|
+
const middle = median(widths.filter((width) => Number.isFinite(width)));
|
|
34
|
+
const fuzz = Number.isFinite(middle) && middle !== 0 ? 1e-8 * middle : Number.EPSILON * 1000;
|
|
35
|
+
const last = breaks.length - 1;
|
|
36
|
+
/* The closed end moves outward and a reading on it stays inside. The other end stays put. */
|
|
37
|
+
const fuzzy = breaks.map((edge, at) => closed === "right" ? edge + (at === 0 ? -fuzz : fuzz) : edge + (at === last ? fuzz : -fuzz));
|
|
38
|
+
return { breaks, fuzzy, rightClosed: closed === "right" };
|
|
39
|
+
}
|
|
40
|
+
/** Edges a stated width puts across a range, aligned on an edge or on a center. */
|
|
41
|
+
function widthBreaks(range, width, center, boundary, closed) {
|
|
42
|
+
if (!(width > 0) || !Number.isFinite(width))
|
|
43
|
+
return null;
|
|
44
|
+
/* Half a width. The ends of the data land in the outer half of their own intervals. */
|
|
45
|
+
const edge = boundary ?? (center == null ? width / 2 : center - width / 2);
|
|
46
|
+
const origin = edge + Math.floor((range[0] - edge) / width) * width;
|
|
47
|
+
// A hair under a full width. A reading on the last edge adds no interval past it.
|
|
48
|
+
const reach = range[1] + (1 - 1e-8) * width;
|
|
49
|
+
const steps = Math.floor((reach - origin) / width + 1e-10);
|
|
50
|
+
if (!Number.isFinite(steps) || steps > 1e6)
|
|
51
|
+
return null;
|
|
52
|
+
const breaks = Array.from({ length: Math.max(1, steps) + 1 }, (_, at) => origin + at * width);
|
|
53
|
+
return edgesOf(breaks, closed);
|
|
54
|
+
}
|
|
55
|
+
/** Edges a stated count of intervals puts across a range. */
|
|
56
|
+
function countBreaks(range, bins, center, boundary, closed) {
|
|
57
|
+
if (!Number.isInteger(bins) || bins < 1)
|
|
58
|
+
return null;
|
|
59
|
+
if (zeroRange(range))
|
|
60
|
+
return widthBreaks(range, 0.1, center, boundary, closed);
|
|
61
|
+
if (bins === 1)
|
|
62
|
+
return widthBreaks(range, range[1] - range[0], undefined, range[0], closed);
|
|
63
|
+
let width = (range[1] - range[0]) / (bins - 1);
|
|
64
|
+
let edge = boundary;
|
|
65
|
+
if (center == null)
|
|
66
|
+
edge ??= range[0] - width / 2;
|
|
67
|
+
/*
|
|
68
|
+
An end of the data on an edge would leave the last interval empty. The width closes up to
|
|
69
|
+
divide the range evenly. ggplot's own correction.
|
|
70
|
+
*/
|
|
71
|
+
if (edge != null && range.some((end) => mod(end, width) === mod(edge, width))) {
|
|
72
|
+
width = (range[1] - range[0]) / bins;
|
|
73
|
+
}
|
|
74
|
+
return widthBreaks(range, width, center, edge, closed);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Edges for a range, given what the author stated.
|
|
78
|
+
*
|
|
79
|
+
* `null` where the options contradict. Stated edges win, then a width, then a count, and thirty
|
|
80
|
+
* where nothing was said.
|
|
81
|
+
*/
|
|
82
|
+
export function binBreaks(range, options = {}) {
|
|
83
|
+
const { breaks, closed } = options;
|
|
84
|
+
const binwidth = perAxis(options.binwidth, 0);
|
|
85
|
+
const bins = perAxis(options.bins, 0);
|
|
86
|
+
const center = perAxis(options.center, 0);
|
|
87
|
+
const boundary = perAxis(options.boundary, 0);
|
|
88
|
+
if (breaks)
|
|
89
|
+
return breaks.length > 1 ? edgesOf(breaks, closed) : null;
|
|
90
|
+
if (center != null && boundary != null)
|
|
91
|
+
return null;
|
|
92
|
+
if (binwidth != null)
|
|
93
|
+
return widthBreaks(range, binwidth, center, boundary, closed);
|
|
94
|
+
return countBreaks(range, bins ?? DEFAULT_BINS, center, boundary, closed);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Which interval a reading lands in, or `-1` where it lands outside them all.
|
|
98
|
+
*
|
|
99
|
+
* Read against the fuzzed edges. A reading on an edge lands on the side `closed` names.
|
|
100
|
+
*/
|
|
101
|
+
export function binIndex(value, edges) {
|
|
102
|
+
const { fuzzy, rightClosed } = edges;
|
|
103
|
+
const last = fuzzy.length - 1;
|
|
104
|
+
if (last < 1 || value < fuzzy[0] || value > fuzzy[last])
|
|
105
|
+
return -1;
|
|
106
|
+
/* Binary search. A quarter of a million readings walk this once each. */
|
|
107
|
+
let low = 0;
|
|
108
|
+
let high = last;
|
|
109
|
+
while (high - low > 1) {
|
|
110
|
+
const middle = (low + high) >> 1;
|
|
111
|
+
if (value < fuzzy[middle])
|
|
112
|
+
high = middle;
|
|
113
|
+
else
|
|
114
|
+
low = middle;
|
|
115
|
+
}
|
|
116
|
+
/* A reading on an interior edge goes to the interval the closed end points at. */
|
|
117
|
+
if (rightClosed && value === fuzzy[low] && low > 0)
|
|
118
|
+
return low - 1;
|
|
119
|
+
if (!rightClosed && value === fuzzy[high] && high < last)
|
|
120
|
+
return high;
|
|
121
|
+
return Math.min(low, last - 1);
|
|
122
|
+
}
|
|
123
|
+
/** The intervals a set of edges describes, in order, before any reading is counted into them. */
|
|
124
|
+
export function intervalsOf(edges) {
|
|
125
|
+
return edges.breaks.slice(1).map((to, at) => {
|
|
126
|
+
const from = edges.breaks[at];
|
|
127
|
+
return { from, to, at: (from + to) / 2, width: to - from, count: 0 };
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* The same intervals with an empty one at each end. ggplot's `pad`.
|
|
132
|
+
*
|
|
133
|
+
* The added interval takes the width of the one beside it.
|
|
134
|
+
*/
|
|
135
|
+
export function padded(intervals) {
|
|
136
|
+
const first = intervals[0];
|
|
137
|
+
const last = intervals.at(-1);
|
|
138
|
+
if (!first || !last)
|
|
139
|
+
return [...intervals];
|
|
140
|
+
return [
|
|
141
|
+
{ from: first.from - first.width, to: first.from, at: first.at - first.width, width: first.width, count: 0 },
|
|
142
|
+
...intervals,
|
|
143
|
+
{ from: last.to, to: last.to + last.width, at: last.at + last.width, width: last.width, count: 0 },
|
|
144
|
+
];
|
|
145
|
+
}
|
|
146
|
+
/** The entries one of the three drops keeps, read off what each contains. */
|
|
147
|
+
export function keptUnder(entries, holds, drop) {
|
|
148
|
+
if (drop === "all")
|
|
149
|
+
return entries.filter(holds);
|
|
150
|
+
if (drop !== "extremes")
|
|
151
|
+
return [...entries];
|
|
152
|
+
const first = entries.findIndex(holds);
|
|
153
|
+
if (first < 0)
|
|
154
|
+
return [];
|
|
155
|
+
let last = entries.length - 1;
|
|
156
|
+
while (last > first && !holds(entries[last]))
|
|
157
|
+
last -= 1;
|
|
158
|
+
return entries.slice(first, last + 1);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* The options a stated binning amounts to.
|
|
162
|
+
*
|
|
163
|
+
* A bare number states the width. `auto` reads a width off the data, and an options object naming
|
|
164
|
+
* no resolution keeps that reading.
|
|
165
|
+
*/
|
|
166
|
+
export function binOptionsOf(bin, autoWidth) {
|
|
167
|
+
if (bin == null || bin === "auto")
|
|
168
|
+
return { binwidth: autoWidth() };
|
|
169
|
+
if (typeof bin === "number")
|
|
170
|
+
return { binwidth: bin };
|
|
171
|
+
if (bin.binwidth != null || bin.bins != null || bin.breaks != null)
|
|
172
|
+
return bin;
|
|
173
|
+
return { ...bin, binwidth: autoWidth() };
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Edges for one axis of a lattice.
|
|
177
|
+
*
|
|
178
|
+
* Zero is the edge a lattice aligns on, where a one-dimensional bin takes half a width.
|
|
179
|
+
* `stat_bin_2d` does the same.
|
|
180
|
+
*/
|
|
181
|
+
export function latticeEdges(range, options, axis) {
|
|
182
|
+
const binwidth = perAxis(options.binwidth, axis);
|
|
183
|
+
const bins = perAxis(options.bins, axis);
|
|
184
|
+
const center = perAxis(options.center, axis);
|
|
185
|
+
const boundary = perAxis(options.boundary, axis);
|
|
186
|
+
const aligned = center == null && boundary == null ? { boundary: 0 } : { center, boundary };
|
|
187
|
+
return binBreaks(range, {
|
|
188
|
+
...aligned,
|
|
189
|
+
...(binwidth != null ? { binwidth } : {}),
|
|
190
|
+
...(binwidth == null ? { bins: bins ?? DEFAULT_BINS } : {}),
|
|
191
|
+
closed: options.closed,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a position scale can place.
|
|
3
|
+
*
|
|
4
|
+
* A transform refuses a reading outside its reach: at or below zero on a log axis. A stated range
|
|
5
|
+
* censors a reading past it.
|
|
6
|
+
*/
|
|
7
|
+
import type { Trace } from "./layers.js";
|
|
8
|
+
import { type AxisTransform } from "./transform.js";
|
|
9
|
+
/** What a reading past a stated range becomes. ggplot's `oob`. */
|
|
10
|
+
export type OutOfBounds = "censor" | "squish" | "keep";
|
|
11
|
+
/** One axis's rules for the positions read against it. */
|
|
12
|
+
export interface Bounds {
|
|
13
|
+
transform: AxisTransform;
|
|
14
|
+
/** The range the author stated. Absent places each reading the transform allows. */
|
|
15
|
+
range?: readonly [number, number];
|
|
16
|
+
oob: OutOfBounds;
|
|
17
|
+
}
|
|
18
|
+
/** The traces as placed, with a count per reason a reading dropped. */
|
|
19
|
+
export interface Placed {
|
|
20
|
+
traces: Trace[];
|
|
21
|
+
/** Readings a log axis had no place for. */
|
|
22
|
+
offLog: number;
|
|
23
|
+
/** Readings below zero on a square-root axis. */
|
|
24
|
+
offRoot: number;
|
|
25
|
+
/** Readings a censoring range dropped. */
|
|
26
|
+
offRange: number;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The traces with each reading the axes cannot place taken out.
|
|
30
|
+
*
|
|
31
|
+
* Positions are checked. An interval's bounds and a bar's length are extents, and the plot area
|
|
32
|
+
* clips those.
|
|
33
|
+
*/
|
|
34
|
+
export declare function placeTraces(traces: Trace[], x: Bounds | null, y: Bounds | null, y2: Bounds | null): Placed;
|
|
35
|
+
/**
|
|
36
|
+
* Intervals and whiskers reaching past where the transform places, at or below zero on a log axis.
|
|
37
|
+
*
|
|
38
|
+
* Each draws to the axis floor. The footer counts them.
|
|
39
|
+
*/
|
|
40
|
+
export declare function flooredCount(traces: Trace[], transform: AxisTransform): number;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a position scale can place.
|
|
3
|
+
*
|
|
4
|
+
* A transform refuses a reading outside its reach: at or below zero on a log axis. A stated range
|
|
5
|
+
* censors a reading past it.
|
|
6
|
+
*/
|
|
7
|
+
import { isLog, TRANSFORMS } from "./transform.js";
|
|
8
|
+
/** Which verdict a reading the transform refuses takes. */
|
|
9
|
+
const refusal = (bounds) => (isLog(bounds.transform) ? "log" : "root");
|
|
10
|
+
/** Where one position falls against one axis. A null axis or a category places everything. */
|
|
11
|
+
function verdict(value, bounds) {
|
|
12
|
+
if (bounds == null || typeof value !== "number")
|
|
13
|
+
return "in";
|
|
14
|
+
if (!TRANSFORMS[bounds.transform].admits(value))
|
|
15
|
+
return refusal(bounds);
|
|
16
|
+
const range = bounds.range;
|
|
17
|
+
if (range &&
|
|
18
|
+
bounds.oob === "censor" &&
|
|
19
|
+
(value < Math.min(range[0], range[1]) || value > Math.max(range[0], range[1])))
|
|
20
|
+
return "range";
|
|
21
|
+
return "in";
|
|
22
|
+
}
|
|
23
|
+
/** The first reason a reading drops, over each position it names. The transform's answer leads. */
|
|
24
|
+
function worst(...verdicts) {
|
|
25
|
+
if (verdicts.includes("log"))
|
|
26
|
+
return "log";
|
|
27
|
+
if (verdicts.includes("root"))
|
|
28
|
+
return "root";
|
|
29
|
+
if (verdicts.includes("range"))
|
|
30
|
+
return "range";
|
|
31
|
+
return "in";
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The traces with each reading the axes cannot place taken out.
|
|
35
|
+
*
|
|
36
|
+
* Positions are checked. An interval's bounds and a bar's length are extents, and the plot area
|
|
37
|
+
* clips those.
|
|
38
|
+
*/
|
|
39
|
+
export function placeTraces(traces, x, y, y2) {
|
|
40
|
+
let offLog = 0;
|
|
41
|
+
let offRoot = 0;
|
|
42
|
+
let offRange = 0;
|
|
43
|
+
const kept = (at) => {
|
|
44
|
+
if (at === "log")
|
|
45
|
+
offLog += 1;
|
|
46
|
+
if (at === "root")
|
|
47
|
+
offRoot += 1;
|
|
48
|
+
if (at === "range")
|
|
49
|
+
offRange += 1;
|
|
50
|
+
return at === "in";
|
|
51
|
+
};
|
|
52
|
+
const placed = traces.map((trace) => {
|
|
53
|
+
const data = trace.data;
|
|
54
|
+
if (data.at !== "records")
|
|
55
|
+
return trace;
|
|
56
|
+
const vertical = trace.axis === "y2" ? y2 : y;
|
|
57
|
+
if (data.geom === "segment") {
|
|
58
|
+
const segments = data.segments.filter((one) => kept(worst(verdict(one.x, x), verdict(one.xEnd, x), verdict(one.y, vertical), verdict(one.yEnd, vertical))));
|
|
59
|
+
return segments.length === data.segments.length ? trace : { ...trace, data: { ...data, segments } };
|
|
60
|
+
}
|
|
61
|
+
/* An interval wholly outside the transform's reach has nothing to draw. One reaching out of it runs to the floor. */
|
|
62
|
+
if (data.geom === "ribbon" || data.geom === "range") {
|
|
63
|
+
const readings = data.readings.filter((one) => kept(worst(verdict(one.x, x), one.mid == null ? "in" : verdict(one.mid, vertical), vertical && !TRANSFORMS[vertical.transform].admits(one.hi) ? refusal(vertical) : "in")));
|
|
64
|
+
return readings.length === data.readings.length ? trace : { ...trace, data: { ...data, readings } };
|
|
65
|
+
}
|
|
66
|
+
if (data.geom === "point3d" || !("points" in data))
|
|
67
|
+
return trace;
|
|
68
|
+
/* A line breaks where a reading dropped. ggplot breaks a path at a censored value too. */
|
|
69
|
+
let dropped = false;
|
|
70
|
+
const points = data.points.flatMap((one) => {
|
|
71
|
+
if (!kept(worst(verdict(one.x, x), verdict(one.y, vertical)))) {
|
|
72
|
+
dropped = true;
|
|
73
|
+
return [];
|
|
74
|
+
}
|
|
75
|
+
const breaks = dropped && data.geom === "line";
|
|
76
|
+
dropped = false;
|
|
77
|
+
return [breaks ? { ...one, gap: true } : one];
|
|
78
|
+
});
|
|
79
|
+
return points.length === data.points.length ? trace : { ...trace, data: { ...data, points } };
|
|
80
|
+
});
|
|
81
|
+
return { traces: placed, offLog, offRoot, offRange };
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Intervals and whiskers reaching past where the transform places, at or below zero on a log axis.
|
|
85
|
+
*
|
|
86
|
+
* Each draws to the axis floor. The footer counts them.
|
|
87
|
+
*/
|
|
88
|
+
export function flooredCount(traces, transform) {
|
|
89
|
+
const { admits } = TRANSFORMS[transform];
|
|
90
|
+
let count = 0;
|
|
91
|
+
for (const trace of traces) {
|
|
92
|
+
if (trace.axis === "y2")
|
|
93
|
+
continue;
|
|
94
|
+
const data = trace.data;
|
|
95
|
+
if (data.at === "records" && (data.geom === "ribbon" || data.geom === "range")) {
|
|
96
|
+
for (const one of data.readings)
|
|
97
|
+
if (!admits(one.lo))
|
|
98
|
+
count += 1;
|
|
99
|
+
}
|
|
100
|
+
else if (data.at === "groups" && "bars" in data) {
|
|
101
|
+
for (const bar of data.bars)
|
|
102
|
+
if (bar.error != null && !admits(bar.value - bar.error))
|
|
103
|
+
count += 1;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return count;
|
|
107
|
+
}
|