a lightweight, pure golang graphviz-compatible dot language implementation
WARNING: this package is a WIP and will introduce breaking changes while on major version zero.
Dot provides interfaces and ready-to-use concrete types to create graphviz-compatible graphs using its dot language.
This package was inspired/initially forked from emicklei/dot, but has too many breaking changes compared to the original - namely interface usage and other distinct design decisions - that I decided to maintain it separately. If you need a simpler, no-brainy option, use emicklei's dot package.
Clone the repository and then run make
to build, test, generate coverage and
lint the code.
Golang 1.12+ and ideally a modules-enabled application. Dot has no dependencies.
Add it to your modules with
go get -u github.com/wwmoraes/dot
And then:
package main
import (
"os"
"github.com/wwmoraes/dot"
)
func main() {
graph := dot.New()
graph.SetAttributeString("label", "an amazing graph")
clusterA := graph.Subgraph(WithID("Cluster A"), WithCluster())
clusterA.SetAttributeString("label", "Cluster A")
clusterB := graph.Subgraph(WithID("Cluster B"), WithCluster())
clusterB.SetAttributeString("label", "Cluster B")
clusterA.
Node("one").
Edge(clusterA.Node("two")).
Edge(clusterB.Node("three")).
Edge(graph.Node("Outside")).
Edge(clusterB.Node("four")).
Edge(clusterA.Node("one"))
fd, _ := os.Create("sample.dot")
graph.WriteTo(fd)
}
The constants sub-package has all supported keys defined as variables, and can be used instead of plain strings to avoid both duplication and errors:
package main
import (
"os"
"github.com/wwmoraes/dot"
"github.com/wwmoraes/dot/constants"
)
func main() {
graph := dot.New()
graph.SetAttributeString(constants.KeyLabel, "a graph")
node := graph.Node("n1")
node.SetAttributeString(constants.KeyLabel, "a node")
edge := graph.Edge(graph.Node("n2"), graph.Node("n3"))
edge.SetAttributeString(constants.KeyLabel, "a edge")
fd, _ := os.Create("sample.dot")
graph.WriteTo(fd)
}
You can also set literals and HTML values using the helper functions:
package main
import (
"os"
"github.com/wwmoraes/dot"
"github.com/wwmoraes/dot/constants"
)
func main() {
graph := dot.New()
graph.Node("n1").SetAttributeLiteral(constants.KeyLabel, `a left label\l`)
graph.Node("n2").SetAttributeHTML(constants.KeyLabel, `<b>a bold label</b>`)
fd, _ := os.Create("sample.dot")
graph.WriteTo(fd)
}
- @emicklei for the amazing original package, which initially helped me get rid of a graphviz cgo package I used on the kubegraph project
- @damianopetrungaro for the reviews and discussion about golang ways - my personal master Yoda!