Go bindings for Graphviz ( port of version 2.40.1
)
- No need to install Graphviz library (
orbrew install graphviz
)apt-get install graphviz
- Supports parsing for DOT language
- Supports rendering graph in pure Go
- Supports switch renderer to your own
- Supports type safed property setting
gvc
cgraph
cdt
are available as sub package
circo
dot
fdp
neato
nop
nop1
nop2
osage
patchwork
sfdp
twopi
dot
svg
png
jpg
$ go get github.com/ns-ggeorgiev/go-graphviz
package main
import (
"bytes"
"fmt"
"log"
"github.com/ns-ggeorgiev/go-graphviz"
)
func main() {
g := graphviz.New()
graph, err := g.Graph()
if err != nil {
log.Fatal(err)
}
defer func() {
if err := graph.Close(); err != nil {
log.Fatal(err)
}
g.Close()
}()
n, err := graph.CreateNode("n")
if err != nil {
log.Fatal(err)
}
m, err := graph.CreateNode("m")
if err != nil {
log.Fatal(err)
}
e, err := graph.CreateEdge("e", n, m)
if err != nil {
log.Fatal(err)
}
e.SetLabel("e")
var buf bytes.Buffer
if err := g.Render(graph, "dot", &buf); err != nil {
log.Fatal(err)
}
fmt.Println(buf.String())
}
path := "/path/to/dot.gv"
b, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal(err)
}
graph, err := graphviz.ParseBytes(b)
g := graphviz.New()
graph, err := g.Graph()
if err != nil {
log.Fatal(err)
}
// create your graph
// 1. write encoded PNG data to buffer
var buf bytes.Buffer
if err := g.Render(graph, graphviz.PNG, &buf); err != nil {
log.Fatal(err)
}
// 2. get as image.Image instance
image, err := g.RenderImage(graph)
if err != nil {
log.Fatal(err)
}
// 3. write to file directly
if err := g.RenderFilename(graph, graphviz.PNG, "/path/to/graph.png"); err != nil {
log.Fatal(err)
}
$ go get github.com/ns-ggeorgiev/go-graphviz/cmd/dot
Usage:
dot [OPTIONS]
Application Options:
-T= specify output format ( currently supported: dot svg png jpg ) (default: dot)
-K= specify layout engine ( currently supported: circo dot fdp neato nop nop1 nop2 osage patchwork sfdp twopi )
-o= specify output file name
Help Options:
-h, --help Show this help message
go-graphviz
has four layers.
graphviz
package provides facade interface for manipulating all features of graphviz librarygvc
cgraph
cdt
are sub packages ( FYI: C library section in https://www.graphviz.org/documentation )internal/ccall
package provides bridge interface between Go and Cgo-graphviz
includes full graphviz sources
MIT