-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot.go
52 lines (41 loc) · 1006 Bytes
/
dot.go
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
package gorax
import (
"fmt"
"github.com/emicklei/dot"
)
// ToDOTGraph walks the Tree and converts it into a dot.Graph
func (t *Tree) ToDOTGraph() *dot.Graph {
// create new dot graph
graph := dot.NewGraph(dot.Directed)
walk(&t.root, func(key string, node *node) bool {
n := graph.Node(key)
if node.isKey() {
// set value in label
n.Attr("label", fmt.Sprintf("%s|%v", key, node.getValue()))
// change shape
n.Attr("shape", "record")
}
if node.isLeaf() {
// leaf nodes are blue
n.Attr("color", "blue")
}
if node.isCompressed() {
// compressed nodes are green
n.Attr("color", "green")
// add compressed edge
n.Edge(graph.Node(key+node.key)).
Label(node.key).
Attr("color", "green")
} else {
// add all other edges
for i := 0; i < len(node.key); i++ {
n.Edge(graph.Node(key + string(node.key[i]))).
Label(string(node.key[i]))
}
}
return false
})
// create root
graph.Node("").Attr("shape", "point")
return graph
}