@tetrascience-npm/ts-lib-viz 0.0.0-stage → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +2171 -2
- package/dist/src/builder/PlotPanel.d.ts +42 -0
- package/dist/src/builder/PlotPanel.js +433 -0
- package/dist/src/builder/Popover.d.ts +37 -0
- package/dist/src/builder/Popover.js +32 -0
- package/dist/src/builder/Stepper.d.ts +21 -0
- package/dist/src/builder/Stepper.js +41 -0
- package/dist/src/builder/index.d.ts +10 -0
- package/dist/src/builder/index.js +10 -0
- package/dist/src/core/adapters.d.ts +86 -0
- package/dist/src/core/adapters.js +219 -0
- package/dist/src/core/aggregate.d.ts +74 -0
- package/dist/src/core/aggregate.js +102 -0
- package/dist/src/core/annotations.d.ts +34 -0
- package/dist/src/core/annotations.js +71 -0
- package/dist/src/core/band-order.d.ts +86 -0
- package/dist/src/core/band-order.js +132 -0
- package/dist/src/core/binning.d.ts +123 -0
- package/dist/src/core/binning.js +193 -0
- package/dist/src/core/bounds.d.ts +40 -0
- package/dist/src/core/bounds.js +107 -0
- package/dist/src/core/brush.d.ts +73 -0
- package/dist/src/core/brush.js +102 -0
- package/dist/src/core/build.d.ts +322 -0
- package/dist/src/core/build.js +496 -0
- package/dist/src/core/camera.d.ts +152 -0
- package/dist/src/core/camera.js +303 -0
- package/dist/src/core/capabilities.d.ts +24 -0
- package/dist/src/core/capabilities.js +307 -0
- package/dist/src/core/chord.d.ts +49 -0
- package/dist/src/core/chord.js +57 -0
- package/dist/src/core/circular.d.ts +83 -0
- package/dist/src/core/circular.js +63 -0
- package/dist/src/core/cluster.d.ts +136 -0
- package/dist/src/core/cluster.js +607 -0
- package/dist/src/core/critique.d.ts +92 -0
- package/dist/src/core/critique.js +905 -0
- package/dist/src/core/density.d.ts +38 -0
- package/dist/src/core/density.js +82 -0
- package/dist/src/core/figure-set.d.ts +34 -0
- package/dist/src/core/figure-set.js +55 -0
- package/dist/src/core/finding-codes.d.ts +10 -0
- package/dist/src/core/finding-codes.js +1 -0
- package/dist/src/core/fit-types.d.ts +46 -0
- package/dist/src/core/fit-types.js +7 -0
- package/dist/src/core/fitter.d.ts +62 -0
- package/dist/src/core/fitter.js +102 -0
- package/dist/src/core/frame-geometry.d.ts +407 -0
- package/dist/src/core/frame-geometry.js +612 -0
- package/dist/src/core/frame.d.ts +329 -0
- package/dist/src/core/frame.js +1432 -0
- package/dist/src/core/geom-table.d.ts +503 -0
- package/dist/src/core/geom-table.js +484 -0
- package/dist/src/core/geom-types.d.ts +182 -0
- package/dist/src/core/geom-types.js +17 -0
- package/dist/src/core/grammar.d.ts +359 -0
- package/dist/src/core/grammar.js +397 -0
- package/dist/src/core/heat-reductions.d.ts +36 -0
- package/dist/src/core/heat-reductions.js +133 -0
- package/dist/src/core/hexbin.d.ts +84 -0
- package/dist/src/core/hexbin.js +168 -0
- package/dist/src/core/hierarchy.d.ts +59 -0
- package/dist/src/core/hierarchy.js +248 -0
- package/dist/src/core/hit.d.ts +75 -0
- package/dist/src/core/hit.js +37 -0
- package/dist/src/core/index.d.ts +33 -0
- package/dist/src/core/index.js +18 -0
- package/dist/src/core/internal.d.ts +58 -0
- package/dist/src/core/internal.js +58 -0
- package/dist/src/core/labels.d.ts +37 -0
- package/dist/src/core/labels.js +54 -0
- package/dist/src/core/layers.d.ts +594 -0
- package/dist/src/core/layers.js +2892 -0
- package/dist/src/core/load.d.ts +17 -0
- package/dist/src/core/load.js +356 -0
- package/dist/src/core/marginal.d.ts +65 -0
- package/dist/src/core/marginal.js +116 -0
- package/dist/src/core/mesh.d.ts +69 -0
- package/dist/src/core/mesh.js +237 -0
- package/dist/src/core/omission.d.ts +41 -0
- package/dist/src/core/omission.js +62 -0
- package/dist/src/core/palette-sets.d.ts +623 -0
- package/dist/src/core/palette-sets.js +1058 -0
- package/dist/src/core/palette.d.ts +871 -0
- package/dist/src/core/palette.js +511 -0
- package/dist/src/core/plot-data.d.ts +71 -0
- package/dist/src/core/plot-data.js +82 -0
- package/dist/src/core/predicate.d.ts +50 -0
- package/dist/src/core/predicate.js +20 -0
- package/dist/src/core/profile.d.ts +111 -0
- package/dist/src/core/profile.js +221 -0
- package/dist/src/core/readout.d.ts +108 -0
- package/dist/src/core/readout.js +215 -0
- package/dist/src/core/region.d.ts +56 -0
- package/dist/src/core/region.js +101 -0
- package/dist/src/core/rings.d.ts +96 -0
- package/dist/src/core/rings.js +171 -0
- package/dist/src/core/rules.d.ts +45 -0
- package/dist/src/core/rules.js +69 -0
- package/dist/src/core/sankey.d.ts +49 -0
- package/dist/src/core/sankey.js +96 -0
- package/dist/src/core/select.d.ts +52 -0
- package/dist/src/core/select.js +216 -0
- package/dist/src/core/side-track.d.ts +98 -0
- package/dist/src/core/side-track.js +197 -0
- package/dist/src/core/sizing.d.ts +54 -0
- package/dist/src/core/sizing.js +37 -0
- package/dist/src/core/spec-edit.d.ts +68 -0
- package/dist/src/core/spec-edit.js +105 -0
- package/dist/src/core/spec.d.ts +1111 -0
- package/dist/src/core/spec.js +39 -0
- package/dist/src/core/stats.d.ts +161 -0
- package/dist/src/core/stats.js +318 -0
- package/dist/src/core/tick-format.d.ts +21 -0
- package/dist/src/core/tick-format.js +87 -0
- package/dist/src/core/tokens.d.ts +47 -0
- package/dist/src/core/tokens.js +138 -0
- package/dist/src/core/transform.d.ts +21 -0
- package/dist/src/core/transform.js +19 -0
- package/dist/src/core/voxels.d.ts +53 -0
- package/dist/src/core/voxels.js +129 -0
- package/dist/src/core/zoom.d.ts +76 -0
- package/dist/src/core/zoom.js +103 -0
- package/dist/src/graph/GraphSurface.d.ts +127 -0
- package/dist/src/graph/GraphSurface.js +423 -0
- package/dist/src/graph/data.d.ts +103 -0
- package/dist/src/graph/data.js +140 -0
- package/dist/src/graph/index.d.ts +7 -0
- package/dist/src/graph/index.js +7 -0
- package/dist/src/mcp/data.d.ts +28 -0
- package/dist/src/mcp/data.js +131 -0
- package/dist/src/mcp/index.d.ts +7 -0
- package/dist/src/mcp/index.js +7 -0
- package/dist/src/mcp/paths.d.ts +12 -0
- package/dist/src/mcp/paths.js +30 -0
- package/dist/src/mcp/render.d.ts +67 -0
- package/dist/src/mcp/render.js +189 -0
- package/dist/src/mcp/server.d.ts +7 -0
- package/dist/src/mcp/server.js +25 -0
- package/dist/src/mcp/tools.d.ts +82 -0
- package/dist/src/mcp/tools.js +412 -0
- package/dist/src/react/BrushLayer.d.ts +20 -0
- package/dist/src/react/BrushLayer.js +98 -0
- package/dist/src/react/ColorLegend.d.ts +45 -0
- package/dist/src/react/ColorLegend.js +64 -0
- package/dist/src/react/Figure.d.ts +19 -0
- package/dist/src/react/Figure.js +6 -0
- package/dist/src/react/Frame.d.ts +87 -0
- package/dist/src/react/Frame.js +192 -0
- package/dist/src/react/MarkLegend.d.ts +32 -0
- package/dist/src/react/MarkLegend.js +24 -0
- package/dist/src/react/SeriesLegend.d.ts +38 -0
- package/dist/src/react/SeriesLegend.js +27 -0
- package/dist/src/react/SizeLegend.d.ts +24 -0
- package/dist/src/react/SizeLegend.js +25 -0
- package/dist/src/react/Toolbar.d.ts +109 -0
- package/dist/src/react/Toolbar.js +483 -0
- package/dist/src/react/Tooltip.d.ts +37 -0
- package/dist/src/react/Tooltip.js +59 -0
- package/dist/src/react/Viz.d.ts +112 -0
- package/dist/src/react/Viz.js +1311 -0
- package/dist/src/react/export.d.ts +32 -0
- package/dist/src/react/export.js +181 -0
- package/dist/src/react/geoms/Area.d.ts +38 -0
- package/dist/src/react/geoms/Area.js +48 -0
- package/dist/src/react/geoms/Bars.d.ts +130 -0
- package/dist/src/react/geoms/Bars.js +164 -0
- package/dist/src/react/geoms/Box.d.ts +50 -0
- package/dist/src/react/geoms/Box.js +32 -0
- package/dist/src/react/geoms/Chord.d.ts +60 -0
- package/dist/src/react/geoms/Chord.js +78 -0
- package/dist/src/react/geoms/Contour.d.ts +44 -0
- package/dist/src/react/geoms/Contour.js +78 -0
- package/dist/src/react/geoms/DensityCurve.d.ts +30 -0
- package/dist/src/react/geoms/DensityCurve.js +38 -0
- package/dist/src/react/geoms/FitCurve.d.ts +26 -0
- package/dist/src/react/geoms/FitCurve.js +52 -0
- package/dist/src/react/geoms/Freqpoly.d.ts +30 -0
- package/dist/src/react/geoms/Freqpoly.js +45 -0
- package/dist/src/react/geoms/Gauge.d.ts +42 -0
- package/dist/src/react/geoms/Gauge.js +90 -0
- package/dist/src/react/geoms/Heatmap.d.ts +79 -0
- package/dist/src/react/geoms/Heatmap.js +51 -0
- package/dist/src/react/geoms/Hexbin.d.ts +74 -0
- package/dist/src/react/geoms/Hexbin.js +89 -0
- package/dist/src/react/geoms/Icicle.d.ts +38 -0
- package/dist/src/react/geoms/Icicle.js +9 -0
- package/dist/src/react/geoms/Ideogram.d.ts +58 -0
- package/dist/src/react/geoms/Ideogram.js +77 -0
- package/dist/src/react/geoms/Interval.d.ts +30 -0
- package/dist/src/react/geoms/Interval.js +39 -0
- package/dist/src/react/geoms/Line.d.ts +39 -0
- package/dist/src/react/geoms/Line.js +52 -0
- package/dist/src/react/geoms/Parallel.d.ts +38 -0
- package/dist/src/react/geoms/Parallel.js +131 -0
- package/dist/src/react/geoms/Pie.d.ts +62 -0
- package/dist/src/react/geoms/Pie.js +59 -0
- package/dist/src/react/geoms/Points.d.ts +84 -0
- package/dist/src/react/geoms/Points.js +207 -0
- package/dist/src/react/geoms/Points3d.d.ts +49 -0
- package/dist/src/react/geoms/Points3d.js +58 -0
- package/dist/src/react/geoms/Radar.d.ts +39 -0
- package/dist/src/react/geoms/Radar.js +59 -0
- package/dist/src/react/geoms/Range.d.ts +31 -0
- package/dist/src/react/geoms/Range.js +20 -0
- package/dist/src/react/geoms/RingStack.d.ts +39 -0
- package/dist/src/react/geoms/RingStack.js +65 -0
- package/dist/src/react/geoms/Rule.d.ts +29 -0
- package/dist/src/react/geoms/Rule.js +51 -0
- package/dist/src/react/geoms/Sankey.d.ts +24 -0
- package/dist/src/react/geoms/Sankey.js +51 -0
- package/dist/src/react/geoms/Segment.d.ts +36 -0
- package/dist/src/react/geoms/Segment.js +39 -0
- package/dist/src/react/geoms/Sunburst.d.ts +46 -0
- package/dist/src/react/geoms/Sunburst.js +64 -0
- package/dist/src/react/geoms/Surface.d.ts +32 -0
- package/dist/src/react/geoms/Surface.js +49 -0
- package/dist/src/react/geoms/Text.d.ts +35 -0
- package/dist/src/react/geoms/Text.js +40 -0
- package/dist/src/react/geoms/Treemap.d.ts +39 -0
- package/dist/src/react/geoms/Treemap.js +16 -0
- package/dist/src/react/geoms/Violin.d.ts +56 -0
- package/dist/src/react/geoms/Violin.js +60 -0
- package/dist/src/react/geoms/Volume.d.ts +37 -0
- package/dist/src/react/geoms/Volume.js +40 -0
- package/dist/src/react/geoms/Waterfall.d.ts +43 -0
- package/dist/src/react/geoms/Waterfall.js +18 -0
- package/dist/src/react/geoms/cube-stage.d.ts +39 -0
- package/dist/src/react/geoms/cube-stage.js +85 -0
- package/dist/src/react/geoms/hierarchy-cells.d.ts +42 -0
- package/dist/src/react/geoms/hierarchy-cells.js +59 -0
- package/dist/src/react/geoms/sector-axis.d.ts +66 -0
- package/dist/src/react/geoms/sector-axis.js +99 -0
- package/dist/src/react/gl/GlMesh.d.ts +22 -0
- package/dist/src/react/gl/GlMesh.js +78 -0
- package/dist/src/react/gl/GlPoints.d.ts +45 -0
- package/dist/src/react/gl/GlPoints.js +117 -0
- package/dist/src/react/gl/GlVolume.d.ts +28 -0
- package/dist/src/react/gl/GlVolume.js +112 -0
- package/dist/src/react/gl/GlWalls.d.ts +24 -0
- package/dist/src/react/gl/GlWalls.js +55 -0
- package/dist/src/react/gl/context.d.ts +71 -0
- package/dist/src/react/gl/context.js +98 -0
- package/dist/src/react/gl/encode.d.ts +120 -0
- package/dist/src/react/gl/encode.js +215 -0
- package/dist/src/react/gl/index.d.ts +10 -0
- package/dist/src/react/gl/index.js +8 -0
- package/dist/src/react/gl/refresh.d.ts +7 -0
- package/dist/src/react/gl/refresh.js +19 -0
- package/dist/src/react/gl/renderer.d.ts +377 -0
- package/dist/src/react/gl/renderer.js +1081 -0
- package/dist/src/react/gl/shaders.d.ts +92 -0
- package/dist/src/react/gl/shaders.js +405 -0
- package/dist/src/react/gl/surface.d.ts +31 -0
- package/dist/src/react/gl/surface.js +73 -0
- package/dist/src/react/index.d.ts +12 -0
- package/dist/src/react/index.js +11 -0
- package/dist/src/react/orbit.d.ts +38 -0
- package/dist/src/react/orbit.js +81 -0
- package/dist/src/react/sides/DendrogramPanel.d.ts +32 -0
- package/dist/src/react/sides/DendrogramPanel.js +39 -0
- package/dist/src/react/sides/DensityPanel.d.ts +31 -0
- package/dist/src/react/sides/DensityPanel.js +37 -0
- package/dist/src/react/sides/HistogramPanel.d.ts +30 -0
- package/dist/src/react/sides/HistogramPanel.js +18 -0
- package/dist/src/react/sides/RugPanel.d.ts +28 -0
- package/dist/src/react/sides/RugPanel.js +21 -0
- package/dist/src/react/sides/TrackPanel.d.ts +37 -0
- package/dist/src/react/sides/TrackPanel.js +27 -0
- package/dist/src/react/theme.d.ts +48 -0
- package/dist/src/react/theme.js +148 -0
- package/dist/src/react/useFits.d.ts +26 -0
- package/dist/src/react/useFits.js +62 -0
- package/dist/src/react/useZoom.d.ts +50 -0
- package/dist/src/react/useZoom.js +141 -0
- package/package.json +164 -5
- package/theme/default.css +62 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a hover says, whichever geom is under the pointer.
|
|
3
|
+
*
|
|
4
|
+
* One union and one reader. A hover on any geom ends in one card.
|
|
5
|
+
*/
|
|
6
|
+
const digitsFor = (field, value) => field?.digits ?? (Math.abs(value) >= 100 ? 0 : 2);
|
|
7
|
+
/** Currency symbols, written ahead of the number the way American English writes a price. */
|
|
8
|
+
const CURRENCY = new Set(["$", "€", "£", "¥"]);
|
|
9
|
+
/** A number at fixed digits with its thousands grouped. */
|
|
10
|
+
const grouped = (value, digits) => value.toLocaleString("en-US", { minimumFractionDigits: digits, maximumFractionDigits: digits });
|
|
11
|
+
/** Writes a unit beside a number. A currency symbol leads it. Any other unit follows after a space. */
|
|
12
|
+
const withUnit = (text, unit) => {
|
|
13
|
+
if (!unit)
|
|
14
|
+
return text;
|
|
15
|
+
if (!CURRENCY.has(unit))
|
|
16
|
+
return `${text} ${unit}`;
|
|
17
|
+
return text.startsWith("-") ? `-${unit}${text.slice(1)}` : `${unit}${text}`;
|
|
18
|
+
};
|
|
19
|
+
export const numberOf = (field, value) => withUnit(grouped(value, digitsFor(field, value)), field?.unit);
|
|
20
|
+
const DAY = 86_400_000;
|
|
21
|
+
/**
|
|
22
|
+
* Epoch milliseconds as a date and a clock time, in UTC or the viewer's zone.
|
|
23
|
+
*
|
|
24
|
+
* A reading at midnight writes the date alone.
|
|
25
|
+
*/
|
|
26
|
+
const timeOf = (value, zone) => {
|
|
27
|
+
const local = zone === "local";
|
|
28
|
+
const midnight = local ? new Date(value).setHours(0, 0, 0, 0) === value : value % DAY === 0;
|
|
29
|
+
return new Date(value).toLocaleString("en-US", {
|
|
30
|
+
dateStyle: "medium",
|
|
31
|
+
...(midnight ? {} : { timeStyle: "short" }),
|
|
32
|
+
...(local ? {} : { timeZone: "UTC" }),
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
/** A reading on a position scale as its axis writes it: the scale's name for it, a time, or a number. */
|
|
36
|
+
const readingOf = (field, scale, value) => scale?.labels?.[String(value)] ??
|
|
37
|
+
((scale?.time ?? field?.timeScale) ? timeOf(value, scale?.timezone) : numberOf(field, value));
|
|
38
|
+
/**
|
|
39
|
+
* A value beside the interval drawn around it, as one string.
|
|
40
|
+
*
|
|
41
|
+
* The half-width takes the value's digits and the unit is written once at the end.
|
|
42
|
+
*/
|
|
43
|
+
export const intervalOf = (field, value, half) => {
|
|
44
|
+
const digits = digitsFor(field, value);
|
|
45
|
+
const unit = field?.unit;
|
|
46
|
+
if (unit && CURRENCY.has(unit))
|
|
47
|
+
return `${withUnit(grouped(value, digits), unit)} ± ${withUnit(grouped(half, digits), unit)}`;
|
|
48
|
+
return withUnit(`${grouped(value, digits)} ± ${grouped(half, digits)}`, unit);
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* The readout shows what the plot says it shows.
|
|
52
|
+
*
|
|
53
|
+
* Bound channels are the default and the author can add the fields nobody plotted.
|
|
54
|
+
*/
|
|
55
|
+
export function tooltipRows(hit, context) {
|
|
56
|
+
const { plot, data, xField, fieldAt } = context;
|
|
57
|
+
/* A bar drawn with an interval reports it in the readout. A whisker is read to the pixel. */
|
|
58
|
+
if (hit.kind === "bar" || hit.kind === "slice") {
|
|
59
|
+
const aggregate = hit.trace.data.geom === "bar" ? hit.trace.data.aggregate : "mean";
|
|
60
|
+
const half = hit.datum.error;
|
|
61
|
+
/*
|
|
62
|
+
A fill divided this geom by its band's total and the value is a share.
|
|
63
|
+
|
|
64
|
+
The aggregate names another reduction and the unit was divided away. Both rows go.
|
|
65
|
+
*/
|
|
66
|
+
const shares = hit.trace.data.at === "groups" && "bars" in hit.trace.data && hit.trace.data.shares === true;
|
|
67
|
+
const measure = shares ? undefined : hit.trace.field;
|
|
68
|
+
return [
|
|
69
|
+
{
|
|
70
|
+
label: hit.trace.field ? `${shares ? "share" : aggregate} of ${hit.trace.field.label}` : "records",
|
|
71
|
+
value: half != null && half > 0 ? intervalOf(measure, hit.datum.value, half) : numberOf(measure, hit.datum.value),
|
|
72
|
+
color: hit.trace.color,
|
|
73
|
+
},
|
|
74
|
+
...(hit.datum.n == null ? [] : [{ label: "rows", value: hit.datum.n.toLocaleString() }]),
|
|
75
|
+
];
|
|
76
|
+
}
|
|
77
|
+
if (hit.kind === "cell") {
|
|
78
|
+
/* A categorical cell names its category. Its value is that category's share of the rows. */
|
|
79
|
+
if (hit.cell.category != null) {
|
|
80
|
+
return [
|
|
81
|
+
{ label: hit.trace.colorField?.label ?? "category", value: hit.cell.category, color: hit.cell.color },
|
|
82
|
+
{ label: "share", value: numberOf(undefined, hit.cell.value) },
|
|
83
|
+
{ label: "rows", value: hit.cell.n.toLocaleString() },
|
|
84
|
+
];
|
|
85
|
+
}
|
|
86
|
+
const aggregate = hit.trace.data.geom === "heatmap" ? hit.trace.data.aggregate : "mean";
|
|
87
|
+
return [
|
|
88
|
+
{
|
|
89
|
+
label: hit.trace.field ? `${aggregate} of ${hit.trace.field.label}` : "records",
|
|
90
|
+
value: numberOf(hit.trace.field, hit.cell.value),
|
|
91
|
+
color: hit.trace.color,
|
|
92
|
+
},
|
|
93
|
+
{ label: "rows", value: hit.cell.n.toLocaleString() },
|
|
94
|
+
];
|
|
95
|
+
}
|
|
96
|
+
/*
|
|
97
|
+
A ribbon's thickness is the only number on a flow and a reader takes no value off thickness.
|
|
98
|
+
The width field names what was counted.
|
|
99
|
+
*/
|
|
100
|
+
if (hit.kind === "flow" || hit.kind === "sector") {
|
|
101
|
+
const width = plot.layers.find((series) => series.id === hit.trace.layerId)?.aes.value;
|
|
102
|
+
const field = typeof width === "string" ? fieldAt.get(width) : undefined;
|
|
103
|
+
const value = hit.kind === "flow" ? hit.link.value : hit.sector.value;
|
|
104
|
+
return [
|
|
105
|
+
{
|
|
106
|
+
label: field?.label ?? "rows",
|
|
107
|
+
value: field ? numberOf(field, value) : value.toLocaleString(),
|
|
108
|
+
color: hit.trace.color,
|
|
109
|
+
},
|
|
110
|
+
];
|
|
111
|
+
}
|
|
112
|
+
/* A node reads as the total under it. The trace names what was summed. */
|
|
113
|
+
if (hit.kind === "node") {
|
|
114
|
+
return [{ label: hit.trace.field?.label ?? "rows", value: numberOf(hit.trace.field, hit.cell.value) }];
|
|
115
|
+
}
|
|
116
|
+
/* A line is read across its axes. The readout is the record's value on each. */
|
|
117
|
+
if (hit.kind === "record") {
|
|
118
|
+
const axes = hit.trace.data.geom === "parcoords" ? hit.trace.data.axes : [];
|
|
119
|
+
return axes.map((axis, index) => ({
|
|
120
|
+
label: axis.label,
|
|
121
|
+
value: numberOf(fieldAt.get(axis.id), hit.line.values[index] ?? 0),
|
|
122
|
+
color: index === 0 ? hit.trace.color : undefined,
|
|
123
|
+
}));
|
|
124
|
+
}
|
|
125
|
+
/* The swatch takes the fill the geom drew, which the ramp reads off the median. */
|
|
126
|
+
if (hit.kind === "box" || hit.kind === "violin") {
|
|
127
|
+
return [
|
|
128
|
+
{
|
|
129
|
+
label: "median",
|
|
130
|
+
value: numberOf(hit.trace.field, hit.datum.median),
|
|
131
|
+
color: hit.trace.ramp(hit.datum.median),
|
|
132
|
+
},
|
|
133
|
+
{ label: "mean", value: numberOf(hit.trace.field, hit.datum.mean) },
|
|
134
|
+
{ label: "count", value: hit.datum.n.toLocaleString("en-US") },
|
|
135
|
+
];
|
|
136
|
+
}
|
|
137
|
+
const yScale = hit.trace.axis === "y2" ? plot.scales?.y2 : plot.scales?.y;
|
|
138
|
+
/* A run reads from its start to its end on each axis. An end equal to its start is written once. */
|
|
139
|
+
if (hit.kind === "segment") {
|
|
140
|
+
const { datum } = hit.hit;
|
|
141
|
+
const run = (field, scale, from, to) => from === to
|
|
142
|
+
? readingOf(field, scale, from)
|
|
143
|
+
: `${readingOf(field, scale, from)} to ${readingOf(field, scale, to)}`;
|
|
144
|
+
return [
|
|
145
|
+
{
|
|
146
|
+
label: hit.trace.field?.label ?? "Value",
|
|
147
|
+
value: run(hit.trace.field, yScale, datum.y, datum.yEnd),
|
|
148
|
+
color: hit.trace.color,
|
|
149
|
+
},
|
|
150
|
+
...(xField
|
|
151
|
+
? [
|
|
152
|
+
{
|
|
153
|
+
label: xField.label,
|
|
154
|
+
value: typeof datum.x === "string" || typeof datum.xEnd === "string"
|
|
155
|
+
? String(datum.x)
|
|
156
|
+
: run(xField, plot.scales?.x, datum.x, datum.xEnd),
|
|
157
|
+
},
|
|
158
|
+
]
|
|
159
|
+
: []),
|
|
160
|
+
];
|
|
161
|
+
}
|
|
162
|
+
/*
|
|
163
|
+
A cube's third measure has no axis a reader can take a value off. The card writes it beside
|
|
164
|
+
the pair that do have axes.
|
|
165
|
+
*/
|
|
166
|
+
const zField = hit.kind === "cube" && hit.trace.data.geom === "point3d" ? hit.trace.data.zField : undefined;
|
|
167
|
+
const z = hit.hit.datum.z;
|
|
168
|
+
/* The point's own row, in the table its layer read. Its place in the trace is no row. */
|
|
169
|
+
const source = hit.trace.table ?? data;
|
|
170
|
+
const held = hit.hit.datum;
|
|
171
|
+
const row = held.plotRow ?? (typeof held.row === "number" ? held.row : hit.hit.index);
|
|
172
|
+
return [
|
|
173
|
+
{
|
|
174
|
+
label: hit.trace.field?.label ?? "Value",
|
|
175
|
+
value: readingOf(hit.trace.field, yScale, hit.hit.datum.y),
|
|
176
|
+
color: hit.trace.color,
|
|
177
|
+
},
|
|
178
|
+
...(xField
|
|
179
|
+
? [{ label: xField.label, value: hit.hit.datum.category ?? readingOf(xField, plot.scales?.x, hit.hit.datum.x) }]
|
|
180
|
+
: []),
|
|
181
|
+
...(zField && z != null ? [{ label: zField.label, value: numberOf(zField, z) }] : []),
|
|
182
|
+
...(hit.trace.colorField
|
|
183
|
+
? [
|
|
184
|
+
{
|
|
185
|
+
label: hit.trace.colorField.label,
|
|
186
|
+
value: source.text(row, hit.trace.colorField.id) ?? "—",
|
|
187
|
+
},
|
|
188
|
+
]
|
|
189
|
+
: []),
|
|
190
|
+
...(plot.tooltip ?? []).flatMap((id) => {
|
|
191
|
+
const field = fieldAt.get(id);
|
|
192
|
+
const value = source.text(row, id);
|
|
193
|
+
return field && value != null ? [{ label: field.label, value }] : [];
|
|
194
|
+
}),
|
|
195
|
+
];
|
|
196
|
+
}
|
|
197
|
+
export const tooltipTitle = (hit) => {
|
|
198
|
+
if (hit.kind === "point" || hit.kind === "cube")
|
|
199
|
+
return hit.hit.datum.key ?? hit.trace.name;
|
|
200
|
+
if (hit.kind === "record")
|
|
201
|
+
return hit.line.key ?? hit.trace.name;
|
|
202
|
+
if (hit.kind === "segment")
|
|
203
|
+
return hit.trace.name;
|
|
204
|
+
// A ribbon is named by the two ends it joins. Neither end names it alone.
|
|
205
|
+
if (hit.kind === "flow")
|
|
206
|
+
return `${hit.link.source} to ${hit.link.target}`;
|
|
207
|
+
if (hit.kind === "sector")
|
|
208
|
+
return hit.sector.id;
|
|
209
|
+
if (hit.kind === "node")
|
|
210
|
+
return hit.cell.label;
|
|
211
|
+
// A cell is named by its pair. Neither axis names it alone.
|
|
212
|
+
if (hit.kind === "cell")
|
|
213
|
+
return `${hit.cell.x}, ${hit.cell.y}`;
|
|
214
|
+
return hit.datum.category;
|
|
215
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A region dragged over a scatter, and the records inside it.
|
|
3
|
+
*
|
|
4
|
+
* Shiny's `brushedPoints` and plotly's box and lasso select. The answer is rows, keys and a filter.
|
|
5
|
+
*/
|
|
6
|
+
import type { Trace } from "./layers.js";
|
|
7
|
+
import type { Predicate } from "./predicate.js";
|
|
8
|
+
import type { ChartSpec } from "./spec.js";
|
|
9
|
+
/** What a drag draws: a box between two corners, or a lasso through its vertices. */
|
|
10
|
+
export type BrushShape = "box" | "lasso";
|
|
11
|
+
/** A pixel in the plot area. */
|
|
12
|
+
export interface Pixel {
|
|
13
|
+
x: number;
|
|
14
|
+
y: number;
|
|
15
|
+
}
|
|
16
|
+
/** Whether a pixel falls inside a region. A lasso closes on its first vertex. */
|
|
17
|
+
export declare function insideRegion(shape: BrushShape, region: readonly Pixel[], at: Pixel): boolean;
|
|
18
|
+
/** The records a dragged region holds, and a filter naming them. */
|
|
19
|
+
export interface Brushed {
|
|
20
|
+
shape: BrushShape;
|
|
21
|
+
/** The region in data units: a box's two corners, or the lasso's vertices in order. */
|
|
22
|
+
region: {
|
|
23
|
+
x: number | string;
|
|
24
|
+
y: number | string;
|
|
25
|
+
}[];
|
|
26
|
+
/** Table rows inside the region, ascending, each once. */
|
|
27
|
+
rows: number[];
|
|
28
|
+
/** Keys of the same records, where a key column names each record once. Empty otherwise. */
|
|
29
|
+
keys: string[];
|
|
30
|
+
/**
|
|
31
|
+
* A filter for the same records. A box over two value axes and unmoved geoms states its ranges. The rest name keys.
|
|
32
|
+
*
|
|
33
|
+
* Null where no key column names the records.
|
|
34
|
+
*/
|
|
35
|
+
predicate: Predicate | null;
|
|
36
|
+
}
|
|
37
|
+
/** The pixel mapping of one axis and its inverse, as a brush reads them. */
|
|
38
|
+
export interface BrushAxis {
|
|
39
|
+
scale: (value: number | string) => number;
|
|
40
|
+
invert: (px: number) => number | string;
|
|
41
|
+
/** Distance between two neighboring band centers, where the axis runs bands. A jitter's offset there reads in it. */
|
|
42
|
+
step?: number;
|
|
43
|
+
}
|
|
44
|
+
export interface BrushContext {
|
|
45
|
+
spec: ChartSpec;
|
|
46
|
+
/** Column with record identity, where the table names one. */
|
|
47
|
+
keyField?: string;
|
|
48
|
+
/** Whether the pair of scales is flipped, as the band geoms take it. */
|
|
49
|
+
transposed?: boolean;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The records inside a region, off the scatter traces of one panel.
|
|
53
|
+
*
|
|
54
|
+
* Tested at the pixel each geom draws at, jitter and all. The filter states the region.
|
|
55
|
+
*/
|
|
56
|
+
export declare function brushedOf(traces: readonly Trace[], shape: BrushShape, region: readonly Pixel[], x: BrushAxis, y: BrushAxis, context: BrushContext): Brushed;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A region dragged over a scatter, and the records inside it.
|
|
3
|
+
*
|
|
4
|
+
* Shiny's `brushedPoints` and plotly's box and lasso select. The answer is rows, keys and a filter.
|
|
5
|
+
*/
|
|
6
|
+
import { placePoints } from "./hit.js";
|
|
7
|
+
/** Whether a pixel falls inside a region. A lasso closes on its first vertex. */
|
|
8
|
+
export function insideRegion(shape, region, at) {
|
|
9
|
+
if (shape === "box") {
|
|
10
|
+
const [a, b] = region;
|
|
11
|
+
if (!a || !b)
|
|
12
|
+
return false;
|
|
13
|
+
return (at.x >= Math.min(a.x, b.x) &&
|
|
14
|
+
at.x <= Math.max(a.x, b.x) &&
|
|
15
|
+
at.y >= Math.min(a.y, b.y) &&
|
|
16
|
+
at.y <= Math.max(a.y, b.y));
|
|
17
|
+
}
|
|
18
|
+
/* Even-odd crossings of a ray running right from the pixel. */
|
|
19
|
+
let inside = false;
|
|
20
|
+
for (let i = 0, j = region.length - 1; i < region.length; j = i, i += 1) {
|
|
21
|
+
const p = region[i];
|
|
22
|
+
const q = region[j];
|
|
23
|
+
if (p.y > at.y !== q.y > at.y && at.x < ((q.x - p.x) * (at.y - p.y)) / (q.y - p.y) + p.x)
|
|
24
|
+
inside = !inside;
|
|
25
|
+
}
|
|
26
|
+
return inside;
|
|
27
|
+
}
|
|
28
|
+
const oneField = (channel) => typeof channel === "string" ? channel : channel?.[0];
|
|
29
|
+
/** The scatter traces reading the figure's own table. A layer with rows of its own is left out. */
|
|
30
|
+
const brushable = (trace) => trace.data.at === "records" && trace.data.geom === "point" && trace.table == null;
|
|
31
|
+
/**
|
|
32
|
+
* The records inside a region, off the scatter traces of one panel.
|
|
33
|
+
*
|
|
34
|
+
* Tested at the pixel each geom draws at, jitter and all. The filter states the region.
|
|
35
|
+
*/
|
|
36
|
+
export function brushedOf(traces, shape, region, x, y, context) {
|
|
37
|
+
const transposed = context.transposed === true;
|
|
38
|
+
const rows = new Set();
|
|
39
|
+
const keys = new Set();
|
|
40
|
+
const pairs = new Map();
|
|
41
|
+
/* The row each key names. A key naming two rows names a group and no record. */
|
|
42
|
+
const owner = new Map();
|
|
43
|
+
let unique = true;
|
|
44
|
+
/* A geom a jitter or a nudge moved off its reading. A range would name records drawn outside the region. */
|
|
45
|
+
let moved = false;
|
|
46
|
+
for (const trace of traces) {
|
|
47
|
+
if (!brushable(trace) || !("points" in trace.data))
|
|
48
|
+
continue;
|
|
49
|
+
const series = context.spec.layers.find((one) => one.id === trace.layerId);
|
|
50
|
+
const xField = oneField(series?.aes.x);
|
|
51
|
+
const yField = oneField(series?.aes.y);
|
|
52
|
+
if (xField && yField)
|
|
53
|
+
pairs.set(`${xField}\u0000${yField}`, [xField, yField]);
|
|
54
|
+
const points = trace.data.points;
|
|
55
|
+
for (const point of points) {
|
|
56
|
+
if (point.dx || point.dy)
|
|
57
|
+
moved = true;
|
|
58
|
+
if (point.key == null)
|
|
59
|
+
continue;
|
|
60
|
+
if ((owner.get(point.key) ?? point.row) !== point.row)
|
|
61
|
+
unique = false;
|
|
62
|
+
owner.set(point.key, point.row);
|
|
63
|
+
}
|
|
64
|
+
for (const placed of placePoints(points, x.scale, y.scale, { transposed, step: transposed ? y.step : x.step })) {
|
|
65
|
+
if (!insideRegion(shape, region, placed))
|
|
66
|
+
continue;
|
|
67
|
+
const point = points[placed.index];
|
|
68
|
+
rows.add(point.plotRow ?? point.row);
|
|
69
|
+
if (point.key != null)
|
|
70
|
+
keys.add(point.key);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const inData = region.map((at) => transposed ? { x: y.invert(at.y), y: x.invert(at.x) } : { x: x.invert(at.x), y: y.invert(at.y) });
|
|
74
|
+
const named = unique ? [...keys] : [];
|
|
75
|
+
return {
|
|
76
|
+
shape,
|
|
77
|
+
region: inData,
|
|
78
|
+
rows: [...rows].sort((a, b) => a - b),
|
|
79
|
+
keys: named,
|
|
80
|
+
predicate: filterOf(shape, inData, moved ? [] : [...pairs.values()], named, unique ? context.keyField : undefined),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/** Ranges for a box over two value axes, the keys otherwise. */
|
|
84
|
+
function filterOf(shape, corners, pairs, keys, keyField) {
|
|
85
|
+
const [a, b] = corners;
|
|
86
|
+
if (shape === "box" && a && b && pairs.length > 0) {
|
|
87
|
+
const { x: ax, y: ay } = a;
|
|
88
|
+
const { x: bx, y: by } = b;
|
|
89
|
+
if (typeof ax === "number" && typeof ay === "number" && typeof bx === "number" && typeof by === "number") {
|
|
90
|
+
const boxes = pairs.map(([xField, yField]) => ({
|
|
91
|
+
kind: "and",
|
|
92
|
+
of: [
|
|
93
|
+
{ kind: "range", field: xField, lo: Math.min(ax, bx), hi: Math.max(ax, bx) },
|
|
94
|
+
{ kind: "range", field: yField, lo: Math.min(ay, by), hi: Math.max(ay, by) },
|
|
95
|
+
],
|
|
96
|
+
}));
|
|
97
|
+
return boxes.length === 1 ? boxes[0] : { kind: "or", of: boxes };
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return keyField ? { kind: "in", field: keyField, values: keys } : null;
|
|
101
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rings stacked outside a circular figure.
|
|
3
|
+
*
|
|
4
|
+
* A ring holds one reading per sector. Angles come from the sectors and radii come from the stack.
|
|
5
|
+
*/
|
|
6
|
+
import { type Aggregate } from "./aggregate.js";
|
|
7
|
+
import type { PlotData, PlotField } from "./plot-data.js";
|
|
8
|
+
export interface RingPanel {
|
|
9
|
+
/** Radial extent, in pixels. */
|
|
10
|
+
size: number;
|
|
11
|
+
/** Gap between this ring and what it sits outside, in pixels. */
|
|
12
|
+
gap?: number;
|
|
13
|
+
content: RingContent;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* What one ring draws.
|
|
17
|
+
*
|
|
18
|
+
* `bar` reads a measure per sector, and `stack` splits the same measure by a second column.
|
|
19
|
+
* `point` draws where the figure places a record: one point per ribbon on a chord, reduced by
|
|
20
|
+
* `aggregate`, and one per row at its own position on an ideogram.
|
|
21
|
+
*/
|
|
22
|
+
export type RingContent = {
|
|
23
|
+
kind: "bar";
|
|
24
|
+
field: string;
|
|
25
|
+
aggregate?: Aggregate;
|
|
26
|
+
} | {
|
|
27
|
+
kind: "stack";
|
|
28
|
+
field: string;
|
|
29
|
+
by: string;
|
|
30
|
+
aggregate?: Aggregate;
|
|
31
|
+
} | {
|
|
32
|
+
kind: "point";
|
|
33
|
+
field: string;
|
|
34
|
+
aggregate?: Aggregate;
|
|
35
|
+
};
|
|
36
|
+
/** A ring's place in the stack, in pixels from the center. */
|
|
37
|
+
export interface RingBand {
|
|
38
|
+
inner: number;
|
|
39
|
+
outer: number;
|
|
40
|
+
}
|
|
41
|
+
/** One row's reading, drawn at its own position inside its sector. */
|
|
42
|
+
export interface RingDot {
|
|
43
|
+
sector: string;
|
|
44
|
+
/** The reading's position, in the sector's own units. */
|
|
45
|
+
at: number;
|
|
46
|
+
value: number;
|
|
47
|
+
}
|
|
48
|
+
/** One pair's reading, drawn where that pair's own ribbon sits. */
|
|
49
|
+
export interface RingPoint {
|
|
50
|
+
source: string;
|
|
51
|
+
target: string;
|
|
52
|
+
value: number;
|
|
53
|
+
}
|
|
54
|
+
/** One reading of a ring. `floor` is where a stacked segment starts. */
|
|
55
|
+
export interface RingBar {
|
|
56
|
+
sector: string;
|
|
57
|
+
level?: string;
|
|
58
|
+
value: number;
|
|
59
|
+
floor: number;
|
|
60
|
+
}
|
|
61
|
+
export interface RingReading {
|
|
62
|
+
kind: RingContent["kind"];
|
|
63
|
+
/** The column's own name, for the legend and the footer. */
|
|
64
|
+
label: string;
|
|
65
|
+
/** Where the ring's scale runs, in the column's units. */
|
|
66
|
+
domain: [number, number];
|
|
67
|
+
/** Levels a stack drew, in stacking order. Empty on plain bars. */
|
|
68
|
+
levels: string[];
|
|
69
|
+
bars: RingBar[];
|
|
70
|
+
/** One entry per pair, for a ring that draws a point where each ribbon sits. */
|
|
71
|
+
points: RingPoint[];
|
|
72
|
+
/** One entry per row, for a ring that draws a point at its own position. */
|
|
73
|
+
dots: RingDot[];
|
|
74
|
+
}
|
|
75
|
+
/** Where a row's own position is read from, for a ring that plots one point per row. */
|
|
76
|
+
export interface RingPosition {
|
|
77
|
+
sector: string;
|
|
78
|
+
start: string;
|
|
79
|
+
end?: string;
|
|
80
|
+
}
|
|
81
|
+
/** Gap between one ring and the next where the author states none, in pixels. */
|
|
82
|
+
export declare const RING_GAP = 4;
|
|
83
|
+
/** Levels one stack draws. Past this the segments are hairlines. */
|
|
84
|
+
export declare const RING_LEVEL_CAP = 8;
|
|
85
|
+
/**
|
|
86
|
+
* Where each ring sits, working outward from a radius.
|
|
87
|
+
*
|
|
88
|
+
* The first ring clears the figure it stands outside by its own gap.
|
|
89
|
+
*/
|
|
90
|
+
export declare function ringBands(from: number, rings: readonly RingPanel[]): RingBand[];
|
|
91
|
+
/**
|
|
92
|
+
* One ring's readings, against the sectors a circular figure drew.
|
|
93
|
+
*
|
|
94
|
+
* A row is credited to the sector at each of its ends. A pair names two.
|
|
95
|
+
*/
|
|
96
|
+
export declare function ringReading(content: RingContent, sectors: readonly string[], ends: readonly string[], data: PlotData, fieldAt: Map<string, PlotField>, position?: RingPosition): RingReading | null;
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rings stacked outside a circular figure.
|
|
3
|
+
*
|
|
4
|
+
* A ring holds one reading per sector. Angles come from the sectors and radii come from the stack.
|
|
5
|
+
*/
|
|
6
|
+
import { reduceBucket } from "./aggregate.js";
|
|
7
|
+
/** Gap between one ring and the next where the author states none, in pixels. */
|
|
8
|
+
export const RING_GAP = 4;
|
|
9
|
+
/** Levels one stack draws. Past this the segments are hairlines. */
|
|
10
|
+
export const RING_LEVEL_CAP = 8;
|
|
11
|
+
/**
|
|
12
|
+
* Where each ring sits, working outward from a radius.
|
|
13
|
+
*
|
|
14
|
+
* The first ring clears the figure it stands outside by its own gap.
|
|
15
|
+
*/
|
|
16
|
+
export function ringBands(from, rings) {
|
|
17
|
+
let at = from;
|
|
18
|
+
return rings.map((ring) => {
|
|
19
|
+
const inner = at + (ring.gap ?? RING_GAP);
|
|
20
|
+
const outer = inner + Math.max(0, ring.size);
|
|
21
|
+
at = outer;
|
|
22
|
+
return { inner, outer };
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
const empty = () => ({ n: 0, sum: 0, sumSq: 0, min: Infinity, max: -Infinity });
|
|
26
|
+
const take = (bucket, value) => {
|
|
27
|
+
bucket.n += 1;
|
|
28
|
+
bucket.sum += value;
|
|
29
|
+
bucket.sumSq += value * value;
|
|
30
|
+
bucket.min = Math.min(bucket.min, value);
|
|
31
|
+
bucket.max = Math.max(bucket.max, value);
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* One ring's readings, against the sectors a circular figure drew.
|
|
35
|
+
*
|
|
36
|
+
* A row is credited to the sector at each of its ends. A pair names two.
|
|
37
|
+
*/
|
|
38
|
+
export function ringReading(content, sectors, ends, data, fieldAt, position) {
|
|
39
|
+
const measure = fieldAt.get(content.field);
|
|
40
|
+
if (!measure || sectors.length === 0)
|
|
41
|
+
return null;
|
|
42
|
+
const within = new Set(sectors);
|
|
43
|
+
const named = measure.unit ? `${measure.label} (${measure.unit})` : measure.label;
|
|
44
|
+
/* A row keeps its own position, which is what a genomic track plots against. */
|
|
45
|
+
if (content.kind === "point" && position) {
|
|
46
|
+
const dots = [];
|
|
47
|
+
let reach = 0;
|
|
48
|
+
for (let row = 0; row < data.rowCount; row += 1) {
|
|
49
|
+
const value = data.number(row, content.field);
|
|
50
|
+
const name = data.text(row, position.sector);
|
|
51
|
+
const start = data.number(row, position.start);
|
|
52
|
+
if (value == null || !Number.isFinite(value))
|
|
53
|
+
continue;
|
|
54
|
+
if (name == null || !within.has(name) || start == null)
|
|
55
|
+
continue;
|
|
56
|
+
const end = position.end ? data.number(row, position.end) : null;
|
|
57
|
+
dots.push({ sector: name, at: end == null ? start : (start + end) / 2, value });
|
|
58
|
+
reach = Math.max(reach, value);
|
|
59
|
+
}
|
|
60
|
+
if (dots.length === 0)
|
|
61
|
+
return null;
|
|
62
|
+
return { kind: "point", label: named, domain: [0, reach > 0 ? reach : 1], levels: [], bars: [], points: [], dots };
|
|
63
|
+
}
|
|
64
|
+
/*
|
|
65
|
+
A point ring over ribbons reads a pair instead of a sector.
|
|
66
|
+
|
|
67
|
+
One reading per pair, drawn where that pair's ribbon already sits. A row keeps no point of its
|
|
68
|
+
own: a ribbon stands for the whole pair and a single row's reading would misname it.
|
|
69
|
+
*/
|
|
70
|
+
if (content.kind === "point") {
|
|
71
|
+
const [sourceId, targetId] = ends;
|
|
72
|
+
if (!sourceId || !targetId)
|
|
73
|
+
return null;
|
|
74
|
+
const pairs = new Map();
|
|
75
|
+
for (let row = 0; row < data.rowCount; row += 1) {
|
|
76
|
+
const value = data.number(row, content.field);
|
|
77
|
+
const source = data.text(row, sourceId);
|
|
78
|
+
const target = data.text(row, targetId);
|
|
79
|
+
if (value == null || !Number.isFinite(value))
|
|
80
|
+
continue;
|
|
81
|
+
if (source == null || target == null || !within.has(source) || !within.has(target))
|
|
82
|
+
continue;
|
|
83
|
+
const key = `${source}\u0000${target}`;
|
|
84
|
+
const held = pairs.get(key) ?? { source, target, bucket: empty() };
|
|
85
|
+
take(held.bucket, value);
|
|
86
|
+
pairs.set(key, held);
|
|
87
|
+
}
|
|
88
|
+
const points = [];
|
|
89
|
+
let reach = 0;
|
|
90
|
+
for (const { source, target, bucket } of pairs.values()) {
|
|
91
|
+
const value = reduceBucket(bucket, content.aggregate ?? "mean");
|
|
92
|
+
if (!Number.isFinite(value))
|
|
93
|
+
continue;
|
|
94
|
+
points.push({ source, target, value });
|
|
95
|
+
reach = Math.max(reach, value);
|
|
96
|
+
}
|
|
97
|
+
if (points.length === 0)
|
|
98
|
+
return null;
|
|
99
|
+
return {
|
|
100
|
+
kind: "point",
|
|
101
|
+
label: named,
|
|
102
|
+
domain: [0, reach > 0 ? reach : 1],
|
|
103
|
+
levels: [],
|
|
104
|
+
bars: [],
|
|
105
|
+
points,
|
|
106
|
+
dots: [],
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
const aggregate = content.aggregate ?? "mean";
|
|
110
|
+
const stacked = content.kind === "stack";
|
|
111
|
+
const buckets = new Map();
|
|
112
|
+
const weight = new Map();
|
|
113
|
+
for (let row = 0; row < data.rowCount; row += 1) {
|
|
114
|
+
const value = data.number(row, content.field);
|
|
115
|
+
if (value == null || !Number.isFinite(value))
|
|
116
|
+
continue;
|
|
117
|
+
const level = stacked ? (data.text(row, content.by) ?? "") : "";
|
|
118
|
+
if (stacked && level === "")
|
|
119
|
+
continue;
|
|
120
|
+
for (const id of ends) {
|
|
121
|
+
const name = data.text(row, id);
|
|
122
|
+
if (name == null || !within.has(name))
|
|
123
|
+
continue;
|
|
124
|
+
const levels = buckets.get(name) ?? new Map();
|
|
125
|
+
const bucket = levels.get(level) ?? empty();
|
|
126
|
+
take(bucket, value);
|
|
127
|
+
levels.set(level, bucket);
|
|
128
|
+
buckets.set(name, levels);
|
|
129
|
+
weight.set(level, (weight.get(level) ?? 0) + value);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (buckets.size === 0)
|
|
133
|
+
return null;
|
|
134
|
+
/* The largest levels lead. The tail past the cap is left out. */
|
|
135
|
+
const levels = stacked
|
|
136
|
+
? [...weight]
|
|
137
|
+
.sort((a, b) => b[1] - a[1])
|
|
138
|
+
.slice(0, RING_LEVEL_CAP)
|
|
139
|
+
.map(([level]) => level)
|
|
140
|
+
: [""];
|
|
141
|
+
const bars = [];
|
|
142
|
+
let reach = 0;
|
|
143
|
+
for (const sector of sectors) {
|
|
144
|
+
const held = buckets.get(sector);
|
|
145
|
+
if (!held)
|
|
146
|
+
continue;
|
|
147
|
+
let floor = 0;
|
|
148
|
+
for (const level of levels) {
|
|
149
|
+
const bucket = held.get(level);
|
|
150
|
+
if (!bucket)
|
|
151
|
+
continue;
|
|
152
|
+
const value = reduceBucket(bucket, aggregate);
|
|
153
|
+
if (!Number.isFinite(value))
|
|
154
|
+
continue;
|
|
155
|
+
bars.push({ sector, ...(stacked ? { level } : {}), value, floor });
|
|
156
|
+
floor += value;
|
|
157
|
+
}
|
|
158
|
+
reach = Math.max(reach, floor);
|
|
159
|
+
}
|
|
160
|
+
if (bars.length === 0)
|
|
161
|
+
return null;
|
|
162
|
+
return {
|
|
163
|
+
kind: content.kind,
|
|
164
|
+
label: named,
|
|
165
|
+
domain: [0, reach > 0 ? reach : 1],
|
|
166
|
+
levels: stacked ? levels : [],
|
|
167
|
+
bars,
|
|
168
|
+
points: [],
|
|
169
|
+
dots: [],
|
|
170
|
+
};
|
|
171
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reference lines drawn as layers, and where each one crosses the plot area.
|
|
3
|
+
*
|
|
4
|
+
* ggplot's `geom_hline`, `geom_vline` and `geom_abline`. A sloped line reads transformed space.
|
|
5
|
+
*/
|
|
6
|
+
import { type AxisTransform } from "./transform.js";
|
|
7
|
+
/** One reference line, in the units its geom reads. */
|
|
8
|
+
export type RuleLine = {
|
|
9
|
+
kind: "h";
|
|
10
|
+
y: number;
|
|
11
|
+
label?: string;
|
|
12
|
+
} | {
|
|
13
|
+
kind: "v";
|
|
14
|
+
x: number;
|
|
15
|
+
label?: string;
|
|
16
|
+
} | {
|
|
17
|
+
kind: "ab";
|
|
18
|
+
slope: number;
|
|
19
|
+
intercept: number;
|
|
20
|
+
label?: string;
|
|
21
|
+
};
|
|
22
|
+
/** A continuous axis as a reference line reads it. A band axis is null. */
|
|
23
|
+
export interface RuleAxis {
|
|
24
|
+
domain: [number, number];
|
|
25
|
+
log?: boolean;
|
|
26
|
+
transform?: AxisTransform;
|
|
27
|
+
}
|
|
28
|
+
/** Where a line crosses the plot area. A horizontal line names its y and a vertical line its x. */
|
|
29
|
+
export type RuleSegment = {
|
|
30
|
+
y: number;
|
|
31
|
+
} | {
|
|
32
|
+
x: number;
|
|
33
|
+
} | {
|
|
34
|
+
x0: number;
|
|
35
|
+
y0: number;
|
|
36
|
+
x1: number;
|
|
37
|
+
y1: number;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* The part of a line inside the plot area, in data units, or null.
|
|
41
|
+
*
|
|
42
|
+
* A horizontal or vertical line on a band axis marks the category its value names. A sloped line
|
|
43
|
+
* needs continuous axes.
|
|
44
|
+
*/
|
|
45
|
+
export declare function ruleSegment(rule: RuleLine, x: RuleAxis | null, y: RuleAxis | null): RuleSegment | null;
|