Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
376 lines (331 loc) · 9.34 KB
/
Copy pathexample_test.go
File metadata and controls
376 lines (331 loc) · 9.34 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package layout_test
import (
"fmt"
"log"
"os"
"github.com/loov/layout"
"github.com/loov/layout/format/svg"
"github.com/loov/layout/format/text"
)
// The usual flow: build a graph, lay it out and write it in some format.
func Example() {
graph := layout.NewDigraph()
graph.Edge("A", "B")
graph.Edge("A", "C")
graph.Edge("B", "D")
graph.Edge("C", "D")
l, err := layout.Hierarchical(graph, layout.Options{})
if err != nil {
log.Fatal(err)
}
if err := svg.Write(os.Stdout, l); err != nil {
log.Fatal(err)
}
}
// Hierarchical returns where each node and edge goes, which is all a
// custom renderer needs.
func ExampleHierarchical() {
graph := layout.NewDigraph()
graph.Edge("A", "B")
graph.Edge("A", "C")
l, err := layout.Hierarchical(graph, layout.Options{})
if err != nil {
log.Fatal(err)
}
for i, node := range graph.Nodes {
fmt.Printf("%s at %v,%v\n", node.ID, l.Nodes[i].Center.X, l.Nodes[i].Center.Y)
}
for i, edge := range graph.Edges {
fmt.Printf("%v through %v\n", edge, l.Edges[i].Path)
}
// Output:
// A at 64,32
// B at 32,96
// C at 96,96
// A->B through [{55.703705 48} {39.11111 80}]
// A->C through [{72.296295 48} {88.888885 80}]
}
// Fast spends fewer sweeps on reducing crossings, for large graphs;
// Quality spends more. Align packs the nodes to one side.
func ExampleOptions() {
graph := layout.NewDigraph()
graph.Edge("root", "a")
graph.Edge("root", "b")
graph.Edge("a", "leaf")
opts := layout.Fast
opts.Align = layout.AlignLeft
opts.ForText = true
l, err := layout.Hierarchical(graph, opts)
if err != nil {
log.Fatal(err)
}
if err := text.Write(os.Stdout, l); err != nil {
log.Fatal(err)
}
// Output:
// ╭──────╮
// │ root │
// ╰───┬─┬╯
// │ ╰───╮
// ▼ ▼
// ╭───╮ ╭───╮
// │ a │ │ b │
// ╰─┬─╯ ╰───╯
// │
// ▼
// ╭──────╮
// │ leaf │
// ╰──────╯
}
// Force suits graphs without a direction, such as a cycle.
func ExampleForce() {
graph := layout.NewGraph()
graph.Edge("a", "b")
graph.Edge("b", "c")
graph.Edge("c", "a")
l, err := layout.Force(graph, layout.ForceOptions{})
if err != nil {
log.Fatal(err)
}
for i, edge := range graph.Edges {
fmt.Printf("%v: %d points\n", edge, len(l.Edges[i].Path))
}
// Output:
// a->b: 2 points
// b->c: 2 points
// c->a: 2 points
}
// Options.ForText lays the graph out for drawing on a character grid.
func Example_text() {
graph := layout.NewDigraph()
graph.Edge("fetch", "parse")
graph.Edge("parse", "render").Label = "ok"
graph.Edge("parse", "report").Label = "error"
l, err := layout.Hierarchical(graph, layout.Options{ForText: true})
if err != nil {
log.Fatal(err)
}
if err := text.Write(os.Stdout, l); err != nil {
log.Fatal(err)
}
// Output:
// ╭───────╮
// │ fetch │
// ╰───┬───╯
// │
// ▼
// ╭───────╮
// │ parse │
// ╰──┬───┬╯
// │ │
// │ │error
// │ok ╰─────╮
// ▼ ▼
// ╭────────╮ ╭────────╮
// │ render │ │ report │
// ╰────────╯ ╰────────╯
}
// RankDir turns the layout sideways.
func Example_rankDir() {
graph := layout.NewDigraph()
graph.RankDir = layout.LeftToRight
graph.Edge("idle", "running")
graph.Edge("running", "done")
l, err := layout.Hierarchical(graph, layout.Options{ForText: true})
if err != nil {
log.Fatal(err)
}
if err := text.Write(os.Stdout, l); err != nil {
log.Fatal(err)
}
// Output:
// ╭──────╮ ╭─────────╮ ╭──────╮
// │ idle ├─▶│ running ├─▶│ done │
// ╰──────╯ ╰─────────╯ ╰──────╯
}
// Clusters draw a box around a group of nodes, which the layout keeps
// together.
func Example_clusters() {
graph := layout.NewDigraph()
graph.Edge("client", "api")
graph.Edge("api", "db")
graph.Edge("api", "cache")
graph.Clusters = []*layout.Cluster{{
ID: "backend",
Label: "backend",
Nodes: []*layout.Node{graph.Node("db"), graph.Node("cache")},
}}
l, err := layout.Hierarchical(graph, layout.Options{ForText: true})
if err != nil {
log.Fatal(err)
}
if err := text.Write(os.Stdout, l); err != nil {
log.Fatal(err)
}
// Output:
// ╭────────╮
// │ client │
// ╰───┬────╯
// │
// ▼
// ╭─────╮
// │ api │
// ╰─┬─┬─╯
// │ ╰────╮
// ┌ backend ┈╂┈┈┈┈┈┈╂┈┈┈┈┈┐
// ┊ ▼ ▼ ┊
// ┊ ╭────╮ ╭───────╮ ┊
// ┊ │ db │ │ cache │ ┊
// ┊ ╰────╯ ╰───────╯ ┊
// └┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┘
}
// A Record node draws its label as a table of fields, using the
// Graphviz record syntax: | separates fields and braces flip between
// rows and columns.
func Example_records() {
graph := layout.NewDigraph()
user := graph.Node("user")
user.Shape = layout.Record
user.Label = "User|{id|name}"
graph.Edge("user", "session")
l, err := layout.Hierarchical(graph, layout.Options{ForText: true})
if err != nil {
log.Fatal(err)
}
if err := text.Write(os.Stdout, l); err != nil {
log.Fatal(err)
}
// Output:
// ┌─────────────┐
// │ │ id │
// │ User │──────│
// │ │ name │
// └──────┬──────┘
// │
// ▼
// ╭─────────╮
// │ session │
// ╰─────────╯
}
// SameRank, MinRank and MaxRank constrain which rank nodes go on.
func Example_ranks() {
graph := layout.NewDigraph()
graph.Edge("a", "b")
graph.Edge("b", "c")
graph.Edge("x", "y")
graph.SameRank = [][]*layout.Node{{graph.Node("b"), graph.Node("x")}}
graph.MaxRank = []*layout.Node{graph.Node("y")}
l, err := layout.Hierarchical(graph, layout.Options{})
if err != nil {
log.Fatal(err)
}
for _, id := range []string{"b", "x", "c", "y"} {
fmt.Printf("%s at y=%v\n", id, l.Node(graph.Node(id)).Center.Y)
}
// Output:
// b at y=96
// x at y=96
// c at y=160
// y at y=160
}
// When every node has a Pos, layout keeps the positions and only routes
// the edges, like dot -n.
func Example_pinned() {
graph := layout.NewDigraph()
graph.Node("a").Pos = &layout.Vector{X: 0, Y: 0}
graph.Node("b").Pos = &layout.Vector{X: 200, Y: 100}
graph.Edge("a", "b")
l, err := layout.Hierarchical(graph, layout.Options{})
if err != nil {
log.Fatal(err)
}
fmt.Println(l.Node(graph.Node("b")).Center)
fmt.Println(len(l.Edges[0].Path) > 0)
// Output:
// {200 100}
// true
}
// MeasureText replaces the built-in text width estimate, for example
// with measurements from the font the output will use. It returns the
// width of one line.
func Example_measureText() {
graph := layout.NewDigraph()
graph.MeasureText = func(line, fontName string, fontSize layout.Length) layout.Length {
// a monospace font with characters 0.6 em wide
return layout.Length(len(line)) * fontSize * 0.6
}
graph.Edge("short", "a much longer label")
l, err := layout.Hierarchical(graph, layout.Options{})
if err != nil {
log.Fatal(err)
}
for i, node := range graph.Nodes {
fmt.Printf("%q is %v wide\n", node.ID, l.Nodes[i].Size.X)
}
// Output:
// "short" is 56 wide
// "a much longer label" is 173.6 wide
}
// Nodes, edges and clusters carry their own styling.
func Example_styling() {
graph := layout.NewDigraph()
start := graph.Node("start")
start.Shape = layout.PointShape
ok := graph.Node("ok")
ok.Shape = layout.Box
ok.FillColor = layout.RGB{R: 0x98, G: 0xFB, B: 0x98}
ok.Peripheries = 2
graph.Edge("start", "check")
pass := graph.Edge("check", "ok")
pass.Label = "pass"
pass.LineColor = layout.RGB{G: 0x80}
fail := graph.Edge("check", "retry")
fail.LineStyle = layout.Dashed
fail.ArrowHead = layout.ArrowVee
fail.FromPort = layout.East
l, err := layout.Hierarchical(graph, layout.Options{})
if err != nil {
log.Fatal(err)
}
if err := svg.Write(os.Stdout, l); err != nil {
log.Fatal(err)
}
}
// Diagnose counts layout defects, such as edge crossings, so that tests
// can check layout quality without looking at the drawing.
func ExampleDiagnose() {
graph := layout.NewDigraph()
graph.Edge("a", "d")
graph.Edge("b", "c")
graph.Edge("a", "c")
graph.Edge("b", "d")
l, err := layout.Hierarchical(graph, layout.Options{})
if err != nil {
log.Fatal(err)
}
diag := layout.Diagnose(l)
fmt.Println("node overlaps:", diag.NodeOverlaps)
fmt.Println("edge crossings:", diag.EdgeCrossings)
// Output:
// node overlaps: 0
// edge crossings: 1
}
func ExampleColorByName() {
color, ok := layout.ColorByName("forestgreen")
fmt.Println(ok)
fmt.Println(color.RGBA8())
// Output:
// true
// 34 139 34 255
}
// Edge finds or creates the edge and both of its nodes.
func ExampleGraph_Edge() {
graph := layout.NewDigraph()
first := graph.Edge("a", "b")
again := graph.Edge("a", "b")
fmt.Println(first == again)
fmt.Println(len(graph.Nodes), len(graph.Edges))
// Output:
// true
// 2 1
}