@tetrascience-npm/ts-lib-viz 0.0.0-stage → 0.1.4
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 +28 -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,612 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The frame's arithmetic, apart from its drawing.
|
|
3
|
+
*
|
|
4
|
+
* Axis specs, tick stepping and the plot area inside a margin. The frame derivation and the zoom
|
|
5
|
+
* pass read these without a DOM.
|
|
6
|
+
*/
|
|
7
|
+
import { scaleBand, scaleLinear, scaleLog, scaleSqrt, scaleSymlog, scaleTime, scaleUtc, } from "d3-scale";
|
|
8
|
+
import { METRICS } from "./sizing.js";
|
|
9
|
+
import { compactFor } from "./tick-format.js";
|
|
10
|
+
import { isLog, TRANSFORMS, transformOf } from "./transform.js";
|
|
11
|
+
/**
|
|
12
|
+
* Tick length and type size an axis falls back to.
|
|
13
|
+
*
|
|
14
|
+
* The regular column of `METRICS`. A spec built with metrics has its own resolved values.
|
|
15
|
+
*/
|
|
16
|
+
export const TICK_LEN = METRICS.regular.tickLength;
|
|
17
|
+
export const TICK_PX = METRICS.regular.axis;
|
|
18
|
+
/**
|
|
19
|
+
* How far above the plot area a vertical axis title draws.
|
|
20
|
+
*
|
|
21
|
+
* `Frame` draws the title at this offset and the critic measures the top tick label against it.
|
|
22
|
+
*/
|
|
23
|
+
export const Y_TITLE_ABOVE = 5;
|
|
24
|
+
export const isBand = (spec) => "values" in spec;
|
|
25
|
+
export const chromeOf = (spec, titleAlign) => ({
|
|
26
|
+
lineWidth: spec.lineWidth ?? 1,
|
|
27
|
+
tickLength: spec.tickLength ?? TICK_LEN,
|
|
28
|
+
tickSide: spec.tickSide ?? "out",
|
|
29
|
+
tickAngle: spec.tickAngle ?? 0,
|
|
30
|
+
titleAlign: spec.titleAlign ?? titleAlign,
|
|
31
|
+
fontSize: spec.fontSize ?? TICK_PX,
|
|
32
|
+
ink: spec.ink ?? "muted",
|
|
33
|
+
rule: spec.lineWidth != null,
|
|
34
|
+
grid: spec.grid ?? false,
|
|
35
|
+
minorGrid: spec.minorGrid ?? false,
|
|
36
|
+
});
|
|
37
|
+
/**
|
|
38
|
+
* Rough glyph width at the tick size. Reserving room costs no text measurement.
|
|
39
|
+
*
|
|
40
|
+
* Slightly over the average. A label wider than its room draws over whatever is outside the
|
|
41
|
+
* margin.
|
|
42
|
+
*/
|
|
43
|
+
const CHAR_W = 0.62;
|
|
44
|
+
/**
|
|
45
|
+
* How wide a run of characters draws at a given type size.
|
|
46
|
+
*
|
|
47
|
+
* Anything reserving room for text reads this one estimate.
|
|
48
|
+
*/
|
|
49
|
+
export const textRun = (characters, fontSize) => characters * fontSize * CHAR_W;
|
|
50
|
+
/**
|
|
51
|
+
* Gap between a tick and the label beside it, in pixels.
|
|
52
|
+
*
|
|
53
|
+
* Read by the drawing and by the room reserved for it. One number for both.
|
|
54
|
+
*/
|
|
55
|
+
export const LABEL_GAP = 4;
|
|
56
|
+
/**
|
|
57
|
+
* Room the ticks and their labels take outside the plot area.
|
|
58
|
+
*
|
|
59
|
+
* A turned label reaches across. The room it needs is the longest label's width with the turn
|
|
60
|
+
* applied.
|
|
61
|
+
*/
|
|
62
|
+
export function tickRoom(chrome, labeled, longest, axis) {
|
|
63
|
+
const beyond = chrome.tickSide === "out" ? chrome.tickLength : 0;
|
|
64
|
+
if (!labeled)
|
|
65
|
+
return beyond;
|
|
66
|
+
const line = chrome.fontSize;
|
|
67
|
+
const run = textRun(longest, chrome.fontSize);
|
|
68
|
+
const radians = (chrome.tickAngle * Math.PI) / 180;
|
|
69
|
+
const upright = axis === "x" ? line : run;
|
|
70
|
+
const across = axis === "x" ? run : line;
|
|
71
|
+
const reach = Math.abs(Math.cos(radians)) * upright + Math.abs(Math.sin(radians)) * across;
|
|
72
|
+
return beyond + reach + LABEL_GAP;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Room one label takes along its own axis.
|
|
76
|
+
*
|
|
77
|
+
* The arithmetic above with the axes swapped. A column label needs its whole run and a row label
|
|
78
|
+
* needs one line.
|
|
79
|
+
*/
|
|
80
|
+
export function bandReach(chrome, longest, axis) {
|
|
81
|
+
const line = chrome.fontSize;
|
|
82
|
+
const run = textRun(longest, chrome.fontSize);
|
|
83
|
+
const radians = (chrome.tickAngle * Math.PI) / 180;
|
|
84
|
+
const flat = axis === "x" ? run : line;
|
|
85
|
+
const turned = axis === "x" ? line : run;
|
|
86
|
+
return Math.abs(Math.cos(radians)) * flat + Math.abs(Math.sin(radians)) * turned;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Room one axis needs, for a caller setting the frame's margin.
|
|
90
|
+
*
|
|
91
|
+
* A facet shares one margin across its panels. The chrome belongs to the axis.
|
|
92
|
+
*/
|
|
93
|
+
/**
|
|
94
|
+
* Room a title takes past its own line of type.
|
|
95
|
+
*
|
|
96
|
+
* Half of it opens the gap after the tick labels. The other half is the margin past the title.
|
|
97
|
+
*/
|
|
98
|
+
export const TITLE_PAD = 8;
|
|
99
|
+
/** Share of a line of type above its baseline. The rest hangs below it. */
|
|
100
|
+
export const ASCENT = 0.75;
|
|
101
|
+
export function axisRoom(spec, axis,
|
|
102
|
+
/** Longest label in characters, which a caller knows for categories and guesses for numbers. */
|
|
103
|
+
longest, hasTitle) {
|
|
104
|
+
const chrome = chromeOf(spec, "end");
|
|
105
|
+
return tickRoom(chrome, spec.labelTicks !== false, longest, axis) + (hasTitle ? chrome.fontSize + TITLE_PAD : 0);
|
|
106
|
+
}
|
|
107
|
+
/*
|
|
108
|
+
The polar arithmetic. One layout answers the label radius and a slice's room.
|
|
109
|
+
|
|
110
|
+
Angles run clockwise from twelve o'clock, in radians. Screen coordinates are `sin` across and
|
|
111
|
+
`-cos` down.
|
|
112
|
+
*/
|
|
113
|
+
/** Room a ring leaves between its outer edge and the panel, in pixels. */
|
|
114
|
+
export const POLAR_INSET = 10;
|
|
115
|
+
/** Where a slice's label sits, as a fraction of the way from the hole to the outer edge. */
|
|
116
|
+
const LABEL_AT = 0.68;
|
|
117
|
+
/**
|
|
118
|
+
* Places a ring in a panel of this size.
|
|
119
|
+
*
|
|
120
|
+
* The outer radius comes off the shorter side. `hole` is a fraction of it. Zero draws a pie.
|
|
121
|
+
*/
|
|
122
|
+
export function ringLayout(width, height, hole = 0) {
|
|
123
|
+
const outer = Math.max(0, Math.min(width, height) / 2 - POLAR_INSET);
|
|
124
|
+
const inner = Math.min(Math.max(0, hole), 0.95) * outer;
|
|
125
|
+
return {
|
|
126
|
+
cx: width / 2,
|
|
127
|
+
cy: height / 2,
|
|
128
|
+
outer,
|
|
129
|
+
inner,
|
|
130
|
+
labelRadius: inner + (outer - inner) * LABEL_AT,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Divides a full turn between the values, in arrival order.
|
|
135
|
+
*
|
|
136
|
+
* A negative reading takes no arc. An empty total returns no slices.
|
|
137
|
+
*/
|
|
138
|
+
export function sliceAngles(values) {
|
|
139
|
+
const shares = values.map((value) => Math.max(0, value));
|
|
140
|
+
const total = shares.reduce((sum, share) => sum + share, 0);
|
|
141
|
+
if (!(total > 0))
|
|
142
|
+
return [];
|
|
143
|
+
const out = [];
|
|
144
|
+
let at = 0;
|
|
145
|
+
for (const share of shares) {
|
|
146
|
+
const to = at + (share / total) * Math.PI * 2;
|
|
147
|
+
out.push({ from: at, to, mid: (at + to) / 2 });
|
|
148
|
+
at = to;
|
|
149
|
+
}
|
|
150
|
+
return out;
|
|
151
|
+
}
|
|
152
|
+
/** Where an angle at this radius falls, in the panel's own pixels. */
|
|
153
|
+
export const polarPoint = (layout, radius, angle) => ({
|
|
154
|
+
x: layout.cx + radius * Math.sin(angle),
|
|
155
|
+
y: layout.cy - radius * Math.cos(angle),
|
|
156
|
+
});
|
|
157
|
+
/**
|
|
158
|
+
* Joins two angles at one radius, as an SVG path.
|
|
159
|
+
*
|
|
160
|
+
* The curve is a quadratic bending toward the center. A half-turn link passes through it.
|
|
161
|
+
*/
|
|
162
|
+
export function linkPath(layout, radius, from, to) {
|
|
163
|
+
const head = polarPoint(layout, radius, from);
|
|
164
|
+
const tail = polarPoint(layout, radius, to);
|
|
165
|
+
return `M ${head.x} ${head.y} Q ${layout.cx} ${layout.cy} ${tail.x} ${tail.y}`;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* The path of one arc, as SVG.
|
|
169
|
+
*
|
|
170
|
+
* A ring with a hole draws two arcs and two edges. A pie closes through the center.
|
|
171
|
+
*/
|
|
172
|
+
export function arcPath(layout, from, to) {
|
|
173
|
+
/* A full turn ends where it starts, and an arc between one point and itself draws nothing. Two halves close it. */
|
|
174
|
+
if (to - from >= Math.PI * 2 - 1e-9) {
|
|
175
|
+
const circle = (radius, sweep) => {
|
|
176
|
+
const head = polarPoint(layout, radius, from);
|
|
177
|
+
const foot = polarPoint(layout, radius, from + Math.PI);
|
|
178
|
+
return `M ${head.x} ${head.y} A ${radius} ${radius} 0 1 ${sweep} ${foot.x} ${foot.y} A ${radius} ${radius} 0 1 ${sweep} ${head.x} ${head.y} Z`;
|
|
179
|
+
};
|
|
180
|
+
return layout.inner <= 0 ? circle(layout.outer, 1) : `${circle(layout.outer, 1)} ${circle(layout.inner, 0)}`;
|
|
181
|
+
}
|
|
182
|
+
const large = to - from > Math.PI ? 1 : 0;
|
|
183
|
+
const outerStart = polarPoint(layout, layout.outer, from);
|
|
184
|
+
const outerEnd = polarPoint(layout, layout.outer, to);
|
|
185
|
+
const sweep = `A ${layout.outer} ${layout.outer} 0 ${large} 1 ${outerEnd.x} ${outerEnd.y}`;
|
|
186
|
+
if (layout.inner <= 0) {
|
|
187
|
+
return `M ${layout.cx} ${layout.cy} L ${outerStart.x} ${outerStart.y} ${sweep} Z`;
|
|
188
|
+
}
|
|
189
|
+
const innerEnd = polarPoint(layout, layout.inner, to);
|
|
190
|
+
const innerStart = polarPoint(layout, layout.inner, from);
|
|
191
|
+
return [
|
|
192
|
+
`M ${outerStart.x} ${outerStart.y}`,
|
|
193
|
+
sweep,
|
|
194
|
+
`L ${innerEnd.x} ${innerEnd.y}`,
|
|
195
|
+
`A ${layout.inner} ${layout.inner} 0 ${large} 0 ${innerStart.x} ${innerStart.y}`,
|
|
196
|
+
"Z",
|
|
197
|
+
].join(" ");
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Slices with labels that fit around the ring.
|
|
201
|
+
*
|
|
202
|
+
* The label ring's circumference over one label's run. `longest` is the drawn text in characters.
|
|
203
|
+
*/
|
|
204
|
+
export function slicesLegible(layout, fontSize, longest) {
|
|
205
|
+
const ring = 2 * Math.PI * layout.labelRadius;
|
|
206
|
+
const room = Math.max(textRun(longest, fontSize), fontSize);
|
|
207
|
+
return Math.max(1, Math.floor(ring / room));
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* How far a dial sweeps, in radians.
|
|
211
|
+
*
|
|
212
|
+
* A half turn, opening upward. The arc runs from nine o'clock over the top to three o'clock.
|
|
213
|
+
*/
|
|
214
|
+
export const GAUGE_SWEEP = Math.PI;
|
|
215
|
+
/**
|
|
216
|
+
* Places a dial in a panel, with an arc of the stated weight.
|
|
217
|
+
*
|
|
218
|
+
* A dial draws a half circle above its center and its number below.
|
|
219
|
+
*/
|
|
220
|
+
export function dialLayout(width, height, weight, sweep = GAUGE_SWEEP, scale = 1) {
|
|
221
|
+
/* A half turn draws above its center. A wider one wraps below and takes the panel's middle. */
|
|
222
|
+
const wrapped = sweep > Math.PI;
|
|
223
|
+
const cy = height * (wrapped ? 0.5 : DIAL_BASELINE);
|
|
224
|
+
const room = Math.min(width / 2, wrapped ? Math.min(cy, height - cy) : cy);
|
|
225
|
+
const outer = Math.max(0, room - POLAR_INSET) * Math.max(0, scale);
|
|
226
|
+
const inner = Math.max(0, outer - Math.min(weight, outer));
|
|
227
|
+
return { cx: width / 2, cy, outer, inner, labelRadius: inner + (outer - inner) / 2 };
|
|
228
|
+
}
|
|
229
|
+
/** Where a dial's center falls down the panel, leaving the rest for its number. */
|
|
230
|
+
const DIAL_BASELINE = 0.68;
|
|
231
|
+
/**
|
|
232
|
+
* Where one reading falls along a dial, as an angle.
|
|
233
|
+
*
|
|
234
|
+
* Clamped to the ends. A reading outside the range draws pinned at the end.
|
|
235
|
+
*/
|
|
236
|
+
export function gaugeAngle(value, range, sweep = GAUGE_SWEEP) {
|
|
237
|
+
const [lo, hi] = range;
|
|
238
|
+
const span = hi - lo;
|
|
239
|
+
const at = span === 0 ? 0.5 : Math.min(1, Math.max(0, (value - lo) / span));
|
|
240
|
+
return -sweep / 2 + at * sweep;
|
|
241
|
+
}
|
|
242
|
+
/** Room outside a radar's outermost ring for the measure names at the spoke ends, in pixels. */
|
|
243
|
+
const SPOKE_LABEL = 22;
|
|
244
|
+
/**
|
|
245
|
+
* Places a radar's rules in a panel.
|
|
246
|
+
*
|
|
247
|
+
* The radius comes off the shorter side. A measure's name draws outside the outermost ring and
|
|
248
|
+
* the radius gives up that room first.
|
|
249
|
+
*/
|
|
250
|
+
export function spokeLayout(spokes, width, height) {
|
|
251
|
+
return {
|
|
252
|
+
cx: width / 2,
|
|
253
|
+
cy: height / 2,
|
|
254
|
+
radius: Math.max(0, Math.min(width, height) / 2 - POLAR_INSET - SPOKE_LABEL),
|
|
255
|
+
spokes: Math.max(1, spokes),
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Where a reading falls along one spoke, in panel pixels.
|
|
260
|
+
*
|
|
261
|
+
* `at` runs zero to one along the rule. The first spoke points up and the rest divide the turn
|
|
262
|
+
* clockwise.
|
|
263
|
+
*/
|
|
264
|
+
export function spokePoint(layout, index, at) {
|
|
265
|
+
const angle = (index / layout.spokes) * Math.PI * 2;
|
|
266
|
+
const radius = layout.radius * at;
|
|
267
|
+
return { x: layout.cx + radius * Math.sin(angle), y: layout.cy - radius * Math.cos(angle) };
|
|
268
|
+
}
|
|
269
|
+
/** Ticks a reader can count from: multiples of the step, landing on round numbers. */
|
|
270
|
+
export const TICK_LIMIT = 200;
|
|
271
|
+
export function everyStep(domain, step) {
|
|
272
|
+
if (!(step > 0) || !Number.isFinite(step))
|
|
273
|
+
return null;
|
|
274
|
+
const lo = Math.min(domain[0], domain[1]);
|
|
275
|
+
const hi = Math.max(domain[0], domain[1]);
|
|
276
|
+
// A step far below the span would generate thousands of ticks and paint the axis solid.
|
|
277
|
+
if ((hi - lo) / step > TICK_LIMIT)
|
|
278
|
+
return null;
|
|
279
|
+
const out = [];
|
|
280
|
+
const first = Math.ceil(lo / step) * step;
|
|
281
|
+
for (let at = first; at <= hi + step * 1e-9; at += step) {
|
|
282
|
+
// Re-derived from the multiple. A 0.1 step stays off 0.30000004.
|
|
283
|
+
out.push(Number((Math.round(at / step) * step).toPrecision(12)));
|
|
284
|
+
}
|
|
285
|
+
/* No multiple of the step lands inside. The caller falls back to the scale's own ticks. */
|
|
286
|
+
return out.length === 0 ? null : out;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Whole powers of the base on a log axis.
|
|
290
|
+
*
|
|
291
|
+
* `scaleLog.ticks()` returns the minor ticks between decades. Thinned past the cap.
|
|
292
|
+
*/
|
|
293
|
+
export function decades(all, cap = 6, base = 10) {
|
|
294
|
+
const whole = all.filter((value) => {
|
|
295
|
+
const log = base === 10 ? Math.log10(value) : Math.log2(value);
|
|
296
|
+
return Math.abs(log - Math.round(log)) < 1e-9;
|
|
297
|
+
});
|
|
298
|
+
// A domain inside one decade has no whole ones. The minor ticks are the answer.
|
|
299
|
+
if (whole.length < 2)
|
|
300
|
+
return all.length <= cap ? all : thin(all, cap);
|
|
301
|
+
return whole.length <= cap ? whole : thin(whole, cap);
|
|
302
|
+
}
|
|
303
|
+
const thin = (values, cap) => values.filter((_, index) => index % Math.ceil(values.length / cap) === 0);
|
|
304
|
+
/**
|
|
305
|
+
* Pseudo-log ticks: zero and the signed whole decades inside a domain.
|
|
306
|
+
*
|
|
307
|
+
* Past `perSide` decades a side the labels skip decades. Past a hundred they start at ten.
|
|
308
|
+
*/
|
|
309
|
+
function signedDecades(domain, perSide = Infinity) {
|
|
310
|
+
const lo = Math.min(domain[0], domain[1]);
|
|
311
|
+
const hi = Math.max(domain[0], domain[1]);
|
|
312
|
+
const top = Math.floor(Math.log10(Math.max(Math.abs(lo), Math.abs(hi), 1)));
|
|
313
|
+
const first = Number.isFinite(perSide) && top >= 2 ? 1 : 0;
|
|
314
|
+
const stride = Math.max(1, Math.ceil((top + 1 - first) / perSide));
|
|
315
|
+
const out = lo <= 0 && hi >= 0 ? [0] : [];
|
|
316
|
+
for (let exponent = first; exponent <= top; exponent += stride) {
|
|
317
|
+
for (const value of [-(10 ** exponent), 10 ** exponent])
|
|
318
|
+
if (value >= lo && value <= hi)
|
|
319
|
+
out.push(value);
|
|
320
|
+
}
|
|
321
|
+
return out.sort((a, b) => a - b);
|
|
322
|
+
}
|
|
323
|
+
/** The d3 scale that draws a transform. */
|
|
324
|
+
const scaleFor = (transform) => transform === "log10"
|
|
325
|
+
? scaleLog()
|
|
326
|
+
: transform === "log2"
|
|
327
|
+
? scaleLog().base(2)
|
|
328
|
+
: transform === "sqrt"
|
|
329
|
+
? scaleSqrt()
|
|
330
|
+
: transform === "pseudo_log"
|
|
331
|
+
? scaleSymlog()
|
|
332
|
+
: scaleLinear();
|
|
333
|
+
/**
|
|
334
|
+
* The plot area inside a frame of this size.
|
|
335
|
+
*
|
|
336
|
+
* A caller that rewrites a domain has to know the pixels the rule runs between.
|
|
337
|
+
*/
|
|
338
|
+
export const innerSize = (width, height, margin) => ({
|
|
339
|
+
width: Math.max(10, width - margin.left - margin.right),
|
|
340
|
+
height: Math.max(10, height - margin.top - margin.bottom),
|
|
341
|
+
});
|
|
342
|
+
/** Tallest a fixed data aspect draws the plot area, as a multiple of its width. */
|
|
343
|
+
export const RATIO_CAP = 4;
|
|
344
|
+
/**
|
|
345
|
+
* Plot-area height that holds a fixed data aspect. ggplot's `coord_fixed`.
|
|
346
|
+
*
|
|
347
|
+
* `ratio` is units of y per unit of x on screen, both in transformed space. Null over a band.
|
|
348
|
+
*/
|
|
349
|
+
export function fixedHeight(x, y, width, ratio) {
|
|
350
|
+
if (isBand(x) || isBand(y) || !(ratio > 0))
|
|
351
|
+
return null;
|
|
352
|
+
const span = (spec) => {
|
|
353
|
+
const { forward } = TRANSFORMS[transformOf(spec)];
|
|
354
|
+
return Math.abs(forward(spec.domain[1]) - forward(spec.domain[0]));
|
|
355
|
+
};
|
|
356
|
+
const across = span(x);
|
|
357
|
+
const down = span(y);
|
|
358
|
+
return across > 0 && down > 0 && Number.isFinite(across) && Number.isFinite(down)
|
|
359
|
+
? Math.min(width * ratio * (down / across), width * RATIO_CAP)
|
|
360
|
+
: null;
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Room the side panels take off each edge.
|
|
364
|
+
*
|
|
365
|
+
* Answerable from the list alone. The panel size is derived before the frame exists.
|
|
366
|
+
*/
|
|
367
|
+
export function sideRoom(sides) {
|
|
368
|
+
const held = sides ?? [];
|
|
369
|
+
const total = (edge) => held.reduce((sum, panel) => (panel.position === edge ? sum + Math.max(0, panel.size) : sum), 0);
|
|
370
|
+
return { top: total("top"), right: total("right"), bottom: total("bottom"), left: total("left") };
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Where the side panels draw, and what the plot area keeps.
|
|
374
|
+
*
|
|
375
|
+
* A side takes its extent off its own edge before the margin. Two sides on one edge stack
|
|
376
|
+
* outward.
|
|
377
|
+
*/
|
|
378
|
+
export function sideLayout(sides, width, height, margin) {
|
|
379
|
+
const held = sides ?? [];
|
|
380
|
+
const room = sideRoom(held);
|
|
381
|
+
const inner = innerSize(width - room.left - room.right, height - room.top - room.bottom, margin);
|
|
382
|
+
/*
|
|
383
|
+
Each edge splits in two -- the panels inside the axis labels and those past them.
|
|
384
|
+
|
|
385
|
+
`inner` reads the totals. The run is outside panels, labels, inside panels, geoms.
|
|
386
|
+
*/
|
|
387
|
+
const within = (panel) => panel.inside ?? panel.content.kind === "track";
|
|
388
|
+
/* Carried with its place in the declaration. Two equal panels are two entries either way. */
|
|
389
|
+
const on = (edge, inside) => held.flatMap((panel, source) => (panel.position === edge && within(panel) === inside ? [{ panel, source }] : []));
|
|
390
|
+
const spanOf = (edge, inside) => on(edge, inside).reduce((sum, { panel }) => sum + Math.max(0, panel.size), 0);
|
|
391
|
+
const insideRoom = {
|
|
392
|
+
top: spanOf("top", true),
|
|
393
|
+
right: spanOf("right", true),
|
|
394
|
+
bottom: spanOf("bottom", true),
|
|
395
|
+
left: spanOf("left", true),
|
|
396
|
+
};
|
|
397
|
+
const origin = { x: room.left + margin.left, y: room.top + margin.top };
|
|
398
|
+
/* Outward from the geoms. The running sum is what the earlier panels took. */
|
|
399
|
+
const stacked = (edge, inside) => {
|
|
400
|
+
let running = 0;
|
|
401
|
+
return on(edge, inside).map(({ panel, source }) => {
|
|
402
|
+
const before = running;
|
|
403
|
+
running += Math.max(0, panel.size);
|
|
404
|
+
return { panel, source, before, size: Math.max(0, panel.size) };
|
|
405
|
+
});
|
|
406
|
+
};
|
|
407
|
+
/* The near edge of the geoms on each side. An inside panel stacks away from it. */
|
|
408
|
+
const near = {
|
|
409
|
+
top: origin.y,
|
|
410
|
+
bottom: origin.y + inner.height,
|
|
411
|
+
left: origin.x,
|
|
412
|
+
right: origin.x + inner.width,
|
|
413
|
+
};
|
|
414
|
+
/* And the near edge of the labels, which is where an outside panel stacks away from. */
|
|
415
|
+
const past = {
|
|
416
|
+
top: room.top - insideRoom.top,
|
|
417
|
+
bottom: near.bottom + insideRoom.bottom + margin.bottom,
|
|
418
|
+
left: room.left - insideRoom.left,
|
|
419
|
+
right: near.right + insideRoom.right + margin.right,
|
|
420
|
+
};
|
|
421
|
+
const across = (y, height) => ({ x: origin.x, y, width: inner.width, height });
|
|
422
|
+
const down = (x, width) => ({ x, y: origin.y, width, height: inner.height });
|
|
423
|
+
const regions = [
|
|
424
|
+
...stacked("top", true).map(({ panel, source, before, size }) => ({
|
|
425
|
+
panel,
|
|
426
|
+
source,
|
|
427
|
+
...across(near.top - before - size, size),
|
|
428
|
+
})),
|
|
429
|
+
...stacked("top", false).map(({ panel, source, before, size }) => ({
|
|
430
|
+
panel,
|
|
431
|
+
source,
|
|
432
|
+
...across(past.top - before - size, size),
|
|
433
|
+
})),
|
|
434
|
+
...stacked("bottom", true).map(({ panel, source, before, size }) => ({
|
|
435
|
+
panel,
|
|
436
|
+
source,
|
|
437
|
+
...across(near.bottom + before, size),
|
|
438
|
+
})),
|
|
439
|
+
...stacked("bottom", false).map(({ panel, source, before, size }) => ({
|
|
440
|
+
panel,
|
|
441
|
+
source,
|
|
442
|
+
...across(past.bottom + before, size),
|
|
443
|
+
})),
|
|
444
|
+
...stacked("left", true).map(({ panel, source, before, size }) => ({
|
|
445
|
+
panel,
|
|
446
|
+
source,
|
|
447
|
+
...down(near.left - before - size, size),
|
|
448
|
+
})),
|
|
449
|
+
...stacked("left", false).map(({ panel, source, before, size }) => ({
|
|
450
|
+
panel,
|
|
451
|
+
source,
|
|
452
|
+
...down(past.left - before - size, size),
|
|
453
|
+
})),
|
|
454
|
+
...stacked("right", true).map(({ panel, source, before, size }) => ({
|
|
455
|
+
panel,
|
|
456
|
+
source,
|
|
457
|
+
...down(near.right + before, size),
|
|
458
|
+
})),
|
|
459
|
+
...stacked("right", false).map(({ panel, source, before, size }) => ({
|
|
460
|
+
panel,
|
|
461
|
+
source,
|
|
462
|
+
...down(past.right + before, size),
|
|
463
|
+
})),
|
|
464
|
+
];
|
|
465
|
+
/* Declaration order. The stacking rule reads it. */
|
|
466
|
+
return {
|
|
467
|
+
room,
|
|
468
|
+
inner,
|
|
469
|
+
inside: insideRoom,
|
|
470
|
+
/* Back in declaration order, by each panel's own place in the list. */
|
|
471
|
+
regions: regions.sort((a, b) => a.source - b.source).map(({ source: _source, ...region }) => region),
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
// ---- Scale building ----------------------------------------------------------
|
|
475
|
+
/**
|
|
476
|
+
* What decides where an axis puts a value, as a comparable string.
|
|
477
|
+
*
|
|
478
|
+
* A scale is rebuilt on each render. The four inputs below are the whole of the mapping.
|
|
479
|
+
*/
|
|
480
|
+
/** A rename table as a stable string. Declaration order stays out of the key. */
|
|
481
|
+
const namesKey = (names) => names
|
|
482
|
+
? Object.keys(names)
|
|
483
|
+
.sort()
|
|
484
|
+
.map((key) => `${key}=${names[key]}`)
|
|
485
|
+
.join(",")
|
|
486
|
+
: "";
|
|
487
|
+
export const scaleKey = (spec, range) => isBand(spec)
|
|
488
|
+
? `band|${range[0]},${range[1]}|${spec.padding ?? 0.24}|${spec.values.join("\u0000")}|${(spec.gaps ?? []).join(",")}|${spec.gapSize ?? 0}|${namesKey(spec.names)}`
|
|
489
|
+
: `${keyOf(transformOf(spec))}${spec.reverse ? "|reversed" : ""}|${spec.time ? "time" : "plain"}|${spec.clamp === false ? "free" : "held"}|${spec.squish?.join(",") ?? ""}|${range[0]},${range[1]}|${spec.domain[0]},${spec.domain[1]}`;
|
|
490
|
+
const keyOf = (transform) => transform === "identity" ? "linear" : transform === "log10" ? "log" : transform;
|
|
491
|
+
export function buildAxis(spec, range, defaultTicks, titleAlign) {
|
|
492
|
+
const chrome = chromeOf(spec, titleAlign);
|
|
493
|
+
if (isBand(spec)) {
|
|
494
|
+
/*
|
|
495
|
+
A break takes its room out of the range and the bands step over what is left.
|
|
496
|
+
|
|
497
|
+
The total is capped at half the span.
|
|
498
|
+
*/
|
|
499
|
+
const span = range[1] - range[0];
|
|
500
|
+
const toward = span >= 0 ? 1 : -1;
|
|
501
|
+
const breaks = [...new Set(spec.gaps ?? [])].filter((at) => at > 0 && at < spec.values.length);
|
|
502
|
+
const room = Math.min(breaks.length * (spec.gapSize ?? 0), Math.abs(span) / 2);
|
|
503
|
+
const each = breaks.length > 0 ? room / breaks.length : 0;
|
|
504
|
+
const band = scaleBand()
|
|
505
|
+
.domain(spec.values)
|
|
506
|
+
.range([range[0], range[1] - toward * room])
|
|
507
|
+
.padding(spec.padding ?? 0.24);
|
|
508
|
+
const shift = new Map();
|
|
509
|
+
let running = 0;
|
|
510
|
+
const at = new Set(breaks);
|
|
511
|
+
spec.values.forEach((value, index) => {
|
|
512
|
+
if (at.has(index))
|
|
513
|
+
running += each;
|
|
514
|
+
shift.set(value, toward * running);
|
|
515
|
+
});
|
|
516
|
+
const center = (v) => (band(v) ?? 0) + band.bandwidth() / 2 + (shift.get(v) ?? 0);
|
|
517
|
+
return {
|
|
518
|
+
scale: (v) => center(String(v)),
|
|
519
|
+
/* A band has no inverse. The nearest center stands in and a drag lands in one category. */
|
|
520
|
+
invert: (px) => spec.values.reduce((best, value) => (Math.abs(center(value) - px) < Math.abs(center(best) - px) ? value : best), spec.values[0] ?? ""),
|
|
521
|
+
bandwidth: band.bandwidth(),
|
|
522
|
+
step: band.step(),
|
|
523
|
+
ticks: spec.values,
|
|
524
|
+
labelTicks: spec.labelTicks !== false,
|
|
525
|
+
minor: [],
|
|
526
|
+
format: (v) => spec.names?.[String(v)] ?? String(v),
|
|
527
|
+
label: spec.label,
|
|
528
|
+
chrome,
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
const held = spec.clamp !== false;
|
|
532
|
+
/* A reversed axis puts the low end of its domain at the far end of the rule. */
|
|
533
|
+
const toward = spec.reverse ? [range[1], range[0]] : range;
|
|
534
|
+
const [lo, hi] = [Math.min(spec.domain[0], spec.domain[1]), Math.max(spec.domain[0], spec.domain[1])];
|
|
535
|
+
const inside = (values) => values.filter((value) => value >= lo && value <= hi);
|
|
536
|
+
const [floor, ceiling] = spec.squish
|
|
537
|
+
? [Math.min(spec.squish[0], spec.squish[1]), Math.max(spec.squish[0], spec.squish[1])]
|
|
538
|
+
: [-Infinity, Infinity];
|
|
539
|
+
const pinned = (value) => Math.min(ceiling, Math.max(floor, value));
|
|
540
|
+
/*
|
|
541
|
+
Time as a scale, apart from a number format.
|
|
542
|
+
|
|
543
|
+
d3 picks the interval off the span. Ticks come back as `Date` and leave here as epoch numbers.
|
|
544
|
+
*/
|
|
545
|
+
if (spec.time) {
|
|
546
|
+
const clock = (spec.timezone === "local" ? scaleTime() : scaleUtc()).domain(spec.domain).range(toward).clamp(held);
|
|
547
|
+
const asDate = clock.tickFormat();
|
|
548
|
+
const write = spec.format ?? ((value) => asDate(new Date(value)));
|
|
549
|
+
const at = (count) => clock.ticks(count).map((tick) => +tick);
|
|
550
|
+
const wanted = spec.tickCount ?? defaultTicks;
|
|
551
|
+
const major = spec.breaks
|
|
552
|
+
? inside(spec.breaks)
|
|
553
|
+
: ((spec.tickStep ? everyStep(spec.domain, spec.tickStep) : null) ?? at(wanted));
|
|
554
|
+
const minor = (spec.minorBreaks ? inside(spec.minorBreaks) : spec.minorTicks === false ? [] : at(wanted * 4)).filter((value) => !major.includes(value));
|
|
555
|
+
return {
|
|
556
|
+
scale: (value) => clock(new Date(pinned(Number(value)))),
|
|
557
|
+
invert: (px) => +clock.invert(px),
|
|
558
|
+
bandwidth: 0,
|
|
559
|
+
step: 0,
|
|
560
|
+
ticks: major,
|
|
561
|
+
labelTicks: spec.labelTicks !== false,
|
|
562
|
+
minor,
|
|
563
|
+
format: (value) => spec.names?.[String(value)] ?? write(Number(value)),
|
|
564
|
+
label: spec.label,
|
|
565
|
+
chrome,
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
const transform = transformOf(spec);
|
|
569
|
+
const scale = scaleFor(transform).domain(spec.domain).range(toward).clamp(held);
|
|
570
|
+
/* A break the transform has no place for drops, the way a reading there does. */
|
|
571
|
+
const { admits } = TRANSFORMS[transform];
|
|
572
|
+
const placeable = (values) => inside(values).filter(admits);
|
|
573
|
+
const automatic = () => {
|
|
574
|
+
if (isLog(transform))
|
|
575
|
+
return decades(scale.ticks(), spec.tickCount, transform === "log2" ? 2 : 10);
|
|
576
|
+
const powers = transform === "pseudo_log" ? signedDecades(spec.domain, 4) : [];
|
|
577
|
+
return powers.length >= 2 ? powers : scale.ticks(spec.tickCount ?? defaultTicks);
|
|
578
|
+
};
|
|
579
|
+
const stepped = spec.breaks ? placeable(spec.breaks) : spec.tickStep ? everyStep(spec.domain, spec.tickStep) : null;
|
|
580
|
+
const major = stepped ?? automatic();
|
|
581
|
+
/* The default formatter is picked after the ticks. Its decimals come off the gap between them.
|
|
582
|
+
An author's own format is left alone. */
|
|
583
|
+
const format = spec.format ?? compactFor(major);
|
|
584
|
+
/*
|
|
585
|
+
Minors are what the majors left behind.
|
|
586
|
+
|
|
587
|
+
On a log axis those are the 2, 3, 5 between decades. On a linear axis they subdivide the
|
|
588
|
+
labeled interval.
|
|
589
|
+
*/
|
|
590
|
+
const subdivisions = () => isLog(transform)
|
|
591
|
+
? scale.ticks()
|
|
592
|
+
: transform === "pseudo_log"
|
|
593
|
+
? signedDecades(spec.domain)
|
|
594
|
+
: scale.ticks((spec.tickCount ?? defaultTicks) * 4);
|
|
595
|
+
const minor = (spec.minorBreaks ? placeable(spec.minorBreaks) : spec.minorTicks === false ? [] : subdivisions()).filter((value) => !major.includes(value));
|
|
596
|
+
/* A value the transform has no place for draws at the low end of the rule, where ggplot draws -Inf. */
|
|
597
|
+
const place = (value) => (admits(value) ? scale(value) : toward[0]);
|
|
598
|
+
return {
|
|
599
|
+
scale: (v) => place(pinned(Number(v))),
|
|
600
|
+
invert: (px) => scale.invert(px),
|
|
601
|
+
bandwidth: 0,
|
|
602
|
+
step: 0,
|
|
603
|
+
ticks: major,
|
|
604
|
+
labelTicks: spec.labelTicks !== false,
|
|
605
|
+
minor,
|
|
606
|
+
format: (v) => spec.names?.[String(v)] ?? format(Number(v)),
|
|
607
|
+
label: spec.label,
|
|
608
|
+
chrome,
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
/** The longest label an axis draws, in characters. */
|
|
612
|
+
export const longestOf = (axis) => axis.ticks.reduce((most, tick) => Math.max(most, axis.format(tick).length), 0);
|