v0.23.0 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2023 License: Apache-2.0 Imports: 4 Imported by: 39



Package draw provides functions for visualizing graph structures. At this time, draw supports the DOT language which can be interpreted by Graphviz, Grappa, and others.



This section is empty.


This section is empty.


func DOT added in v0.8.0

func DOT[K comparable, T any](g graph.Graph[K, T], w io.Writer, options ...func(*description)) error

DOT renders the given graph structure in DOT language into an io.Writer, for example a file. The generated output can be passed to Graphviz or other visualization tools supporting DOT.

The following example renders a directed graph into a file my-graph.gv:

g := graph.New(graph.IntHash, graph.Directed())

_ = g.AddVertex(1)
_ = g.AddVertex(2)
_ = g.AddVertex(3, graph.VertexAttribute("style", "filled"), graph.VertexAttribute("fillcolor", "red"))

_ = g.AddEdge(1, 2, graph.EdgeWeight(10), graph.EdgeAttribute("color", "red"))
_ = g.AddEdge(1, 3)

file, _ := os.Create("./my-graph.gv")
_ = draw.DOT(g, file)

To generate an SVG from the created file using Graphviz, use a command such as the following:

dot -Tsvg -O my-graph.gv

Another possibility is to use os.Stdout as an io.Writer, print the DOT output to stdout, and pipe it as follows:

go run main.go | dot -Tsvg > output.svg

DOT also accepts the GraphAttribute functional option, which can be used to add global attributes when rendering the graph:

_ = draw.DOT(g, file, draw.GraphAttribute("label", "my-graph"))

func GraphAttribute added in v0.17.0

func GraphAttribute(key, value string) func(*description)

GraphAttribute is a functional option for the DOT method.


This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL