-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
91 lines (84 loc) · 2.13 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { DirectedGraph } from "@vizdom/vizdom-ts-node";
import fs from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
import util from "node:util";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Create a new graph. We're overriding several default spacing attributes just
// for easy comparison of the JSON output. In most cases, overriding the margin
// is enough.
const graph = new DirectedGraph({
layout: {
rank_sep: 10, // Sets the spacing between rows (ranks)
// overriding to `0` just for this demo
node_sep: 0, // No space between columns (horizontal) for nodes
edge_sep: 0, // No space between columns (horizontal) edge labels
// supply your own margin dimensions if needed
margin_x: 0,
margin_y: 0,
},
});
// Add vertices
const v0 = graph.new_vertex(
{
layout: {
// supply your own bounding box dimensions
shape_w: 10,
shape_h: 10,
},
render: {
id: "v0",
},
},
{
compute_bounding_box: false,
}
);
const v1 = graph.new_vertex(
{
layout: {
// supply your own bounding box dimensions
shape_w: 10,
shape_h: 10,
},
render: {
id: "v1",
},
},
{
compute_bounding_box: false,
}
);
// Add an edge between the vertices
graph.new_edge(
v0,
v1,
{
layout: {
// We can also set to `0` to avoid the positioned graph from including
// space for edge label boxes if there's nothing to render (no label, nor
// any DOM nodes `getBoundingClientRect()`)
shape_w: 0,
shape_h: 0,
},
render: {
id: "e1",
},
},
{
compute_bounding_box: false,
}
);
// Position the graph
const positioned = graph.layout();
const json = positioned.to_json();
// Obtain a JS/TS object directly for manipulation
const jsonObj = json.to_obj();
console.log(
util.inspect(jsonObj, { showHidden: false, depth: null, colors: true })
);
// ...Or obtain a json string
//
// NOTE: You can make it compact json string using `json.to_string()`
await fs.writeFile(path.join(__dirname, "graph.json"), json.to_string_pretty());