@hpcc-js/wasm-graphviz 1.22.1 → 1.24.0
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/README.md +176 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +3 -3
- package/package.json +3 -3
- package/src/graphviz.ts +696 -1
- package/src/index.ts +1 -1
- package/src/types.ts +607 -0
- package/types/graphviz.d.ts +443 -0
- package/types/graphvizlib.d.ts +63 -0
- package/types/types.d.ts +299 -0
package/src/graphviz.ts
CHANGED
|
@@ -1,12 +1,47 @@
|
|
|
1
1
|
// @ts-expect-error importing from a wasm file is resolved via a custom esbuild plugin
|
|
2
2
|
import load, { reset } from "../../../build/packages/graphviz/graphvizlib.wasm";
|
|
3
|
-
import type { MainModule } from "../types/graphvizlib.js";
|
|
3
|
+
import type { MainModule, CGraph as CGraphWasm, CSubgraph as CSubgraphWasm } from "../types/graphvizlib.js";
|
|
4
|
+
import type {
|
|
5
|
+
NodeDotAttr, NodeAttrs,
|
|
6
|
+
EdgeDotAttr, EdgeAttrs,
|
|
7
|
+
ClusterDotAttr, ClusterAttrs,
|
|
8
|
+
GraphDotAttr, GraphAttrs as GraphAttrs
|
|
9
|
+
} from "./types.ts";
|
|
4
10
|
|
|
5
11
|
/**
|
|
6
12
|
* Various graphic and data formats for end user, web, documents and other applications. See [Output Formats](https://graphviz.gitlab.io/docs/outputs/) for more information.
|
|
7
13
|
*/
|
|
8
14
|
export type Format = "svg" | "svg_inline" | "dot" | "json" | "dot_json" | "xdot_json" | "plain" | "plain-ext" | "canon";
|
|
9
15
|
|
|
16
|
+
/**
|
|
17
|
+
* An edge returned by graph-traversal methods.
|
|
18
|
+
*/
|
|
19
|
+
export interface EdgeInfo {
|
|
20
|
+
/** The tail (source) node name. */
|
|
21
|
+
tail: string;
|
|
22
|
+
/** The head (target) node name. */
|
|
23
|
+
head: string;
|
|
24
|
+
/**
|
|
25
|
+
* The cgraph edge key that distinguishes parallel edges.
|
|
26
|
+
* Typically `""` for single (non-keyed) edges.
|
|
27
|
+
*/
|
|
28
|
+
key: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** @internal Parses a JSON string array returned by C++ traversal methods. */
|
|
32
|
+
function parseNames(json: string): string[] {
|
|
33
|
+
return JSON.parse(json) as string[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** @internal Parses a flat JSON triple array `[tail,head,key,...]` into EdgeInfo[]. */
|
|
37
|
+
function parseEdges(json: string): EdgeInfo[] {
|
|
38
|
+
const flat = JSON.parse(json) as string[];
|
|
39
|
+
const result: EdgeInfo[] = [];
|
|
40
|
+
for (let i = 0; i + 2 < flat.length; i += 3)
|
|
41
|
+
result.push({ tail: flat[i]!, head: flat[i + 1]!, key: flat[i + 2]! });
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
|
|
10
45
|
/**
|
|
11
46
|
* Various algorithms for projecting abstract graphs into a space for visualization. See [Layout Engines](https://graphviz.gitlab.io/docs/layouts/) for more details.
|
|
12
47
|
*/
|
|
@@ -73,6 +108,616 @@ function createFiles(graphviz: any, _options?: Options) {
|
|
|
73
108
|
|
|
74
109
|
let g_graphviz: Promise<Graphviz> | undefined;
|
|
75
110
|
|
|
111
|
+
/**
|
|
112
|
+
* The type of graph to create.
|
|
113
|
+
* - `"directed"` – directed graph (`digraph`)
|
|
114
|
+
* - `"undirected"` – undirected graph (`graph`)
|
|
115
|
+
* - `"strict directed"` – directed graph that disallows parallel edges and self-loops
|
|
116
|
+
* - `"strict undirected"` – undirected graph that disallows parallel edges and self-loops
|
|
117
|
+
*/
|
|
118
|
+
export type GraphType = "directed" | "undirected" | "strict directed" | "strict undirected";
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* A subgraph (or cluster) inside a {@link Graph}.
|
|
122
|
+
*
|
|
123
|
+
* Obtain via {@link Graph.addSubgraph}. All mutation methods return `this`
|
|
124
|
+
* for chaining. **Call {@link Subgraph.delete} (or use the `using` keyword)
|
|
125
|
+
* when finished** to free the underlying WASM wrapper — the actual subgraph
|
|
126
|
+
* data is owned by the parent {@link Graph} and is freed with it.
|
|
127
|
+
*
|
|
128
|
+
* Clusters are subgraphs whose name starts with `"cluster"`. Layout engines
|
|
129
|
+
* draw them as a bounded rectangle:
|
|
130
|
+
*
|
|
131
|
+
* ```ts
|
|
132
|
+
* using cluster = graph.addSubgraph("cluster_0");
|
|
133
|
+
* cluster
|
|
134
|
+
* .setAttr("label", "My Cluster")
|
|
135
|
+
* .setAttr("style", "filled")
|
|
136
|
+
* .setAttr("color", "lightblue")
|
|
137
|
+
* .addEdge("a", "b");
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
export class Subgraph {
|
|
141
|
+
private _sg: CSubgraphWasm;
|
|
142
|
+
|
|
143
|
+
/** @internal */
|
|
144
|
+
constructor(sg: CSubgraphWasm) {
|
|
145
|
+
this._sg = sg;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Create (or find) a node and add it to this subgraph.
|
|
150
|
+
*/
|
|
151
|
+
addNode(name: string): this {
|
|
152
|
+
this._sg.addNode(name);
|
|
153
|
+
return this;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Create an edge inside this subgraph. Both endpoints are created
|
|
158
|
+
* automatically if they do not already exist.
|
|
159
|
+
*/
|
|
160
|
+
addEdge(tail: string, head: string, key: string = ""): this {
|
|
161
|
+
this._sg.addEdge(tail, head, key);
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Remove a node from this subgraph only. The node (and any edges
|
|
167
|
+
* connecting it) remains in the root graph and all other subgraphs.
|
|
168
|
+
* No-op if the node is not in this subgraph.
|
|
169
|
+
*/
|
|
170
|
+
removeNode(name: string): this {
|
|
171
|
+
this._sg.removeNode(name);
|
|
172
|
+
return this;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Remove a single edge from this subgraph only. The edge remains in the
|
|
177
|
+
* root graph and all other subgraphs. No-op if the edge is not present.
|
|
178
|
+
*/
|
|
179
|
+
removeEdge(tail: string, head: string, key: string = ""): this {
|
|
180
|
+
this._sg.removeEdge(tail, head, key);
|
|
181
|
+
return this;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Set an attribute on the subgraph itself (e.g. `"label"`, `"style"`,
|
|
186
|
+
* `"color"`, `"bgcolor"`).
|
|
187
|
+
*
|
|
188
|
+
* When `attr` is a known cluster attribute the value type is inferred
|
|
189
|
+
* automatically. A generic `string | number | boolean` fallback is
|
|
190
|
+
* provided for custom or less-common attributes.
|
|
191
|
+
*/
|
|
192
|
+
setAttr<K extends ClusterDotAttr>(attr: K, value: NonNullable<ClusterAttrs[K]>): this;
|
|
193
|
+
setAttr(attr: string, value: string | number | boolean): this;
|
|
194
|
+
setAttr(attr: string, value: unknown): this {
|
|
195
|
+
this._sg.setAttr(attr, String(value));
|
|
196
|
+
return this;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Clear a subgraph-level attribute by resetting it to its default (empty)
|
|
201
|
+
* value. Equivalent to `setAttr(attr, "")`.
|
|
202
|
+
*/
|
|
203
|
+
removeAttr(attr: string): this {
|
|
204
|
+
return this.setAttr(attr, "");
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Set an attribute on a node inside this subgraph.
|
|
209
|
+
*
|
|
210
|
+
* When `attr` is a known node attribute the value type is inferred
|
|
211
|
+
* automatically. A generic `string | number | boolean` fallback is
|
|
212
|
+
* provided for custom attributes.
|
|
213
|
+
*/
|
|
214
|
+
setNodeAttr<K extends NodeDotAttr>(node: string, attr: K, value: NonNullable<NodeAttrs[K]>): this;
|
|
215
|
+
setNodeAttr(node: string, attr: string, value: string | number | boolean): this;
|
|
216
|
+
setNodeAttr(node: string, attr: string, value: unknown): this {
|
|
217
|
+
this._sg.setNodeAttr(node, attr, String(value));
|
|
218
|
+
return this;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Clear a node attribute inside this subgraph by resetting it to its
|
|
223
|
+
* default (empty) value. Equivalent to `setNodeAttr(node, attr, "")`.
|
|
224
|
+
*/
|
|
225
|
+
removeNodeAttr(node: string, attr: string): this {
|
|
226
|
+
return this.setNodeAttr(node, attr, "");
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Set an attribute on an edge inside this subgraph (identified by
|
|
231
|
+
* `tail`, `head`, and `key`).
|
|
232
|
+
*
|
|
233
|
+
* When `attr` is a known edge attribute the value type is inferred
|
|
234
|
+
* automatically. A generic `string | number | boolean` fallback is
|
|
235
|
+
* provided for custom attributes.
|
|
236
|
+
*/
|
|
237
|
+
setEdgeAttr<K extends EdgeDotAttr>(tail: string, head: string, key: string, attr: K, value: NonNullable<EdgeAttrs[K]>): this;
|
|
238
|
+
setEdgeAttr(tail: string, head: string, key: string, attr: string, value: string | number | boolean): this;
|
|
239
|
+
setEdgeAttr(tail: string, head: string, key: string, attr: string, value: unknown): this {
|
|
240
|
+
this._sg.setEdgeAttr(tail, head, key, attr, String(value));
|
|
241
|
+
return this;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Clear an edge attribute inside this subgraph by resetting it to its
|
|
246
|
+
* default (empty) value. Equivalent to `setEdgeAttr(tail, head, key, attr, "")`.
|
|
247
|
+
*/
|
|
248
|
+
removeEdgeAttr(tail: string, head: string, key: string, attr: string): this {
|
|
249
|
+
return this.setEdgeAttr(tail, head, key, attr, "");
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// ---- Existence checks -----------------------------------------------
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Returns `true` if a node with the given name exists in this subgraph.
|
|
256
|
+
*/
|
|
257
|
+
hasNode(name: string): boolean {
|
|
258
|
+
return this._sg.hasNode(name);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Returns `true` if an edge from `tail` to `head` exists in this subgraph.
|
|
263
|
+
* Pass `key` to check for a specific parallel edge; omit (or pass `""`) to
|
|
264
|
+
* check for any edge between the two nodes.
|
|
265
|
+
*/
|
|
266
|
+
hasEdge(tail: string, head: string, key: string = ""): boolean {
|
|
267
|
+
return this._sg.hasEdge(tail, head, key);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// ---- Count queries --------------------------------------------------
|
|
271
|
+
|
|
272
|
+
/** Returns the number of nodes in this subgraph. */
|
|
273
|
+
nodeCount(): number { return this._sg.nodeCount(); }
|
|
274
|
+
|
|
275
|
+
/** Returns the number of edges in this subgraph. */
|
|
276
|
+
edgeCount(): number { return this._sg.edgeCount(); }
|
|
277
|
+
|
|
278
|
+
// ---- Attribute reading ----------------------------------------------
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Returns the current value of a subgraph-level attribute, or `""` if
|
|
282
|
+
* the attribute has not been set.
|
|
283
|
+
*/
|
|
284
|
+
getAttr(attr: string): string {
|
|
285
|
+
return this._sg.getAttr(attr);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Returns the current value of the named attribute on a node in this
|
|
290
|
+
* subgraph, or `""` if the node or attribute does not exist.
|
|
291
|
+
*/
|
|
292
|
+
getNodeAttr(node: string, attr: string): string {
|
|
293
|
+
return this._sg.getNodeAttr(node, attr);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Returns the current value of the named attribute on an edge in this
|
|
298
|
+
* subgraph, or `""` if the edge or attribute does not exist.
|
|
299
|
+
* Pass `key = ""` for the first (or only) edge between `tail` and `head`.
|
|
300
|
+
*/
|
|
301
|
+
getEdgeAttr(tail: string, head: string, key: string, attr: string): string {
|
|
302
|
+
return this._sg.getEdgeAttr(tail, head, key, attr);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// ---- Graph traversal ------------------------------------------------
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Returns the names of all nodes in this subgraph (in internal iteration
|
|
309
|
+
* order).
|
|
310
|
+
*/
|
|
311
|
+
nodeNames(): string[] {
|
|
312
|
+
return parseNames(this._sg.nodeNames());
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Returns all edges in this subgraph. Each unique edge is listed exactly
|
|
317
|
+
* once.
|
|
318
|
+
*/
|
|
319
|
+
edges(): EdgeInfo[] {
|
|
320
|
+
return parseEdges(this._sg.edges());
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Returns the out-edges of the named node in this subgraph. Returns `[]`
|
|
325
|
+
* if the node does not exist.
|
|
326
|
+
*/
|
|
327
|
+
outEdges(node: string): EdgeInfo[] {
|
|
328
|
+
return parseEdges(this._sg.outEdges(node));
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Returns the in-edges of the named node in this subgraph. Returns `[]`
|
|
333
|
+
* if the node does not exist.
|
|
334
|
+
*/
|
|
335
|
+
inEdges(node: string): EdgeInfo[] {
|
|
336
|
+
return parseEdges(this._sg.inEdges(node));
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Returns all edges incident to the named node in this subgraph (both in
|
|
341
|
+
* and out). Returns `[]` if the node does not exist.
|
|
342
|
+
*/
|
|
343
|
+
nodeEdges(node: string): EdgeInfo[] {
|
|
344
|
+
return parseEdges(this._sg.nodeEdges(node));
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Release the WASM wrapper. The subgraph data itself is freed when the
|
|
349
|
+
* parent {@link Graph} is deleted.
|
|
350
|
+
*/
|
|
351
|
+
delete(): void {
|
|
352
|
+
this._sg.delete();
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** @internal – supports the `using` keyword (explicit resource management). */
|
|
356
|
+
[Symbol.dispose](): void {
|
|
357
|
+
this.delete();
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* A programmatic graph builder backed by the cgraph library.
|
|
363
|
+
*
|
|
364
|
+
* Obtain an instance via {@link Graphviz.createGraph} and call
|
|
365
|
+
* {@link Graph.toDot} to serialise to a DOT string that can be passed to
|
|
366
|
+
* {@link Graphviz.layout} (or any of its convenience wrappers).
|
|
367
|
+
*
|
|
368
|
+
* **Always call {@link Graph.delete} (or use the `using` keyword) when
|
|
369
|
+
* finished** to release the underlying WASM memory.
|
|
370
|
+
*
|
|
371
|
+
* ```ts
|
|
372
|
+
* const graphviz = await Graphviz.load();
|
|
373
|
+
*
|
|
374
|
+
* using graph = graphviz.createGraph("G");
|
|
375
|
+
* graph.addNode("a");
|
|
376
|
+
* graph.addNode("b");
|
|
377
|
+
* graph.addEdge("a", "b");
|
|
378
|
+
* graph.setNodeAttr("a", "color", "red");
|
|
379
|
+
* graph.setEdgeAttr("a", "b", "", "label", "hello");
|
|
380
|
+
* graph.setGraphAttr("rankdir", "LR");
|
|
381
|
+
*
|
|
382
|
+
* const svg = graphviz.dot(graph.toDot());
|
|
383
|
+
* ```
|
|
384
|
+
*/
|
|
385
|
+
export class Graph {
|
|
386
|
+
private _graph: CGraphWasm;
|
|
387
|
+
private _module: MainModule;
|
|
388
|
+
|
|
389
|
+
/** @internal */
|
|
390
|
+
constructor(g: CGraphWasm, module: MainModule) {
|
|
391
|
+
this._graph = g;
|
|
392
|
+
this._module = module;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Create a node. If a node with this name already exists it is returned
|
|
397
|
+
* unchanged.
|
|
398
|
+
*/
|
|
399
|
+
addNode(name: string): this {
|
|
400
|
+
this._graph.addNode(name);
|
|
401
|
+
return this;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Create an edge from `tail` to `head`. Both nodes are created
|
|
406
|
+
* automatically if they do not already exist. `key` distinguishes
|
|
407
|
+
* parallel edges between the same pair of nodes; omit (or pass `""`) for
|
|
408
|
+
* an anonymous edge.
|
|
409
|
+
*/
|
|
410
|
+
addEdge(tail: string, head: string, key: string = ""): this {
|
|
411
|
+
this._graph.addEdge(tail, head, key);
|
|
412
|
+
return this;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Set a graph-level attribute (e.g. `"rankdir"`, `"label"`, `"bgcolor"`).
|
|
417
|
+
*
|
|
418
|
+
* When `attr` is a known graph attribute the value type is inferred
|
|
419
|
+
* automatically. A generic `string | number | boolean` fallback is
|
|
420
|
+
* provided for custom or less-common attributes.
|
|
421
|
+
*/
|
|
422
|
+
setGraphAttr<K extends GraphDotAttr>(attr: K, value: NonNullable<GraphAttrs[K]>): this;
|
|
423
|
+
setGraphAttr(attr: string, value: string | number | boolean): this;
|
|
424
|
+
setGraphAttr(attr: string, value: unknown): this {
|
|
425
|
+
this._graph.setGraphAttr(attr, String(value));
|
|
426
|
+
return this;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Set an attribute on a named node (e.g. `"color"`, `"label"`, `"shape"`).
|
|
431
|
+
* The node must exist; call {@link addNode} first if needed.
|
|
432
|
+
*
|
|
433
|
+
* When `attr` is a known node attribute the value type is inferred
|
|
434
|
+
* automatically. A generic `string | number | boolean` fallback is
|
|
435
|
+
* provided for custom attributes.
|
|
436
|
+
*/
|
|
437
|
+
setNodeAttr<K extends NodeDotAttr>(node: string, attr: K, value: NonNullable<NodeAttrs[K]>): this;
|
|
438
|
+
setNodeAttr(node: string, attr: string, value: string | number | boolean): this;
|
|
439
|
+
setNodeAttr(node: string, attr: string, value: unknown): this {
|
|
440
|
+
this._graph.setNodeAttr(node, attr, String(value));
|
|
441
|
+
return this;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Set an attribute on an edge identified by `(tail, head, key)`.
|
|
446
|
+
* Use the same `key` that was passed to {@link addEdge}; pass `""` for
|
|
447
|
+
* anonymous edges.
|
|
448
|
+
*
|
|
449
|
+
* When `attr` is a known edge attribute the value type is inferred
|
|
450
|
+
* automatically. A generic `string | number | boolean` fallback is
|
|
451
|
+
* provided for custom attributes.
|
|
452
|
+
*/
|
|
453
|
+
setEdgeAttr<K extends EdgeDotAttr>(tail: string, head: string, key: string, attr: K, value: NonNullable<EdgeAttrs[K]>): this;
|
|
454
|
+
setEdgeAttr(tail: string, head: string, key: string, attr: string, value: string | number | boolean): this;
|
|
455
|
+
setEdgeAttr(tail: string, head: string, key: string, attr: string, value: unknown): this {
|
|
456
|
+
this._graph.setEdgeAttr(tail, head, key, attr, String(value));
|
|
457
|
+
return this;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Remove a node and all its edges from the graph. Removing from the root
|
|
462
|
+
* graph also removes the node from every subgraph.
|
|
463
|
+
* No-op if the node does not exist.
|
|
464
|
+
*/
|
|
465
|
+
removeNode(name: string): this {
|
|
466
|
+
this._graph.removeNode(name);
|
|
467
|
+
return this;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Remove a single edge identified by `(tail, head, key)`.
|
|
472
|
+
* Pass `""` for `key` on anonymous edges.
|
|
473
|
+
* No-op if the edge does not exist.
|
|
474
|
+
*/
|
|
475
|
+
removeEdge(tail: string, head: string, key: string = ""): this {
|
|
476
|
+
this._graph.removeEdge(tail, head, key);
|
|
477
|
+
return this;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Dissolve a subgraph / cluster boundary. Nodes and edges that belonged
|
|
482
|
+
* to the subgraph remain in the parent graph.
|
|
483
|
+
* No-op if no subgraph with that name exists.
|
|
484
|
+
*/
|
|
485
|
+
removeSubgraph(name: string): this {
|
|
486
|
+
this._graph.removeSubgraph(name);
|
|
487
|
+
return this;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Clear a graph-level attribute by resetting it to its default (empty)
|
|
492
|
+
* value. Equivalent to `setGraphAttr(attr, "")`.
|
|
493
|
+
*/
|
|
494
|
+
removeGraphAttr(attr: string): this {
|
|
495
|
+
return this.setGraphAttr(attr, "");
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Clear a node attribute by resetting it to its default (empty) value.
|
|
500
|
+
* Equivalent to `setNodeAttr(node, attr, "")`.
|
|
501
|
+
*/
|
|
502
|
+
removeNodeAttr(node: string, attr: string): this {
|
|
503
|
+
return this.setNodeAttr(node, attr, "");
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* Clear an edge attribute by resetting it to its default (empty) value.
|
|
508
|
+
* Equivalent to `setEdgeAttr(tail, head, key, attr, "")`.
|
|
509
|
+
*/
|
|
510
|
+
removeEdgeAttr(tail: string, head: string, key: string, attr: string): this {
|
|
511
|
+
return this.setEdgeAttr(tail, head, key, attr, "");
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// ---- Existence checks -----------------------------------------------
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* Returns `true` if a node with the given name exists in the graph.
|
|
518
|
+
*/
|
|
519
|
+
hasNode(name: string): boolean {
|
|
520
|
+
return this._graph.hasNode(name);
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* Returns `true` if an edge from `tail` to `head` exists in the graph.
|
|
525
|
+
* Pass `key` to check for a specific parallel edge; omit (or pass `""`) to
|
|
526
|
+
* check for any edge between the two nodes.
|
|
527
|
+
*/
|
|
528
|
+
hasEdge(tail: string, head: string, key: string = ""): boolean {
|
|
529
|
+
return this._graph.hasEdge(tail, head, key);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* Returns `true` if a subgraph with the given name exists.
|
|
534
|
+
*/
|
|
535
|
+
hasSubgraph(name: string): boolean {
|
|
536
|
+
return this._graph.hasSubgraph(name);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// ---- Count queries --------------------------------------------------
|
|
540
|
+
|
|
541
|
+
/** Returns the number of nodes in the graph. */
|
|
542
|
+
nodeCount(): number { return this._graph.nodeCount(); }
|
|
543
|
+
|
|
544
|
+
/** Returns the number of edges in the graph. */
|
|
545
|
+
edgeCount(): number { return this._graph.edgeCount(); }
|
|
546
|
+
|
|
547
|
+
/** Returns the number of direct subgraphs of this graph. */
|
|
548
|
+
subgraphCount(): number { return this._graph.subgraphCount(); }
|
|
549
|
+
|
|
550
|
+
// ---- Attribute reading ----------------------------------------------
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* Returns the current value of a graph-level attribute, or `""` if the
|
|
554
|
+
* attribute has not been set.
|
|
555
|
+
*/
|
|
556
|
+
getGraphAttr(attr: string): string {
|
|
557
|
+
return this._graph.getGraphAttr(attr);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* Returns the current value of the named attribute on a node, or `""` if
|
|
562
|
+
* the node or attribute does not exist.
|
|
563
|
+
*/
|
|
564
|
+
getNodeAttr(node: string, attr: string): string {
|
|
565
|
+
return this._graph.getNodeAttr(node, attr);
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* Returns the current value of the named attribute on an edge, or `""` if
|
|
570
|
+
* the edge or attribute does not exist.
|
|
571
|
+
* Pass `key = ""` for the first (or only) edge between `tail` and `head`.
|
|
572
|
+
*/
|
|
573
|
+
getEdgeAttr(tail: string, head: string, key: string, attr: string): string {
|
|
574
|
+
return this._graph.getEdgeAttr(tail, head, key, attr);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
// ---- Graph traversal ------------------------------------------------
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* Returns the names of all nodes in the graph (in internal iteration
|
|
581
|
+
* order).
|
|
582
|
+
*/
|
|
583
|
+
nodeNames(): string[] {
|
|
584
|
+
return parseNames(this._graph.nodeNames());
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Returns the names of all direct subgraphs.
|
|
589
|
+
*/
|
|
590
|
+
subgraphNames(): string[] {
|
|
591
|
+
return parseNames(this._graph.subgraphNames());
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* Returns all edges in the graph. Each unique edge is listed exactly
|
|
596
|
+
* once.
|
|
597
|
+
*/
|
|
598
|
+
edges(): EdgeInfo[] {
|
|
599
|
+
return parseEdges(this._graph.edges());
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* Returns the out-edges of the named node. Returns `[]` if the node does
|
|
604
|
+
* not exist.
|
|
605
|
+
*/
|
|
606
|
+
outEdges(node: string): EdgeInfo[] {
|
|
607
|
+
return parseEdges(this._graph.outEdges(node));
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* Returns the in-edges of the named node. Returns `[]` if the node does
|
|
612
|
+
* not exist.
|
|
613
|
+
*/
|
|
614
|
+
inEdges(node: string): EdgeInfo[] {
|
|
615
|
+
return parseEdges(this._graph.inEdges(node));
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* Returns all edges incident to the named node (both in and out).
|
|
620
|
+
* Returns `[]` if the node does not exist.
|
|
621
|
+
*/
|
|
622
|
+
nodeEdges(node: string): EdgeInfo[] {
|
|
623
|
+
return parseEdges(this._graph.nodeEdges(node));
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* Create (or return an existing) named subgraph. Use a name beginning
|
|
628
|
+
* with `"cluster"` to have layout engines render it as a bounded cluster.
|
|
629
|
+
*
|
|
630
|
+
* Returns a {@link Subgraph} wrapper. **Call {@link Subgraph.delete} (or
|
|
631
|
+
* use the `using` keyword) when finished** to free the WASM wrapper. The
|
|
632
|
+
* subgraph data itself is owned by this graph.
|
|
633
|
+
*
|
|
634
|
+
* ```ts
|
|
635
|
+
* using cluster = graph.addSubgraph("cluster_0");
|
|
636
|
+
* cluster.setAttr("label", "My Cluster").addEdge("a", "b");
|
|
637
|
+
* ```
|
|
638
|
+
*/
|
|
639
|
+
addSubgraph(name: string): Subgraph {
|
|
640
|
+
// addSubgraph only returns null when the internal graph pointer is null,
|
|
641
|
+
// which cannot happen while this Graph instance is alive.
|
|
642
|
+
return new Subgraph(this._graph.addSubgraph(name)!);
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* Look up an existing subgraph by name without creating a new one.
|
|
647
|
+
* Returns a {@link Subgraph} wrapper if a subgraph with that name exists,
|
|
648
|
+
* or `null` if it does not. **Call {@link Subgraph.delete} (or use the
|
|
649
|
+
* `using` keyword) when finished** to free the WASM wrapper.
|
|
650
|
+
*
|
|
651
|
+
* ```ts
|
|
652
|
+
* using sg = graph.getSubgraph("cluster_0");
|
|
653
|
+
* if (sg) {
|
|
654
|
+
* console.log(sg.nodeNames());
|
|
655
|
+
* }
|
|
656
|
+
* ```
|
|
657
|
+
*/
|
|
658
|
+
getSubgraph(name: string): Subgraph | null {
|
|
659
|
+
const sg = this._graph.getSubgraph(name);
|
|
660
|
+
return sg ? new Subgraph(sg) : null;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
/**
|
|
664
|
+
* Render the graph directly to the specified format without first
|
|
665
|
+
* serialising to DOT. Equivalent to:
|
|
666
|
+
* ```ts
|
|
667
|
+
* graphviz.layout(graph.toDot(), outputFormat, layoutEngine, options)
|
|
668
|
+
* ```
|
|
669
|
+
* but avoids the DOT round-trip.
|
|
670
|
+
*
|
|
671
|
+
* @param outputFormat The output format (default `"svg"`).
|
|
672
|
+
* @param layoutEngine The layout engine to use (default `"dot"`).
|
|
673
|
+
* @param options Optional images / extra files for the renderer.
|
|
674
|
+
* @returns The rendered output as a string.
|
|
675
|
+
*/
|
|
676
|
+
layout(outputFormat: Format = "svg", layoutEngine: Engine = "dot", options?: Options): string {
|
|
677
|
+
if (options?.images?.length || options?.files?.length) {
|
|
678
|
+
// Write images/files to the Emscripten FS before rendering.
|
|
679
|
+
const helper = new this._module.CGraphviz(0, 0);
|
|
680
|
+
try {
|
|
681
|
+
createFiles(helper, options);
|
|
682
|
+
} finally {
|
|
683
|
+
helper.delete();
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
let retVal = "";
|
|
687
|
+
let errorMsg = "";
|
|
688
|
+
try {
|
|
689
|
+
retVal = this._graph.layout(outputFormat, layoutEngine);
|
|
690
|
+
} catch (e: any) {
|
|
691
|
+
errorMsg = (e as Error).message;
|
|
692
|
+
}
|
|
693
|
+
errorMsg = this._module.CGraphviz.lastError() || errorMsg;
|
|
694
|
+
if (!retVal && errorMsg) {
|
|
695
|
+
throw new Error(errorMsg);
|
|
696
|
+
}
|
|
697
|
+
return retVal;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* Serialise the graph to a DOT-language string.
|
|
702
|
+
*/
|
|
703
|
+
toDot(): string {
|
|
704
|
+
return this._graph.toDot();
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* Release the underlying WASM object. Must be called when the graph is
|
|
709
|
+
* no longer needed (or use the `using` keyword with TypeScript ≥ 5.2).
|
|
710
|
+
*/
|
|
711
|
+
delete(): void {
|
|
712
|
+
this._graph.delete();
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
/** @internal – supports the `using` keyword (explicit resource management). */
|
|
716
|
+
[Symbol.dispose](): void {
|
|
717
|
+
this.delete();
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
|
|
76
721
|
/**
|
|
77
722
|
* The Graphviz layout algorithms take descriptions of graphs in a simple text language, and make diagrams in useful formats, such as images and SVG for web pages or display in an interactive graph browser.
|
|
78
723
|
*
|
|
@@ -80,6 +725,7 @@ let g_graphviz: Promise<Graphviz> | undefined;
|
|
|
80
725
|
*
|
|
81
726
|
* See [graphviz.org](https://graphviz.org/) for more details.
|
|
82
727
|
*
|
|
728
|
+
* ### Rendering from a DOT string
|
|
83
729
|
* ```ts
|
|
84
730
|
* import { Graphviz } from "@hpcc-js/wasm/graphviz";
|
|
85
731
|
*
|
|
@@ -88,6 +734,23 @@ let g_graphviz: Promise<Graphviz> | undefined;
|
|
|
88
734
|
* const dot = "digraph G { Hello -> World }";
|
|
89
735
|
* const svg = graphviz.dot(dot);
|
|
90
736
|
* ```
|
|
737
|
+
*
|
|
738
|
+
* ### Programmatic graph construction
|
|
739
|
+
* ```ts
|
|
740
|
+
* import { Graphviz } from "@hpcc-js/wasm/graphviz";
|
|
741
|
+
*
|
|
742
|
+
* const graphviz = await Graphviz.load();
|
|
743
|
+
*
|
|
744
|
+
* using graph = graphviz.createGraph("G");
|
|
745
|
+
* graph
|
|
746
|
+
* .addNode("a")
|
|
747
|
+
* .addNode("b")
|
|
748
|
+
* .addEdge("a", "b")
|
|
749
|
+
* .setNodeAttr("a", "color", "red")
|
|
750
|
+
* .setGraphAttr("rankdir", "LR");
|
|
751
|
+
*
|
|
752
|
+
* const svg = graph.layout(); // render without a DOT round-trip
|
|
753
|
+
* ```
|
|
91
754
|
*
|
|
92
755
|
* ### Online Demos
|
|
93
756
|
* * https://raw.githack.com/hpcc-systems/hpcc-js-wasm/main/index.html
|
|
@@ -377,4 +1040,36 @@ export class Graphviz {
|
|
|
377
1040
|
nop2(dotSource: string): string {
|
|
378
1041
|
return this.layout(dotSource, "dot", "nop2");
|
|
379
1042
|
}
|
|
1043
|
+
|
|
1044
|
+
/**
|
|
1045
|
+
* Programmatically create a graph using the cgraph library.
|
|
1046
|
+
*
|
|
1047
|
+
* Returns a {@link Graph} builder that lets you add nodes, edges, and
|
|
1048
|
+
* attributes without writing DOT source by hand. Call
|
|
1049
|
+
* {@link Graph.toDot} to get the DOT string and pass it to
|
|
1050
|
+
* {@link layout} (or any convenience wrapper).
|
|
1051
|
+
*
|
|
1052
|
+
* **You must call {@link Graph.delete} when finished** to free the
|
|
1053
|
+
* underlying WASM memory, or use the `using` keyword (TypeScript ≥ 5.2).
|
|
1054
|
+
*
|
|
1055
|
+
* @param name The graph name (default `"G"`).
|
|
1056
|
+
* @param type The graph type (default `"directed"`).
|
|
1057
|
+
*
|
|
1058
|
+
* ```ts
|
|
1059
|
+
* using graph = graphviz.createGraph("G");
|
|
1060
|
+
* graph
|
|
1061
|
+
* .addNode("a")
|
|
1062
|
+
* .addNode("b")
|
|
1063
|
+
* .addEdge("a", "b")
|
|
1064
|
+
* .setNodeAttr("a", "color", "red")
|
|
1065
|
+
* .setGraphAttr("rankdir", "LR");
|
|
1066
|
+
*
|
|
1067
|
+
* const svg = graphviz.dot(graph.toDot());
|
|
1068
|
+
* ```
|
|
1069
|
+
*/
|
|
1070
|
+
createGraph(name: string = "G", type: GraphType = "directed"): Graph {
|
|
1071
|
+
const directed = !type.includes("undirected") ? 1 : 0;
|
|
1072
|
+
const strict = type.startsWith("strict") ? 1 : 0;
|
|
1073
|
+
return new Graph(new this._module.CGraph(name, directed, strict), this._module);
|
|
1074
|
+
}
|
|
380
1075
|
}
|