@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,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The plot spec -- what an author states and a renderer reads.
|
|
3
|
+
*
|
|
4
|
+
* The package's public grammar, and its fields are semver surface. A chart is data.
|
|
5
|
+
*/
|
|
6
|
+
/** The adjustment a position asks for, whichever of the two shapes it was written in. */
|
|
7
|
+
export const positionKind = (position) => position == null ? "identity" : typeof position === "string" ? position : position.kind;
|
|
8
|
+
/** What a position states beyond its name, as the wide set. A name alone states nothing. */
|
|
9
|
+
export const positionArgs = (position) => (position == null || typeof position === "string" ? {} : position);
|
|
10
|
+
/**
|
|
11
|
+
* The patterns in the order a bound linetype aesthetic hands them out.
|
|
12
|
+
*
|
|
13
|
+
* Solid leads and one group draws an unbroken run. The rest run from the most distinct down.
|
|
14
|
+
*/
|
|
15
|
+
export const LINETYPES = ["solid", "dashed", "dotted", "longdash", "dotdash", "twodash"];
|
|
16
|
+
/**
|
|
17
|
+
* One pattern as an SVG dasharray, in CSS pixels.
|
|
18
|
+
*
|
|
19
|
+
* Solid answers undefined and the run draws whole. The numbers are R's own linetype patterns as
|
|
20
|
+
* on and off lengths.
|
|
21
|
+
*/
|
|
22
|
+
export function dashArray(dash) {
|
|
23
|
+
switch (dash) {
|
|
24
|
+
case "dashed":
|
|
25
|
+
return "4 4";
|
|
26
|
+
case "dotted":
|
|
27
|
+
return "1 3";
|
|
28
|
+
case "dotdash":
|
|
29
|
+
return "1 3 4 3";
|
|
30
|
+
case "longdash":
|
|
31
|
+
return "8 4";
|
|
32
|
+
case "twodash":
|
|
33
|
+
return "2 2 6 2";
|
|
34
|
+
default:
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** The spec format this version of the package writes, stated on `ChartSpec.version`. */
|
|
39
|
+
export const SPEC_VERSION = 1;
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The distribution summaries the geoms draw.
|
|
3
|
+
*
|
|
4
|
+
* Each rule and kernel matches R's own. A reader takes a bandwidth off the figure and quotes it.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Which of R's nine quantile definitions a summary takes.
|
|
8
|
+
*
|
|
9
|
+
* Types 1 to 3 step between order statistics and types 4 to 9 interpolate. Type 7 is R's default.
|
|
10
|
+
*/
|
|
11
|
+
export type QuantileType = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
|
|
12
|
+
/**
|
|
13
|
+
* One quantile of a list of readings, at one of R's nine definitions.
|
|
14
|
+
*
|
|
15
|
+
* Sorts a copy and the caller's array keeps its order. Answers null where nothing finite arrived.
|
|
16
|
+
*/
|
|
17
|
+
export declare function quantileOf(values: number[], p: number, type?: QuantileType): number | null;
|
|
18
|
+
/**
|
|
19
|
+
* How the bandwidth is chosen -- a width in the data's units, or a rule.
|
|
20
|
+
*
|
|
21
|
+
* A number states the smoothing outright. The two named rules are R's closed formulas.
|
|
22
|
+
*/
|
|
23
|
+
export type BandwidthRule = "nrd0" | "nrd";
|
|
24
|
+
export type Bandwidth = number | BandwidthRule;
|
|
25
|
+
/**
|
|
26
|
+
* Silverman's rule, R's `bw.nrd0`. The default bandwidth.
|
|
27
|
+
*
|
|
28
|
+
* Nine tenths of the narrower of the deviation and the interquartile range over 1.34, times the
|
|
29
|
+
* fifth root of the count.
|
|
30
|
+
*/
|
|
31
|
+
export declare function bwNrd0(values: number[]): number;
|
|
32
|
+
/**
|
|
33
|
+
* Scott's rule, R's `bw.nrd`.
|
|
34
|
+
*
|
|
35
|
+
* The same narrower-of-two spread as Silverman's at a coefficient of 1.06, and it smooths about
|
|
36
|
+
* eighteen percent wider.
|
|
37
|
+
*/
|
|
38
|
+
export declare function bwNrd(values: number[]): number;
|
|
39
|
+
/**
|
|
40
|
+
* The bandwidth a rule or a stated width resolves to, before `adjust` multiplies it.
|
|
41
|
+
*
|
|
42
|
+
* A number passes through as it stands, in the data's own units.
|
|
43
|
+
*/
|
|
44
|
+
export declare function bandwidthOf(values: number[], bw?: Bandwidth): number;
|
|
45
|
+
/**
|
|
46
|
+
* Bandwidth for one axis of a two-dimensional kernel density.
|
|
47
|
+
*
|
|
48
|
+
* Scott's rule at an interquartile divisor of 1.34 and four times the width, which is R's own
|
|
49
|
+
* two-dimensional estimate.
|
|
50
|
+
*/
|
|
51
|
+
export declare function bandwidth2d(values: number[]): number;
|
|
52
|
+
/**
|
|
53
|
+
* Which kernel the density sums.
|
|
54
|
+
*
|
|
55
|
+
* R's seven. Each is scaled to a standard deviation of one bandwidth. The six with bounded
|
|
56
|
+
* support stop at a fixed multiple of it.
|
|
57
|
+
*/
|
|
58
|
+
export type Kernel = "gaussian" | "epanechnikov" | "rectangular" | "triangular" | "biweight" | "cosine" | "optcosine";
|
|
59
|
+
/** Five-number summary plus outliers. */
|
|
60
|
+
export interface BoxStats {
|
|
61
|
+
q1: number;
|
|
62
|
+
median: number;
|
|
63
|
+
/** Arithmetic mean over every reading, outliers included. */
|
|
64
|
+
mean: number;
|
|
65
|
+
q3: number;
|
|
66
|
+
/** Whisker ends: the extreme values inside the fence `coef` set. */
|
|
67
|
+
lo: number;
|
|
68
|
+
hi: number;
|
|
69
|
+
outliers: number[];
|
|
70
|
+
n: number;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* What a box's summary reads, which is R's `stat_boxplot`.
|
|
74
|
+
*
|
|
75
|
+
* A `coef` of zero collapses the whiskers onto the quartiles and calls each reading outside the
|
|
76
|
+
* middle half an outlier.
|
|
77
|
+
*/
|
|
78
|
+
export interface BoxOptions {
|
|
79
|
+
/**
|
|
80
|
+
* How far a whisker reaches past the quartiles, in interquartile ranges. Default 1.5.
|
|
81
|
+
*
|
|
82
|
+
* Anything past the fence draws as an outlier. The figure states a value other than 1.5.
|
|
83
|
+
*/
|
|
84
|
+
coef?: number;
|
|
85
|
+
/** Which of R's nine quantile definitions the quartiles take. Default 7, which is R's own. */
|
|
86
|
+
quantileType?: QuantileType;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* The five numbers and the outliers, at the fence and quantile definition asked for.
|
|
90
|
+
*
|
|
91
|
+
* Answers null under three readings. A whisker reaches the last reading inside the fence.
|
|
92
|
+
*/
|
|
93
|
+
export declare function boxStats(values: number[], options?: BoxOptions): BoxStats | null;
|
|
94
|
+
export interface Density {
|
|
95
|
+
/** Values the density was evaluated at, low to high. */
|
|
96
|
+
grid: number[];
|
|
97
|
+
/** Density at each grid point, on whichever normalization was asked for. */
|
|
98
|
+
weight: number[];
|
|
99
|
+
/** The bandwidth the curve was smoothed at, after `adjust`, in the data's own units. */
|
|
100
|
+
bandwidth: number;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* What a kernel density estimate reads, which is R's `stat_density`.
|
|
104
|
+
*
|
|
105
|
+
* Each field changes the curve and none is visible in it. A figure should state the bandwidth.
|
|
106
|
+
*/
|
|
107
|
+
export interface KdeOptions {
|
|
108
|
+
/** Bandwidth, or the rule that reads one off the data. Default Silverman's. */
|
|
109
|
+
bw?: Bandwidth;
|
|
110
|
+
/**
|
|
111
|
+
* Multiplier on whatever `bw` resolved to. Default 1.
|
|
112
|
+
*
|
|
113
|
+
* `0.5` halves the rule's answer and `2` doubles it. The reach past the data follows it.
|
|
114
|
+
*/
|
|
115
|
+
adjust?: number;
|
|
116
|
+
/** Which kernel is summed. Default gaussian. */
|
|
117
|
+
kernel?: Kernel;
|
|
118
|
+
/** Points the curve is evaluated at, evenly spaced. Default 512, which is R's. */
|
|
119
|
+
n?: number;
|
|
120
|
+
/**
|
|
121
|
+
* Stops the curve at the smallest and largest reading. Default false.
|
|
122
|
+
*
|
|
123
|
+
* Untrimmed, the grid reaches three bandwidths past each end. A violin wants it trimmed.
|
|
124
|
+
*/
|
|
125
|
+
trim?: boolean;
|
|
126
|
+
/**
|
|
127
|
+
* Range the measure can occupy. Default unbounded.
|
|
128
|
+
*
|
|
129
|
+
* A finite bound clips the grid there and folds the mass that fell outside back in. The area
|
|
130
|
+
* under the curve stays one.
|
|
131
|
+
*/
|
|
132
|
+
bounds?: readonly [number, number];
|
|
133
|
+
/**
|
|
134
|
+
* What the weights are scaled against. Default `density`.
|
|
135
|
+
*
|
|
136
|
+
* `density` integrates to one and two groups stay comparable. `peak` divides each estimate by
|
|
137
|
+
* its own maximum.
|
|
138
|
+
*/
|
|
139
|
+
normalize?: "peak" | "density";
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Kernel density estimate, the shape a violin and a density curve both draw.
|
|
143
|
+
*
|
|
144
|
+
* Above `KDE_BINS` readings the values are binned before the kernel. Answers null under three
|
|
145
|
+
* readings.
|
|
146
|
+
*/
|
|
147
|
+
export declare function kde(values: number[], options?: KdeOptions): Density | null;
|
|
148
|
+
/** Summary statistics for a set of repeated readings at one condition. */
|
|
149
|
+
export interface Spread {
|
|
150
|
+
mean: number;
|
|
151
|
+
sd: number;
|
|
152
|
+
/** Standard error of the mean. */
|
|
153
|
+
sem: number;
|
|
154
|
+
n: number;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* A mean and its error, with no interval claimed off a single reading.
|
|
158
|
+
*
|
|
159
|
+
* One reading reports zero for `sd` and `sem`. A zero-width interval would read as a tight one.
|
|
160
|
+
*/
|
|
161
|
+
export declare function spread(values: number[]): Spread | null;
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The distribution summaries the geoms draw.
|
|
3
|
+
*
|
|
4
|
+
* Each rule and kernel matches R's own. A reader takes a bandwidth off the figure and quotes it.
|
|
5
|
+
*/
|
|
6
|
+
/** R's own tolerance for calling an interpolation weight zero or one. */
|
|
7
|
+
const FUZZ = 4 * Number.EPSILON;
|
|
8
|
+
/**
|
|
9
|
+
* The pair each continuous type is defined by, low then high.
|
|
10
|
+
*
|
|
11
|
+
* Type 4's two halves differ, and a single number per type would misplace it.
|
|
12
|
+
*/
|
|
13
|
+
const CONTINUOUS = {
|
|
14
|
+
4: [0, 1],
|
|
15
|
+
5: [0.5, 0.5],
|
|
16
|
+
6: [0, 0],
|
|
17
|
+
7: [1, 1],
|
|
18
|
+
8: [1 / 3, 1 / 3],
|
|
19
|
+
9: [3 / 8, 3 / 8],
|
|
20
|
+
};
|
|
21
|
+
/** R's modulo, which answers a positive remainder where the numerator is negative. */
|
|
22
|
+
const odd = (value) => ((value % 2) + 2) % 2 === 1;
|
|
23
|
+
/**
|
|
24
|
+
* One quantile off a sorted list, at one of R's nine definitions.
|
|
25
|
+
*
|
|
26
|
+
* The order statistic is clamped at both ends, the way R pads.
|
|
27
|
+
*/
|
|
28
|
+
function quantileSorted(sorted, p, type) {
|
|
29
|
+
const n = sorted.length;
|
|
30
|
+
/* 1-based and clamped. An index either side of the sample answers the extreme reading. */
|
|
31
|
+
const at = (index) => sorted[Math.min(n - 1, Math.max(0, index - 1))];
|
|
32
|
+
let position;
|
|
33
|
+
let weight;
|
|
34
|
+
if (type <= 3) {
|
|
35
|
+
const exact = type === 3 ? n * p - 0.5 : n * p;
|
|
36
|
+
const below = Math.floor(exact + FUZZ);
|
|
37
|
+
const above = exact > below;
|
|
38
|
+
weight = type === 1 ? Number(above) : type === 2 ? (Number(above) + 1) / 2 : Number(exact !== below || odd(below));
|
|
39
|
+
position = below;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
const [a, b] = CONTINUOUS[type];
|
|
43
|
+
const nppm = a + p * (n + 1 - a - b);
|
|
44
|
+
position = Math.floor(nppm + FUZZ);
|
|
45
|
+
weight = nppm - position;
|
|
46
|
+
if (Math.abs(weight) < FUZZ)
|
|
47
|
+
weight = 0;
|
|
48
|
+
}
|
|
49
|
+
const lower = at(position);
|
|
50
|
+
const upper = at(position + 1);
|
|
51
|
+
if (weight === 0)
|
|
52
|
+
return lower;
|
|
53
|
+
if (weight === 1)
|
|
54
|
+
return upper;
|
|
55
|
+
return lower === upper ? lower : (1 - weight) * lower + weight * upper;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* One quantile of a list of readings, at one of R's nine definitions.
|
|
59
|
+
*
|
|
60
|
+
* Sorts a copy and the caller's array keeps its order. Answers null where nothing finite arrived.
|
|
61
|
+
*/
|
|
62
|
+
export function quantileOf(values, p, type = 7) {
|
|
63
|
+
const sorted = values.filter(Number.isFinite).sort((a, b) => a - b);
|
|
64
|
+
return sorted.length === 0 ? null : quantileSorted(sorted, p, type);
|
|
65
|
+
}
|
|
66
|
+
/** Sample standard deviation, on the denominator R's `sd` uses. */
|
|
67
|
+
function deviation(values) {
|
|
68
|
+
const n = values.length;
|
|
69
|
+
if (n < 2)
|
|
70
|
+
return 0;
|
|
71
|
+
let sum = 0;
|
|
72
|
+
for (const value of values)
|
|
73
|
+
sum += value;
|
|
74
|
+
const mean = sum / n;
|
|
75
|
+
let squares = 0;
|
|
76
|
+
for (const value of values)
|
|
77
|
+
squares += (value - mean) ** 2;
|
|
78
|
+
return Math.sqrt(squares / (n - 1));
|
|
79
|
+
}
|
|
80
|
+
/** The readings a bandwidth rule runs over. Non-finite entries drop, as `quantileOf` drops them. */
|
|
81
|
+
const finiteOf = (values) => values.filter(Number.isFinite);
|
|
82
|
+
/**
|
|
83
|
+
* Silverman's rule, R's `bw.nrd0`. The default bandwidth.
|
|
84
|
+
*
|
|
85
|
+
* Nine tenths of the narrower of the deviation and the interquartile range over 1.34, times the
|
|
86
|
+
* fifth root of the count.
|
|
87
|
+
*/
|
|
88
|
+
export function bwNrd0(values) {
|
|
89
|
+
const finite = finiteOf(values);
|
|
90
|
+
const sd = deviation(finite);
|
|
91
|
+
const iqr = (quantileOf(finite, 0.75) ?? 0) - (quantileOf(finite, 0.25) ?? 0);
|
|
92
|
+
let spread = Math.min(sd, iqr / 1.34);
|
|
93
|
+
if (spread === 0)
|
|
94
|
+
spread = sd || Math.abs(finite[0] ?? 0) || 1;
|
|
95
|
+
return 0.9 * spread * finite.length ** -0.2;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Scott's rule, R's `bw.nrd`.
|
|
99
|
+
*
|
|
100
|
+
* The same narrower-of-two spread as Silverman's at a coefficient of 1.06, and it smooths about
|
|
101
|
+
* eighteen percent wider.
|
|
102
|
+
*/
|
|
103
|
+
export function bwNrd(values) {
|
|
104
|
+
const finite = finiteOf(values);
|
|
105
|
+
const iqr = (quantileOf(finite, 0.75) ?? 0) - (quantileOf(finite, 0.25) ?? 0);
|
|
106
|
+
return 1.06 * Math.min(deviation(finite), iqr / 1.34) * finite.length ** -0.2;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The bandwidth a rule or a stated width resolves to, before `adjust` multiplies it.
|
|
110
|
+
*
|
|
111
|
+
* A number passes through as it stands, in the data's own units.
|
|
112
|
+
*/
|
|
113
|
+
export function bandwidthOf(values, bw = "nrd0") {
|
|
114
|
+
if (typeof bw === "number")
|
|
115
|
+
return bw;
|
|
116
|
+
return bw === "nrd" ? bwNrd(values) : bwNrd0(values);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Bandwidth for one axis of a two-dimensional kernel density.
|
|
120
|
+
*
|
|
121
|
+
* Scott's rule at an interquartile divisor of 1.34 and four times the width, which is R's own
|
|
122
|
+
* two-dimensional estimate.
|
|
123
|
+
*/
|
|
124
|
+
export function bandwidth2d(values) {
|
|
125
|
+
const finite = finiteOf(values);
|
|
126
|
+
const iqr = (quantileOf(finite, 0.75) ?? 0) - (quantileOf(finite, 0.25) ?? 0);
|
|
127
|
+
return 4 * 1.06 * Math.min(deviation(finite), iqr / 1.34) * finite.length ** -0.2;
|
|
128
|
+
}
|
|
129
|
+
/** Support half-width per bandwidth, at a standard deviation of one bandwidth each. */
|
|
130
|
+
const SUPPORT = {
|
|
131
|
+
gaussian: 1,
|
|
132
|
+
rectangular: Math.sqrt(3),
|
|
133
|
+
triangular: Math.sqrt(6),
|
|
134
|
+
epanechnikov: Math.sqrt(5),
|
|
135
|
+
biweight: Math.sqrt(7),
|
|
136
|
+
cosine: 1 / Math.sqrt(1 / 3 - 2 / Math.PI ** 2),
|
|
137
|
+
optcosine: 1 / Math.sqrt(1 - 8 / Math.PI ** 2),
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* One kernel's weight at a distance, given the scaled support.
|
|
141
|
+
*
|
|
142
|
+
* `reach` is the bandwidth times the kernel's own factor. The caller scales once per curve.
|
|
143
|
+
*/
|
|
144
|
+
function weigh(kernel, distance, bandwidth, reach) {
|
|
145
|
+
if (kernel === "gaussian") {
|
|
146
|
+
const z = distance / bandwidth;
|
|
147
|
+
return Math.exp(-0.5 * z * z) / (bandwidth * Math.sqrt(2 * Math.PI));
|
|
148
|
+
}
|
|
149
|
+
const away = Math.abs(distance);
|
|
150
|
+
if (away >= reach)
|
|
151
|
+
return 0;
|
|
152
|
+
const unit = away / reach;
|
|
153
|
+
switch (kernel) {
|
|
154
|
+
case "rectangular":
|
|
155
|
+
return 0.5 / reach;
|
|
156
|
+
case "triangular":
|
|
157
|
+
return (1 - unit) / reach;
|
|
158
|
+
case "epanechnikov":
|
|
159
|
+
return (0.75 * (1 - unit * unit)) / reach;
|
|
160
|
+
case "biweight":
|
|
161
|
+
return ((15 / 16) * (1 - unit * unit) ** 2) / reach;
|
|
162
|
+
case "cosine":
|
|
163
|
+
return (1 + Math.cos((Math.PI * distance) / reach)) / (2 * reach);
|
|
164
|
+
default:
|
|
165
|
+
return ((Math.PI / 4) * Math.cos((Math.PI * distance) / (2 * reach))) / reach;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* The five numbers and the outliers, at the fence and quantile definition asked for.
|
|
170
|
+
*
|
|
171
|
+
* Answers null under three readings. A whisker reaches the last reading inside the fence.
|
|
172
|
+
*/
|
|
173
|
+
export function boxStats(values, options = {}) {
|
|
174
|
+
const sorted = values.filter(Number.isFinite).sort((a, b) => a - b);
|
|
175
|
+
if (sorted.length < 3)
|
|
176
|
+
return null;
|
|
177
|
+
const type = options.quantileType ?? 7;
|
|
178
|
+
const q1 = quantileSorted(sorted, 0.25, type);
|
|
179
|
+
const q3 = quantileSorted(sorted, 0.75, type);
|
|
180
|
+
const iqr = q3 - q1;
|
|
181
|
+
const coef = options.coef ?? 1.5;
|
|
182
|
+
const fenceLo = q1 - coef * iqr;
|
|
183
|
+
const fenceHi = q3 + coef * iqr;
|
|
184
|
+
const inside = sorted.filter((value) => value >= fenceLo && value <= fenceHi);
|
|
185
|
+
return {
|
|
186
|
+
q1,
|
|
187
|
+
median: quantileSorted(sorted, 0.5, type),
|
|
188
|
+
mean: sorted.reduce((sum, value) => sum + value, 0) / sorted.length,
|
|
189
|
+
q3,
|
|
190
|
+
lo: Math.min(q1, inside[0] ?? q1),
|
|
191
|
+
hi: Math.max(q3, inside[inside.length - 1] ?? q3),
|
|
192
|
+
outliers: sorted.filter((value) => value < fenceLo || value > fenceHi),
|
|
193
|
+
n: sorted.length,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
/** Points the density is evaluated at where the caller states none. R's own default. */
|
|
197
|
+
const KDE_POINTS = 512;
|
|
198
|
+
/**
|
|
199
|
+
* Bandwidths of grid past each end of the data, where the estimate is not trimmed.
|
|
200
|
+
*
|
|
201
|
+
* R's `density` reaches three and the curve tapers to nothing at both ends.
|
|
202
|
+
*/
|
|
203
|
+
const TAIL_BANDWIDTHS = 3;
|
|
204
|
+
/**
|
|
205
|
+
* Bins the readings are collapsed into before the kernel runs.
|
|
206
|
+
*
|
|
207
|
+
* Below the bin count the pass costs more than it saves. The error is a part in ten thousand.
|
|
208
|
+
*/
|
|
209
|
+
const KDE_BINS = 512;
|
|
210
|
+
/**
|
|
211
|
+
* Kernel density estimate, the shape a violin and a density curve both draw.
|
|
212
|
+
*
|
|
213
|
+
* Above `KDE_BINS` readings the values are binned before the kernel. Answers null under three
|
|
214
|
+
* readings.
|
|
215
|
+
*/
|
|
216
|
+
export function kde(values, options = {}) {
|
|
217
|
+
const finite = values.filter(Number.isFinite);
|
|
218
|
+
if (finite.length < 3)
|
|
219
|
+
return null;
|
|
220
|
+
let lo = Infinity;
|
|
221
|
+
let hi = -Infinity;
|
|
222
|
+
for (const value of finite) {
|
|
223
|
+
if (value < lo)
|
|
224
|
+
lo = value;
|
|
225
|
+
if (value > hi)
|
|
226
|
+
hi = value;
|
|
227
|
+
}
|
|
228
|
+
const span = hi - lo;
|
|
229
|
+
if (!(span > 0))
|
|
230
|
+
return null;
|
|
231
|
+
const bandwidth = bandwidthOf(finite, options.bw ?? "nrd0") * (options.adjust ?? 1);
|
|
232
|
+
if (!(bandwidth > 0) || !Number.isFinite(bandwidth))
|
|
233
|
+
return null;
|
|
234
|
+
const kernel = options.kernel ?? "gaussian";
|
|
235
|
+
const reach = bandwidth * SUPPORT[kernel];
|
|
236
|
+
const points = Math.max(2, Math.floor(options.n ?? KDE_POINTS));
|
|
237
|
+
/*
|
|
238
|
+
The readings the sum runs over, as a value and a count each.
|
|
239
|
+
|
|
240
|
+
One entry per reading below the bin count and one per bin above it.
|
|
241
|
+
*/
|
|
242
|
+
let at;
|
|
243
|
+
let held;
|
|
244
|
+
if (finite.length <= KDE_BINS) {
|
|
245
|
+
at = finite;
|
|
246
|
+
held = [];
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
const binWidth = span / KDE_BINS;
|
|
250
|
+
const counts = new Float64Array(KDE_BINS + 1);
|
|
251
|
+
for (const value of finite)
|
|
252
|
+
counts[Math.round((value - lo) / binWidth)] += 1;
|
|
253
|
+
at = [];
|
|
254
|
+
held = [];
|
|
255
|
+
for (let bin = 0; bin <= KDE_BINS; bin += 1) {
|
|
256
|
+
if (counts[bin] === 0)
|
|
257
|
+
continue;
|
|
258
|
+
at.push(lo + bin * binWidth);
|
|
259
|
+
held.push(counts[bin]);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/** The estimate at one position. The kernel summed over the readings and normalized. */
|
|
263
|
+
const estimate = (position) => {
|
|
264
|
+
let total = 0;
|
|
265
|
+
for (let index = 0; index < at.length; index += 1) {
|
|
266
|
+
const weight = weigh(kernel, position - at[index], bandwidth, reach);
|
|
267
|
+
if (weight !== 0)
|
|
268
|
+
total += weight * (held[index] ?? 1);
|
|
269
|
+
}
|
|
270
|
+
return total / finite.length;
|
|
271
|
+
};
|
|
272
|
+
/*
|
|
273
|
+
The grid -- the data's range, reached past on an untrimmed curve, then clipped to bounds.
|
|
274
|
+
|
|
275
|
+
A finite bound moves the end of the grid onto it.
|
|
276
|
+
*/
|
|
277
|
+
const tail = options.trim ? 0 : TAIL_BANDWIDTHS * bandwidth;
|
|
278
|
+
const [boundLo, boundHi] = options.bounds ?? [-Infinity, Infinity];
|
|
279
|
+
const from = Math.max(lo - tail, boundLo);
|
|
280
|
+
const to = Math.min(hi + tail, boundHi);
|
|
281
|
+
if (!(to > from))
|
|
282
|
+
return null;
|
|
283
|
+
const grid = [];
|
|
284
|
+
const weight = [];
|
|
285
|
+
let peak = 0;
|
|
286
|
+
for (let step = 0; step < points; step += 1) {
|
|
287
|
+
const position = from + ((to - from) * step) / (points - 1);
|
|
288
|
+
/* Reflected at each finite bound. Mass the kernel put outside the limit lands inside.
|
|
289
|
+
The mirror sits on the bound itself, which an untrimmed grid can stop short of. */
|
|
290
|
+
let value = estimate(position);
|
|
291
|
+
if (Number.isFinite(boundLo))
|
|
292
|
+
value += estimate(2 * boundLo - position);
|
|
293
|
+
if (Number.isFinite(boundHi))
|
|
294
|
+
value += estimate(2 * boundHi - position);
|
|
295
|
+
grid.push(position);
|
|
296
|
+
weight.push(value);
|
|
297
|
+
if (value > peak)
|
|
298
|
+
peak = value;
|
|
299
|
+
}
|
|
300
|
+
if ((options.normalize ?? "density") === "peak") {
|
|
301
|
+
return { grid, weight: weight.map((value) => (peak > 0 ? value / peak : 0)), bandwidth };
|
|
302
|
+
}
|
|
303
|
+
return { grid, weight, bandwidth };
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* A mean and its error, with no interval claimed off a single reading.
|
|
307
|
+
*
|
|
308
|
+
* One reading reports zero for `sd` and `sem`. A zero-width interval would read as a tight one.
|
|
309
|
+
*/
|
|
310
|
+
export function spread(values) {
|
|
311
|
+
const clean = values.filter(Number.isFinite);
|
|
312
|
+
if (clean.length === 0)
|
|
313
|
+
return null;
|
|
314
|
+
const mean = clean.reduce((s, v) => s + v, 0) / clean.length;
|
|
315
|
+
const variance = clean.length > 1 ? clean.reduce((s, v) => s + (v - mean) ** 2, 0) / (clean.length - 1) : 0;
|
|
316
|
+
const sd = Math.sqrt(variance);
|
|
317
|
+
return { mean, sd, sem: clean.length > 1 ? sd / Math.sqrt(clean.length) : 0, n: clean.length };
|
|
318
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How an axis prints its numbers.
|
|
3
|
+
*
|
|
4
|
+
* `auto` is the house shortener and reads well across magnitudes -- 1.2k for 1200. The other
|
|
5
|
+
* three take a decimal count. Built on `d3-format`.
|
|
6
|
+
*/
|
|
7
|
+
import type { TickFormat } from "./spec.js";
|
|
8
|
+
export declare const TICK_FORMATS: {
|
|
9
|
+
id: TickFormat;
|
|
10
|
+
label: string;
|
|
11
|
+
}[];
|
|
12
|
+
/** The house shortener. Enough precision to tell values apart and short enough to sit under a tick. */
|
|
13
|
+
export declare function compact(value: number): string;
|
|
14
|
+
/**
|
|
15
|
+
* Decimals enough to tell one axis's ticks apart.
|
|
16
|
+
*
|
|
17
|
+
* Takes the decimals the smallest gap occupies. A tick needing more gets them. Above a thousand
|
|
18
|
+
* it defers to `compact`.
|
|
19
|
+
*/
|
|
20
|
+
export declare function compactFor(ticks: readonly number[]): (value: number) => string;
|
|
21
|
+
export declare function tickFormatter(kind?: TickFormat, decimals?: number): (value: number) => string;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How an axis prints its numbers.
|
|
3
|
+
*
|
|
4
|
+
* `auto` is the house shortener and reads well across magnitudes -- 1.2k for 1200. The other
|
|
5
|
+
* three take a decimal count. Built on `d3-format`.
|
|
6
|
+
*/
|
|
7
|
+
import { format as d3format } from "d3-format";
|
|
8
|
+
export const TICK_FORMATS = [
|
|
9
|
+
{ id: "auto", label: "auto" },
|
|
10
|
+
{ id: "fixed", label: "0.00" },
|
|
11
|
+
{ id: "scientific", label: "1e0" },
|
|
12
|
+
{ id: "percent", label: "%" },
|
|
13
|
+
];
|
|
14
|
+
/** The house shortener. Enough precision to tell values apart and short enough to sit under a tick. */
|
|
15
|
+
export function compact(value) {
|
|
16
|
+
const abs = Math.abs(value);
|
|
17
|
+
if (abs >= 1_000_000)
|
|
18
|
+
return `${(value / 1_000_000).toFixed(abs % 1_000_000 === 0 ? 0 : 1)}M`;
|
|
19
|
+
if (abs >= 1_000)
|
|
20
|
+
return `${(value / 1_000).toFixed(abs % 1_000 === 0 ? 0 : 1)}k`;
|
|
21
|
+
if (abs >= 10)
|
|
22
|
+
return value.toFixed(0);
|
|
23
|
+
if (abs >= 1)
|
|
24
|
+
return value.toFixed(1);
|
|
25
|
+
return value.toPrecision(2);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Decimals a number needs to print as itself, capped at six.
|
|
29
|
+
*
|
|
30
|
+
* Read off the number's own representation. A quarter step occupies two decimals.
|
|
31
|
+
*/
|
|
32
|
+
function decimalsFor(value) {
|
|
33
|
+
const size = Math.abs(value);
|
|
34
|
+
if (!Number.isFinite(size) || size === 0)
|
|
35
|
+
return 0;
|
|
36
|
+
for (let places = 0; places < 6; places += 1) {
|
|
37
|
+
if (Math.abs(Number(value.toFixed(places)) - value) < size * 1e-9)
|
|
38
|
+
return places;
|
|
39
|
+
}
|
|
40
|
+
return 6;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Decimals enough to tell one axis's ticks apart.
|
|
44
|
+
*
|
|
45
|
+
* Takes the decimals the smallest gap occupies. A tick needing more gets them. Above a thousand
|
|
46
|
+
* it defers to `compact`.
|
|
47
|
+
*/
|
|
48
|
+
export function compactFor(ticks) {
|
|
49
|
+
let gap = Infinity;
|
|
50
|
+
for (let i = 1; i < ticks.length; i += 1) {
|
|
51
|
+
const step = Math.abs(ticks[i] - ticks[i - 1]);
|
|
52
|
+
if (step > 0 && step < gap)
|
|
53
|
+
gap = step;
|
|
54
|
+
}
|
|
55
|
+
if (!Number.isFinite(gap))
|
|
56
|
+
return compact;
|
|
57
|
+
if (ticks.some((tick) => Math.abs(tick) >= 1_000))
|
|
58
|
+
return compact;
|
|
59
|
+
const shortened = ticks.map(compact);
|
|
60
|
+
/* A label with no point has zero decimals. */
|
|
61
|
+
const decimals = shortened.map((label) => {
|
|
62
|
+
const point = label.indexOf(".");
|
|
63
|
+
return point < 0 ? 0 : label.length - point - 1;
|
|
64
|
+
});
|
|
65
|
+
const separated = new Set(shortened).size === shortened.length;
|
|
66
|
+
const even = new Set(decimals).size <= 1;
|
|
67
|
+
if (separated && even)
|
|
68
|
+
return compact;
|
|
69
|
+
let wanted = decimalsFor(gap);
|
|
70
|
+
for (const tick of ticks)
|
|
71
|
+
wanted = Math.max(wanted, decimalsFor(tick));
|
|
72
|
+
return d3format(`,.${Math.min(6, wanted)}f`);
|
|
73
|
+
}
|
|
74
|
+
export function tickFormatter(kind = "auto", decimals = 2) {
|
|
75
|
+
const places = Math.max(0, Math.min(10, Math.trunc(decimals)));
|
|
76
|
+
switch (kind) {
|
|
77
|
+
case "fixed":
|
|
78
|
+
return d3format(`,.${places}f`);
|
|
79
|
+
case "scientific":
|
|
80
|
+
return d3format(`.${places}e`);
|
|
81
|
+
case "percent":
|
|
82
|
+
// A fraction. 0.42 prints as 42%. A column already in points would double-count.
|
|
83
|
+
return d3format(`.${places}%`);
|
|
84
|
+
default:
|
|
85
|
+
return compact;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The theming contract -- which custom properties a chart paints with.
|
|
3
|
+
*
|
|
4
|
+
* Names, shipped values and the parse to a shader triple. A token appears here when the package
|
|
5
|
+
* reads it, and `theme/default.css` declares the same set.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Custom properties a chart reads.
|
|
9
|
+
*
|
|
10
|
+
* `--viz-paper` is the page color. A chart draws on a transparent background per chart-design
|
|
11
|
+
* rule 3 and a panel tone would read as a border.
|
|
12
|
+
*/
|
|
13
|
+
export declare const VIZ_TOKENS: readonly ["--viz-paper", "--viz-ink", "--viz-ink-muted", "--viz-rule", "--viz-annotation", "--viz-leader", "--viz-status-fail", "--viz-status-warn", "--viz-tooltip-bg", "--viz-tooltip-ink", "--viz-tooltip-ink-muted", "--viz-tooltip-ring"];
|
|
14
|
+
export type VizToken = (typeof VIZ_TOKENS)[number];
|
|
15
|
+
/** What each token paints, for a host deciding what to override. Frozen. */
|
|
16
|
+
export declare const TOKEN_MEANING: Readonly<Record<VizToken, string>>;
|
|
17
|
+
/** A resolved set, as color strings. What the SVG path already gets for free. */
|
|
18
|
+
export type ThemeSnapshot = Record<VizToken, string>;
|
|
19
|
+
/**
|
|
20
|
+
* Values the package ships. Frozen.
|
|
21
|
+
*
|
|
22
|
+
* Neutral grays and two semantic tones. A host overrides any of them further up the tree. The
|
|
23
|
+
* two status tones lighten in dark mode.
|
|
24
|
+
*/
|
|
25
|
+
export declare const LIGHT_THEME: Readonly<Record<VizToken, string>>;
|
|
26
|
+
export declare const DARK_THEME: Readonly<Record<VizToken, string>>;
|
|
27
|
+
/** Channels in the range 0 to 1. A shader uniform and `packColors` both take these. */
|
|
28
|
+
export type Rgb = readonly [number, number, number];
|
|
29
|
+
/**
|
|
30
|
+
* A color string as three channels.
|
|
31
|
+
*
|
|
32
|
+
* Hex and `rgb()` only. `oklch()` and `hsl()` return null. `resolveTheme` pushes a value through
|
|
33
|
+
* a probe element and reads back computed `rgb()`.
|
|
34
|
+
*/
|
|
35
|
+
export declare function rgbOf(css: string): Rgb | null;
|
|
36
|
+
/**
|
|
37
|
+
* A snapshot as channels, for the buffers the renderer packs.
|
|
38
|
+
*
|
|
39
|
+
* A token that is absent or did not parse falls back to the shipped value for that theme.
|
|
40
|
+
*/
|
|
41
|
+
export declare function rgbSnapshot(snapshot: Partial<ThemeSnapshot>, fallback?: ThemeSnapshot): Record<VizToken, Rgb>;
|
|
42
|
+
/**
|
|
43
|
+
* Whether the paper behind the geoms is dark.
|
|
44
|
+
*
|
|
45
|
+
* Read off the token. A host installs its own values and the package sees colors.
|
|
46
|
+
*/
|
|
47
|
+
export declare function onDarkPaper(snapshot: ThemeSnapshot): boolean;
|