@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,1311 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* `Viz`: the one component a caller renders, drawing whatever a spec asked for.
|
|
4
|
+
*
|
|
5
|
+
* A figure is a spec and this is what draws it, so the kinds cannot drift apart: `plot()` authors
|
|
6
|
+
* the chain and the geoms come off the grammar table, and there is no second code path to disagree
|
|
7
|
+
* with the first. The pipeline is `toPlotData`, `buildPanels`, the geom for each layer's geometry,
|
|
8
|
+
* wrapped in the figure chrome, legend and tooltip.
|
|
9
|
+
*
|
|
10
|
+
* The component is also the consumer the grammar decisions were missing. It measures its own panel
|
|
11
|
+
* and passes `sizingFor` through, runs the critic on each render and hands findings to
|
|
12
|
+
* `onCritique`, sends override admissions and the build's caveats through `footerOf`, and applies
|
|
13
|
+
* chart-design rule 7's third row where a scatter's count crosses the switch.
|
|
14
|
+
*/
|
|
15
|
+
import { useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
16
|
+
import { bandsUnder, binPoints, axisLabel, breakCount, buildPanels, colorsOf, critique, densityCells, densityGrid, placePoints, fitRequestsOver, fitsFor, drawnFits, footerOf, narrow, numberOf, GROWN_CAP, plotArea, zoomedAxes, BAND_LIMIT, FACET_CAP, LIGHT_THEME, metricsFor, onDarkPaper, overrideNotes, paint, partitionOf, palettesFor, tooltipRows, REGULAR, tooltipTitle, transformOf, rgbSnapshot, sameSubstitution, shapeOf, sideRoom, fixedHeight, tickFormatter, trackFill, trackRamp, toPlotData, treatmentOf, selectedOf, layerScale, } from "../core/internal.js";
|
|
17
|
+
import { Figure } from "./Figure.js";
|
|
18
|
+
import { Frame } from "./Frame.js";
|
|
19
|
+
import { BrushLayer } from "./BrushLayer.js";
|
|
20
|
+
import { GlPoints } from "./gl/GlPoints.js";
|
|
21
|
+
import { useSharedRenderer } from "./gl/context.js";
|
|
22
|
+
import { useVizTheme } from "./theme.js";
|
|
23
|
+
import { SeriesLegend } from "./SeriesLegend.js";
|
|
24
|
+
import { SizeLegend } from "./SizeLegend.js";
|
|
25
|
+
import { MarkLegend } from "./MarkLegend.js";
|
|
26
|
+
import { ColorLegend } from "./ColorLegend.js";
|
|
27
|
+
import { Points3d } from "./geoms/Points3d.js";
|
|
28
|
+
import { Surface } from "./geoms/Surface.js";
|
|
29
|
+
import { Volume } from "./geoms/Volume.js";
|
|
30
|
+
import { TooltipCard, TooltipLayer } from "./Tooltip.js";
|
|
31
|
+
import { Box } from "./geoms/Box.js";
|
|
32
|
+
import { Bars } from "./geoms/Bars.js";
|
|
33
|
+
import { Hexbin } from "./geoms/Hexbin.js";
|
|
34
|
+
import { Heatmap } from "./geoms/Heatmap.js";
|
|
35
|
+
import { Contour } from "./geoms/Contour.js";
|
|
36
|
+
import { Line } from "./geoms/Line.js";
|
|
37
|
+
import { Points } from "./geoms/Points.js";
|
|
38
|
+
import { DendrogramPanel } from "./sides/DendrogramPanel.js";
|
|
39
|
+
import { TrackPanel } from "./sides/TrackPanel.js";
|
|
40
|
+
import { HistogramPanel } from "./sides/HistogramPanel.js";
|
|
41
|
+
import { DensityPanel } from "./sides/DensityPanel.js";
|
|
42
|
+
import { RugPanel } from "./sides/RugPanel.js";
|
|
43
|
+
import { FitCurve } from "./geoms/FitCurve.js";
|
|
44
|
+
import { Violin } from "./geoms/Violin.js";
|
|
45
|
+
import { Area } from "./geoms/Area.js";
|
|
46
|
+
import { DensityCurve } from "./geoms/DensityCurve.js";
|
|
47
|
+
import { Freqpoly } from "./geoms/Freqpoly.js";
|
|
48
|
+
import { Interval } from "./geoms/Interval.js";
|
|
49
|
+
import { Range } from "./geoms/Range.js";
|
|
50
|
+
import { Segment } from "./geoms/Segment.js";
|
|
51
|
+
import { Rule } from "./geoms/Rule.js";
|
|
52
|
+
import { Text } from "./geoms/Text.js";
|
|
53
|
+
import { Parallel } from "./geoms/Parallel.js";
|
|
54
|
+
import { Radar } from "./geoms/Radar.js";
|
|
55
|
+
import { Waterfall } from "./geoms/Waterfall.js";
|
|
56
|
+
import { Sankey } from "./geoms/Sankey.js";
|
|
57
|
+
import { Chord } from "./geoms/Chord.js";
|
|
58
|
+
import { Ideogram } from "./geoms/Ideogram.js";
|
|
59
|
+
import { Treemap } from "./geoms/Treemap.js";
|
|
60
|
+
import { Icicle } from "./geoms/Icicle.js";
|
|
61
|
+
import { Sunburst } from "./geoms/Sunburst.js";
|
|
62
|
+
import { Pie } from "./geoms/Pie.js";
|
|
63
|
+
import { Gauge } from "./geoms/Gauge.js";
|
|
64
|
+
import { useFits } from "./useFits.js";
|
|
65
|
+
import { useZoom } from "./useZoom.js";
|
|
66
|
+
import { exportPng, exportSvg } from "./export.js";
|
|
67
|
+
import { COLORBLIND, download, dragsOf, facetFields, fileName, paletteOptions, Toolbar, viewOf, viewPalettes, viewSpec, } from "./Toolbar.js";
|
|
68
|
+
/** Height a chart takes when the caller names none. */
|
|
69
|
+
const DEFAULT_HEIGHT = 320;
|
|
70
|
+
/** Widest a chart guesses itself before the observer reports, so the first paint is not a sliver. */
|
|
71
|
+
const UNMEASURED_WIDTH = 640;
|
|
72
|
+
/** Gap between facet panels, and the narrowest panel worth cutting a column for. */
|
|
73
|
+
const FACET_GAP = 16;
|
|
74
|
+
const FACET_MIN = 260;
|
|
75
|
+
/** A facet strip's height, its type and its padding, and the axis title row under the grid, gap included. */
|
|
76
|
+
const STRIP_HEIGHT = 22;
|
|
77
|
+
const SHARED_TITLE_HEIGHT = 20;
|
|
78
|
+
/** Pointer travel that makes a press a drag and not a click, in pixels. The turn, the pan and the brush read 3. */
|
|
79
|
+
const DRAG_FLOOR = 3;
|
|
80
|
+
/**
|
|
81
|
+
* The axis a frame draws where the geoms position themselves.
|
|
82
|
+
*
|
|
83
|
+
* A cube runs three scales in projection and a treemap positions by nesting, so a pair of flat
|
|
84
|
+
* rules around either one measures nothing. A band with no categories draws no ticks and no rule,
|
|
85
|
+
* and the margin gives the room back to the geoms.
|
|
86
|
+
*/
|
|
87
|
+
const BARE_AXIS = { values: [] };
|
|
88
|
+
const NO_VIEW = {};
|
|
89
|
+
const NO_TOOLS = [];
|
|
90
|
+
/** The toolbar items that move a flat view or brush it. */
|
|
91
|
+
const VIEW_TOOLS = ["zoom", "reset", "pan", "zoomBox", "box", "lasso"];
|
|
92
|
+
const WHOLE_PANEL = { top: 8, right: 8, bottom: 8, left: 8 };
|
|
93
|
+
/**
|
|
94
|
+
* The panel's own width, reported by a ResizeObserver for `sizingFor` to read.
|
|
95
|
+
*
|
|
96
|
+
* The holder is a callback ref. A holder moved to another element gets measured there.
|
|
97
|
+
*/
|
|
98
|
+
function useMeasuredWidth() {
|
|
99
|
+
const [element, holder] = useState(null);
|
|
100
|
+
const [width, setWidth] = useState(0);
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
if (!element)
|
|
103
|
+
return;
|
|
104
|
+
setWidth(element.getBoundingClientRect().width);
|
|
105
|
+
// jsdom has no observer; the chart draws at the fallback width there, which the tests accept.
|
|
106
|
+
if (typeof ResizeObserver === "undefined")
|
|
107
|
+
return;
|
|
108
|
+
const observer = new ResizeObserver((entries) => {
|
|
109
|
+
const box = entries[0]?.contentRect;
|
|
110
|
+
if (box)
|
|
111
|
+
setWidth(box.width);
|
|
112
|
+
});
|
|
113
|
+
observer.observe(element);
|
|
114
|
+
return () => observer.disconnect();
|
|
115
|
+
}, [element]);
|
|
116
|
+
return { holder, element, width };
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The plot area a stated band size asks for, in pixels, or null where the figure fills its container.
|
|
120
|
+
*
|
|
121
|
+
* Reads the distinct values off the table for each banded axis the author gave a size, which is what
|
|
122
|
+
* `partitionOf` already does to cut panels and what the frame will do again to build its bands. Three
|
|
123
|
+
* walks of one column is cheap beside the alternative, which is building the figure once to find out
|
|
124
|
+
* how big it should have been.
|
|
125
|
+
*
|
|
126
|
+
* An axis with no stated size keeps whatever the container gave it, so a grid can fix its rows and
|
|
127
|
+
* let its columns breathe.
|
|
128
|
+
*/
|
|
129
|
+
export function contentExtent(spec, data) {
|
|
130
|
+
const bound = (channel) => spec.layers.map((series) => series.aes[channel]).find((id) => typeof id === "string");
|
|
131
|
+
const along = (channel) => {
|
|
132
|
+
const axis = spec.scales?.[channel];
|
|
133
|
+
const size = axis?.bandSize;
|
|
134
|
+
const id = bound(channel);
|
|
135
|
+
if (!size || !id)
|
|
136
|
+
return null;
|
|
137
|
+
const seen = new Set();
|
|
138
|
+
for (let row = 0; row < data.rowCount; row += 1) {
|
|
139
|
+
const value = data.text(row, id);
|
|
140
|
+
if (value != null)
|
|
141
|
+
seen.add(value);
|
|
142
|
+
}
|
|
143
|
+
if (seen.size === 0)
|
|
144
|
+
return null;
|
|
145
|
+
/*
|
|
146
|
+
The bands the axis will actually hold, which is not always the distinct values in the column.
|
|
147
|
+
|
|
148
|
+
A kmeans order draws its centroids, so eight of them over four hundred rows want eight bands of
|
|
149
|
+
room. And the frame caps a grown band, so asking for room for five thousand rows when a thousand
|
|
150
|
+
draw would reserve a column of empty page.
|
|
151
|
+
*/
|
|
152
|
+
const bands = Math.min(bandsUnder(axis.order, seen.size), GROWN_CAP);
|
|
153
|
+
/*
|
|
154
|
+
Breaks are room, so the sum pays for them.
|
|
155
|
+
|
|
156
|
+
The gap pixel is the regular column's, since the metrics are resolved from this answer and
|
|
157
|
+
cannot be read before it. A break is a handful of pixels and the sizing breakpoints are hundreds
|
|
158
|
+
apart, so the estimate reaches the same class the real one does, and the band scale spends
|
|
159
|
+
whatever the frame settles on.
|
|
160
|
+
*/
|
|
161
|
+
return bands * size + breakCount(axis.order, bands) * REGULAR.gap;
|
|
162
|
+
};
|
|
163
|
+
const width = along("x");
|
|
164
|
+
const height = along("y");
|
|
165
|
+
return width == null && height == null ? null : { width, height };
|
|
166
|
+
}
|
|
167
|
+
function hoverStore() {
|
|
168
|
+
let held = null;
|
|
169
|
+
const listeners = new Set();
|
|
170
|
+
return {
|
|
171
|
+
get: () => held,
|
|
172
|
+
set: (hover) => {
|
|
173
|
+
held = hover;
|
|
174
|
+
for (const listener of listeners)
|
|
175
|
+
listener();
|
|
176
|
+
},
|
|
177
|
+
subscribe: (listener) => {
|
|
178
|
+
listeners.add(listener);
|
|
179
|
+
return () => listeners.delete(listener);
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
/** The card over one panel, the one subtree a hover renders. */
|
|
184
|
+
function PanelTooltip({ hovers, panel, bounds, }) {
|
|
185
|
+
const hover = useSyncExternalStore(hovers.subscribe, hovers.get, hovers.get);
|
|
186
|
+
return hover && hover.panel === panel ? (_jsx(TooltipLayer, { at: hover.at, bounds: bounds, children: _jsx(TooltipCard, { title: hover.title, rows: hover.rows, note: hover.note }) })) : null;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* One hit, reported as the card a reader sees.
|
|
190
|
+
*
|
|
191
|
+
* `LayerHit` is the union a hover collapses to, and `tooltipTitle` and `tooltipRows` are what turn
|
|
192
|
+
* one into a name and a set of rows. Holding the translation here is what stops a branch from
|
|
193
|
+
* building a card by hand and leaving the name off it, which is a readout of bare numbers with
|
|
194
|
+
* nothing saying what they belong to.
|
|
195
|
+
*
|
|
196
|
+
* The pixel is the one the geom reported beside its datum, where the card sits.
|
|
197
|
+
*/
|
|
198
|
+
function reportHit(ctx, found) {
|
|
199
|
+
if (!found) {
|
|
200
|
+
ctx.onHover(null);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
ctx.onHover({
|
|
204
|
+
panel: ctx.panel,
|
|
205
|
+
at: found.at,
|
|
206
|
+
title: tooltipTitle(found.hit),
|
|
207
|
+
rows: tooltipRows(found.hit, { plot: ctx.spec, data: ctx.data, xField: ctx.xField, fieldAt: ctx.fieldAt }),
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Rule 7's third row on the GPU: the records binned at device-pixel resolution, drawn as one
|
|
212
|
+
* opaque cell per pixel through the shared GL layer.
|
|
213
|
+
*
|
|
214
|
+
* Resolution is the thing this treatment refuses to lose, so the grid is one cell per device
|
|
215
|
+
* pixel and nothing visual is coarser than the screen. The colors come off the live theme:
|
|
216
|
+
* paper to ink by log density, which is what makes the layer follow a theme flip the way the SVG
|
|
217
|
+
* geoms do for free. That is the wiring decision 18 was owed: `useVizTheme` reads the tokens,
|
|
218
|
+
* `rgbSnapshot` packs them, and the GL buffers repack where a value moved.
|
|
219
|
+
*/
|
|
220
|
+
function DensityScatter({ points, x, y, width, height, transposed, ctx, }) {
|
|
221
|
+
const dpr = (typeof window === "undefined" ? 1 : window.devicePixelRatio) || 1;
|
|
222
|
+
/* The plot area in pixels, rising from its foot. The GPU draws the cells against it. */
|
|
223
|
+
const box = useMemo(() => ({ x: [0, width], y: [0, height] }), [width, height]);
|
|
224
|
+
/* The readings as drawn, across then up, jitter folded in. Packed once per table and read on each zoom. */
|
|
225
|
+
const columns = useMemo(() => {
|
|
226
|
+
const across = new Float64Array(points.length);
|
|
227
|
+
const up = new Float64Array(points.length);
|
|
228
|
+
for (let at = 0; at < points.length; at += 1) {
|
|
229
|
+
const point = points[at];
|
|
230
|
+
const px = point.x + (point.dx ?? 0);
|
|
231
|
+
const py = point.y + (point.dy ?? 0);
|
|
232
|
+
across[at] = transposed ? py : px;
|
|
233
|
+
up[at] = transposed ? px : py;
|
|
234
|
+
}
|
|
235
|
+
return { across, up };
|
|
236
|
+
}, [points, transposed]);
|
|
237
|
+
/* The window in data units where both axes map straight to pixels. A transformed or reversed axis bins as drawn. */
|
|
238
|
+
const affine = ctx.domains?.affine ? ctx.domains : null;
|
|
239
|
+
const domain = useMemo(() => (affine ? { x: affine.x, y: affine.y } : box),
|
|
240
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- the four numbers are the window
|
|
241
|
+
[affine?.x[0], affine?.x[1], affine?.y[0], affine?.y[1], box]);
|
|
242
|
+
const cells = useMemo(() => {
|
|
243
|
+
const grid = densityGrid(Math.max(1, Math.round(width * dpr)), Math.max(1, Math.round(height * dpr)));
|
|
244
|
+
if (domain !== box) {
|
|
245
|
+
binPoints(grid, columns.across, columns.up, domain.x, domain.y);
|
|
246
|
+
return densityCells(grid, domain.x, domain.y);
|
|
247
|
+
}
|
|
248
|
+
const placed = placePoints(points, x, y, { transposed });
|
|
249
|
+
binPoints(grid, placed.map((at) => at.x), placed.map((at) => height - at.y), box.x, box.y);
|
|
250
|
+
return densityCells(grid, box.x, box.y);
|
|
251
|
+
}, [points, columns, domain, x, y, transposed, box, width, height, dpr]);
|
|
252
|
+
const glPoints = useMemo(() => {
|
|
253
|
+
const out = new Array(cells.cells);
|
|
254
|
+
for (let at = 0; at < cells.cells; at += 1) {
|
|
255
|
+
out[at] = { x: cells.xs[at], y: cells.ys[at], weight: cells.weight[at] };
|
|
256
|
+
}
|
|
257
|
+
return out;
|
|
258
|
+
}, [cells]);
|
|
259
|
+
const ramp = useMemo(() => {
|
|
260
|
+
const packed = rgbSnapshot(ctx.theme ?? LIGHT_THEME);
|
|
261
|
+
const paper = packed["--viz-paper"];
|
|
262
|
+
const ink = packed["--viz-ink"];
|
|
263
|
+
return (weight) => {
|
|
264
|
+
const t = 0.2 + 0.8 * Math.min(1, Math.max(0, weight));
|
|
265
|
+
return [
|
|
266
|
+
paper[0] + (ink[0] - paper[0]) * t,
|
|
267
|
+
paper[1] + (ink[1] - paper[1]) * t,
|
|
268
|
+
paper[2] + (ink[2] - paper[2]) * t,
|
|
269
|
+
];
|
|
270
|
+
};
|
|
271
|
+
}, [ctx.theme]);
|
|
272
|
+
const { onSubstitute, panel } = ctx;
|
|
273
|
+
useEffect(() => {
|
|
274
|
+
onSubstitute(panel, { drew: "density grid", cells: cells.cells, rows: points.length });
|
|
275
|
+
}, [onSubstitute, panel, cells, points.length]);
|
|
276
|
+
return (_jsx(GlPoints, { points: glPoints, x: x, y: y, xDomain: domain.x, yDomain: domain.y, width: width, height: height, ramp: ramp, radius: 0.5 / dpr, opacity: 1 }));
|
|
277
|
+
}
|
|
278
|
+
/** The series a trace came from holds how its geoms are drawn, apart from what they encode. */
|
|
279
|
+
const styleOf = (ctx, trace) => ctx.spec.layers.find((series) => series.id === trace.layerId)?.style;
|
|
280
|
+
/**
|
|
281
|
+
* The lattice parameters the same series stated, for a geom that bins in two dimensions.
|
|
282
|
+
*
|
|
283
|
+
* A series stating none of them comes back absent, and the geom keeps the pixel radius it sizes its
|
|
284
|
+
* cells from.
|
|
285
|
+
*/
|
|
286
|
+
const latticeOf = (ctx, trace) => {
|
|
287
|
+
const series = ctx.spec.layers.find((held) => held.id === trace.layerId);
|
|
288
|
+
const { binwidth, bins, center, boundary, closed, drop } = series?.statParams ?? {};
|
|
289
|
+
const stated = { bins, center, boundary, closed, drop };
|
|
290
|
+
if (binwidth == null && Object.values(stated).every((value) => value == null))
|
|
291
|
+
return undefined;
|
|
292
|
+
return { ...stated, ...(binwidth != null ? { binwidth } : {}) };
|
|
293
|
+
};
|
|
294
|
+
/**
|
|
295
|
+
* What the grid writes inside its cells, resolved to a formatter and a type size.
|
|
296
|
+
*
|
|
297
|
+
* The format falls back to the color scale's, so a figure that stated its measure's shape once has
|
|
298
|
+
* the cells, the legend and the readout agreeing without saying it twice. The type size falls back
|
|
299
|
+
* to the panel's axis size, which is what ADR 0011 derives from the panel's own width.
|
|
300
|
+
*/
|
|
301
|
+
function numbersOf(ctx, trace, metrics) {
|
|
302
|
+
const layer = ctx.spec.layers.find((series) => series.id === trace.layerId);
|
|
303
|
+
const asked = layer?.numbers;
|
|
304
|
+
if (!layer || !asked)
|
|
305
|
+
return undefined;
|
|
306
|
+
const scale = layerScale(ctx.spec, layer);
|
|
307
|
+
const cut = asked.when;
|
|
308
|
+
return {
|
|
309
|
+
format: tickFormatter(asked.format ?? scale?.format, asked.decimals ?? scale?.decimals),
|
|
310
|
+
wanted: cut ? (value) => (cut.op === "above" ? value > cut.value : value < cut.value) : undefined,
|
|
311
|
+
color: asked.color,
|
|
312
|
+
fontSize: asked.fontSize ?? metrics.axis,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* A scatter trace under chart-design rule 7.
|
|
317
|
+
*
|
|
318
|
+
* One geom per record to `FADE_ABOVE`, smaller and translucent to `GL_ABOVE`, and a density geom
|
|
319
|
+
* above that, because a reader misjudges an overplotted block however fast it draws. What stood in
|
|
320
|
+
* is reported upward, so `footerOf` states the substitution to the reader instead of leaving them
|
|
321
|
+
* to think each hexagon is a record.
|
|
322
|
+
*/
|
|
323
|
+
function ScatterTrace({ trace, points, x, y, width, height, radius, ctx, transposed = false, step, }) {
|
|
324
|
+
const treatment = treatmentOf(points.length);
|
|
325
|
+
const style = styleOf(ctx, trace);
|
|
326
|
+
const { onSubstitute, panel } = ctx;
|
|
327
|
+
useEffect(() => {
|
|
328
|
+
if (treatment !== "binned")
|
|
329
|
+
onSubstitute(panel, null);
|
|
330
|
+
}, [treatment, onSubstitute, panel]);
|
|
331
|
+
const reportPoint = (hit) => reportHit(ctx, hit && { hit: { kind: "point", trace, hit }, at: hit.at });
|
|
332
|
+
/*
|
|
333
|
+
The GPU draws the density grid where it can. Without WebGL2, or under a band axis a grid
|
|
334
|
+
cannot bin, the hexbin is the treatment: coarser cells, but the same rule and the same footer.
|
|
335
|
+
|
|
336
|
+
The renderer arrives after mount, so the first render draws the hexbin whichever side of a
|
|
337
|
+
hydration boundary it runs on and the grid takes over once there is something to draw it with.
|
|
338
|
+
*/
|
|
339
|
+
const gl = useSharedRenderer() != null;
|
|
340
|
+
if (treatment === "binned" && gl && ctx.domains) {
|
|
341
|
+
return (_jsx(DensityScatter, { points: points, x: x, y: y, width: width, height: height, transposed: transposed, ctx: ctx }));
|
|
342
|
+
}
|
|
343
|
+
if (treatment === "binned") {
|
|
344
|
+
return (_jsx(Hexbin, { points: points, x: x, y: y, mode: "binned", tiling: style?.tiling, bin: latticeOf(ctx, trace), ramp: trace.sequential ?? ctx.palettes.sequential, width: width, height: height, onBins: (cells) => onSubstitute(panel, { drew: "hexbin", cells, rows: points.length }), onHover: (bin) => ctx.onHover(bin
|
|
345
|
+
? {
|
|
346
|
+
panel: ctx.panel,
|
|
347
|
+
at: bin.at,
|
|
348
|
+
rows: [{ label: "records", value: bin.datum.count.toLocaleString() }],
|
|
349
|
+
note: "hexagonal density; individual records are not hoverable",
|
|
350
|
+
}
|
|
351
|
+
: null) }));
|
|
352
|
+
}
|
|
353
|
+
return (_jsx(Points, { points: points, x: x, y: y, transposed: transposed, step: step, faded: treatment === "faded", ramp: trace.ramp, radius: treatment === "faded" ? undefined : (style?.size ?? radius), opacity: style?.alpha, shape: style?.shape, hollow: style?.hollow, halo: style?.halo, width: width, height: height, onHover: reportPoint, onSelect: ctx.onSelect ? (hit) => ctx.onSelect?.({ kind: "point", trace, hit }) : undefined }));
|
|
354
|
+
}
|
|
355
|
+
/** The geom a trace draws, picked off the payload the layer engine resolved. */
|
|
356
|
+
function TraceGeoms({ trace, child, ctx }) {
|
|
357
|
+
const { x, y, xBand, width, height, metrics } = child;
|
|
358
|
+
/* Which way round the frame drew, and the slot on whichever axis carries the categories. */
|
|
359
|
+
const transposed = ctx.transposed;
|
|
360
|
+
const band = transposed ? child.yBand : xBand;
|
|
361
|
+
const style = styleOf(ctx, trace);
|
|
362
|
+
const data = trace.data;
|
|
363
|
+
if (data.at === "records") {
|
|
364
|
+
if (data.geom === "point") {
|
|
365
|
+
return (_jsx(ScatterTrace, { trace: trace, points: data.points, x: x, y: y, transposed: transposed, step: transposed ? child.yStep : child.xStep, width: width, height: height, radius: metrics.pointRadius, ctx: ctx }));
|
|
366
|
+
}
|
|
367
|
+
if (data.geom === "point3d") {
|
|
368
|
+
return (_jsx(Points3d, { points: data.points, xDomain: data.xDomain, yDomain: data.yDomain, zDomain: data.zDomain, width: width, height: height, ramp: trace.ramp, radius: style?.size ?? metrics.pointRadius, panes: style?.panes, theme: ctx.theme, labels: {
|
|
369
|
+
x: axisLabel(ctx.xField) ?? "X",
|
|
370
|
+
y: axisLabel(trace.field) ?? "Y",
|
|
371
|
+
z: axisLabel(data.zField) ?? data.zField.label,
|
|
372
|
+
}, onHover: (hit) => reportHit(ctx, hit && { hit: { kind: "cube", trace, hit }, at: hit.at }), onSelect: ctx.onSelect ? (hit) => ctx.onSelect?.({ kind: "cube", trace, hit }) : undefined }));
|
|
373
|
+
}
|
|
374
|
+
if (data.geom === "line") {
|
|
375
|
+
return (_jsx(Line, { points: data.points, x: x, y: y, color: trace.color, step: style?.step, lineWidth: style?.linewidth, opacity: style?.alpha,
|
|
376
|
+
/* A bound dash split the series, so each run draws the pattern its own level was given. */
|
|
377
|
+
dash: trace.linetypeChannel?.at ?? style?.linetype }));
|
|
378
|
+
}
|
|
379
|
+
if (data.geom === "parcoords") {
|
|
380
|
+
return (_jsx(Parallel, { axes: data.axes, lines: data.lines, width: width, height: height, ramp: trace.ramp, color: trace.color, opacity: style?.alpha, ranges: ctx.ranges, onRanges: ctx.onRanges, onHover: (found) => reportHit(ctx, found && { hit: { kind: "record", trace, line: found.datum, at: found.at }, at: found.at }), onSelect: ctx.onSelect
|
|
381
|
+
? (found) => ctx.onSelect?.({ kind: "record", trace, line: found.datum, at: found.at })
|
|
382
|
+
: undefined }));
|
|
383
|
+
}
|
|
384
|
+
/*
|
|
385
|
+
The same rules turned round a center, so the payload is the parallel plot's and the geom differs.
|
|
386
|
+
|
|
387
|
+
No brush reaches it. A range dragged down a flat rule reads as a span of that measure, and the
|
|
388
|
+
same drag along a spoke crosses the polygons of every record between the two, so the gesture
|
|
389
|
+
the parallel plot offers has no reading here.
|
|
390
|
+
*/
|
|
391
|
+
if (data.geom === "radar") {
|
|
392
|
+
return (_jsx(Radar, { axes: data.axes, lines: data.lines, width: width, height: height, ramp: trace.ramp, color: trace.color, fill: style?.fill, opacity: style?.alpha, fontSize: metrics.axis, onHover: (found) => reportHit(ctx, found && { hit: { kind: "record", trace, line: found.datum, at: found.at }, at: found.at }), onSelect: ctx.onSelect
|
|
393
|
+
? (found) => ctx.onSelect?.({ kind: "record", trace, line: found.datum, at: found.at })
|
|
394
|
+
: undefined }));
|
|
395
|
+
}
|
|
396
|
+
if (data.geom === "hex") {
|
|
397
|
+
return (_jsx(Hexbin, { points: data.points, x: x, y: y, mode: "binned", tiling: style?.tiling,
|
|
398
|
+
/* An empty lattice takes thirty cells per axis, stat_bin_hex's default. */
|
|
399
|
+
bin: latticeOf(ctx, trace) ?? {}, ramp: trace.sequential ?? ctx.palettes.sequential, fill: style?.fill, stroke: style?.color, lineWidth: style?.linewidth, opacity: style?.alpha }));
|
|
400
|
+
}
|
|
401
|
+
if (data.geom === "contour") {
|
|
402
|
+
return (_jsx(Contour, { points: data.points, x: x, y: y, width: width, height: height, ramp: trace.sequential ?? ctx.palettes.sequential, adjust: data.adjust, grid: data.grid, levelRamp: trace.ramp, fill: style?.fill, stroke: style?.color, opacity: style?.alpha }));
|
|
403
|
+
}
|
|
404
|
+
/* A mark per reading, where the band beside it closes one region over all of them. */
|
|
405
|
+
if (data.geom === "range") {
|
|
406
|
+
return (_jsx(Range, { points: data.readings, x: x, y: y, color: trace.color, fill: style?.fill, glyph: data.glyph, thickness: style?.thickness, opacity: style?.alpha, lineWidth: style?.linewidth }));
|
|
407
|
+
}
|
|
408
|
+
if (data.geom === "ribbon") {
|
|
409
|
+
return (_jsx(Interval, { points: data.readings, x: x, y: y, color: trace.color, stroke: style?.color, lineWidth: style?.linewidth, opacity: style?.alpha }));
|
|
410
|
+
}
|
|
411
|
+
if (data.geom === "segment") {
|
|
412
|
+
return (_jsx(Segment, { points: data.segments, x: x, y: y, transposed: transposed, color: trace.color, opacity: style?.alpha, lineWidth: style?.linewidth, arrow: style?.arrow, onHover: (hit) => reportHit(ctx, hit && { hit: { kind: "segment", trace, hit }, at: hit.at }), onSelect: ctx.onSelect ? (hit) => ctx.onSelect?.({ kind: "segment", trace, hit }) : undefined }));
|
|
413
|
+
}
|
|
414
|
+
/* A label per row. It writes in foreground ink until the layer maps a color or states one. */
|
|
415
|
+
if (data.geom === "text") {
|
|
416
|
+
const inked = Boolean(seriesOf(ctx.spec, trace.layerId)?.aes.color) || style?.color != null;
|
|
417
|
+
return (_jsx(Text, { points: data.points, x: x, y: y, band: band, step: transposed ? child.yStep : child.xStep, slot: data.slot, transposed: transposed, ramp: trace.colorField ? trace.ramp : undefined, color: inked ? trace.color : undefined, fontSize: style?.fontSize ?? metrics.annotation, hjust: style?.hjust, vjust: style?.vjust, boxed: data.boxed }));
|
|
418
|
+
}
|
|
419
|
+
/* A reference line per row. Rule 4 ink unless the layer maps a color or states one. */
|
|
420
|
+
if (data.geom === "hline" || data.geom === "vline" || data.geom === "abline") {
|
|
421
|
+
const inked = Boolean(seriesOf(ctx.spec, trace.layerId)?.aes.color) || style?.color != null;
|
|
422
|
+
return (_jsx(Rule, { rules: data.rules, x: x, y: y, xAxis: ctx.ruleAxes.x, yAxis: ctx.ruleAxes.y, xBand: ctx.ruleAxes.xBand, yBand: ctx.ruleAxes.yBand, width: width, height: height, transposed: transposed, color: inked ? trace.color : undefined, dash: trace.linetypeChannel?.at ?? style?.linetype, lineWidth: style?.linewidth ?? (inked ? 1.5 : 1), opacity: style?.alpha, fontSize: metrics.annotation }));
|
|
423
|
+
}
|
|
424
|
+
/*
|
|
425
|
+
A smooth's geoms are nothing. Its curve draws in the overlay below, off the coordinates the
|
|
426
|
+
service returned, so the records it carries are what the request was built from and not
|
|
427
|
+
something to put on the page. Stated instead of left to the fall-through, since a reader
|
|
428
|
+
meeting an empty branch cannot tell a deliberate blank from a missing geom.
|
|
429
|
+
*/
|
|
430
|
+
if (data.geom === "smooth")
|
|
431
|
+
return null;
|
|
432
|
+
return null;
|
|
433
|
+
}
|
|
434
|
+
if (data.geom === "isosurface" || data.geom === "volume") {
|
|
435
|
+
return (_jsx(Volume, { field: data.field, width: width, height: height, ramp: style?.fill ? () => style.fill : (trace.sequential ?? ctx.palettes.sequential), iso: data.geom === "isosurface", ...(style?.isoLevel != null ? { isoLevel: style.isoLevel } : {}), panes: style?.panes, theme: ctx.theme, labels: {
|
|
436
|
+
x: axisLabel(ctx.xField) ?? "X",
|
|
437
|
+
y: axisLabel(trace.field) ?? "Y",
|
|
438
|
+
z: axisLabel(trace.zField) ?? trace.zField?.label ?? "Z",
|
|
439
|
+
} }));
|
|
440
|
+
}
|
|
441
|
+
if (data.geom === "surface" || data.geom === "mesh3d") {
|
|
442
|
+
return (_jsx(Surface, { mesh: data.mesh, xDomain: data.xDomain, yDomain: data.yDomain, width: width, height: height, ramp: style?.fill ? () => style.fill : (trace.sequential ?? ctx.palettes.sequential), panes: style?.panes, theme: ctx.theme, labels: {
|
|
443
|
+
x: axisLabel(ctx.xField) ?? "X",
|
|
444
|
+
y: axisLabel(trace.field) ?? "Y",
|
|
445
|
+
z: axisLabel(data.zField) ?? data.zField.label,
|
|
446
|
+
} }));
|
|
447
|
+
}
|
|
448
|
+
/*
|
|
449
|
+
The two packed layouts, which read one hierarchy and take the panel whole.
|
|
450
|
+
|
|
451
|
+
The rectangles arrive as fractions of the plot area, so the geom scales them and no scale from
|
|
452
|
+
the frame reaches either. The value is written under a treemap's name because a packed layout has
|
|
453
|
+
no axis to carry it, which is rule 9's own answer for a geom ending in a value.
|
|
454
|
+
*/
|
|
455
|
+
/* A genome round a circle. The sectors carry base pairs and the rings read the same rows. */
|
|
456
|
+
if (data.geom === "ideogram") {
|
|
457
|
+
const written = polarText(ctx, trace, metrics);
|
|
458
|
+
const layer = seriesOf(ctx.spec, trace.layerId);
|
|
459
|
+
const scale = (layer && layerScale(ctx.spec, layer)) ?? {};
|
|
460
|
+
return (_jsx(Ideogram, { sectors: data.sectors, tiles: data.tiles, levels: data.levels, links: data.links, linkDomain: data.linkDomain, width: width, height: height, identity: ctx.palettes.categorical, categoricalSize: ctx.palettes.categoricalSize, ramp: trace.sequential ?? ctx.palettes.sequential, center: scale.center ?? ctx.palettes.center, stain: ctx.palettes.stain, fill: style?.fill, opacity: style?.alpha, gap: style?.gap, rotate: style?.rotate, fontSize: written.numbers?.fontSize ?? metrics.axis, numbers: written.numbers, panels: seriesOf(ctx.spec, trace.layerId)?.rings ?? [], rings: data.rings }));
|
|
461
|
+
}
|
|
462
|
+
/* The same flow round a center, with an axis per sector in that sector's own units. */
|
|
463
|
+
if (data.geom === "chord") {
|
|
464
|
+
const written = polarText(ctx, trace, metrics);
|
|
465
|
+
const layer = seriesOf(ctx.spec, trace.layerId);
|
|
466
|
+
const scale = (layer && layerScale(ctx.spec, layer)) ?? {};
|
|
467
|
+
return (_jsx(Chord, { links: data.links, width: width, height: height, identity: ctx.palettes.categorical, categoricalSize: ctx.palettes.categoricalSize, ramp: trace.sequential ?? ctx.palettes.sequential, center: scale.center ?? ctx.palettes.center, fill: style?.fill, opacity: style?.alpha, gap: style?.gap, rotate: style?.rotate, inset: style?.inset, fontSize: written.numbers?.fontSize ?? metrics.axis, numbers: written.numbers, panels: seriesOf(ctx.spec, trace.layerId)?.rings ?? [], rings: data.rings, onHover: (found) => reportHit(ctx, found && {
|
|
468
|
+
hit: found.kind === "flow"
|
|
469
|
+
? { kind: "flow", trace, link: found.datum }
|
|
470
|
+
: { kind: "sector", trace, sector: found.datum },
|
|
471
|
+
at: found.at,
|
|
472
|
+
}) }));
|
|
473
|
+
}
|
|
474
|
+
/* A flow, which takes the panel whole the way a packed layout does and writes its own labels. */
|
|
475
|
+
if (data.geom === "sankey") {
|
|
476
|
+
return (_jsx(Sankey, { nodes: data.nodes, links: data.links, normalize: data.normalize, width: width, height: height, identity: ctx.palettes.categorical, fill: style?.fill, opacity: style?.alpha, format: (value) => numberOf(trace.field, value), fontSize: metrics.axis }));
|
|
477
|
+
}
|
|
478
|
+
if (data.geom === "treemap") {
|
|
479
|
+
return (_jsx(Treemap, { cells: data.cells, width: width, height: height, identity: ctx.palettes.categorical, levels: trace.colorLevels, fill: style?.fill, opacity: style?.alpha, stroke: style?.color, format: (value) => numberOf(trace.field, value), fontSize: metrics.axis }));
|
|
480
|
+
}
|
|
481
|
+
/* The same layout round a center, so it reads the cells the icicle reads and writes its numbers. */
|
|
482
|
+
if (data.geom === "sunburst") {
|
|
483
|
+
const written = polarText(ctx, trace, metrics);
|
|
484
|
+
return (_jsx(Sunburst, { cells: data.cells, width: width, height: height, identity: ctx.palettes.categorical, levels: trace.colorLevels, fill: style?.fill, opacity: style?.alpha, stroke: style?.color, numbers: written.numbers, names: written.names, onHover: (found) => reportHit(ctx, found && { hit: { kind: "node", trace, cell: found.datum }, at: found.at }), onSelect: ctx.onSelect ? (found) => ctx.onSelect?.({ kind: "node", trace, cell: found.datum }) : undefined }));
|
|
485
|
+
}
|
|
486
|
+
if (data.geom === "icicle") {
|
|
487
|
+
return (_jsx(Icicle, { cells: data.cells, width: width, height: height, identity: ctx.palettes.categorical, levels: trace.colorLevels, fill: style?.fill, opacity: style?.alpha, stroke: style?.color, fontSize: metrics.axis }));
|
|
488
|
+
}
|
|
489
|
+
/*
|
|
490
|
+
Signed steps, whose floors `stepped` resolved against the band's own order.
|
|
491
|
+
|
|
492
|
+
The sign takes the hue, so the fall reaches for a second entry of the categorical map instead of
|
|
493
|
+
the series' own. The search past it covers the figure that puts a waterfall second: the trace's
|
|
494
|
+
color is that map's entry already, and a fall painted the same hue leaves the direction unread.
|
|
495
|
+
*/
|
|
496
|
+
if (data.geom === "waterfall") {
|
|
497
|
+
const rise = style?.fill ?? trace.color;
|
|
498
|
+
const fall = [1, 2, 0].map(ctx.palettes.categorical).find((hue) => hue !== rise) ?? ctx.palettes.categorical(1);
|
|
499
|
+
return (_jsx(Waterfall, { data: data.bars, x: x, y: y, band: band || (transposed ? height : width) / Math.max(1, data.bars.length), transposed: transposed, maxWidth: style?.thickness, opacity: style?.alpha, stroke: style?.color, lineWidth: style?.linewidth, rise: rise, fall: fall, onHover: (found) => reportHit(ctx, found && { hit: { kind: "bar", trace, datum: found.datum }, at: found.at }), onSelect: ctx.onSelect ? (found) => ctx.onSelect?.({ kind: "bar", trace, datum: found.datum }) : undefined }));
|
|
500
|
+
}
|
|
501
|
+
if (data.geom === "bar") {
|
|
502
|
+
return (_jsx(Bars, { data: data.bars, x: x, y: y, band: band || (transposed ? height : width) / Math.max(1, data.bars.length), slot: data.slot, transposed: transposed, maxWidth: style?.thickness, color: trace.color, opacity: style?.alpha, stroke: style?.color, lineWidth: style?.linewidth, onHover: (found) => reportHit(ctx, found && { hit: { kind: "bar", trace, datum: found.datum }, at: found.at }), onSelect: ctx.onSelect ? (found) => ctx.onSelect?.({ kind: "bar", trace, datum: found.datum }) : undefined }));
|
|
503
|
+
}
|
|
504
|
+
if (data.geom === "line") {
|
|
505
|
+
return (_jsx(Line, { points: data.bars.map((bar) => ({
|
|
506
|
+
x: bar.at ?? bar.category,
|
|
507
|
+
// The stack's top is what the edge runs along, and its floor is where the fill closes.
|
|
508
|
+
y: bar.value + (bar.floor ?? 0),
|
|
509
|
+
...(bar.floor == null ? {} : { floor: bar.floor }),
|
|
510
|
+
})), x: x, y: y, color: trace.color, step: style?.step, lineWidth: style?.linewidth, opacity: style?.alpha,
|
|
511
|
+
/* The band path reaches this call site, so the bound dash is read here as well. */
|
|
512
|
+
dash: trace.linetypeChannel?.at ?? style?.linetype }));
|
|
513
|
+
}
|
|
514
|
+
if (data.geom === "freqpoly") {
|
|
515
|
+
return (_jsx(Freqpoly, { data: data.bars, x: x, y: y, color: trace.color, step: style?.step, lineWidth: style?.linewidth, opacity: style?.alpha, dash: trace.linetypeChannel?.at ?? style?.linetype }));
|
|
516
|
+
}
|
|
517
|
+
if (data.geom === "density") {
|
|
518
|
+
return (_jsx(DensityCurve, { curve: data.curve, x: x, y: y, color: trace.color, fill: style?.fill, fillOpacity: style?.alpha, lineWidth: style?.linewidth }));
|
|
519
|
+
}
|
|
520
|
+
if (data.geom === "area") {
|
|
521
|
+
return (_jsx(Area, { points: data.bars.map((bar) => ({
|
|
522
|
+
x: bar.at ?? bar.category,
|
|
523
|
+
// The stack's top is what the edge runs along, and its floor is where the fill closes.
|
|
524
|
+
y: bar.value + (bar.floor ?? 0),
|
|
525
|
+
...(bar.floor == null ? {} : { floor: bar.floor }),
|
|
526
|
+
})), x: x, y: y, color: trace.color, stroke: style?.color, lineWidth: style?.linewidth, opacity: style?.alpha }));
|
|
527
|
+
}
|
|
528
|
+
if (data.geom === "boxplot") {
|
|
529
|
+
return (_jsx(Box, { data: data.boxes, x: x, y: y, band: band, transposed: transposed, thickness: style?.thickness, opacity: style?.alpha, stroke: style?.color, lineWidth: style?.linewidth, ramp: trace.ramp, onHover: (found) => reportHit(ctx, found && { hit: { kind: "box", trace, datum: found.datum }, at: found.at }), onSelect: ctx.onSelect ? (found) => ctx.onSelect?.({ kind: "box", trace, datum: found.datum }) : undefined }));
|
|
530
|
+
}
|
|
531
|
+
if (data.geom === "violin") {
|
|
532
|
+
return (_jsx(Violin, { data: data.violins, x: x, y: y, band: band, transposed: transposed, thickness: style?.thickness, side: style?.half, opacity: style?.alpha, stroke: style?.color, lineWidth: style?.linewidth, ramp: trace.ramp, onHover: (found) => reportHit(ctx, found && { hit: { kind: "violin", trace, datum: found.datum }, at: found.at }), onSelect: ctx.onSelect ? (found) => ctx.onSelect?.({ kind: "violin", trace, datum: found.datum }) : undefined }));
|
|
533
|
+
}
|
|
534
|
+
if (data.geom === "heatmap") {
|
|
535
|
+
return (_jsx(Heatmap, { data: data.cells, x: x, y: y, xBand: xBand, yBand: child.yBand, ramp: trace.ramp, stroke: style?.color, lineWidth: style?.linewidth, opacity: style?.alpha, numbers: numbersOf(ctx, trace, metrics), cornerRadius: style?.cornerRadius, cellGap: style?.cellGap, fadeBySupport: style?.fadeBySupport, paper: (ctx.theme ?? LIGHT_THEME)["--viz-paper"], theme: ctx.theme ?? LIGHT_THEME, onHover: (found) => reportHit(ctx, found && { hit: { kind: "cell", trace, cell: found.datum }, at: found.at }), onSelect: ctx.onSelect ? (found) => ctx.onSelect?.({ kind: "cell", trace, cell: found.datum }) : undefined }));
|
|
536
|
+
}
|
|
537
|
+
/*
|
|
538
|
+
The polar geoms, which take the panel whole and read no scale off the frame.
|
|
539
|
+
|
|
540
|
+
A ring's slices come off the same `BarDatum` payload the bars path builds, so the hover reports
|
|
541
|
+
a `slice` hit that one readout serves beside the bars. The identity map colors by position in the
|
|
542
|
+
ring, and the identity itself travels on the label, which is what the coordinate is permitted on.
|
|
543
|
+
See the ADR "Polar is permitted where the figure writes its numbers".
|
|
544
|
+
*/
|
|
545
|
+
if (data.geom === "pie") {
|
|
546
|
+
const written = polarText(ctx, trace, metrics);
|
|
547
|
+
/* A series naming a sequential map paints its ordered slices at even steps along it. */
|
|
548
|
+
const named = seriesOf(ctx.spec, trace.layerId)?.palette;
|
|
549
|
+
const stepped = named ? ctx.palettes.sequentialFor?.(named) : undefined;
|
|
550
|
+
const last = Math.max(1, data.bars.length - 1);
|
|
551
|
+
return (_jsx(Pie, { data: data.bars, width: width, height: height, identity: stepped ? (index) => stepped(index / last) : ctx.palettes.categorical, fill: style?.fill, hole: style?.innerRadius ?? 0, opacity: style?.alpha, stroke: style?.color, lineWidth: style?.linewidth, numbers: written.numbers, names: written.names, onHover: (found) => reportHit(ctx, found && { hit: { kind: "slice", trace, datum: found.datum }, at: found.at }), onSelect: ctx.onSelect ? (found) => ctx.onSelect?.({ kind: "slice", trace, datum: found.datum }) : undefined }));
|
|
552
|
+
}
|
|
553
|
+
if (data.geom === "gauge") {
|
|
554
|
+
const written = polarText(ctx, trace, metrics);
|
|
555
|
+
return (_jsx(Gauge, { value: data.value, range: data.range, width: width, height: height, color: trace.color, thickness: style?.thickness, opacity: style?.alpha, sweep: style?.sweep, radius: style?.radius, bands: style?.sections, ramp: trace.sequential ?? ctx.palettes.sequential, numbers: written.numbers, label: axisLabel(trace.field) ?? trace.field?.label }));
|
|
556
|
+
}
|
|
557
|
+
return null;
|
|
558
|
+
}
|
|
559
|
+
/** Whether a value axis maps its domain straight onto pixels, low to high. */
|
|
560
|
+
const straight = (axis) => transformOf(axis) === "identity" && !axis.reverse && axis.domain[0] < axis.domain[1];
|
|
561
|
+
/** Two axis specs in the data's roles, as a reference line reads them. A band axis is null beside its categories. */
|
|
562
|
+
const ruleAxesOf = (x, y) => ({
|
|
563
|
+
x: "domain" in x ? x : null,
|
|
564
|
+
y: "domain" in y ? y : null,
|
|
565
|
+
...("values" in x ? { xBand: x.values } : {}),
|
|
566
|
+
...("values" in y ? { yBand: y.values } : {}),
|
|
567
|
+
});
|
|
568
|
+
/** Panel height that gives the data a fixed aspect, over the margin and the sides. Null over a band. */
|
|
569
|
+
function ratioHeight(frame, width, room, ratio) {
|
|
570
|
+
const { margin } = frame;
|
|
571
|
+
const plot = fixedHeight(frame.x, frame.y, width - margin.left - margin.right - room.left - room.right, ratio);
|
|
572
|
+
return plot == null ? null : Math.max(1, Math.round(plot + margin.top + margin.bottom + room.top + room.bottom));
|
|
573
|
+
}
|
|
574
|
+
/** The series a trace was built from, for a guide reading what that series stated. */
|
|
575
|
+
const seriesOf = (spec, layerId) => spec.layers.find((held) => held.id === layerId);
|
|
576
|
+
/**
|
|
577
|
+
* The text a polar geom writes on itself, off the series' own two fields.
|
|
578
|
+
*
|
|
579
|
+
* `numbers` writes the value and `label` writes the category, and a ring carrying neither draws
|
|
580
|
+
* `angle-encodes-comparison` at `bad`. A `percent` format asks for the share of the total instead of
|
|
581
|
+
* the raw reduction, since a reader of a pie is usually after the share and the geom is the one
|
|
582
|
+
* holding the total.
|
|
583
|
+
*/
|
|
584
|
+
function polarText(ctx, trace, metrics) {
|
|
585
|
+
const series = ctx.spec.layers.find((held) => held.id === trace.layerId);
|
|
586
|
+
const numbers = numbersOf(ctx, trace, metrics);
|
|
587
|
+
return {
|
|
588
|
+
numbers: numbers && { ...numbers, share: series?.numbers?.format === "percent" },
|
|
589
|
+
names: series?.directLabels?.when.kind === "all",
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* What draws into one side panel.
|
|
594
|
+
*
|
|
595
|
+
* The frame owns where the panel sits and this owns what goes in it, which is the split the `side`
|
|
596
|
+
* render prop makes. The panel arrives with the geoms' own scales inside a group already translated
|
|
597
|
+
* to the plot area's origin along the shared axis, so nothing here builds a scale: reading the band
|
|
598
|
+
* scale for a category gives the pixel that category's cells were drawn at.
|
|
599
|
+
*
|
|
600
|
+
* The dendrogram is the only content built. The track, the histogram and the rug are steps 4 and 5
|
|
601
|
+
* of the ADR "A figure takes side panels that share one axis".
|
|
602
|
+
*/
|
|
603
|
+
function sideContent(child, frame, palettes, dark, style) {
|
|
604
|
+
const across = child.panel.position === "top" || child.panel.position === "bottom";
|
|
605
|
+
const axis = across ? "x" : "y";
|
|
606
|
+
if (child.panel.content.kind === "dendrogram") {
|
|
607
|
+
const tree = frame.clusterTrees[axis];
|
|
608
|
+
/* A band with no clustering behind it draws nothing, and the critic refuses the spec as wrong. */
|
|
609
|
+
if (!tree)
|
|
610
|
+
return null;
|
|
611
|
+
return (_jsx(DendrogramPanel, { tree: tree, at: across ? child.x : child.y, position: child.panel.position, width: child.width, height: child.height, depth: child.panel.content.depth }));
|
|
612
|
+
}
|
|
613
|
+
if (child.panel.content.kind === "track") {
|
|
614
|
+
const field = child.panel.content.field;
|
|
615
|
+
const track = frame.tracks.find((held) => held.field === field && held.axis === axis);
|
|
616
|
+
/* A track that resolved to nothing draws nothing, and the critic says which of the three ways. */
|
|
617
|
+
if (!track)
|
|
618
|
+
return null;
|
|
619
|
+
return (_jsx(TrackPanel, { track: track, at: across ? child.x : child.y, band: across ? child.xBand : child.yBand, position: child.panel.position, width: child.width, height: child.height, fill: trackFill(track, palettes, dark), stroke: style?.color, lineWidth: style?.linewidth }));
|
|
620
|
+
}
|
|
621
|
+
const marginal = frame.marginals.find((held) => held.axis === axis && held.kind === child.panel.content.kind);
|
|
622
|
+
/* A marginal on a band has no measure to distribute, which the critic reports. */
|
|
623
|
+
if (!marginal)
|
|
624
|
+
return null;
|
|
625
|
+
const along = across ? child.x : child.y;
|
|
626
|
+
const shared = { marginal, along, position: child.panel.position, width: child.width, height: child.height };
|
|
627
|
+
/* One trace lends the margin its own hue. Pooled traces keep the muted ink. */
|
|
628
|
+
const tint = frame.traces.length === 1 ? frame.traces[0].color : undefined;
|
|
629
|
+
if (child.panel.content.kind === "histogram")
|
|
630
|
+
return _jsx(HistogramPanel, { ...shared, fill: tint });
|
|
631
|
+
if (child.panel.content.kind === "density")
|
|
632
|
+
return _jsx(DensityPanel, { ...shared });
|
|
633
|
+
return _jsx(RugPanel, { ...shared, stroke: tint });
|
|
634
|
+
}
|
|
635
|
+
/**
|
|
636
|
+
* A chart, from the spec that describes it. The one component the package draws through.
|
|
637
|
+
*
|
|
638
|
+
* The package's own claim is that a chart is data: the same spec renders in a notebook, on a
|
|
639
|
+
* dashboard and in an export. This is where that becomes true for a caller. `plot()` authors a spec
|
|
640
|
+
* and hands it here, the builder panel edits one and hands it here, and a spec read back from a
|
|
641
|
+
* store takes the same route.
|
|
642
|
+
*
|
|
643
|
+
* A namespace of eighteen kind components used to sit over this, each compiling its own props
|
|
644
|
+
* through a second writer of `ChartSpec`. Two writers of one schema is what the ADR "Authoring is a
|
|
645
|
+
* builder over the chart spec" removed: the props path emitted one series and could not express a
|
|
646
|
+
* layered figure at all, and the two drifted, so a chart built through the namespace compiled a spec
|
|
647
|
+
* asking for no curve and the `fitter` below could never fire.
|
|
648
|
+
*
|
|
649
|
+
* The data goes through `toPlotData` memoized on reference identity and the frame is rebuilt only
|
|
650
|
+
* when the spec or the data moved, so a caller holding both steady re-renders without recomputing
|
|
651
|
+
* either.
|
|
652
|
+
*/
|
|
653
|
+
export function Viz({ spec: authored, data, fieldOverrides, keyField, set, palettes: chosen, fitter, zoom = false, brush, onBrush, onCritique, onSelect, toolbar, }) {
|
|
654
|
+
/* Keyed on what the overrides say. An object written inline is a new identity each render. */
|
|
655
|
+
const fieldsKey = JSON.stringify(fieldOverrides ?? null);
|
|
656
|
+
const plotData = useMemo(() => toPlotData(data, { fieldOverrides, keyField }),
|
|
657
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- `fieldsKey` is the overrides as the table reads them
|
|
658
|
+
[data, fieldsKey, keyField]);
|
|
659
|
+
/* The reader's toolbar choices the toolbar still offers, laid over the authored spec. Empty returns the spec itself. */
|
|
660
|
+
const [chosenView, setToolView] = useState(NO_VIEW);
|
|
661
|
+
const exporting = useRef(false);
|
|
662
|
+
const facetKey = JSON.stringify(toolbar?.facet?.by ?? null);
|
|
663
|
+
const facetBy = useMemo(() => facetFields(plotData, toolbar?.facet?.by, authored.facet?.by),
|
|
664
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- `facetKey` is the host's facet list by content
|
|
665
|
+
[plotData, facetKey, authored.facet?.by]);
|
|
666
|
+
const toolKey = JSON.stringify(toolbar ? [toolbar.items, toolbar.palette?.options ?? null] : null);
|
|
667
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- `toolKey` is the listed items and maps by content
|
|
668
|
+
const toolView = useMemo(() => viewOf(chosenView, toolbar, facetBy), [chosenView, toolKey, facetBy]);
|
|
669
|
+
const targetsKey = JSON.stringify(toolbar?.colorblind ?? null);
|
|
670
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- `targetsKey` is the targets by content
|
|
671
|
+
const targets = useMemo(() => ({ ...COLORBLIND, ...toolbar?.colorblind }), [targetsKey]);
|
|
672
|
+
const spec = useMemo(() => viewSpec(authored, toolView, targets), [authored, toolView, targets]);
|
|
673
|
+
const { headline, subtitle, caption } = spec;
|
|
674
|
+
const { holder, element: measuredElement, width: measured } = useMeasuredWidth();
|
|
675
|
+
/**
|
|
676
|
+
* One facet cell, measured.
|
|
677
|
+
*
|
|
678
|
+
* The grid decides how wide a panel is. It knows what else is in the row, what the gaps come to and
|
|
679
|
+
* what the container gave it, and arithmetic beside it does not: computing a panel width from the
|
|
680
|
+
* figure's own measurement put the drawings twenty pixels wider than the cells they sat in, because
|
|
681
|
+
* the legend was in the row and the sum had not been told. So one cell reports its width and every
|
|
682
|
+
* panel takes it, which holds because the tracks are equal fractions and equal fractions are equal.
|
|
683
|
+
*
|
|
684
|
+
* The first cell and not each of them: five observers would answer five times with one number.
|
|
685
|
+
*/
|
|
686
|
+
const { holder: cellHolder, width: cellWidth } = useMeasuredWidth();
|
|
687
|
+
/* The measured element as a ref object, one per element, for the theme to read. */
|
|
688
|
+
const themeRef = useMemo(() => ({ current: measuredElement }), [measuredElement]);
|
|
689
|
+
const theme = useVizTheme(themeRef);
|
|
690
|
+
/* The magnitude map spends its range on the half of itself the paper leaves visible. */
|
|
691
|
+
const dark = theme ? onDarkPaper(theme) : false;
|
|
692
|
+
const { palette: picked, colorblind } = toolView;
|
|
693
|
+
const palettes = useMemo(() => viewPalettes(chosen ?? palettesFor({ dark }), { palette: picked, colorblind }, targets, dark), [chosen, dark, picked, colorblind, targets]);
|
|
694
|
+
/*
|
|
695
|
+
Keyed on the facet field and the table, which is all `partitionOf` reads.
|
|
696
|
+
|
|
697
|
+
Keying it on the whole spec re-cut a quarter of a million rows every time a color moved, which
|
|
698
|
+
is the split the function's own docstring claims and the dependency list defeated.
|
|
699
|
+
*/
|
|
700
|
+
const partition = useMemo(() => partitionOf(spec, plotData), [spec.facet?.by, plotData]);
|
|
701
|
+
/*
|
|
702
|
+
The panel's measured width, then the stated width, then the fallback.
|
|
703
|
+
|
|
704
|
+
A stated width sizes the figure. The panel measures what the row leaves it beside a side guide.
|
|
705
|
+
*/
|
|
706
|
+
const width = measured || spec.width || UNMEASURED_WIDTH;
|
|
707
|
+
/* Panel size decides the sizing class, and a facet cuts the width into columns first. */
|
|
708
|
+
const facets = partition.field ? Math.min(partition.groups.length, FACET_CAP) || 1 : 1;
|
|
709
|
+
/* An author's count wins over the width, since asking for a row is saying which to lose. */
|
|
710
|
+
const asked = spec.facet?.columns;
|
|
711
|
+
const columns = asked != null && asked > 0
|
|
712
|
+
? Math.max(1, Math.min(facets, Math.trunc(asked)))
|
|
713
|
+
: Math.max(1, Math.min(facets, Math.floor((width + FACET_GAP) / (FACET_MIN + FACET_GAP)) || 1));
|
|
714
|
+
const panelWidth = facets > 1 ? Math.floor((width - (columns - 1) * FACET_GAP) / columns) : width;
|
|
715
|
+
/*
|
|
716
|
+
The height, resolved against the width one panel actually got.
|
|
717
|
+
|
|
718
|
+
A stated height is the figure's. A facet's rows divide it, less their strips, the gaps between
|
|
719
|
+
them and the axis title under the grid. A ratio is read per panel, and each of twelve panels holds
|
|
720
|
+
the shape. An explicit height is the more specific instruction and wins.
|
|
721
|
+
*/
|
|
722
|
+
const facetRows = Math.ceil(facets / columns);
|
|
723
|
+
const perRow = (whole) => partition.field
|
|
724
|
+
? Math.max(1, Math.floor((whole - (facetRows - 1) * FACET_GAP - facetRows * STRIP_HEIGHT - SHARED_TITLE_HEIGHT) / facetRows))
|
|
725
|
+
: whole;
|
|
726
|
+
const statedHeight = spec.height != null
|
|
727
|
+
? perRow(spec.height)
|
|
728
|
+
: spec.aspect
|
|
729
|
+
? Math.max(1, Math.round(panelWidth * spec.aspect))
|
|
730
|
+
: DEFAULT_HEIGHT;
|
|
731
|
+
/*
|
|
732
|
+
The reader's ranges, and the build with them laid over it.
|
|
733
|
+
|
|
734
|
+
A third pass and not part of the build: a range changes which records are drawn and nothing
|
|
735
|
+
about what they are, so narrowing through `buildPanels` would re-walk the table on each frame
|
|
736
|
+
of a drag to arrive at the lines already in hand. The count comes back with the panels because
|
|
737
|
+
the footer states what was kept.
|
|
738
|
+
*/
|
|
739
|
+
const [ranges, setRanges] = useState({});
|
|
740
|
+
/*
|
|
741
|
+
The plot area a stated band size asks for, before any margin is known.
|
|
742
|
+
|
|
743
|
+
This is what breaks the circle the inversion creates. Going the usual way the panel is given, the
|
|
744
|
+
margin comes off the metrics and the plot area is what is left; going this way the plot area is
|
|
745
|
+
stated and the panel is what it plus the margin comes to, so the margin would need metrics that
|
|
746
|
+
need a panel that needs the margin. Reading the categories off the table settles the plot area
|
|
747
|
+
first, and the metrics come off whichever end is known.
|
|
748
|
+
|
|
749
|
+
Counted from the table and not from a built frame, because the build takes the metrics as an
|
|
750
|
+
argument and cannot run before them.
|
|
751
|
+
*/
|
|
752
|
+
const content = useMemo(() => (spec.sizing === "content" ? contentExtent(spec, plotData) : null), [
|
|
753
|
+
spec.sizing,
|
|
754
|
+
spec.scales?.x?.bandSize,
|
|
755
|
+
spec.scales?.y?.bandSize,
|
|
756
|
+
spec.scales?.x?.order,
|
|
757
|
+
spec.scales?.y?.order,
|
|
758
|
+
spec.layers,
|
|
759
|
+
plotData,
|
|
760
|
+
]);
|
|
761
|
+
const metrics = metricsFor(content?.width ?? panelWidth, content?.height ?? statedHeight);
|
|
762
|
+
/*
|
|
763
|
+
The expensive half, keyed on the spec as the build reads it.
|
|
764
|
+
|
|
765
|
+
`shapeOf` leaves out the one field `paint` reapplies, so a color the author picks reuses the
|
|
766
|
+
layers and repaints a couple of dozen traces. Keying on the spec object rebuilt a quarter of a
|
|
767
|
+
million points on each frame of a color drag, which is what `shapeOf`, `colorsOf` and `paint`
|
|
768
|
+
were written to prevent while nothing outside the tests called them.
|
|
769
|
+
*/
|
|
770
|
+
const shape = useMemo(() => shapeOf(spec), [spec]);
|
|
771
|
+
const colors = useMemo(() => colorsOf(spec), [spec]);
|
|
772
|
+
/* The pinned colors by content, for the same reason as the fields. */
|
|
773
|
+
const assignmentsKey = JSON.stringify(set?.assignments ?? null);
|
|
774
|
+
const layers = useMemo(() => buildPanels(spec, plotData, { palettes, partition, metrics, pinned: set?.assignments }),
|
|
775
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- `shape` is the spec as the build reads it
|
|
776
|
+
[shape, plotData, palettes, partition, metrics, assignmentsKey]);
|
|
777
|
+
/* Returns what it was handed where nothing carries a color of its own, so identity survives. */
|
|
778
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- `colors` is the spec as `paint` reads it
|
|
779
|
+
const painted = useMemo(() => paint(layers, spec.layers, palettes, spec.scales), [layers, colors, palettes]);
|
|
780
|
+
/* The reader's ranges laid over the build, which is a filter and not a rebuild. */
|
|
781
|
+
const { panels: built, kept } = useMemo(() => narrow(painted, ranges), [painted, ranges]);
|
|
782
|
+
const [hovers] = useState(hoverStore);
|
|
783
|
+
const panelCount = built.status === "ready" ? built.panels.length : 0;
|
|
784
|
+
const firstFrame = built.status === "ready" ? (built.panels[0]?.frame ?? null) : null;
|
|
785
|
+
/*
|
|
786
|
+
A side is room the plot area gives up, so a figure sized to its content asks for it on top.
|
|
787
|
+
|
|
788
|
+
Going the other way the sides would eat into a stated band size, and a dendrogram beside a
|
|
789
|
+
twelve-pixel row would leave the rows at ten. Under a facet the sides are refused: the panels
|
|
790
|
+
share one band and a cluster order belongs to the cells that produced it, so a tree drawn beside
|
|
791
|
+
each panel would point at rows that panel never held. The critic says so in the same breath.
|
|
792
|
+
*/
|
|
793
|
+
const sides = spec.facet?.by ? undefined : spec.sides;
|
|
794
|
+
const room = sideRoom(sides);
|
|
795
|
+
/* A fixed data aspect sets the height off the width and both domains. A facet's panels share them. */
|
|
796
|
+
const height = (spec.ratio != null && firstFrame && ratioHeight(firstFrame, panelWidth, room, spec.ratio)) || statedHeight;
|
|
797
|
+
/*
|
|
798
|
+
The reader's own view, over the one panel.
|
|
799
|
+
|
|
800
|
+
A facet is a small multiple over shared scales, so twelve panels each holding their own window
|
|
801
|
+
would be twelve charts that no longer compare. Sharing one window across them is the right
|
|
802
|
+
answer, and it wants a gesture resolved against whichever panel the pointer is over, which is
|
|
803
|
+
its own piece of work. So a faceted chart does not take the gesture.
|
|
804
|
+
*/
|
|
805
|
+
/*
|
|
806
|
+
A listed drag mode takes the drag. The wheel stays with the `zoom` prop.
|
|
807
|
+
|
|
808
|
+
A figure without axes takes neither. Its toolbar lists no view tool.
|
|
809
|
+
*/
|
|
810
|
+
const axisFree = firstFrame?.axisFree === true;
|
|
811
|
+
const listed = toolbar?.items ?? NO_TOOLS;
|
|
812
|
+
const tools = axisFree ? listed.filter((item) => !VIEW_TOOLS.includes(item)) : listed;
|
|
813
|
+
const drags = dragsOf(tools);
|
|
814
|
+
const drag = drags.length === 0 ? undefined : (toolView.drag ?? drags.find((mode) => mode === brush) ?? drags[0]);
|
|
815
|
+
const brushShape = axisFree
|
|
816
|
+
? undefined
|
|
817
|
+
: drag === undefined
|
|
818
|
+
? brush
|
|
819
|
+
: drag === "box" || drag === "lasso"
|
|
820
|
+
? drag
|
|
821
|
+
: undefined;
|
|
822
|
+
const takesGesture = !axisFree && (zoom || drags.length > 0 || tools.includes("zoom") || tools.includes("reset"));
|
|
823
|
+
const view = useZoom(takesGesture && panelCount === 1, panelWidth, height, drag === undefined ? zoom && brushShape == null : drag === "pan", zoom);
|
|
824
|
+
/* A move off a brush mode clears the held selection with the host. */
|
|
825
|
+
const lastShape = useRef(brushShape);
|
|
826
|
+
useEffect(() => {
|
|
827
|
+
if (lastShape.current != null && lastShape.current !== brushShape)
|
|
828
|
+
onBrush?.(null);
|
|
829
|
+
lastShape.current = brushShape;
|
|
830
|
+
}, [brushShape, onBrush]);
|
|
831
|
+
/* The sole panel's size, and its plot area in the gesture's pixels for the zoom buttons and the zoom box. */
|
|
832
|
+
const soleWidth = firstFrame && content?.width != null
|
|
833
|
+
? content.width + firstFrame.margin.left + firstFrame.margin.right + room.left + room.right
|
|
834
|
+
: panelWidth;
|
|
835
|
+
const soleHeight = firstFrame && content?.height != null
|
|
836
|
+
? content.height + firstFrame.margin.top + firstFrame.margin.bottom + room.top + room.bottom
|
|
837
|
+
: height;
|
|
838
|
+
const soleArea = firstFrame && panelCount === 1 ? plotArea(soleWidth, soleHeight, firstFrame.margin) : null;
|
|
839
|
+
/*
|
|
840
|
+
A click carries what a query can act on, not only what the reader touched.
|
|
841
|
+
|
|
842
|
+
`selectionOf` reads the clicked geom against the compiled spec and the table's key column, so
|
|
843
|
+
the predicate names records instead of pixels. Absent where the author took no callback, which
|
|
844
|
+
is what keeps the geoms free of a click handler nobody is listening for.
|
|
845
|
+
|
|
846
|
+
A pan across a scatter ends over a geom, and the click that follows it is the gesture ending and
|
|
847
|
+
not a reader choosing that record, so the viewport gets asked before the callback fires.
|
|
848
|
+
*/
|
|
849
|
+
const panned = view.panned;
|
|
850
|
+
/* The press under a click, and whether it moved past the drag floor. A turn, a grip or a note drag ends in a click. */
|
|
851
|
+
const press = useRef({ x: 0, y: 0, down: false, moved: false });
|
|
852
|
+
const gesture = {
|
|
853
|
+
onPointerDownCapture: (event) => {
|
|
854
|
+
press.current = { x: event.clientX, y: event.clientY, down: true, moved: false };
|
|
855
|
+
},
|
|
856
|
+
onPointerMoveCapture: (event) => {
|
|
857
|
+
const held = press.current;
|
|
858
|
+
if (held.down && Math.hypot(event.clientX - held.x, event.clientY - held.y) >= DRAG_FLOOR)
|
|
859
|
+
held.moved = true;
|
|
860
|
+
},
|
|
861
|
+
onPointerUpCapture: () => {
|
|
862
|
+
press.current.down = false;
|
|
863
|
+
},
|
|
864
|
+
};
|
|
865
|
+
/* Whether a geom took the click in flight, and whether a selection is held for an empty click to clear. */
|
|
866
|
+
const claimed = useRef(false);
|
|
867
|
+
const holding = useRef(false);
|
|
868
|
+
const reportSelect = useMemo(() => onSelect
|
|
869
|
+
? (hit) => {
|
|
870
|
+
if (panned() || press.current.moved)
|
|
871
|
+
return;
|
|
872
|
+
claimed.current = true;
|
|
873
|
+
holding.current = true;
|
|
874
|
+
onSelect(selectedOf(hit, { spec, data: plotData }));
|
|
875
|
+
}
|
|
876
|
+
: undefined, [onSelect, spec, plotData, panned]);
|
|
877
|
+
/* A click that reached the panels with no geom under it clears what is held. Geoms hear it first. */
|
|
878
|
+
const clearSelect = onSelect
|
|
879
|
+
? () => {
|
|
880
|
+
const took = claimed.current;
|
|
881
|
+
const dragged = panned() || press.current.moved;
|
|
882
|
+
claimed.current = false;
|
|
883
|
+
press.current.moved = false;
|
|
884
|
+
if (took || dragged || !holding.current)
|
|
885
|
+
return;
|
|
886
|
+
holding.current = false;
|
|
887
|
+
onSelect(null);
|
|
888
|
+
}
|
|
889
|
+
: undefined;
|
|
890
|
+
const [substituted, setSubstituted] = useState(null);
|
|
891
|
+
/*
|
|
892
|
+
Held on what the substitution says and not on the object saying it.
|
|
893
|
+
|
|
894
|
+
A geom reports what it drew from an effect, and the answer is a fresh object each time it fires.
|
|
895
|
+
Setting state from that closes a circle wherever the reporting effect's own inputs are rebuilt per
|
|
896
|
+
render, and the footer sentence never changed: the caller re-renders, the effect fires, the state
|
|
897
|
+
takes a new identity, and round again. Same answer `onCritique` takes, which is to compare what
|
|
898
|
+
was said instead of what carried it.
|
|
899
|
+
*/
|
|
900
|
+
const onSubstitute = useMemo(() => (panel, sub) => {
|
|
901
|
+
if (panel === 0)
|
|
902
|
+
setSubstituted((held) => (sameSubstitution(held, sub) ? held : sub));
|
|
903
|
+
}, []);
|
|
904
|
+
/*
|
|
905
|
+
The plot area the geoms were actually given, for a finding that has to reason in drawn pixels.
|
|
906
|
+
|
|
907
|
+
The panel less its margin and less what the sides took, which is `renderPanel`'s own arithmetic
|
|
908
|
+
for the container path. A figure that stated a band size needs none of this and the critic reads
|
|
909
|
+
the stated number first; this is the grid that left the decision to the layout, where working it
|
|
910
|
+
out is the only way to know a cell is four pixels tall.
|
|
911
|
+
*/
|
|
912
|
+
const area = useMemo(() => firstFrame
|
|
913
|
+
? {
|
|
914
|
+
width: Math.max(0, panelWidth - firstFrame.margin.left - firstFrame.margin.right - room.left - room.right),
|
|
915
|
+
height: Math.max(0, height - firstFrame.margin.top - firstFrame.margin.bottom - room.top - room.bottom),
|
|
916
|
+
}
|
|
917
|
+
: undefined, [firstFrame, panelWidth, height, room.left, room.right, room.top, room.bottom]);
|
|
918
|
+
/*
|
|
919
|
+
One hook over the whole plot, per the ADR on facets, and one ask covering its panels. A group
|
|
920
|
+
is keyed by its panel as well as its split value, so two panels holding the same value get a
|
|
921
|
+
curve each, fitted on their own rows. `fitRequestsOver` merges per series on the way out, so
|
|
922
|
+
the fitter still receives one request per series whatever the panel count.
|
|
923
|
+
*/
|
|
924
|
+
const panelTraces = useMemo(() => built.status === "ready" ? built.panels.map((panel) => ({ value: panel.value, traces: panel.frame.traces })) : [], [built]);
|
|
925
|
+
const requests = useMemo(() => fitRequestsOver(panelTraces, spec.layers), [panelTraces, spec.layers]);
|
|
926
|
+
const fits = useFits(requests, fitter);
|
|
927
|
+
const findings = useMemo(
|
|
928
|
+
/* The measured element, so the fit check can say a content-sized figure outgrew its column. */
|
|
929
|
+
() => critique(spec, {
|
|
930
|
+
frame: firstFrame ?? undefined,
|
|
931
|
+
substituted: substituted ?? undefined,
|
|
932
|
+
container: measured || undefined,
|
|
933
|
+
area,
|
|
934
|
+
}), [spec, firstFrame, substituted, measured]);
|
|
935
|
+
/*
|
|
936
|
+
Reported when the findings change and not when the array is rebuilt.
|
|
937
|
+
|
|
938
|
+
Setting state from `onCritique` is the obvious use and what the demo does, and it closed a
|
|
939
|
+
circle: the caller re-renders, a `scales` written inline is a new object, the spec
|
|
940
|
+
memo misses on it, the findings array is rebuilt, and the callback fires again. The findings
|
|
941
|
+
themselves were identical every time round. Keying on what a finding says is the same answer
|
|
942
|
+
`shapeOf` gives the build, and it holds whatever a caller does with their props.
|
|
943
|
+
*/
|
|
944
|
+
const reported = findings.map((finding) => `${finding.code}:${finding.path}:${finding.message}`).join("|");
|
|
945
|
+
/* The latest callback, read when the findings change. An arrow written inline is a new identity each render. */
|
|
946
|
+
const critiqued = useRef(onCritique);
|
|
947
|
+
critiqued.current = onCritique;
|
|
948
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- `reported` is the findings as a reader would read them
|
|
949
|
+
useEffect(() => {
|
|
950
|
+
critiqued.current?.(findings);
|
|
951
|
+
}, [reported]);
|
|
952
|
+
if (built.status === "empty") {
|
|
953
|
+
return (_jsx("div", { ref: holder, style: spec.width == null ? undefined : { width: spec.width }, children: _jsx(Figure, { headline: headline, subtitle: subtitle, children: _jsxs("div", { className: "flex flex-col items-center justify-center gap-1 text-sm", style: { height, color: "var(--viz-ink-muted)" }, children: [_jsx("span", { className: "font-medium", children: built.title }), _jsx("span", { children: built.reason })] }) }) }));
|
|
954
|
+
}
|
|
955
|
+
/*
|
|
956
|
+
A zoom is a step that drops data, so it says so.
|
|
957
|
+
|
|
958
|
+
It leads the footer because `held` is what the reader themselves took out, and it is the one
|
|
959
|
+
line on that list they can put back. Without it a reader arriving at a zoomed figure reads a
|
|
960
|
+
slice of a measure as the whole of it.
|
|
961
|
+
*/
|
|
962
|
+
const held = [
|
|
963
|
+
...(kept ? [`${kept.within.toLocaleString()} of ${kept.of.toLocaleString()} records inside the ranges`] : []),
|
|
964
|
+
...(view.zoomed ? ["showing part of the range"] : []),
|
|
965
|
+
...overrideNotes(spec, findings),
|
|
966
|
+
];
|
|
967
|
+
const footer = firstFrame
|
|
968
|
+
? footerOf(firstFrame, {
|
|
969
|
+
fitted: fits,
|
|
970
|
+
facetDropped: built.facetDropped,
|
|
971
|
+
held,
|
|
972
|
+
substituted: substituted ?? undefined,
|
|
973
|
+
})
|
|
974
|
+
: undefined;
|
|
975
|
+
const footerLine = [footer, caption].filter(Boolean).join(" · ") || undefined;
|
|
976
|
+
const legend = firstFrame && firstFrame.legend.length > 1 ? firstFrame.legend : null;
|
|
977
|
+
/* The traces a guide reads. A layer that keeps out of the legend reaches none of them. */
|
|
978
|
+
const guided = firstFrame?.traces.filter((trace) => trace.showLegend !== false);
|
|
979
|
+
/* What the swatches are of: the identity channel's field, or the field a series split on. */
|
|
980
|
+
const named = guided?.find((trace) => trace.colorLevels?.length || (trace.grouped ? undefined : trace.splitField));
|
|
981
|
+
const legendTitle = axisLabel(named?.colorField ?? (named?.grouped ? undefined : named?.splitField)) ?? undefined;
|
|
982
|
+
/* The scale the swatches are titled off: the layer's own over the chart's, on the layer's paint aesthetic. */
|
|
983
|
+
const swatched = seriesOf(spec, (named ?? guided?.[0])?.layerId ?? "");
|
|
984
|
+
const swatchScale = swatched ? layerScale(spec, swatched) : spec.scales?.color;
|
|
985
|
+
const legendIndent = firstFrame ? (firstFrame.axisFree ? WHOLE_PANEL.left : firstFrame.margin.left) : 0;
|
|
986
|
+
/*
|
|
987
|
+
The cap the footer already states, held to.
|
|
988
|
+
|
|
989
|
+
`fitNote` writes "bands hidden above 12 curves" off the same count, so the line under the chart
|
|
990
|
+
and the paint on it come from one comparison. Counted over the plot and not per series, since
|
|
991
|
+
the shaded areas compound on one frame whoever asked for them.
|
|
992
|
+
*/
|
|
993
|
+
const bandsDrawn = firstFrame ? drawnFits(firstFrame.traces, fits).length <= BAND_LIMIT : true;
|
|
994
|
+
/* A bound size channel draws geoms nobody can read back without one. */
|
|
995
|
+
const sized = guided?.find((trace) => trace.sizeField && trace.sizeDomain && trace.sizeRadii);
|
|
996
|
+
/*
|
|
997
|
+
The three channels that keep a figure readable once its hue is gone, each with a guide.
|
|
998
|
+
|
|
999
|
+
A shape and a categorical opacity carry their levels on one trace, since both resolve per mark
|
|
1000
|
+
and one trace holds every mark. A dash is a pattern per trace, so its guide is gathered back off
|
|
1001
|
+
the set the way the series legend gathers its rows.
|
|
1002
|
+
|
|
1003
|
+
A continuous opacity draws no guide. Three sample marks at three opacities make a ramp a reader
|
|
1004
|
+
cannot take a number off, and the channel is weak enough that the honest answer is to leave the
|
|
1005
|
+
reading to the position scales. `opacityChannel.domain` sits on the trace for a caller who wants
|
|
1006
|
+
to draw one anyway.
|
|
1007
|
+
*/
|
|
1008
|
+
const shaped = guided?.find((trace) => trace.shapeChannel?.levels?.length);
|
|
1009
|
+
const faded = guided?.find((trace) => trace.alphaChannel?.levels?.length);
|
|
1010
|
+
const dashed = guided?.filter((trace) => trace.linetypeChannel) ?? [];
|
|
1011
|
+
const dashLevels = [...new Map(dashed.map((trace) => [trace.linetypeChannel.value, trace.linetypeChannel.at]))];
|
|
1012
|
+
/** Each guide's title, keyed by the aesthetic it maps. The author's word wins over the field's. */
|
|
1013
|
+
const scales = spec.scales;
|
|
1014
|
+
/*
|
|
1015
|
+
Gated on the domain and not on a bound field, which is what lets a grid draw one.
|
|
1016
|
+
|
|
1017
|
+
A heatmap cell holds a reduction, so no field sits on its color channel and `colorField` is
|
|
1018
|
+
absent; the domain is what says a scale ran. `trace.name` already carries what the reduction is
|
|
1019
|
+
called, which is the label a bound field would have supplied.
|
|
1020
|
+
|
|
1021
|
+
Filtered and not found. One bar a chart is what left reference figure 1 short: two series each
|
|
1022
|
+
binding a measure to color drew off two domains and got one guide between them, so a reader had
|
|
1023
|
+
no way to tell which measure a color came from. A split gives many traces off one series and
|
|
1024
|
+
they share a scale, so the set is cut to one bar per column.
|
|
1025
|
+
*/
|
|
1026
|
+
const colored = guided?.filter((trace) => trace.colorDomain) ?? [];
|
|
1027
|
+
const colorBars = [...new Map(colored.map((trace) => [trace.colorField?.id ?? trace.layerId, trace])).values()];
|
|
1028
|
+
/*
|
|
1029
|
+
A legend per annotation track, since a strip of colors nobody can read back is decoration.
|
|
1030
|
+
|
|
1031
|
+
Each is labeled with its own column, which the series legend does not need and two tracks beside
|
|
1032
|
+
one grid do. A category track samples the figure's own categorical map, so a value keeps the color
|
|
1033
|
+
it has anywhere else in the set; a measure track draws its own bar off the map it named.
|
|
1034
|
+
*/
|
|
1035
|
+
const tracks = firstFrame?.tracks ?? [];
|
|
1036
|
+
/*
|
|
1037
|
+
Where the legends sit, and which way they read.
|
|
1038
|
+
|
|
1039
|
+
Rule 2 asks for a legend outside the plot area and writes names under the chart as its worked
|
|
1040
|
+
example. All four edges are outside it, so the rule's argument holds on each; placement inside
|
|
1041
|
+
the panel is the thing it argues against and has no setting here.
|
|
1042
|
+
|
|
1043
|
+
Stacked on a side and run across on a top or a bottom, since a column of rows beside a chart
|
|
1044
|
+
reads down and a row under one reads along. An author can say otherwise.
|
|
1045
|
+
*/
|
|
1046
|
+
const placement = spec.legend?.position ?? "bottom";
|
|
1047
|
+
const onSide = placement === "left" || placement === "right";
|
|
1048
|
+
const legendsFirst = placement === "left" || placement === "top";
|
|
1049
|
+
const stacked = (spec.legend?.direction ?? (onSide ? "vertical" : "horizontal")) === "vertical";
|
|
1050
|
+
const along = { start: "items-start", center: "items-center", end: "items-end" }[spec.legend?.align ?? "start"];
|
|
1051
|
+
const legends = placement === "none" ||
|
|
1052
|
+
!(legend || colorBars.length || sized || shaped || faded || dashLevels.length > 1) ? null : (
|
|
1053
|
+
/*
|
|
1054
|
+
A grid over the guides, each one contributing a name cell and a body cell.
|
|
1055
|
+
|
|
1056
|
+
A flex row wrapped them around each other and two bars naming two measures started at
|
|
1057
|
+
different offsets. A name stands over its own body either way. Down the side the guides
|
|
1058
|
+
run in one column. Across the foot the names take a row and the bodies take the one
|
|
1059
|
+
under it.
|
|
1060
|
+
*/
|
|
1061
|
+
_jsxs("div", { className: `grid ${along} gap-x-4 gap-y-1`, style: {
|
|
1062
|
+
...(stacked
|
|
1063
|
+
? {
|
|
1064
|
+
gridTemplateColumns: "minmax(0, max-content)",
|
|
1065
|
+
...(spec.legend?.align ? { justifyItems: spec.legend.align } : {}),
|
|
1066
|
+
}
|
|
1067
|
+
: {
|
|
1068
|
+
gridTemplateRows: "max-content max-content",
|
|
1069
|
+
gridAutoFlow: "column",
|
|
1070
|
+
gridAutoColumns: "minmax(min-content, max-content)",
|
|
1071
|
+
}),
|
|
1072
|
+
...(spec.legend?.fontSize == null ? {} : { fontSize: spec.legend.fontSize }),
|
|
1073
|
+
}, children: [legend && (_jsx(SeriesLegend, { orientation: stacked ? "column" : "row",
|
|
1074
|
+
/* The channel's own field names the legend, which is what says what the colors are of.
|
|
1075
|
+
A split names its field the same way; unlabeled swatches leave a reader guessing. */
|
|
1076
|
+
label: swatchScale?.title ?? legendTitle, items: legend.map((row) => ({ id: row.key, label: row.label, color: row.color })) })), colorBars.map((bar) => {
|
|
1077
|
+
const layer = seriesOf(spec, bar.layerId);
|
|
1078
|
+
const scale = (layer && layerScale(spec, layer)) ?? {};
|
|
1079
|
+
return (_jsx(ColorLegend, { label: scale.title ?? axisLabel(bar.colorField) ?? bar.name, domain: bar.colorDomain, ramp: bar.ramp, center: scale.center ?? palettes.center, breaks: scale.bins, ticks: scale.breaks?.map((at) => ({ at, label: scale.labels?.[String(at)] })), format: scale.format ? tickFormatter(scale.format, scale.decimals) : undefined }, bar.id));
|
|
1080
|
+
}), tracks.map((track) => track.kind === "identity" ? (_jsx(SeriesLegend, { label: track.label, items: track.levels.map((value) => ({
|
|
1081
|
+
id: value,
|
|
1082
|
+
label: value,
|
|
1083
|
+
color: palettes.categorical(track.codes.get(value) ?? 0),
|
|
1084
|
+
})) }, `t${track.field}`)) : (_jsx(ColorLegend, { label: track.label, domain: track.domain, ramp: trackRamp(track, palettes, dark) }, `t${track.field}`))), sized && (_jsx(SizeLegend, { label: scales?.size?.title ?? axisLabel(sized.sizeField) ?? sized.sizeField.label, domain: sized.sizeDomain, radii: sized.sizeRadii, color: sized.color })), shaped && (_jsx(MarkLegend, { orientation: stacked ? "column" : "row", label: scales?.shape?.title ?? axisLabel(shaped.shapeChannel.field) ?? shaped.shapeChannel.field.label, color: shaped.color, levels: shaped.shapeChannel.levels.map((level) => ({ label: level.value, shape: level.at })) })), faded && (_jsx(MarkLegend, { orientation: stacked ? "column" : "row", label: scales?.alpha?.title ?? axisLabel(faded.alphaChannel.field) ?? faded.alphaChannel.field.label, color: faded.color, levels: faded.alphaChannel.levels.map((level) => ({
|
|
1085
|
+
label: level.value,
|
|
1086
|
+
shape: "circle",
|
|
1087
|
+
opacity: level.at,
|
|
1088
|
+
})) })), dashLevels.length > 1 && (_jsx(MarkLegend, { orientation: stacked ? "column" : "row", label: scales?.linetype?.title ??
|
|
1089
|
+
axisLabel(dashed[0].linetypeChannel.field) ??
|
|
1090
|
+
dashed[0].linetypeChannel.field.label, color: dashed[0].color, levels: dashLevels.map(([value, dash]) => ({ label: value, dash })) }))] }));
|
|
1091
|
+
/*
|
|
1092
|
+
Equal panels, and the first column wider by the room its numbers take.
|
|
1093
|
+
|
|
1094
|
+
Written as a grid track and not as a measurement. `100%` is the grid's own content box, so the
|
|
1095
|
+
browser resolves it after the legend beside it has taken its share, and nothing here can fall out
|
|
1096
|
+
of step with the layout. The tracks sum to the full width: the first is a share plus the axis
|
|
1097
|
+
room, the rest are that share, and the shares are cut after the room and the gaps come off.
|
|
1098
|
+
*/
|
|
1099
|
+
const axisRoom = firstFrame?.axisFree ? 0 : (firstFrame?.margin.left ?? 0);
|
|
1100
|
+
const evenColumns = built.facetField != null && columns > 1 && axisRoom > 0;
|
|
1101
|
+
/*
|
|
1102
|
+
The cell the observer watches is one of the equal ones.
|
|
1103
|
+
|
|
1104
|
+
Every panel takes one measurement, which holds while the tracks are equal fractions. The first
|
|
1105
|
+
track is no longer one of them, so watching it handed the inner panels the wide number and they
|
|
1106
|
+
drew past their own cells into the gap. The first panel adds the room back, which is the same
|
|
1107
|
+
constant its track adds.
|
|
1108
|
+
*/
|
|
1109
|
+
const measuredCell = evenColumns ? 1 : 0;
|
|
1110
|
+
const renderPanel = (panel, index) => {
|
|
1111
|
+
const frame = panel.frame;
|
|
1112
|
+
const facetColumn = built.facetField == null ? 0 : index % columns;
|
|
1113
|
+
/*
|
|
1114
|
+
The numbers go down the first column, so the room for them goes to the first column's cell.
|
|
1115
|
+
|
|
1116
|
+
Every panel reserved it before, which kept the plot areas equal and left four fifths of the
|
|
1117
|
+
reservation as empty page inside the geoms' own box: at five panels the blank between two
|
|
1118
|
+
drawings came to more than either drawing. The plot areas stay equal because the grid hands
|
|
1119
|
+
the first track that width on top of an equal share, so the sum works out per panel without
|
|
1120
|
+
anything here measuring pixels. That is the arithmetic the last attempt at this got wrong: it
|
|
1121
|
+
computed the widths in advance against a container the legend had already narrowed.
|
|
1122
|
+
*/
|
|
1123
|
+
const margin = facetColumn === 0 ? frame.margin : { ...frame.margin, left: 0 };
|
|
1124
|
+
/*
|
|
1125
|
+
A stated band size decides the plot area, so the panel is that plus the room its axes take.
|
|
1126
|
+
|
|
1127
|
+
The margin is the frame's own, derived from the chrome the author set, so this reads it after
|
|
1128
|
+
the build instead of guessing at it beforehand.
|
|
1129
|
+
*/
|
|
1130
|
+
/* Faceting: the cell the grid gave, reported by the cell itself. `panelWidth` is the estimate the
|
|
1131
|
+
first paint draws at, before the observer has answered. */
|
|
1132
|
+
const panelW = content?.width != null
|
|
1133
|
+
? content.width + margin.left + margin.right + room.left + room.right
|
|
1134
|
+
: built.facetField
|
|
1135
|
+
? (cellWidth || panelWidth) + (facetColumn === 0 ? axisRoom : 0)
|
|
1136
|
+
: panelWidth;
|
|
1137
|
+
const panelH = content?.height != null
|
|
1138
|
+
? content.height + frame.margin.top + frame.margin.bottom + room.top + room.bottom
|
|
1139
|
+
: height;
|
|
1140
|
+
/*
|
|
1141
|
+
The gesture read as a pair of domains, which is the whole of what a zoom does here.
|
|
1142
|
+
|
|
1143
|
+
The traces are left alone: what a trace holds was decided by the rows and the bindings, and
|
|
1144
|
+
a hand moving changes neither, so the pass that walked the records is not asked to run
|
|
1145
|
+
again. The geoms reposition on the new scales and `clip` keeps the ones now outside the
|
|
1146
|
+
window off the ticks beside them.
|
|
1147
|
+
*/
|
|
1148
|
+
const axes = view.zoomed
|
|
1149
|
+
? zoomedAxes(frame, view.transform, plotArea(panelW, panelH, frame.margin))
|
|
1150
|
+
: { x: frame.x, y: frame.y };
|
|
1151
|
+
const ctx = {
|
|
1152
|
+
spec,
|
|
1153
|
+
data: plotData,
|
|
1154
|
+
palettes,
|
|
1155
|
+
panel: index,
|
|
1156
|
+
xField: frame.xField,
|
|
1157
|
+
fieldAt: frame.fieldAt,
|
|
1158
|
+
transposed: frame.transposed,
|
|
1159
|
+
/* The window on screen, where both axes run a value scale. The density route asks for both. */
|
|
1160
|
+
domains: "domain" in axes.x && "domain" in axes.y
|
|
1161
|
+
? { x: axes.x.domain, y: axes.y.domain, affine: straight(axes.x) && straight(axes.y) }
|
|
1162
|
+
: null,
|
|
1163
|
+
ruleAxes: ruleAxesOf(frame.transposed ? axes.y : axes.x, frame.transposed ? axes.x : axes.y),
|
|
1164
|
+
theme,
|
|
1165
|
+
onHover: hovers.set,
|
|
1166
|
+
onSelect: reportSelect,
|
|
1167
|
+
onSubstitute,
|
|
1168
|
+
ranges,
|
|
1169
|
+
onRanges: setRanges,
|
|
1170
|
+
};
|
|
1171
|
+
/*
|
|
1172
|
+
One set of numbers down the left and one along the bottom, which is what a facet reads as.
|
|
1173
|
+
|
|
1174
|
+
Each panel wrote its own pair before this, so five panels at a hundred pixels put five sets of
|
|
1175
|
+
tick labels across the figure and each set landed on its neighbor's geoms. The room stays
|
|
1176
|
+
reserved in every panel, since equal margins are what keep the plot areas on one line, and only
|
|
1177
|
+
the outer panels write into it. ggplot hoists the axis out of the panels instead and gets the
|
|
1178
|
+
same reading; this keeps the frame arithmetic per panel, which is what `buildPanels` already
|
|
1179
|
+
hands back.
|
|
1180
|
+
*/
|
|
1181
|
+
const facetted = built.facetField != null;
|
|
1182
|
+
const rows = Math.ceil(built.panels.length / columns);
|
|
1183
|
+
const lastRowFrom = (rows - 1) * columns;
|
|
1184
|
+
const column = index % columns;
|
|
1185
|
+
const row = Math.floor(index / columns);
|
|
1186
|
+
/* Numbers on the outer panels: down the left of the first column, along the bottom of the last
|
|
1187
|
+
row. That is where a reader looks for them and it is one set of each. */
|
|
1188
|
+
const writesY = !facetted || column === 0;
|
|
1189
|
+
const writesX = !facetted || index >= lastRowFrom;
|
|
1190
|
+
/*
|
|
1191
|
+
The name once, near the middle of the run it names.
|
|
1192
|
+
|
|
1193
|
+
The numbers belong to a panel and the name belongs to the axis, so writing it on each panel
|
|
1194
|
+
that carries numbers named the measure five times across one row. The middle panel is as close
|
|
1195
|
+
to centered as a per-panel frame reaches: centering it across the whole row would mean drawing
|
|
1196
|
+
it outside every panel, which is a layout this does not have.
|
|
1197
|
+
*/
|
|
1198
|
+
const titlesY = !facetted || (column === 0 && row === Math.floor((rows - 1) / 2));
|
|
1199
|
+
/* Never inside a panel where the figure facets: the grid writes it once underneath, centered
|
|
1200
|
+
over all of them, because the measure is the same across the row. */
|
|
1201
|
+
const titlesX = !facetted;
|
|
1202
|
+
const outerOnly = (axis, ticks, title) => ticks && title ? axis : { ...axis, ...(ticks ? {} : { labelTicks: false }), ...(title ? {} : { label: undefined }) };
|
|
1203
|
+
return (_jsxs("div", {
|
|
1204
|
+
/* The first cell reports its width and the rest take it, since the tracks are equal. */
|
|
1205
|
+
ref: index === measuredCell ? cellHolder : undefined, className: "flex min-w-0 flex-col", children: [built.facetField && (
|
|
1206
|
+
/*
|
|
1207
|
+
The strip: the level's own name on a card, centered over the panel it names.
|
|
1208
|
+
|
|
1209
|
+
A card and not bare text, because the strip is chrome and the reader has to tell it
|
|
1210
|
+
from the figure's own words. ggplot fills it, and what the fill buys is a row of names
|
|
1211
|
+
that reads as one band across the top instead of five captions.
|
|
1212
|
+
|
|
1213
|
+
The name alone. The row count was here and it said the same number five times over a
|
|
1214
|
+
synthetic table and something misleading over a real one: a panel's count is a fact
|
|
1215
|
+
about the split and not about the level, and the figure already states its own n. One
|
|
1216
|
+
line either way, since a wrapped strip pushes its panel's plot area below its
|
|
1217
|
+
neighbors' and a facet is read by comparing across the row.
|
|
1218
|
+
*/
|
|
1219
|
+
_jsx("span", { "data-viz-strip": "", className: "text-xs font-medium", title: panel.value, style: {
|
|
1220
|
+
color: "var(--viz-ink)",
|
|
1221
|
+
background: "var(--viz-rule)",
|
|
1222
|
+
height: STRIP_HEIGHT,
|
|
1223
|
+
boxSizing: "border-box",
|
|
1224
|
+
lineHeight: `${STRIP_HEIGHT - 6}px`,
|
|
1225
|
+
padding: "3px 6px",
|
|
1226
|
+
textAlign: "center",
|
|
1227
|
+
/*
|
|
1228
|
+
Inset to the plot area, so the strip names the panel and not the panel plus its
|
|
1229
|
+
axis, and its edges line up with the geoms under it. Every panel carries the same
|
|
1230
|
+
margin now, so one inset keeps the strips equal to each other as well as to their
|
|
1231
|
+
own panel: the alignment holds because the numbers are the same, and not because
|
|
1232
|
+
they were computed to match.
|
|
1233
|
+
*/
|
|
1234
|
+
marginLeft: margin.left,
|
|
1235
|
+
marginRight: margin.right,
|
|
1236
|
+
whiteSpace: "nowrap",
|
|
1237
|
+
overflow: "hidden",
|
|
1238
|
+
textOverflow: "ellipsis",
|
|
1239
|
+
}, children: panel.value })), _jsx(Frame, { width: panelW, height: panelH, x: frame.axisFree ? BARE_AXIS : outerOnly(axes.x, writesX, titlesX), y: frame.axisFree ? BARE_AXIS : outerOnly(axes.y, writesY, titlesY), clip: view.zoomed || frame.clip === true, margin: frame.axisFree ? WHOLE_PANEL : margin, metrics: frame.metrics, axisEnds: frame.annotations.axisEnds, notes: frame.annotations.notes, sides: sides, side: (child) => sideContent(child, frame, palettes, dark, spec.layers[0]?.style), overlay: toolView.tooltip === false
|
|
1240
|
+
? undefined
|
|
1241
|
+
: ({ width: innerW, height: innerH }) => (_jsx(PanelTooltip, { hovers: hovers, panel: index, bounds: { width: innerW, height: innerH } })), children: (child) => {
|
|
1242
|
+
const geoms = (_jsxs(_Fragment, { children: [frame.traces.map((trace) => (_jsx(TraceGeoms, { trace: trace, child: child, ctx: ctx }, trace.id))), frame.traces.map((trace) => fitsFor(trace, fits, panel.value).map((fit) => (_jsx(FitCurve, { fit: fit, x: child.x, y: child.y, color: trace.color, ...(trace.data.geom === "smooth"
|
|
1243
|
+
? {
|
|
1244
|
+
lineWidth: seriesOf(spec, trace.layerId)?.style?.linewidth,
|
|
1245
|
+
opacity: seriesOf(spec, trace.layerId)?.style?.alpha,
|
|
1246
|
+
}
|
|
1247
|
+
: {}), showBand: bandsDrawn }, `${trace.id}-${fit.id}`))))] }));
|
|
1248
|
+
if (drag === "zoomBox" && soleArea) {
|
|
1249
|
+
const { left, top } = soleArea;
|
|
1250
|
+
return (_jsx(BrushLayer, { shape: "box", child: child, traces: frame.traces, spec: spec, onRegion: ([a, b]) => view.zoomTo({ left: a.x + left, top: a.y + top, right: b.x + left, bottom: b.y + top }, soleArea), children: geoms }, "zoomBox"));
|
|
1251
|
+
}
|
|
1252
|
+
return brushShape && built.panels.length === 1 ? (_jsx(BrushLayer, { shape: brushShape, child: child, traces: frame.traces, spec: spec, keyField: plotData.keyField, transposed: frame.transposed, onBrush: onBrush, children: geoms }, brushShape)) : (geoms);
|
|
1253
|
+
} })] }, panel.value || "panel"));
|
|
1254
|
+
};
|
|
1255
|
+
/*
|
|
1256
|
+
The horizontal axis is named once, under the whole grid.
|
|
1257
|
+
|
|
1258
|
+
It was named on the middle panel of the last row, which is the closest a per-panel frame reaches
|
|
1259
|
+
to centered and still reads as labeling that one panel. The measure is the same across the row, so
|
|
1260
|
+
the name belongs to the figure: it goes below the grid, centered over all of it, and every panel
|
|
1261
|
+
drops it. The vertical one stays inside its panel, where `titleAlign: "center"` turns it to run
|
|
1262
|
+
along the rule it names.
|
|
1263
|
+
*/
|
|
1264
|
+
const sharedXTitle = built.facetField ? axisLabel(firstFrame?.xField) : undefined;
|
|
1265
|
+
const share = `((100% - ${FACET_GAP * Math.max(0, columns - 1)}px - ${axisRoom}px) / ${columns})`;
|
|
1266
|
+
const facetTracks = columns > 1 && axisRoom > 0
|
|
1267
|
+
? `calc(${share} + ${axisRoom}px) repeat(${columns - 1}, calc(${share}))`
|
|
1268
|
+
: `repeat(${columns}, minmax(0, 1fr))`;
|
|
1269
|
+
const drawn = built.panels.length === 1 ? (renderPanel(built.panels[0], 0)) : (_jsxs("div", { className: "flex min-w-0 flex-col", style: { gap: 4 }, children: [_jsx("div", { className: "grid", style: { gap: FACET_GAP, gridTemplateColumns: facetTracks }, children: built.panels.map(renderPanel) }), sharedXTitle && (_jsx("span", { className: "text-xs", style: { color: "var(--viz-ink-muted)", textAlign: "center", lineHeight: `${SHARED_TITLE_HEIGHT - 4}px` }, children: sharedXTitle }))] }));
|
|
1270
|
+
/*
|
|
1271
|
+
The element the gesture is read against, which is the panel and not the figure.
|
|
1272
|
+
|
|
1273
|
+
Wrapping the headline as well would let a wheel over the title move the geoms under it, and
|
|
1274
|
+
`touch-action` off is what stops a drag on a touchpad scrolling the page instead of panning.
|
|
1275
|
+
Double-click resets, which is the affordance a reader already expects of a zoomed view.
|
|
1276
|
+
*/
|
|
1277
|
+
const panels = takesGesture ? (_jsx("div", { ref: view.surface, style: zoom || drags.length > 0 ? { touchAction: "none" } : undefined, onDoubleClick: view.reset, ...view.handlers, children: drawn })) : (drawn);
|
|
1278
|
+
/* The strip's corner: the plot area's top right, inset four pixels. A facet anchors to the grid's right edge. */
|
|
1279
|
+
const edge = firstFrame?.axisFree ? WHOLE_PANEL : firstFrame?.margin;
|
|
1280
|
+
const stripAt = !edge
|
|
1281
|
+
? undefined
|
|
1282
|
+
: built.facetField
|
|
1283
|
+
? { top: STRIP_HEIGHT + edge.top + 4, right: edge.right + 4 }
|
|
1284
|
+
: { top: edge.top + room.top + 4, left: soleWidth - edge.right - room.right - 4, transform: "translateX(-100%)" };
|
|
1285
|
+
const changeView = (next) => {
|
|
1286
|
+
setToolView(next);
|
|
1287
|
+
toolbar?.onChange?.(next);
|
|
1288
|
+
};
|
|
1289
|
+
/* One PNG in flight at a time. A failed one reports to the console. */
|
|
1290
|
+
const exportAs = (format) => {
|
|
1291
|
+
const root = measuredElement;
|
|
1292
|
+
if (!root)
|
|
1293
|
+
return;
|
|
1294
|
+
const name = fileName(headline, format);
|
|
1295
|
+
if (format === "png") {
|
|
1296
|
+
if (exporting.current)
|
|
1297
|
+
return;
|
|
1298
|
+
exporting.current = true;
|
|
1299
|
+
exportPng(root)
|
|
1300
|
+
.then((blob) => download(blob, name))
|
|
1301
|
+
.catch((error) => console.error("The PNG export failed", error))
|
|
1302
|
+
.finally(() => (exporting.current = false));
|
|
1303
|
+
}
|
|
1304
|
+
else
|
|
1305
|
+
download(new Blob([exportSvg(root, { ground: toolbar?.export?.ground })], { type: "image/svg+xml" }), name);
|
|
1306
|
+
};
|
|
1307
|
+
return (_jsx("div", { "data-viz-ghost": toolbar ? "" : undefined, style: spec.width == null ? undefined : { width: spec.width }, children: _jsx(Figure, { headline: headline, subtitle: subtitle, caption: footerLine, children: _jsxs("div", { className: onSide ? "flex min-w-0 items-center gap-4" : "flex min-w-0 flex-col gap-2", children: [legendsFirst && legends, _jsxs("div", { ref: holder, className: "min-w-0 flex-1", style: toolbar ? { position: "relative" } : undefined, onClick: clearSelect, ...(onSelect ? gesture : {}), children: [panels, toolbar && stripAt && (_jsx(Toolbar, { at: stripAt, options: { ...toolbar, items: tools }, view: toolView, onView: changeView, drag: drag, palettes: paletteOptions(toolbar.palette?.options, {
|
|
1308
|
+
magnitude: firstFrame?.traces.some((trace) => trace.colorField?.numeric) ?? false,
|
|
1309
|
+
centered: (chosen ?? palettesFor({ dark })).center !== undefined,
|
|
1310
|
+
}), facetBy: facetBy, facet: { by: spec.facet?.by ?? null, columns: spec.facet?.columns }, viewLocked: panelCount > 1 ? "A faceted figure keeps its panels at rest" : undefined, facetLocked: authored.sides?.length ? "A facet draws no side panels" : undefined, onZoom: (factor) => soleArea && view.zoomBy(factor, soleArea), onReset: view.reset, onExport: exportAs }))] }), !legendsFirst && legends] }) }) }));
|
|
1311
|
+
}
|