-
Notifications
You must be signed in to change notification settings - Fork 70
/
graphviz.go
141 lines (122 loc) · 2.7 KB
/
graphviz.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
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
package graphviz
import (
"context"
"image"
"io"
"io/fs"
"github.com/goccy/go-graphviz/cgraph"
"github.com/goccy/go-graphviz/gvc"
"github.com/goccy/go-graphviz/internal/wasm"
)
type Graphviz struct {
ctx *gvc.Context
name string
dir *GraphDescriptor
layout Layout
}
type Layout string
const (
CIRCO Layout = "circo"
DOT Layout = "dot"
FDP Layout = "fdp"
NEATO Layout = "neato"
NOP Layout = "nop"
NOP1 Layout = "nop1"
NOP2 Layout = "nop2"
OSAGE Layout = "osage"
PATCHWORK Layout = "patchwork"
SFDP Layout = "sfdp"
TWOPI Layout = "twopi"
)
type Format string
const (
XDOT Format = "dot"
SVG Format = "svg"
PNG Format = "png"
JPG Format = "jpg"
)
func New(ctx context.Context) (*Graphviz, error) {
c, err := gvc.New(ctx)
if err != nil {
return nil, err
}
return &Graphviz{
ctx: c,
dir: Directed,
layout: DOT,
}, nil
}
func NewWithPlugins(ctx context.Context, plugins ...Plugin) (*Graphviz, error) {
c, err := gvc.NewWithPlugins(ctx, plugins...)
if err != nil {
return nil, err
}
return &Graphviz{
ctx: c,
dir: Directed,
layout: DOT,
}, nil
}
func (g *Graphviz) Close() error {
return g.ctx.Close()
}
func (g *Graphviz) SetLayout(layout Layout) *Graphviz {
g.layout = layout
return g
}
func (g *Graphviz) Render(ctx context.Context, graph *Graph, format Format, w io.Writer) (e error) {
defer func() {
if err := g.ctx.FreeLayout(ctx, graph); err != nil {
e = err
}
}()
if err := g.ctx.Layout(ctx, graph, string(g.layout)); err != nil {
return err
}
if err := g.ctx.RenderData(ctx, graph, string(format), w); err != nil {
return err
}
return nil
}
func (g *Graphviz) RenderImage(ctx context.Context, graph *Graph) (img image.Image, e error) {
defer func() {
if err := g.ctx.FreeLayout(ctx, graph); err != nil {
e = err
}
}()
if err := g.ctx.Layout(ctx, graph, string(g.layout)); err != nil {
return nil, err
}
image, err := g.ctx.RenderImage(ctx, graph, string(PNG))
if err != nil {
return nil, err
}
return image, nil
}
func (g *Graphviz) RenderFilename(ctx context.Context, graph *Graph, format Format, path string) (e error) {
defer func() {
if err := g.ctx.FreeLayout(ctx, graph); err != nil {
e = err
}
}()
if err := g.ctx.Layout(ctx, graph, string(g.layout)); err != nil {
return err
}
if err := g.ctx.RenderFilename(ctx, graph, string(format), path); err != nil {
return err
}
return nil
}
func (g *Graphviz) Graph(option ...GraphOption) (*Graph, error) {
for _, opt := range option {
opt(g)
}
graph, err := cgraph.Open(g.name, g.dir, nil)
if err != nil {
return nil, err
}
return graph, nil
}
func SetFileSystem(fs fs.FS) {
wasm.SetWasmFileSystem(fs)
}