@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,423 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* A physics graph drawn on the canvas the charts are already using.
|
|
4
|
+
*
|
|
5
|
+
* cosmos owns the layout. Colors come off the package's tokens.
|
|
6
|
+
*/
|
|
7
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
8
|
+
import { critiqueGraph, pinnedOver, rampPosition, rgbOf, } from "../core/internal.js";
|
|
9
|
+
import { glSurface } from "../react/gl/surface.js";
|
|
10
|
+
import { useVizTheme } from "../react/theme.js";
|
|
11
|
+
import { TooltipCard, TooltipLayer } from "../react/Tooltip.js";
|
|
12
|
+
import { graphTooltip, readGraph } from "./data.js";
|
|
13
|
+
/**
|
|
14
|
+
* Default force settings, applied where a caller passes none.
|
|
15
|
+
*
|
|
16
|
+
* A link long enough that a community has an inside, and a decay that cools inside a few seconds.
|
|
17
|
+
*/
|
|
18
|
+
const DEFAULT_PHYSICS = {
|
|
19
|
+
gravity: 0.5,
|
|
20
|
+
repulsion: 2,
|
|
21
|
+
linkSpring: 2,
|
|
22
|
+
linkDistance: 40,
|
|
23
|
+
friction: 0.83,
|
|
24
|
+
decay: 3000,
|
|
25
|
+
cluster: 0,
|
|
26
|
+
};
|
|
27
|
+
/** The seven cosmos simulation settings, from this package's values under the caller's overrides. */
|
|
28
|
+
const simulationOf = (physics) => ({
|
|
29
|
+
simulationGravity: physics?.gravity ?? DEFAULT_PHYSICS.gravity,
|
|
30
|
+
simulationRepulsion: physics?.repulsion ?? DEFAULT_PHYSICS.repulsion,
|
|
31
|
+
simulationLinkSpring: physics?.linkSpring ?? DEFAULT_PHYSICS.linkSpring,
|
|
32
|
+
simulationLinkDistance: physics?.linkDistance ?? DEFAULT_PHYSICS.linkDistance,
|
|
33
|
+
simulationFriction: physics?.friction ?? DEFAULT_PHYSICS.friction,
|
|
34
|
+
simulationDecay: physics?.decay ?? DEFAULT_PHYSICS.decay,
|
|
35
|
+
simulationCluster: physics?.cluster ?? DEFAULT_PHYSICS.cluster,
|
|
36
|
+
});
|
|
37
|
+
/** Node diameter in pixels, matched to the point geoms. */
|
|
38
|
+
const NODE_SIZE = 5;
|
|
39
|
+
/**
|
|
40
|
+
* The square the layout runs in.
|
|
41
|
+
*
|
|
42
|
+
* cosmos counts positions from a corner. The middle of the space is half of this.
|
|
43
|
+
*/
|
|
44
|
+
const SPACE = 4096;
|
|
45
|
+
/** How far the seed ring stands from the middle, in space units. */
|
|
46
|
+
const SEED_RADIUS = SPACE / 8;
|
|
47
|
+
/** Link width where the table carries no weight to read one from. */
|
|
48
|
+
const LINK_WIDTH = 0.8;
|
|
49
|
+
/**
|
|
50
|
+
* How the view is fitted, and how long the layout spreads first.
|
|
51
|
+
*
|
|
52
|
+
* A few seconds is where the shape arrives.
|
|
53
|
+
*
|
|
54
|
+
* The forces keep running while the camera moves.
|
|
55
|
+
*/
|
|
56
|
+
const FIT = { duration: 600, padding: 0.03, settleMs: 3200 };
|
|
57
|
+
/**
|
|
58
|
+
* How hard a force change restarts the run.
|
|
59
|
+
*
|
|
60
|
+
* A third moves the nodes enough to show what the force did and settles inside a second.
|
|
61
|
+
*/
|
|
62
|
+
const RESTART_ALPHA = 0.3;
|
|
63
|
+
/**
|
|
64
|
+
* Share of the space placed nodes span along the longer side.
|
|
65
|
+
*
|
|
66
|
+
* A fixed layout fits at a settled layout's node size. A force layout seeds wider.
|
|
67
|
+
*/
|
|
68
|
+
const PLACED_SPAN = { fixed: 0.25, force: 0.8 };
|
|
69
|
+
/** Alpha of a node outside `focus`. */
|
|
70
|
+
const RECEDED_ALPHA = 0.28;
|
|
71
|
+
/** Gray where the theme has not resolved. */
|
|
72
|
+
const GRAY = [0.5, 0.5, 0.5, 1];
|
|
73
|
+
const asRgba = (css, fallback) => {
|
|
74
|
+
const rgb = rgbOf(css);
|
|
75
|
+
return rgb ? [rgb[0], rgb[1], rgb[2], 1] : fallback;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Each node's color in the flat array cosmos takes.
|
|
79
|
+
*
|
|
80
|
+
* A value reads the magnitude map. A group reads the identity map. A missing value takes the muted ink.
|
|
81
|
+
*/
|
|
82
|
+
function colorsOf(data, { palettes, codes, muted, focus }) {
|
|
83
|
+
const colors = new Float32Array(data.nodes.length * 4);
|
|
84
|
+
const [lo, hi] = data.valueDomain ?? [0, 1];
|
|
85
|
+
const ink = muted ? asRgba(muted, GRAY) : GRAY;
|
|
86
|
+
data.nodes.forEach((node, at) => {
|
|
87
|
+
if (focus && !focus.has(node.id)) {
|
|
88
|
+
colors.set([ink[0], ink[1], ink[2], RECEDED_ALPHA], at * 4);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (node.value == null && data.valueField) {
|
|
92
|
+
colors.set(ink, at * 4);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const css = node.value != null
|
|
96
|
+
? palettes.sequential(Math.min(1, Math.max(0, rampPosition(node.value, lo, hi, palettes.center))))
|
|
97
|
+
: palettes.categorical(node.group == null ? 0 : (codes.get(node.group) ?? 0));
|
|
98
|
+
colors.set(asRgba(css, GRAY), at * 4);
|
|
99
|
+
});
|
|
100
|
+
return colors;
|
|
101
|
+
}
|
|
102
|
+
/** The positions, sizes and links in the flat arrays cosmos takes. */
|
|
103
|
+
function packed(data, layout) {
|
|
104
|
+
const index = new Map(data.nodes.map((node, at) => [node.id, at]));
|
|
105
|
+
const positions = new Float32Array(data.nodes.length * 2);
|
|
106
|
+
const sizes = new Float32Array(data.nodes.length).fill(NODE_SIZE);
|
|
107
|
+
/* Placed nodes keep their proportions: one scale on both axes, centered in the space. */
|
|
108
|
+
let [minX, maxX, minY, maxY] = [Infinity, -Infinity, Infinity, -Infinity];
|
|
109
|
+
for (const node of data.nodes) {
|
|
110
|
+
if (node.x == null || node.y == null)
|
|
111
|
+
continue;
|
|
112
|
+
minX = Math.min(minX, node.x);
|
|
113
|
+
maxX = Math.max(maxX, node.x);
|
|
114
|
+
minY = Math.min(minY, node.y);
|
|
115
|
+
maxY = Math.max(maxY, node.y);
|
|
116
|
+
}
|
|
117
|
+
const extent = Math.max(maxX - minX, maxY - minY);
|
|
118
|
+
const scale = extent > 0 ? (SPACE * PLACED_SPAN[layout]) / extent : 1;
|
|
119
|
+
data.nodes.forEach((node, at) => {
|
|
120
|
+
if (node.x != null && node.y != null) {
|
|
121
|
+
positions[at * 2] = SPACE / 2 + (node.x - (minX + maxX) / 2) * scale;
|
|
122
|
+
positions[at * 2 + 1] = SPACE / 2 + (node.y - (minY + maxY) / 2) * scale;
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
/* Seeds a ring around the middle of the space. The layout converges faster from a spread. */
|
|
126
|
+
const angle = (at / Math.max(1, data.nodes.length)) * Math.PI * 2;
|
|
127
|
+
positions[at * 2] = SPACE / 2 + Math.cos(angle) * SEED_RADIUS;
|
|
128
|
+
positions[at * 2 + 1] = SPACE / 2 + Math.sin(angle) * SEED_RADIUS;
|
|
129
|
+
});
|
|
130
|
+
/* A cluster index per node, in the order the groups first appear. */
|
|
131
|
+
const groupAt = new Map(data.groups.map((group, at) => [group, at]));
|
|
132
|
+
const clusters = data.nodes.map((node) => (node.group == null ? undefined : groupAt.get(node.group)));
|
|
133
|
+
const links = new Float32Array(data.links.length * 2);
|
|
134
|
+
data.links.forEach((link, at) => {
|
|
135
|
+
links[at * 2] = index.get(link.source) ?? 0;
|
|
136
|
+
links[at * 2 + 1] = index.get(link.target) ?? 0;
|
|
137
|
+
});
|
|
138
|
+
/* A weighted table draws its strong joins thick and pulls them tighter. The width floor keeps a weak join visible. */
|
|
139
|
+
const [linkLo, linkHi] = data.linkValueDomain ?? [0, 0];
|
|
140
|
+
const span = linkHi - linkLo;
|
|
141
|
+
const strengths = data.linkValueDomain == null
|
|
142
|
+
? null
|
|
143
|
+
: Float32Array.from(data.links, (link) => link.value == null || span === 0 ? 0.5 : (link.value - linkLo) / span);
|
|
144
|
+
const widths = strengths == null ? null : strengths.map((strength) => 0.3 + 0.6 * strength);
|
|
145
|
+
return { positions, sizes, links, strengths, widths, clusters };
|
|
146
|
+
}
|
|
147
|
+
export function GraphSurface({ tables, headline, width, height, palettes, ground = false, ownContext = true, physics, display, layout = "force", paused = false, focus, set, onSelect, onHover, onContextMenu, onReady, onCritique, }) {
|
|
148
|
+
const holder = useRef(null);
|
|
149
|
+
const host = useRef(null);
|
|
150
|
+
const graph = useRef(null);
|
|
151
|
+
/* Read off the wrapper, the way the GL geoms read theirs. The resolver turns an `oklch()` into something a renderer parses. */
|
|
152
|
+
const theme = useVizTheme(holder);
|
|
153
|
+
const [problem, setProblem] = useState(null);
|
|
154
|
+
const [ready, setReady] = useState(false);
|
|
155
|
+
const [hover, setHover] = useState(null);
|
|
156
|
+
/* Keyed on the rows and the column names. Tables written inline hold the layout the reader is looking at. */
|
|
157
|
+
const { nodes, links, nodeId, nodeLabel, linkSource, linkTarget, nodeValue, linkValue, nodeX, nodeY, nodeGroup } = tables;
|
|
158
|
+
const fieldsKey = JSON.stringify([tables.fieldOverrides ?? null, tables.valueLimits ?? null]);
|
|
159
|
+
const read = useMemo(() => readGraph(tables),
|
|
160
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- the tables as the read takes them
|
|
161
|
+
[nodes, links, nodeId, nodeLabel, linkSource, linkTarget, nodeValue, linkValue, nodeX, nodeY, nodeGroup, fieldsKey]);
|
|
162
|
+
const { data } = read;
|
|
163
|
+
/* Read by content, as `Viz` reads a set. */
|
|
164
|
+
const pinsKey = JSON.stringify(nodeGroup ? (set?.assignments[nodeGroup] ?? null) : null);
|
|
165
|
+
const codes = useMemo(() => pinnedOver(new Map(data.groups.map((group, at) => [group, at])), nodeGroup ? set?.assignments[nodeGroup] : undefined),
|
|
166
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- the pins as their content
|
|
167
|
+
[data, nodeGroup, pinsKey]);
|
|
168
|
+
const focused = useMemo(() => (focus ? new Set(focus) : null), [focus]);
|
|
169
|
+
const coloring = { palettes, codes, muted: theme?.["--viz-ink-muted"], focus: focused };
|
|
170
|
+
/* cosmos holds the callbacks it was built with. The pointer handlers must read what is current through a ref. */
|
|
171
|
+
const pointScale = display?.pointScale ?? 1;
|
|
172
|
+
const linkScale = display?.linkScale ?? 1;
|
|
173
|
+
const drawLinks = display?.links ?? true;
|
|
174
|
+
const state = {
|
|
175
|
+
read,
|
|
176
|
+
data,
|
|
177
|
+
coloring,
|
|
178
|
+
tooltip: tables.tooltip,
|
|
179
|
+
onSelect,
|
|
180
|
+
onHover,
|
|
181
|
+
onContextMenu,
|
|
182
|
+
onReady,
|
|
183
|
+
pointScale,
|
|
184
|
+
linkScale,
|
|
185
|
+
drawLinks,
|
|
186
|
+
physics,
|
|
187
|
+
layout,
|
|
188
|
+
paused,
|
|
189
|
+
};
|
|
190
|
+
const live = useRef(state);
|
|
191
|
+
live.current = state;
|
|
192
|
+
/* The device attaches once and the data is pushed after, the shape the GL geoms take. */
|
|
193
|
+
useEffect(() => {
|
|
194
|
+
const div = host.current;
|
|
195
|
+
if (!div)
|
|
196
|
+
return;
|
|
197
|
+
let canceled = false;
|
|
198
|
+
/* Released when the simulation stops and taken again when it restarts. */
|
|
199
|
+
let giveBack = null;
|
|
200
|
+
void (async () => {
|
|
201
|
+
const renderer = ownContext ? null : glSurface();
|
|
202
|
+
const gl = ownContext ? null : (renderer?.context() ?? null);
|
|
203
|
+
if (!ownContext && !gl) {
|
|
204
|
+
setProblem("this page has no WebGL2 context to lend");
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
/* One canvas serves one layout. A page with charts already on the GPU must give the graph its own context. */
|
|
208
|
+
const held = renderer?.layerCount() ?? 0;
|
|
209
|
+
if (held > 0) {
|
|
210
|
+
setProblem(`${held} chart ${held === 1 ? "layer is" : "layers are"} already drawing on the shared context, and cosmos takes the canvas it is handed. Leave ownContext on for a graph beside charts on the GPU.`);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
try {
|
|
214
|
+
/* Both libraries load on demand. A consumer that draws no graph pays none of their bundle. */
|
|
215
|
+
const [{ webgl2Adapter }, { Graph }] = await Promise.all([import("@luma.gl/webgl"), import("@cosmos.gl/graph")]);
|
|
216
|
+
if (canceled)
|
|
217
|
+
return;
|
|
218
|
+
/* cosmos owns the clock while it draws, and the charts draw from its tick. The clock goes back on a pause or an end. */
|
|
219
|
+
const lend = () => {
|
|
220
|
+
if (renderer && !giveBack)
|
|
221
|
+
giveBack = renderer.lendClock();
|
|
222
|
+
};
|
|
223
|
+
const giveClockBack = () => {
|
|
224
|
+
giveBack?.();
|
|
225
|
+
giveBack = null;
|
|
226
|
+
};
|
|
227
|
+
const config = {
|
|
228
|
+
/* The colors arrive from the theme effect below, which is also what carries a flip. */
|
|
229
|
+
backgroundColor: [0, 0, 0, 0],
|
|
230
|
+
spaceSize: SPACE,
|
|
231
|
+
/* A node keeps its size on the page as the reader zooms, the way a point geom does. */
|
|
232
|
+
scalePointsOnZoom: true,
|
|
233
|
+
pointSizeScale: live.current.pointScale,
|
|
234
|
+
linkDefaultWidth: LINK_WIDTH,
|
|
235
|
+
linkWidthScale: live.current.linkScale,
|
|
236
|
+
renderLinks: live.current.drawLinks,
|
|
237
|
+
/* A ring marks which node the card names. Dragging pulls a community apart to look inside it. */
|
|
238
|
+
renderHoveredPointRing: true,
|
|
239
|
+
enableDrag: true,
|
|
240
|
+
/* What a picked node leaves the rest at. cosmos's own value washes out a dense panel. */
|
|
241
|
+
pointGreyoutOpacity: 0.08,
|
|
242
|
+
linkGreyoutOpacity: 0,
|
|
243
|
+
/* The card is placed off the point. It stays on the node while the layout moves under a still cursor. */
|
|
244
|
+
onPointMouseOver: (index, position) => {
|
|
245
|
+
const { read, coloring, tooltip, onHover } = live.current;
|
|
246
|
+
/* A group row takes a swatch where the groups paint the nodes. */
|
|
247
|
+
const swatch = read.data.valueField
|
|
248
|
+
? undefined
|
|
249
|
+
: (group) => coloring.palettes.categorical(coloring.codes.get(group) ?? 0);
|
|
250
|
+
const card = graphTooltip(read, index, tooltip, swatch);
|
|
251
|
+
if (!card)
|
|
252
|
+
return;
|
|
253
|
+
const [x, y] = graph.current?.spaceToScreenPosition(position) ?? [0, 0];
|
|
254
|
+
/* The shared canvas spans the viewport. The card is placed inside the holder. */
|
|
255
|
+
const origin = gl ? holder.current?.getBoundingClientRect() : undefined;
|
|
256
|
+
setHover({ ...card, at: { x: x - (origin?.left ?? 0), y: y - (origin?.top ?? 0) } });
|
|
257
|
+
onHover?.(read.data.nodes[index] ?? null);
|
|
258
|
+
},
|
|
259
|
+
onPointMouseOut: () => {
|
|
260
|
+
setHover(null);
|
|
261
|
+
live.current.onHover?.(null);
|
|
262
|
+
},
|
|
263
|
+
onPointClick: (index) => live.current.onSelect?.(live.current.data.nodes[index] ?? null),
|
|
264
|
+
onBackgroundClick: () => live.current.onSelect?.(null),
|
|
265
|
+
onPointContextMenu: (index, _position, event) => {
|
|
266
|
+
const { onContextMenu: menu, data: current } = live.current;
|
|
267
|
+
if (!menu)
|
|
268
|
+
return;
|
|
269
|
+
event.preventDefault();
|
|
270
|
+
menu(current.nodes[index] ?? null);
|
|
271
|
+
},
|
|
272
|
+
/* Fires only where neither a node nor a link is under the pointer. */
|
|
273
|
+
onBackgroundContextMenu: (event) => {
|
|
274
|
+
const menu = live.current.onContextMenu;
|
|
275
|
+
if (!menu)
|
|
276
|
+
return;
|
|
277
|
+
event.preventDefault();
|
|
278
|
+
menu(null);
|
|
279
|
+
},
|
|
280
|
+
onSimulationStart: lend,
|
|
281
|
+
onSimulationUnpause: lend,
|
|
282
|
+
onSimulationTick: () => renderer?.drawNow(),
|
|
283
|
+
onSimulationPause: giveClockBack,
|
|
284
|
+
onSimulationEnd: () => {
|
|
285
|
+
graph.current?.fitView(FIT.duration, FIT.padding);
|
|
286
|
+
giveClockBack();
|
|
287
|
+
},
|
|
288
|
+
...simulationOf(live.current.physics),
|
|
289
|
+
};
|
|
290
|
+
/* The device wraps the renderer's context. Handed nothing, cosmos makes its own canvas inside the div. */
|
|
291
|
+
const device = gl ? webgl2Adapter.attach(gl) : null;
|
|
292
|
+
const instance = new Graph(div, config, device ?? undefined);
|
|
293
|
+
graph.current = instance;
|
|
294
|
+
/* `ready` covers the whole chain. cosmos flips its internal flag before the points and lines exist. */
|
|
295
|
+
await instance.ready;
|
|
296
|
+
if (canceled)
|
|
297
|
+
return;
|
|
298
|
+
setReady(true);
|
|
299
|
+
}
|
|
300
|
+
catch (error) {
|
|
301
|
+
if (!canceled)
|
|
302
|
+
setProblem(error instanceof Error ? error.message : String(error));
|
|
303
|
+
}
|
|
304
|
+
})();
|
|
305
|
+
return () => {
|
|
306
|
+
canceled = true;
|
|
307
|
+
giveBack?.();
|
|
308
|
+
giveBack = null;
|
|
309
|
+
/* cosmos throws out of its own teardown on a device it owns. An unmount effect must hold that throw. */
|
|
310
|
+
try {
|
|
311
|
+
graph.current?.destroy();
|
|
312
|
+
}
|
|
313
|
+
catch (error) {
|
|
314
|
+
console.warn("cosmos threw while tearing itself down", error);
|
|
315
|
+
}
|
|
316
|
+
graph.current = null;
|
|
317
|
+
setReady(false);
|
|
318
|
+
setHover(null);
|
|
319
|
+
};
|
|
320
|
+
}, [ownContext]);
|
|
321
|
+
/* The layout re-runs on a change of nodes, links, places or groups. A value, a label or a card column recolors in place. */
|
|
322
|
+
useEffect(() => {
|
|
323
|
+
const instance = graph.current;
|
|
324
|
+
if (!ready || !instance)
|
|
325
|
+
return;
|
|
326
|
+
const { data } = live.current;
|
|
327
|
+
const fixed = layout === "fixed";
|
|
328
|
+
const { positions, sizes, links, strengths, widths, clusters } = packed(data, layout);
|
|
329
|
+
instance.setPointPositions(positions);
|
|
330
|
+
instance.setPointColors(colorsOf(data, live.current.coloring));
|
|
331
|
+
instance.setPointSizes(sizes);
|
|
332
|
+
instance.setLinks(links);
|
|
333
|
+
if (widths)
|
|
334
|
+
instance.setLinkWidths(widths);
|
|
335
|
+
if (strengths)
|
|
336
|
+
instance.setLinkStrength(strengths);
|
|
337
|
+
instance.setPointClusters(clusters);
|
|
338
|
+
/* A fixed layout pins each node where its table placed it, and runs no forces. */
|
|
339
|
+
instance.setPinnedPoints(fixed ? data.nodes.map((_, at) => at) : null);
|
|
340
|
+
/* `render` draws one frame, `start` runs the forces, and `unpause` must follow: setting positions leaves the simulation paused. */
|
|
341
|
+
instance.render();
|
|
342
|
+
if (!fixed) {
|
|
343
|
+
instance.start();
|
|
344
|
+
instance.unpause();
|
|
345
|
+
if (live.current.paused)
|
|
346
|
+
instance.pause();
|
|
347
|
+
}
|
|
348
|
+
/* Fitted once the shape has arrived. `FIT` states the timer. A fixed layout has arrived already. */
|
|
349
|
+
const settle = window.setTimeout(() => instance.fitView(FIT.duration, FIT.padding), fixed ? 0 : FIT.settleMs);
|
|
350
|
+
return () => window.clearTimeout(settle);
|
|
351
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- the columns the layout reads
|
|
352
|
+
}, [ready, ownContext, layout, nodes, links, nodeId, linkSource, linkTarget, nodeX, nodeY, nodeGroup, linkValue]);
|
|
353
|
+
/* After the layout effect above, which pushes the data first. */
|
|
354
|
+
useEffect(() => {
|
|
355
|
+
if (!ready || !graph.current)
|
|
356
|
+
return;
|
|
357
|
+
live.current.onReady?.({ nodes: data.nodes.length, links: data.links.length, sharedContext: !ownContext });
|
|
358
|
+
}, [ready, data, ownContext]);
|
|
359
|
+
/* A new value, map, pin, focus or theme recolors the nodes where they stand. */
|
|
360
|
+
const muted = coloring.muted;
|
|
361
|
+
useEffect(() => {
|
|
362
|
+
const instance = graph.current;
|
|
363
|
+
if (!ready || !instance)
|
|
364
|
+
return;
|
|
365
|
+
instance.setPointColors(colorsOf(live.current.data, live.current.coloring));
|
|
366
|
+
instance.render();
|
|
367
|
+
}, [ready, data, palettes, codes, focused, muted]);
|
|
368
|
+
/* A pause stops the forces. A resume restarts them at a low alpha. */
|
|
369
|
+
const heldPaused = useRef(paused);
|
|
370
|
+
useEffect(() => {
|
|
371
|
+
const instance = graph.current;
|
|
372
|
+
if (!ready || !instance || heldPaused.current === paused)
|
|
373
|
+
return;
|
|
374
|
+
heldPaused.current = paused;
|
|
375
|
+
if (live.current.layout === "fixed")
|
|
376
|
+
return;
|
|
377
|
+
if (paused)
|
|
378
|
+
instance.pause();
|
|
379
|
+
else
|
|
380
|
+
instance.start(RESTART_ALPHA);
|
|
381
|
+
}, [ready, paused]);
|
|
382
|
+
/* The colors move without rebuilding the graph, and a theme flip keeps the layout the reader is looking at. */
|
|
383
|
+
useEffect(() => {
|
|
384
|
+
const instance = graph.current;
|
|
385
|
+
if (!ready || !instance || !theme)
|
|
386
|
+
return;
|
|
387
|
+
instance.setConfigPartial({
|
|
388
|
+
backgroundColor: ground ? asRgba(theme["--viz-paper"], [0, 0, 0, 1]) : [0, 0, 0, 0],
|
|
389
|
+
linkDefaultColor: theme["--viz-ink-muted"],
|
|
390
|
+
hoveredPointRingColor: theme["--viz-ink"],
|
|
391
|
+
});
|
|
392
|
+
instance.render();
|
|
393
|
+
}, [ready, theme, ground]);
|
|
394
|
+
useEffect(() => {
|
|
395
|
+
const instance = graph.current;
|
|
396
|
+
if (!ready || !instance)
|
|
397
|
+
return;
|
|
398
|
+
instance.setConfigPartial({ pointSizeScale: pointScale, linkWidthScale: linkScale, renderLinks: drawLinks });
|
|
399
|
+
instance.render();
|
|
400
|
+
}, [ready, pointScale, linkScale, drawLinks]);
|
|
401
|
+
/* The forces move on the running graph. A settled layout is restarted at a low alpha to let the shape move. */
|
|
402
|
+
/* Keyed on the values. An inline object from a parent is a new identity each render. */
|
|
403
|
+
const forces = JSON.stringify(simulationOf(physics));
|
|
404
|
+
useEffect(() => {
|
|
405
|
+
const instance = graph.current;
|
|
406
|
+
if (!ready || !instance)
|
|
407
|
+
return;
|
|
408
|
+
instance.setConfigPartial(simulationOf(live.current.physics));
|
|
409
|
+
/* A paused or fixed graph takes the new forces at its next start. */
|
|
410
|
+
if (live.current.layout === "force" && !live.current.paused)
|
|
411
|
+
instance.start(RESTART_ALPHA);
|
|
412
|
+
}, [ready, forces]);
|
|
413
|
+
/* Run on the data and reported when the findings change. A caller hears about a claim before anything draws. */
|
|
414
|
+
const findings = useMemo(() => critiqueGraph({ headline, links: data.links.length, linksDropped: data.linksDropped }), [headline, data]);
|
|
415
|
+
const reported = findings.map((finding) => `${finding.code}:${finding.path}:${finding.message}`).join("|");
|
|
416
|
+
const critiqued = useRef(onCritique);
|
|
417
|
+
critiqued.current = onCritique;
|
|
418
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- `reported` is the findings as a reader would read them
|
|
419
|
+
useEffect(() => {
|
|
420
|
+
critiqued.current?.(findings);
|
|
421
|
+
}, [reported]);
|
|
422
|
+
return (_jsxs("div", { ref: holder, style: { position: "relative", width, height }, children: [_jsx("div", { ref: host, style: { width, height } }), hover && (_jsx(TooltipLayer, { at: hover.at, bounds: { width, height }, children: _jsx(TooltipCard, { title: hover.title, rows: hover.rows }) })), data.linksDropped > 0 && (_jsx("span", { style: { position: "absolute", bottom: 0, left: 0, fontSize: 11, opacity: 0.7 }, children: `${data.linksDropped.toLocaleString()} links reached a node the table does not carry` })), problem && (_jsx("span", { style: { position: "absolute", inset: 0, display: "grid", placeItems: "center", fontSize: 12 }, children: problem }))] }));
|
|
423
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two tables a graph reads, over the three input shapes the rest of the package takes.
|
|
3
|
+
*
|
|
4
|
+
* Each table takes rows, columns or an arrow table.
|
|
5
|
+
*/
|
|
6
|
+
import { type FieldOverride, type PlotData, type PlotField, type TooltipRow, type VizData } from "../core/internal.js";
|
|
7
|
+
export interface GraphNode {
|
|
8
|
+
id: string;
|
|
9
|
+
/** Where the node sits, in the table's units. Absent lets the layout place it. */
|
|
10
|
+
x?: number;
|
|
11
|
+
y?: number;
|
|
12
|
+
/** Value behind the node's color, in its own units. */
|
|
13
|
+
value?: number;
|
|
14
|
+
/** The node's group, as text. */
|
|
15
|
+
group?: string;
|
|
16
|
+
/** What the reader calls this node. Falls back to the id where no column names one. */
|
|
17
|
+
label?: string;
|
|
18
|
+
/** Links touching this node. */
|
|
19
|
+
degree: number;
|
|
20
|
+
}
|
|
21
|
+
export interface GraphLink {
|
|
22
|
+
source: string;
|
|
23
|
+
target: string;
|
|
24
|
+
/** Value behind the link's width or color. */
|
|
25
|
+
value?: number;
|
|
26
|
+
}
|
|
27
|
+
export interface GraphData {
|
|
28
|
+
nodes: GraphNode[];
|
|
29
|
+
links: GraphLink[];
|
|
30
|
+
/** Range of the node values, for a ramp that holds across the whole graph. */
|
|
31
|
+
valueDomain: [number, number] | null;
|
|
32
|
+
/**
|
|
33
|
+
* Range of the link values, on the same terms.
|
|
34
|
+
*
|
|
35
|
+
* A value sets a link's width and its spring pull. Both read as a fraction of this range.
|
|
36
|
+
*/
|
|
37
|
+
linkValueDomain: [number, number] | null;
|
|
38
|
+
/** The value column's own label, unit and digits. The tooltip writes it the way a chart does. */
|
|
39
|
+
valueField?: PlotField;
|
|
40
|
+
/** The groups, in the order the node table first names them. */
|
|
41
|
+
groups: string[];
|
|
42
|
+
/** The group column's own label. */
|
|
43
|
+
groupField?: PlotField;
|
|
44
|
+
/**
|
|
45
|
+
* Links naming an end no node carries.
|
|
46
|
+
*
|
|
47
|
+
* Counted and reported. The reader should see how much of the source the view dropped.
|
|
48
|
+
*/
|
|
49
|
+
linksDropped: number;
|
|
50
|
+
}
|
|
51
|
+
/** Which column carries what. A table of edges may name its ends anything. */
|
|
52
|
+
export interface GraphTables {
|
|
53
|
+
nodes: VizData;
|
|
54
|
+
links: VizData;
|
|
55
|
+
/** Column naming each node. Defaults to `id`. */
|
|
56
|
+
nodeId?: string;
|
|
57
|
+
/** Column naming a node for the reader. Absent leaves the id doing the naming. */
|
|
58
|
+
nodeLabel?: string;
|
|
59
|
+
/** Columns naming a link's ends. Default to `source` and `target`. */
|
|
60
|
+
linkSource?: string;
|
|
61
|
+
linkTarget?: string;
|
|
62
|
+
/** Column behind a node's color, and behind a link's width. */
|
|
63
|
+
nodeValue?: string;
|
|
64
|
+
linkValue?: string;
|
|
65
|
+
/** The two ends the magnitude map spans, in the value's units. A value past an end takes that end's color. */
|
|
66
|
+
valueLimits?: readonly [number, number];
|
|
67
|
+
/** Columns placing a node, in their own units. A fixed layout pins each node there. */
|
|
68
|
+
nodeX?: string;
|
|
69
|
+
nodeY?: string;
|
|
70
|
+
/** Column naming a node's group. A group colors a node with no value, and the cluster force pulls it together. */
|
|
71
|
+
nodeGroup?: string;
|
|
72
|
+
/** Node columns the card adds after the value and the links, in this order. */
|
|
73
|
+
tooltip?: readonly string[];
|
|
74
|
+
/**
|
|
75
|
+
* Units and labels the two tables cannot carry, keyed by column.
|
|
76
|
+
*
|
|
77
|
+
* The same override a chart takes. A value then reads as its own measure in the tooltip.
|
|
78
|
+
*/
|
|
79
|
+
fieldOverrides?: Record<string, FieldOverride>;
|
|
80
|
+
}
|
|
81
|
+
/** The graph, the node table it came from, and the table row behind each node. */
|
|
82
|
+
export interface GraphRead {
|
|
83
|
+
data: GraphData;
|
|
84
|
+
nodeTable: PlotData;
|
|
85
|
+
rows: number[];
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The two tables read into what the layout takes.
|
|
89
|
+
*
|
|
90
|
+
* Drops any link naming an end no node carries, and counts what it dropped.
|
|
91
|
+
*/
|
|
92
|
+
export declare function toGraphData(tables: GraphTables): GraphData;
|
|
93
|
+
/** `toGraphData`, keeping the node table for the card's extra columns. */
|
|
94
|
+
export declare function readGraph(tables: GraphTables): GraphRead;
|
|
95
|
+
/**
|
|
96
|
+
* Builds one node's hover card: its value, group, links and id, then the `tooltip` columns.
|
|
97
|
+
*
|
|
98
|
+
* `groupColor` paints the group row's swatch.
|
|
99
|
+
*/
|
|
100
|
+
export declare function graphTooltip(read: GraphRead, index: number, tooltip?: readonly string[], groupColor?: (group: string) => string): {
|
|
101
|
+
title: string;
|
|
102
|
+
rows: TooltipRow[];
|
|
103
|
+
} | null;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two tables a graph reads, over the three input shapes the rest of the package takes.
|
|
3
|
+
*
|
|
4
|
+
* Each table takes rows, columns or an arrow table.
|
|
5
|
+
*/
|
|
6
|
+
import { numberOf, toPlotData, } from "../core/internal.js";
|
|
7
|
+
const textAt = (data, row, field) => data.text(row, field);
|
|
8
|
+
/**
|
|
9
|
+
* The two tables read into what the layout takes.
|
|
10
|
+
*
|
|
11
|
+
* Drops any link naming an end no node carries, and counts what it dropped.
|
|
12
|
+
*/
|
|
13
|
+
export function toGraphData(tables) {
|
|
14
|
+
return readGraph(tables).data;
|
|
15
|
+
}
|
|
16
|
+
/** `toGraphData`, keeping the node table for the card's extra columns. */
|
|
17
|
+
export function readGraph(tables) {
|
|
18
|
+
const nodeTable = toPlotData(tables.nodes, { fieldOverrides: tables.fieldOverrides });
|
|
19
|
+
const linkTable = toPlotData(tables.links, { fieldOverrides: tables.fieldOverrides });
|
|
20
|
+
const idField = tables.nodeId ?? "id";
|
|
21
|
+
const sourceField = tables.linkSource ?? "source";
|
|
22
|
+
const targetField = tables.linkTarget ?? "target";
|
|
23
|
+
const nodes = [];
|
|
24
|
+
const rows = [];
|
|
25
|
+
const known = new Set();
|
|
26
|
+
const groups = new Set();
|
|
27
|
+
let lo = Infinity;
|
|
28
|
+
let hi = -Infinity;
|
|
29
|
+
for (let row = 0; row < nodeTable.rowCount; row += 1) {
|
|
30
|
+
const id = textAt(nodeTable, row, idField);
|
|
31
|
+
if (id == null || known.has(id))
|
|
32
|
+
continue;
|
|
33
|
+
known.add(id);
|
|
34
|
+
const value = tables.nodeValue ? (nodeTable.number(row, tables.nodeValue) ?? undefined) : undefined;
|
|
35
|
+
if (value != null) {
|
|
36
|
+
if (value < lo)
|
|
37
|
+
lo = value;
|
|
38
|
+
if (value > hi)
|
|
39
|
+
hi = value;
|
|
40
|
+
}
|
|
41
|
+
const label = tables.nodeLabel ? (textAt(nodeTable, row, tables.nodeLabel) ?? undefined) : undefined;
|
|
42
|
+
const x = tables.nodeX ? nodeTable.number(row, tables.nodeX) : null;
|
|
43
|
+
const y = tables.nodeY ? nodeTable.number(row, tables.nodeY) : null;
|
|
44
|
+
const group = tables.nodeGroup ? (textAt(nodeTable, row, tables.nodeGroup) ?? undefined) : undefined;
|
|
45
|
+
if (group != null)
|
|
46
|
+
groups.add(group);
|
|
47
|
+
nodes.push({
|
|
48
|
+
id,
|
|
49
|
+
degree: 0,
|
|
50
|
+
...(x == null || y == null ? {} : { x, y }),
|
|
51
|
+
...(value == null ? {} : { value }),
|
|
52
|
+
...(group == null ? {} : { group }),
|
|
53
|
+
...(label == null ? {} : { label }),
|
|
54
|
+
});
|
|
55
|
+
rows.push(row);
|
|
56
|
+
}
|
|
57
|
+
const links = [];
|
|
58
|
+
let linksDropped = 0;
|
|
59
|
+
let linkLo = Infinity;
|
|
60
|
+
let linkHi = -Infinity;
|
|
61
|
+
for (let row = 0; row < linkTable.rowCount; row += 1) {
|
|
62
|
+
const source = textAt(linkTable, row, sourceField);
|
|
63
|
+
const target = textAt(linkTable, row, targetField);
|
|
64
|
+
if (source == null || target == null) {
|
|
65
|
+
linksDropped += 1;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (!known.has(source) || !known.has(target)) {
|
|
69
|
+
linksDropped += 1;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
const value = tables.linkValue ? (linkTable.number(row, tables.linkValue) ?? undefined) : undefined;
|
|
73
|
+
if (value != null) {
|
|
74
|
+
if (value < linkLo)
|
|
75
|
+
linkLo = value;
|
|
76
|
+
if (value > linkHi)
|
|
77
|
+
linkHi = value;
|
|
78
|
+
}
|
|
79
|
+
links.push({ source, target, ...(value == null ? {} : { value }) });
|
|
80
|
+
}
|
|
81
|
+
/* Degree counts the surviving links. It states what the picture draws. */
|
|
82
|
+
const at = new Map(nodes.map((node, index) => [node.id, index]));
|
|
83
|
+
for (const link of links) {
|
|
84
|
+
nodes[at.get(link.source)].degree += 1;
|
|
85
|
+
nodes[at.get(link.target)].degree += 1;
|
|
86
|
+
}
|
|
87
|
+
const fieldOf = (id) => (id ? nodeTable.fields.find((field) => field.id === id) : undefined);
|
|
88
|
+
const valueField = fieldOf(tables.nodeValue);
|
|
89
|
+
const groupField = fieldOf(tables.nodeGroup);
|
|
90
|
+
return {
|
|
91
|
+
data: {
|
|
92
|
+
nodes,
|
|
93
|
+
links,
|
|
94
|
+
valueDomain: tables.valueLimits
|
|
95
|
+
? [tables.valueLimits[0], tables.valueLimits[1]]
|
|
96
|
+
: Number.isFinite(lo)
|
|
97
|
+
? [lo, hi]
|
|
98
|
+
: null,
|
|
99
|
+
linkValueDomain: Number.isFinite(linkLo) ? [linkLo, linkHi] : null,
|
|
100
|
+
...(valueField ? { valueField } : {}),
|
|
101
|
+
groups: [...groups],
|
|
102
|
+
...(groupField ? { groupField } : {}),
|
|
103
|
+
linksDropped,
|
|
104
|
+
},
|
|
105
|
+
nodeTable,
|
|
106
|
+
rows,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Builds one node's hover card: its value, group, links and id, then the `tooltip` columns.
|
|
111
|
+
*
|
|
112
|
+
* `groupColor` paints the group row's swatch.
|
|
113
|
+
*/
|
|
114
|
+
export function graphTooltip(read, index, tooltip = [], groupColor) {
|
|
115
|
+
const { data, nodeTable } = read;
|
|
116
|
+
const node = data.nodes[index];
|
|
117
|
+
const row = read.rows[index];
|
|
118
|
+
if (!node || row == null)
|
|
119
|
+
return null;
|
|
120
|
+
const rows = [];
|
|
121
|
+
if (node.value != null) {
|
|
122
|
+
rows.push({ label: data.valueField?.label ?? "Value", value: numberOf(data.valueField, node.value) });
|
|
123
|
+
}
|
|
124
|
+
if (node.group != null) {
|
|
125
|
+
const color = groupColor?.(node.group);
|
|
126
|
+
rows.push({ label: data.groupField?.label ?? "Group", value: node.group, ...(color ? { color } : {}) });
|
|
127
|
+
}
|
|
128
|
+
rows.push({ label: node.degree === 1 ? "link" : "links", value: node.degree.toLocaleString() });
|
|
129
|
+
/* The id gets a row once a label stands in for it. A query takes the id. */
|
|
130
|
+
if (node.label)
|
|
131
|
+
rows.push({ label: "id", value: node.id });
|
|
132
|
+
for (const id of tooltip) {
|
|
133
|
+
const field = nodeTable.fields.find((candidate) => candidate.id === id);
|
|
134
|
+
const number = field?.numeric ? nodeTable.number(row, id) : null;
|
|
135
|
+
const value = number == null ? nodeTable.text(row, id) : numberOf(field, number);
|
|
136
|
+
if (value != null && value !== "")
|
|
137
|
+
rows.push({ label: field?.label ?? id, value });
|
|
138
|
+
}
|
|
139
|
+
return { title: node.label ?? node.id, rows };
|
|
140
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The network graph: two tables, drawn on the package's own WebGL context.
|
|
3
|
+
*
|
|
4
|
+
* cosmos runs the force layout. The package supplies the context, the clock and the tokens.
|
|
5
|
+
*/
|
|
6
|
+
export { toGraphData, type GraphData, type GraphLink, type GraphNode, type GraphTables } from "./data.js";
|
|
7
|
+
export { GraphSurface, type GraphDisplay, type GraphLayout, type GraphPhysics, type GraphSurfaceProps, } from "./GraphSurface.js";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The network graph: two tables, drawn on the package's own WebGL context.
|
|
3
|
+
*
|
|
4
|
+
* cosmos runs the force layout. The package supplies the context, the clock and the tokens.
|
|
5
|
+
*/
|
|
6
|
+
export { toGraphData } from "./data.js";
|
|
7
|
+
export { GraphSurface, } from "./GraphSurface.js";
|