@dxos/react-ui-graph 0.8.4-main.422d1c7879 → 0.8.4-main.43cb759274
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 +102 -5
- package/dist/lib/browser/{chunk-LKC455GK.mjs → chunk-EASNZZ7B.mjs} +1 -1
- package/dist/lib/browser/{chunk-LKC455GK.mjs.map → chunk-EASNZZ7B.mjs.map} +2 -2
- package/dist/lib/browser/index.mjs +1783 -496
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +1 -1
- package/dist/lib/browser/testing/index.mjs.map +2 -2
- package/dist/lib/node-esm/{chunk-JE4KZCUF.mjs → chunk-JSE2X4ML.mjs} +1 -1
- package/dist/lib/node-esm/{chunk-JE4KZCUF.mjs.map → chunk-JSE2X4ML.mjs.map} +2 -2
- package/dist/lib/node-esm/index.mjs +1783 -496
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +1 -1
- package/dist/lib/node-esm/testing/index.mjs.map +2 -2
- package/dist/types/src/components/Graph/Graph.d.ts +20 -2
- package/dist/types/src/components/Graph/Graph.d.ts.map +1 -1
- package/dist/types/src/components/Graph/Graph.stories.d.ts +3 -3
- package/dist/types/src/components/Graph/Graph.stories.d.ts.map +1 -1
- package/dist/types/src/components/Mesh/Mesh.d.ts.map +1 -1
- package/dist/types/src/components/Mesh/Mesh.stories.d.ts.map +1 -1
- package/dist/types/src/components/SVG/FPS.d.ts +20 -0
- package/dist/types/src/components/SVG/FPS.d.ts.map +1 -0
- package/dist/types/src/components/SVG/Grid.d.ts.map +1 -1
- package/dist/types/src/components/SVG/Markers.d.ts.map +1 -1
- package/dist/types/src/components/SVG/Root.d.ts +5 -1
- package/dist/types/src/components/SVG/Root.d.ts.map +1 -1
- package/dist/types/src/components/SVG/SVG.d.ts +3 -1
- package/dist/types/src/components/SVG/SVG.d.ts.map +1 -1
- package/dist/types/src/components/SVG/Svg.stories.d.ts.map +1 -1
- package/dist/types/src/fx/fx.stories.d.ts.map +1 -1
- package/dist/types/src/fx/pulsar.d.ts.map +1 -1
- package/dist/types/src/graph/markers.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-bundle-projector.d.ts +47 -0
- package/dist/types/src/graph/projector/graph-bundle-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-cluster-projector.d.ts +65 -0
- package/dist/types/src/graph/projector/graph-cluster-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-drag.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-force-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-forces.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-lattice-projector.d.ts +23 -0
- package/dist/types/src/graph/projector/graph-lattice-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-plexus-projector.d.ts +67 -0
- package/dist/types/src/graph/projector/graph-plexus-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-plexus-projector.test.d.ts +2 -0
- package/dist/types/src/graph/projector/graph-plexus-projector.test.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-projector.d.ts +8 -0
- package/dist/types/src/graph/projector/graph-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-radial-projector.d.ts +6 -0
- package/dist/types/src/graph/projector/graph-radial-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-relational-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-swarm-projector.d.ts +72 -0
- package/dist/types/src/graph/projector/graph-swarm-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/index.d.ts +5 -0
- package/dist/types/src/graph/projector/index.d.ts.map +1 -1
- package/dist/types/src/graph/projector/projector.d.ts +10 -1
- package/dist/types/src/graph/projector/projector.d.ts.map +1 -1
- package/dist/types/src/graph/renderer/bullets.d.ts.map +1 -1
- package/dist/types/src/graph/renderer/graph-renderer.d.ts +41 -1
- package/dist/types/src/graph/renderer/graph-renderer.d.ts.map +1 -1
- package/dist/types/src/graph/renderer/index.d.ts +1 -0
- package/dist/types/src/graph/renderer/index.d.ts.map +1 -1
- package/dist/types/src/graph/renderer/labels.d.ts +33 -0
- package/dist/types/src/graph/renderer/labels.d.ts.map +1 -0
- package/dist/types/src/graph/renderer/renderer.d.ts +12 -1
- package/dist/types/src/graph/renderer/renderer.d.ts.map +1 -1
- package/dist/types/src/graph/types.d.ts +14 -0
- package/dist/types/src/graph/types.d.ts.map +1 -1
- package/dist/types/src/hooks/useGrid.d.ts.map +1 -1
- package/dist/types/src/hooks/useStateRef.d.ts.map +1 -1
- package/dist/types/src/hooks/useSvgContext.d.ts.map +1 -1
- package/dist/types/src/hooks/useZoom.d.ts.map +1 -1
- package/dist/types/src/stories/experimental.stories.d.ts +7 -0
- package/dist/types/src/stories/experimental.stories.d.ts.map +1 -0
- package/dist/types/src/stories/hooks.stories.d.ts.map +1 -1
- package/dist/types/src/testing/data.d.ts +1 -1
- package/dist/types/src/testing/data.d.ts.map +1 -1
- package/dist/types/src/testing/hooks.d.ts.map +1 -1
- package/dist/types/src/testing/model.d.ts.map +1 -1
- package/dist/types/src/util/events.d.ts.map +1 -1
- package/dist/types/src/util/fraction.d.ts.map +1 -1
- package/dist/types/src/util/points.d.ts.map +1 -1
- package/dist/types/src/util/scale.d.ts.map +1 -1
- package/dist/types/src/util/screen.d.ts.map +1 -1
- package/dist/types/src/util/vector.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +19 -19
- package/src/components/Graph/Graph.stories.tsx +5 -5
- package/src/components/Graph/Graph.tsx +116 -21
- package/src/components/Mesh/Mesh.stories.tsx +6 -4
- package/src/components/Mesh/Mesh.tsx +43 -35
- package/src/components/SVG/FPS.tsx +91 -0
- package/src/components/SVG/Root.tsx +30 -4
- package/src/components/SVG/SVG.tsx +4 -1
- package/src/graph/projector/graph-bundle-projector.ts +210 -0
- package/src/graph/projector/graph-cluster-projector.ts +337 -0
- package/src/graph/projector/graph-force-projector.ts +6 -1
- package/src/graph/projector/graph-hierarchical-projector.ts +1 -0
- package/src/graph/projector/graph-lattice-projector.ts +86 -0
- package/src/graph/projector/graph-plexus-projector.test.ts +102 -0
- package/src/graph/projector/graph-plexus-projector.ts +309 -0
- package/src/graph/projector/graph-projector.ts +22 -1
- package/src/graph/projector/graph-radial-projector.ts +30 -2
- package/src/graph/projector/graph-relational-projector.ts +1 -0
- package/src/graph/projector/graph-swarm-projector.ts +373 -0
- package/src/graph/projector/index.ts +5 -0
- package/src/graph/projector/projector.ts +12 -3
- package/src/graph/renderer/graph-renderer.ts +289 -30
- package/src/graph/renderer/index.ts +1 -0
- package/src/graph/renderer/labels.ts +138 -0
- package/src/graph/renderer/renderer.ts +15 -4
- package/src/graph/types.ts +14 -0
- package/src/stories/experimental.stories.tsx +446 -0
- package/src/stories/hooks.stories.tsx +6 -1
- package/styles/graph.css +51 -2
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type HierarchyNode, cluster as d3Cluster, curveBundle, hierarchy, lineRadial } from 'd3';
|
|
6
|
+
|
|
7
|
+
import { type Graph } from '@dxos/graph';
|
|
8
|
+
import { log } from '@dxos/log';
|
|
9
|
+
|
|
10
|
+
import { type GraphLayoutEdge, type GraphLayoutNode } from '../types';
|
|
11
|
+
import { GraphRadialProjector, type GraphRadialProjectorOptions, updateNode } from './graph-radial-projector';
|
|
12
|
+
|
|
13
|
+
export type GraphBundleProjectorOptions = GraphRadialProjectorOptions & {
|
|
14
|
+
/** Reserved space around the bundle (screen pixels). */
|
|
15
|
+
margin?: number;
|
|
16
|
+
/** Radius for leaf nodes. Default 4. */
|
|
17
|
+
leafRadius?: number;
|
|
18
|
+
/**
|
|
19
|
+
* Group key for each node — synthetic intermediate nodes are created per unique key
|
|
20
|
+
* to give the bundling layout a routing point (lowest common ancestor) for edges
|
|
21
|
+
* between leaves in different groups. Return `undefined` to attach the node directly
|
|
22
|
+
* to the root (all such leaves bundle through the root).
|
|
23
|
+
*/
|
|
24
|
+
groupOf?: (node: GraphLayoutNode) => string | undefined;
|
|
25
|
+
/** Bundling tension passed to `d3.curveBundle.beta`. 0 = straight chord, 1 = max bundled. */
|
|
26
|
+
tension?: number;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const ROOT_ID = '__bundle_root__';
|
|
30
|
+
const GROUP_PREFIX = '__bundle_group__:';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Cartesian → d3-radial polar. d3 measures angle clockwise from "up" (the -y direction).
|
|
34
|
+
* Inverse of `[cos(a - π/2) * r, sin(a - π/2) * r]`.
|
|
35
|
+
*/
|
|
36
|
+
const cartesianToPolar = (x: number, y: number): { angle: number; radius: number } => ({
|
|
37
|
+
angle: Math.atan2(x, -y),
|
|
38
|
+
radius: Math.hypot(x, y),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
type PolarPoint = { angle: number; radius: number };
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Hierarchical edge bundling — Holten 2006 / d3 observable notebook
|
|
45
|
+
* (https://observablehq.com/@d3/hierarchical-edge-bundling).
|
|
46
|
+
*
|
|
47
|
+
* Synthesizes the same root → group → leaf hierarchy as `GraphClusterProjector`, runs
|
|
48
|
+
* d3.cluster to give every node a polar position, but renders ONLY the leaves
|
|
49
|
+
* (root + groups are routing anchors, not visible nodes). Each data edge is replaced
|
|
50
|
+
* with a curve generated by `lineRadial(curveBundle.beta(tension))` over the polar
|
|
51
|
+
* positions of `source.path(target)` — i.e. the chain of ancestors through the lowest
|
|
52
|
+
* common ancestor.
|
|
53
|
+
*
|
|
54
|
+
* Per-frame `onTickFrame` refreshes the leaf endpoints (first + last point of every
|
|
55
|
+
* path) from current cartesian positions so the curves stay glued to leaves during a
|
|
56
|
+
* cross-variant tween (force → bundle, lattice → bundle). Internal ancestor positions
|
|
57
|
+
* are fixed at d3.cluster's final polar layout, which means during the tween curves
|
|
58
|
+
* route through the destination "pivot points" while the leaf endpoints catch up —
|
|
59
|
+
* visually the bundle "snaps into shape" as the leaves arrive.
|
|
60
|
+
*/
|
|
61
|
+
export class GraphBundleProjector<
|
|
62
|
+
NodeData = any,
|
|
63
|
+
Options extends GraphBundleProjectorOptions = any,
|
|
64
|
+
> extends GraphRadialProjector<NodeData, Options> {
|
|
65
|
+
// Per-edge polar point arrays. Points 0 and N-1 are the source / target leaves and are
|
|
66
|
+
// overwritten from current cartesian on every tick; intermediate points stay fixed at
|
|
67
|
+
// their cluster-layout polar coordinates.
|
|
68
|
+
#edgePoints = new Map<string, PolarPoint[]>();
|
|
69
|
+
#lineGen = lineRadial<PolarPoint>()
|
|
70
|
+
.radius((d) => d.radius)
|
|
71
|
+
.angle((d) => d.angle);
|
|
72
|
+
|
|
73
|
+
protected override onUpdate(graph?: Graph.Any) {
|
|
74
|
+
log('onUpdate', { graph: { nodes: graph?.nodes.length, edges: graph?.edges.length } });
|
|
75
|
+
this.mergeData(graph);
|
|
76
|
+
this.doBundleLayout();
|
|
77
|
+
this.emitUpdate('topology');
|
|
78
|
+
this.animate();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Refresh leaf endpoint polar values from current cartesian so the curve tracks
|
|
83
|
+
* its endpoints during the tween. Internal hierarchy positions stay constant.
|
|
84
|
+
*/
|
|
85
|
+
protected override onTickFrame(_t: number): void {
|
|
86
|
+
for (const edge of this.layout.graph.edges) {
|
|
87
|
+
if (edge.type !== 'bundle') {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
const points = this.#edgePoints.get(edge.id);
|
|
91
|
+
if (!points || points.length < 2) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
const s = cartesianToPolar(edge.source.x ?? 0, edge.source.y ?? 0);
|
|
95
|
+
const t = cartesianToPolar(edge.target.x ?? 0, edge.target.y ?? 0);
|
|
96
|
+
points[0] = { angle: s.angle, radius: s.radius };
|
|
97
|
+
points[points.length - 1] = { angle: t.angle, radius: t.radius };
|
|
98
|
+
edge.path = this.#lineGen(points) ?? undefined;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private doBundleLayout() {
|
|
103
|
+
if (!this.context.size) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const dataNodes = [...this.layout.graph.nodes];
|
|
107
|
+
if (!dataNodes.length) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const groupOf = this.options.groupOf;
|
|
112
|
+
|
|
113
|
+
// Synthesize root → groups → leaves hierarchy so d3.cluster gives every node
|
|
114
|
+
// (including the routing-only internal ones) a polar coordinate.
|
|
115
|
+
type HierNode = { id: string; parent?: string; node?: GraphLayoutNode<NodeData> };
|
|
116
|
+
const items: HierNode[] = [{ id: ROOT_ID }];
|
|
117
|
+
const groupIds = new Map<string, string>();
|
|
118
|
+
for (const node of dataNodes) {
|
|
119
|
+
const key = groupOf?.(node);
|
|
120
|
+
let parent = ROOT_ID;
|
|
121
|
+
if (key !== undefined) {
|
|
122
|
+
let groupId = groupIds.get(key);
|
|
123
|
+
if (!groupId) {
|
|
124
|
+
groupId = `${GROUP_PREFIX}${key}`;
|
|
125
|
+
groupIds.set(key, groupId);
|
|
126
|
+
items.push({ id: groupId, parent: ROOT_ID });
|
|
127
|
+
}
|
|
128
|
+
parent = groupId;
|
|
129
|
+
}
|
|
130
|
+
items.push({ id: node.id, parent, node });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const childrenByParent = new Map<string, HierNode[]>();
|
|
134
|
+
for (const item of items) {
|
|
135
|
+
if (item.parent) {
|
|
136
|
+
const list = childrenByParent.get(item.parent) ?? [];
|
|
137
|
+
list.push(item);
|
|
138
|
+
childrenByParent.set(item.parent, list);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const root = hierarchy<HierNode>({ id: ROOT_ID }, (d) => childrenByParent.get(d.id) ?? []);
|
|
143
|
+
|
|
144
|
+
const { width, height } = this.context.size;
|
|
145
|
+
const margin = this.options.margin ?? 120;
|
|
146
|
+
const ringRadius = Math.max(0, Math.min(width, height) / 2 - margin);
|
|
147
|
+
|
|
148
|
+
d3Cluster<HierNode>()
|
|
149
|
+
.size([2 * Math.PI, ringRadius])
|
|
150
|
+
.separation((a: any, b: any) => (a.parent === b.parent ? 1 : 2) / Math.max(1, a.depth))(root);
|
|
151
|
+
|
|
152
|
+
const leafR = this.options.leafRadius ?? 4;
|
|
153
|
+
|
|
154
|
+
// Re-bake the line generator with the configured tension. d3 curve factories return
|
|
155
|
+
// a new curve when `.beta` is called, so this must happen whenever options change.
|
|
156
|
+
const tension = this.options.tension ?? 0.85;
|
|
157
|
+
this.#lineGen = lineRadial<PolarPoint>()
|
|
158
|
+
.curve(curveBundle.beta(tension))
|
|
159
|
+
.radius((d) => d.radius)
|
|
160
|
+
.angle((d) => d.angle);
|
|
161
|
+
|
|
162
|
+
// Index hierarchy nodes by data id for fast lookup when building edge paths.
|
|
163
|
+
const hierById = new Map<string, HierarchyNode<HierNode>>();
|
|
164
|
+
root.each((d) => {
|
|
165
|
+
hierById.set(d.data.id, d);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
// Position the leaf data nodes (cartesian); internal nodes stay layout-only.
|
|
169
|
+
root.leaves().forEach((d: any) => {
|
|
170
|
+
const angle = d.x;
|
|
171
|
+
const radius = d.y;
|
|
172
|
+
const x = Math.cos(angle - Math.PI / 2) * radius;
|
|
173
|
+
const y = Math.sin(angle - Math.PI / 2) * radius;
|
|
174
|
+
if (d.data.node) {
|
|
175
|
+
updateNode(d.data.node, [x, y], leafR);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
this.layout.graph.nodes = dataNodes;
|
|
179
|
+
|
|
180
|
+
// Build bundle edges. `mergeData` already populated `layout.graph.edges` with data
|
|
181
|
+
// edges referencing the leaf GraphLayoutNodes — keep those references (so node
|
|
182
|
+
// animations & hover wiring continue to work) and enhance each with a routed path
|
|
183
|
+
// through the hierarchy.
|
|
184
|
+
this.#edgePoints.clear();
|
|
185
|
+
const incomingEdges = [...this.layout.graph.edges];
|
|
186
|
+
const bundleEdges: GraphLayoutEdge<NodeData>[] = [];
|
|
187
|
+
for (const edge of incomingEdges) {
|
|
188
|
+
const sourceHier = hierById.get(edge.source.id);
|
|
189
|
+
const targetHier = hierById.get(edge.target.id);
|
|
190
|
+
if (!sourceHier || !targetHier || sourceHier === targetHier) {
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
// hierarchy.path returns [source, ...up to LCA, ...down to target] (inclusive endpoints).
|
|
194
|
+
const pathNodes = sourceHier.path(targetHier);
|
|
195
|
+
const points: PolarPoint[] = pathNodes.map((n: any) => ({ angle: n.x, radius: n.y }));
|
|
196
|
+
this.#edgePoints.set(edge.id, points);
|
|
197
|
+
bundleEdges.push({
|
|
198
|
+
id: edge.id,
|
|
199
|
+
type: 'bundle',
|
|
200
|
+
source: edge.source,
|
|
201
|
+
target: edge.target,
|
|
202
|
+
data: edge.data,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
this.layout.graph.edges = bundleEdges;
|
|
206
|
+
|
|
207
|
+
// Seed initial paths so the topology emit has correct geometry to enter into the DOM.
|
|
208
|
+
this.onTickFrame(0);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { cluster as d3Cluster, hierarchy, linkRadial } from 'd3';
|
|
6
|
+
|
|
7
|
+
import { type Graph } from '@dxos/graph';
|
|
8
|
+
import { log } from '@dxos/log';
|
|
9
|
+
|
|
10
|
+
import { type GraphLayoutEdge, type GraphLayoutNode } from '../types';
|
|
11
|
+
import { GraphRadialProjector, type GraphRadialProjectorOptions, updateNode } from './graph-radial-projector';
|
|
12
|
+
|
|
13
|
+
export type GraphClusterProjectorOptions = GraphRadialProjectorOptions & {
|
|
14
|
+
/** Reserved space around the cluster (screen pixels). */
|
|
15
|
+
margin?: number;
|
|
16
|
+
/** Radius for leaf nodes. Default 4. */
|
|
17
|
+
leafRadius?: number;
|
|
18
|
+
/** Radius for the synthetic group nodes (visible as smaller intermediate dots). Default 3. */
|
|
19
|
+
groupRadius?: number;
|
|
20
|
+
/** Radius for the synthetic root node. Default 5. */
|
|
21
|
+
rootRadius?: number;
|
|
22
|
+
/**
|
|
23
|
+
* Group key for each node — synthetic intermediate nodes are created per unique key,
|
|
24
|
+
* giving a root → group → leaf hierarchy. Return `undefined` to attach the node
|
|
25
|
+
* directly to the root.
|
|
26
|
+
*/
|
|
27
|
+
groupOf?: (node: GraphLayoutNode) => string | undefined;
|
|
28
|
+
/** Root id used for the synthetic root node. */
|
|
29
|
+
rootId?: string;
|
|
30
|
+
/** Display label set on the synthetic root node. Renderers read `node.label`. */
|
|
31
|
+
rootLabel?: string;
|
|
32
|
+
/** Optional formatter applied to the `groupOf` key to derive the group's display label.
|
|
33
|
+
* Defaults to the key itself. */
|
|
34
|
+
groupLabel?: (key: string) => string;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const ROOT_ID = '__cluster_root__';
|
|
38
|
+
const GROUP_PREFIX = '__group__:';
|
|
39
|
+
const HIER_EDGE_PREFIX = '__hier__:';
|
|
40
|
+
|
|
41
|
+
/** Node type tags so consumer renderers can distinguish leaves from synthetic structural nodes. */
|
|
42
|
+
export const CLUSTER_NODE_TYPE_LEAF = 'leaf';
|
|
43
|
+
export const CLUSTER_NODE_TYPE_GROUP = 'group';
|
|
44
|
+
export const CLUSTER_NODE_TYPE_ROOT = 'root';
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Standard d3 radial link generator — `curveBumpRadial`. Same shape as the original
|
|
48
|
+
* `RadialTree` component (and the d3 radial-cluster observable notebook).
|
|
49
|
+
*
|
|
50
|
+
* IMPORTANT: relies on the `dx-edge path` CSS setting `fill: none`. Without that, the SVG
|
|
51
|
+
* default fill (black) closes the curve across the source-target chord and visually
|
|
52
|
+
* swallows part of the stroke. The Graph engine's `createEdge` doesn't set `fill='none'`
|
|
53
|
+
* inline (unlike the original RadialTree), so the rule lives in graph.css.
|
|
54
|
+
*/
|
|
55
|
+
const linkPath = linkRadial<any, any>()
|
|
56
|
+
.angle((d: any) => d.x)
|
|
57
|
+
.radius((d: any) => d.y);
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Cartesian → d3-radial polar. d3 measures angle clockwise from "up" (the -y direction).
|
|
61
|
+
* Inverse of `[cos(a - π/2) * r, sin(a - π/2) * r]`.
|
|
62
|
+
*/
|
|
63
|
+
const cartesianToPolar = (x: number, y: number): { angle: number; radius: number } => ({
|
|
64
|
+
angle: Math.atan2(x, -y),
|
|
65
|
+
radius: Math.hypot(x, y),
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Radial cluster (d3.cluster) layout. Materializes a root → group → leaf
|
|
70
|
+
* hierarchy from the `groupOf` callback, runs d3.cluster, and:
|
|
71
|
+
*
|
|
72
|
+
* - Places leaves around the outer ring; synthetic group nodes sit on an
|
|
73
|
+
* inner ring; the root sits at the origin. All three kinds are added to the
|
|
74
|
+
* layout so they render via the consumer's `renderNode` (the `type` field
|
|
75
|
+
* identifies which is which).
|
|
76
|
+
* - Each hierarchy edge carries a `path` string (d3's `linkRadial` curve)
|
|
77
|
+
* computed from CURRENT cartesian endpoint positions every frame via
|
|
78
|
+
* `onTickFrame`. The curve therefore tracks moving endpoints during a
|
|
79
|
+
* cross-variant tween (e.g. force → cluster) instead of snapping to the
|
|
80
|
+
* final polar shape.
|
|
81
|
+
*/
|
|
82
|
+
export class GraphClusterProjector<
|
|
83
|
+
NodeData = any,
|
|
84
|
+
Options extends GraphClusterProjectorOptions = any,
|
|
85
|
+
> extends GraphRadialProjector<NodeData, Options> {
|
|
86
|
+
// Synthetic-node ids (root id, or `${GROUP_PREFIX}${key}`) whose subtree is currently
|
|
87
|
+
// hidden. Clicking the corresponding rendered circle calls toggleCollapsed to flip
|
|
88
|
+
// membership, then re-runs the layout so the renderer's exit transition fades the
|
|
89
|
+
// hidden leaves + hierarchy edges out (and back in on expand).
|
|
90
|
+
readonly #collapsed = new Set<string>();
|
|
91
|
+
|
|
92
|
+
// Snapshot of the full data-node set as of the last mergeData. doClusterLayout always
|
|
93
|
+
// reads from this rather than `layout.graph.nodes` — the layout view is filtered (it
|
|
94
|
+
// drops hidden leaves so the renderer doesn't see them), so re-reading from there
|
|
95
|
+
// would permanently lose the hidden leaves and they couldn't reappear on expand.
|
|
96
|
+
#dataNodes: GraphLayoutNode<NodeData>[] = [];
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Built-in click semantics: clicking a synthetic root / group node toggles its subtree.
|
|
100
|
+
* Leaf clicks fall through to the consumer's `onSelect` (returns `false`).
|
|
101
|
+
*/
|
|
102
|
+
override handleNodeClick(node: GraphLayoutNode<NodeData>, _event: MouseEvent): boolean {
|
|
103
|
+
if (node.type !== CLUSTER_NODE_TYPE_ROOT && node.type !== CLUSTER_NODE_TYPE_GROUP) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
this.toggleCollapsed(node.id);
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Toggle collapse state for a synthetic node id. Triggers a topology re-emit. */
|
|
111
|
+
toggleCollapsed(id: string): void {
|
|
112
|
+
if (this.#collapsed.has(id)) {
|
|
113
|
+
this.#collapsed.delete(id);
|
|
114
|
+
} else {
|
|
115
|
+
this.#collapsed.add(id);
|
|
116
|
+
}
|
|
117
|
+
// Re-run the layout against the current model data so hidden / re-shown nodes are
|
|
118
|
+
// exited / entered through the renderer's join.
|
|
119
|
+
this.doClusterLayout();
|
|
120
|
+
this.emitUpdate('topology');
|
|
121
|
+
this.animate();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Whether a synthetic-node id is currently collapsed. */
|
|
125
|
+
isCollapsed(id: string): boolean {
|
|
126
|
+
return this.#collapsed.has(id);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
protected override onUpdate(graph?: Graph.Any) {
|
|
130
|
+
log('onUpdate', { graph: { nodes: graph?.nodes.length, edges: graph?.edges.length } });
|
|
131
|
+
this.mergeData(graph);
|
|
132
|
+
// Snapshot the full data-node set right after mergeData — before doClusterLayout
|
|
133
|
+
// overwrites `layout.graph.nodes` with the filtered (collapse-aware) view.
|
|
134
|
+
this.#dataNodes = [...this.layout.graph.nodes];
|
|
135
|
+
this.doClusterLayout();
|
|
136
|
+
this.emitUpdate('topology');
|
|
137
|
+
this.animate();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Recompute each hierarchy edge's `path` from current cartesian source/target positions,
|
|
142
|
+
* so the curves track moving endpoints during a cross-variant tween (e.g. force → cluster)
|
|
143
|
+
* instead of being pinned to the final polar layout.
|
|
144
|
+
*/
|
|
145
|
+
protected override onTickFrame(_t: number): void {
|
|
146
|
+
for (const edge of this.layout.graph.edges) {
|
|
147
|
+
if (edge.type !== 'hierarchy') {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
const { source, target } = edge;
|
|
151
|
+
const s = cartesianToPolar(source.x ?? 0, source.y ?? 0);
|
|
152
|
+
const t = cartesianToPolar(target.x ?? 0, target.y ?? 0);
|
|
153
|
+
edge.path = linkPath({ source: { x: s.angle, y: s.radius }, target: { x: t.angle, y: t.radius } });
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private doClusterLayout() {
|
|
158
|
+
if (!this.context.size) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Read from the post-mergeData snapshot, not from `layout.graph.nodes`. The layout
|
|
163
|
+
// view is a FILTERED projection (hidden leaves are dropped so the renderer's d3 join
|
|
164
|
+
// fades them out); reading from it would shrink monotonically across collapses and
|
|
165
|
+
// hidden leaves could never reappear on expand. The snapshot also avoids the
|
|
166
|
+
// "synthetic root re-pushed as its own child → d3.hierarchy infinite recursion" hang
|
|
167
|
+
// we'd otherwise hit, because synthetic stubs were never in this set to begin with.
|
|
168
|
+
const dataNodes = this.#dataNodes;
|
|
169
|
+
if (!dataNodes.length) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const groupOf = this.options.groupOf;
|
|
174
|
+
const rootId = this.options.rootId ?? ROOT_ID;
|
|
175
|
+
|
|
176
|
+
// Synthesize the hierarchy: root → groups → leaves. `groupKey` is carried on synthetic
|
|
177
|
+
// group HierNodes so the rendered stub can pick up the original groupOf return value
|
|
178
|
+
// (the typename, in plugin-explorer's case) as its display label.
|
|
179
|
+
type HierNode = { id: string; parent?: string; node?: GraphLayoutNode<NodeData>; groupKey?: string };
|
|
180
|
+
const items: HierNode[] = [{ id: rootId }];
|
|
181
|
+
const groupIds = new Map<string, string>();
|
|
182
|
+
// Collapsed root → render only the root circle (no groups, no leaves).
|
|
183
|
+
const rootCollapsed = this.#collapsed.has(rootId);
|
|
184
|
+
if (!rootCollapsed) {
|
|
185
|
+
for (const node of dataNodes) {
|
|
186
|
+
const key = groupOf?.(node);
|
|
187
|
+
let parent = rootId;
|
|
188
|
+
if (key !== undefined) {
|
|
189
|
+
let groupId = groupIds.get(key);
|
|
190
|
+
if (!groupId) {
|
|
191
|
+
groupId = `${GROUP_PREFIX}${key}`;
|
|
192
|
+
groupIds.set(key, groupId);
|
|
193
|
+
items.push({ id: groupId, parent: rootId, groupKey: key });
|
|
194
|
+
}
|
|
195
|
+
parent = groupId;
|
|
196
|
+
}
|
|
197
|
+
// Collapsed group → drop its leaves. The group itself still renders so the user
|
|
198
|
+
// can click it again to expand.
|
|
199
|
+
if (this.#collapsed.has(parent)) {
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
items.push({ id: node.id, parent, node });
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const childrenByParent = new Map<string, HierNode[]>();
|
|
207
|
+
for (const item of items) {
|
|
208
|
+
if (item.parent) {
|
|
209
|
+
const list = childrenByParent.get(item.parent) ?? [];
|
|
210
|
+
list.push(item);
|
|
211
|
+
childrenByParent.set(item.parent, list);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const root = hierarchy<HierNode>({ id: rootId }, (d) => childrenByParent.get(d.id) ?? []);
|
|
216
|
+
|
|
217
|
+
const { width, height } = this.context.size;
|
|
218
|
+
const margin = this.options.margin ?? 80;
|
|
219
|
+
const ringRadius = Math.max(0, Math.min(width, height) / 2 - margin);
|
|
220
|
+
|
|
221
|
+
d3Cluster<HierNode>()
|
|
222
|
+
.size([2 * Math.PI, ringRadius])
|
|
223
|
+
// Tighter sibling spacing at deeper levels — matches the previous standalone
|
|
224
|
+
// RadialTree component. Without the `/depth` divisor, large groups span a
|
|
225
|
+
// wide angular arc; the linkRadial(curveBumpPolar) curve to a leaf at the
|
|
226
|
+
// edge of that arc then has its control points placed far apart in
|
|
227
|
+
// cartesian, producing a wide swoop that visually misses the target.
|
|
228
|
+
.separation((a: any, b: any) => (a.parent === b.parent ? 1 : 2) / Math.max(1, a.depth))(root);
|
|
229
|
+
|
|
230
|
+
const leafR = this.options.leafRadius ?? 4;
|
|
231
|
+
const groupR = this.options.groupRadius ?? 3;
|
|
232
|
+
const rootR = this.options.rootRadius ?? 5;
|
|
233
|
+
|
|
234
|
+
// Index any synthetic root / group stubs from the previous layout pass so we can
|
|
235
|
+
// REUSE them (preserving their current x/y as the tween start) instead of creating
|
|
236
|
+
// fresh `{x:0,y:0}` stubs that would snap the DOM element to the origin first.
|
|
237
|
+
const prevStubsById = new Map<string, GraphLayoutNode<NodeData>>();
|
|
238
|
+
for (const node of this.layout.graph.nodes) {
|
|
239
|
+
if (node.type === CLUSTER_NODE_TYPE_ROOT || node.type === CLUSTER_NODE_TYPE_GROUP) {
|
|
240
|
+
prevStubsById.set(node.id, node);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Build the new node set: existing data nodes (re-positioned), plus synthetic root + groups.
|
|
245
|
+
const syntheticNodes = new Map<string, GraphLayoutNode<NodeData>>();
|
|
246
|
+
root.each((d: any) => {
|
|
247
|
+
const id = d.data.id;
|
|
248
|
+
const angle = d.x;
|
|
249
|
+
const radius = d.y;
|
|
250
|
+
const x = Math.cos(angle - Math.PI / 2) * radius;
|
|
251
|
+
const y = Math.sin(angle - Math.PI / 2) * radius;
|
|
252
|
+
|
|
253
|
+
if (d.data.node) {
|
|
254
|
+
// Real leaf — tween its position and mark visible.
|
|
255
|
+
updateNode(d.data.node, [x, y], leafR);
|
|
256
|
+
(d.data.node as any).type = CLUSTER_NODE_TYPE_LEAF;
|
|
257
|
+
(d.data.node as any).hidden = false;
|
|
258
|
+
} else {
|
|
259
|
+
// Synthetic root or group. On the very first cluster mount the stub is brand-new
|
|
260
|
+
// so seed it at origin — root stays put while groups grow outward. On subsequent
|
|
261
|
+
// mounts (toggleCollapsed) we reuse the previous-render stub so its current x/y
|
|
262
|
+
// becomes the tween start; otherwise the DOM element would jump to origin before
|
|
263
|
+
// animating to the new target position.
|
|
264
|
+
const isRoot = id === rootId;
|
|
265
|
+
const stub = prevStubsById.get(id) ?? ({ id, x: 0, y: 0 } as GraphLayoutNode<NodeData>);
|
|
266
|
+
updateNode(stub, [x, y], isRoot ? rootR : groupR);
|
|
267
|
+
stub.type = isRoot ? CLUSTER_NODE_TYPE_ROOT : CLUSTER_NODE_TYPE_GROUP;
|
|
268
|
+
// Surface a display label so renderNode callbacks don't have to parse internal ids.
|
|
269
|
+
if (isRoot) {
|
|
270
|
+
stub.label = this.options.rootLabel;
|
|
271
|
+
} else if (d.data.groupKey !== undefined) {
|
|
272
|
+
stub.label = this.options.groupLabel ? this.options.groupLabel(d.data.groupKey) : d.data.groupKey;
|
|
273
|
+
}
|
|
274
|
+
syntheticNodes.set(id, stub);
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
// Collapse animation: hidden leaves stay in the layout but their target position is
|
|
279
|
+
// the parent group's target (or root for ungrouped leaves). animate() then tweens them
|
|
280
|
+
// toward the parent — visually "absorbed in", while their opacity transitions to 0 via
|
|
281
|
+
// the renderer's hidden-flag transition. On re-expand they tween back out to their ring.
|
|
282
|
+
const visibleLeafIds = new Set<string>();
|
|
283
|
+
for (const item of items) {
|
|
284
|
+
if (item.node) {
|
|
285
|
+
visibleLeafIds.add(item.id);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
for (const node of dataNodes) {
|
|
289
|
+
if (visibleLeafIds.has(node.id)) {
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
// Hidden leaf — find its parent group's target. groupOf may have changed since the
|
|
293
|
+
// last layout pass (rare in practice but cheap to handle), so re-compute the key.
|
|
294
|
+
const key = groupOf?.(node);
|
|
295
|
+
const parentId = key !== undefined && groupIds.has(key) ? (groupIds.get(key) as string) : rootId;
|
|
296
|
+
const parentStub = syntheticNodes.get(parentId);
|
|
297
|
+
const targetX = (parentStub as any)?.tx ?? 0;
|
|
298
|
+
const targetY = (parentStub as any)?.ty ?? 0;
|
|
299
|
+
updateNode(node, [targetX, targetY], leafR);
|
|
300
|
+
(node as any).type = CLUSTER_NODE_TYPE_LEAF;
|
|
301
|
+
node.hidden = true;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// Compose the rendered node list: synthetic structure first (so leaves draw on top).
|
|
305
|
+
const nextNodes: GraphLayoutNode<NodeData>[] = [];
|
|
306
|
+
for (const stub of syntheticNodes.values()) {
|
|
307
|
+
nextNodes.push(stub);
|
|
308
|
+
}
|
|
309
|
+
for (const node of dataNodes) {
|
|
310
|
+
nextNodes.push(node);
|
|
311
|
+
}
|
|
312
|
+
this.layout.graph.nodes = nextNodes;
|
|
313
|
+
|
|
314
|
+
// Build hierarchy edges referencing source/target nodes. The `path` is filled by
|
|
315
|
+
// onTickFrame (and again per tween frame) from current cartesian positions, so we
|
|
316
|
+
// don't precompute it here — leaving it undefined would briefly produce a straight
|
|
317
|
+
// line on first paint, but the tween's first tick (t=0) writes the correct path
|
|
318
|
+
// before the renderer's positions-emit reaches the DOM.
|
|
319
|
+
const hierarchyEdges: GraphLayoutEdge<NodeData>[] = [];
|
|
320
|
+
root.links().forEach((link: any) => {
|
|
321
|
+
const sid = link.source.data.id;
|
|
322
|
+
const tid = link.target.data.id;
|
|
323
|
+
const sourceNode = link.source.data.node ?? syntheticNodes.get(sid);
|
|
324
|
+
const targetNode = link.target.data.node ?? syntheticNodes.get(tid);
|
|
325
|
+
hierarchyEdges.push({
|
|
326
|
+
id: `${HIER_EDGE_PREFIX}${sid}->${tid}`,
|
|
327
|
+
type: 'hierarchy',
|
|
328
|
+
source: sourceNode!,
|
|
329
|
+
target: targetNode!,
|
|
330
|
+
data: undefined,
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
this.layout.graph.edges = hierarchyEdges;
|
|
334
|
+
// Seed initial paths so the topology emit has correct geometry to enter into the DOM.
|
|
335
|
+
this.onTickFrame(0);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
@@ -153,6 +153,7 @@ export type GraphForceProjectorOptions = GraphProjectorOptions & {
|
|
|
153
153
|
/**
|
|
154
154
|
* D3 force directed graph layout using d3 simulation..
|
|
155
155
|
*/
|
|
156
|
+
// TODO(burdon): Rename ForceGraphProjector.
|
|
156
157
|
export class GraphForceProjector<NodeData = any> extends GraphProjector<NodeData, GraphForceProjectorOptions> {
|
|
157
158
|
// https://github.com/d3/d3-force
|
|
158
159
|
private _simulation = forceSimulation<GraphLayoutNode, GraphLayoutEdge>();
|
|
@@ -197,7 +198,8 @@ export class GraphForceProjector<NodeData = any> extends GraphProjector<NodeData
|
|
|
197
198
|
}
|
|
198
199
|
|
|
199
200
|
this._simulation
|
|
200
|
-
|
|
201
|
+
// Each tick only mutates `x/y` — emit 'positions' so the renderer can fast-path.
|
|
202
|
+
.on('tick', () => propagating && this.emitUpdate('positions'))
|
|
201
203
|
.velocityDecay(0.3)
|
|
202
204
|
.alphaDecay(1 - Math.pow(0.001, 1 / 300))
|
|
203
205
|
.alpha(1)
|
|
@@ -228,6 +230,9 @@ export class GraphForceProjector<NodeData = any> extends GraphProjector<NodeData
|
|
|
228
230
|
this.mergeData(graph);
|
|
229
231
|
this.updateLayout();
|
|
230
232
|
this.updateForces(this.forces);
|
|
233
|
+
// Signal topology change so the renderer rebinds enter/exit and runs attribute callbacks once.
|
|
234
|
+
// Subsequent ticks emit 'positions' and skip the expensive path.
|
|
235
|
+
this.emitUpdate('topology');
|
|
231
236
|
this.restart();
|
|
232
237
|
}
|
|
233
238
|
|
|
@@ -26,6 +26,7 @@ export class GraphHierarchicalProjector<
|
|
|
26
26
|
});
|
|
27
27
|
|
|
28
28
|
this.mergeData(graph);
|
|
29
|
+
this.emitUpdate('topology');
|
|
29
30
|
const selected = this.selection?.getSelectedIds()[0];
|
|
30
31
|
if (selected) {
|
|
31
32
|
const node = this.layout.graph.nodes.find((node) => node.id === selected);
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Graph } from '@dxos/graph';
|
|
6
|
+
import { log } from '@dxos/log';
|
|
7
|
+
|
|
8
|
+
import { type GraphLayoutNode } from '../types';
|
|
9
|
+
import { GraphRadialProjector, type GraphRadialProjectorOptions, updateNode } from './graph-radial-projector';
|
|
10
|
+
|
|
11
|
+
export type GraphLatticeProjectorOptions = GraphRadialProjectorOptions & {
|
|
12
|
+
/** Reserved space around the lattice (screen pixels). */
|
|
13
|
+
margin?: number;
|
|
14
|
+
/** Node radius used by `renderNode` consumers. Defaults to the force-graph default (6) so animations across variants don't resize nodes. */
|
|
15
|
+
radius?: number;
|
|
16
|
+
/** Sort key for cells; defaults to the node label / id. Same-typename nodes naturally cluster. */
|
|
17
|
+
sortBy?: (node: GraphLayoutNode) => string;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Lays out nodes as a square-as-possible grid that fits the container.
|
|
22
|
+
* Extends `GraphRadialProjector` to inherit the tween (`updateNode` + `animate`),
|
|
23
|
+
* so switching to / from this layout animates each node from its current
|
|
24
|
+
* position to the grid cell — and the initial mount can also "grow" from
|
|
25
|
+
* existing positions if seeded with a prior projector's layout.
|
|
26
|
+
*/
|
|
27
|
+
export class GraphLatticeProjector<
|
|
28
|
+
NodeData = any,
|
|
29
|
+
Options extends GraphLatticeProjectorOptions = any,
|
|
30
|
+
> extends GraphRadialProjector<NodeData, Options> {
|
|
31
|
+
protected override onUpdate(graph?: Graph.Any) {
|
|
32
|
+
log('onUpdate', { graph: { nodes: graph?.nodes.length, edges: graph?.edges.length } });
|
|
33
|
+
this.mergeData(graph);
|
|
34
|
+
// Compute layout (assigns sx/sy/tx/ty + initial x/y) BEFORE emitting topology so the
|
|
35
|
+
// renderer's first render uses meaningful positions. animate() then tweens to target.
|
|
36
|
+
this.doLatticeLayout();
|
|
37
|
+
this.emitUpdate('topology');
|
|
38
|
+
this.animate();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
private doLatticeLayout() {
|
|
42
|
+
if (!this.context.size) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const nodes = this.layout.graph.nodes;
|
|
47
|
+
const count = nodes.length;
|
|
48
|
+
if (!count) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const sortBy = this.options.sortBy ?? ((n: GraphLayoutNode) => (n.data as any)?.label ?? n.id);
|
|
53
|
+
nodes.sort((a, b) => sortBy(a).localeCompare(sortBy(b)));
|
|
54
|
+
|
|
55
|
+
// Columns ≈ √N so the grid stays as square as possible.
|
|
56
|
+
const columns = Math.max(1, Math.ceil(Math.sqrt(count)));
|
|
57
|
+
const rows = Math.ceil(count / columns);
|
|
58
|
+
|
|
59
|
+
const margin = this.options.margin ?? 16;
|
|
60
|
+
const { width, height } = this.context.size;
|
|
61
|
+
const innerW = Math.max(0, width - 2 * margin);
|
|
62
|
+
const innerH = Math.max(0, height - 2 * margin);
|
|
63
|
+
// Default radius matches the force-graph default (6) so cross-variant animations
|
|
64
|
+
// tween position only, not size. Consumers can pass a smaller `radius` for a
|
|
65
|
+
// denser matrix look.
|
|
66
|
+
const r = this.options.radius ?? 6;
|
|
67
|
+
// Cell size sets the spacing between nodes. Cap at a small multiple of `r`
|
|
68
|
+
// so the lattice stays tight rather than ballooning to fill the container;
|
|
69
|
+
// wide enough that edges crossing between non-adjacent cells remain visible
|
|
70
|
+
// in the gaps between nodes.
|
|
71
|
+
const fit = Math.min(innerW / columns, innerH / rows);
|
|
72
|
+
const cellSize = Math.max(0, Math.min(fit, r * 6));
|
|
73
|
+
|
|
74
|
+
// Center the lattice (SVG viewBox is centered at origin in `<SVG.Root>`).
|
|
75
|
+
const gridW = cellSize * columns;
|
|
76
|
+
const gridH = cellSize * rows;
|
|
77
|
+
const offsetX = -gridW / 2 + cellSize / 2;
|
|
78
|
+
const offsetY = -gridH / 2 + cellSize / 2;
|
|
79
|
+
|
|
80
|
+
nodes.forEach((node, i) => {
|
|
81
|
+
const tx = offsetX + (i % columns) * cellSize;
|
|
82
|
+
const ty = offsetY + Math.floor(i / columns) * cellSize;
|
|
83
|
+
updateNode(node, [tx, ty], r);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|