layout draws graphs in pure Go.
You build a graph in code or read one from a DOT or GraphML file. layout.Hierarchical places the nodes in ranks and routes the edges, in the same style as Graphviz dot. layout.Force is there for graphs without a clear direction. The result can be written as SVG, Unicode text for a terminal, JSON coordinates, DOT or GraphML.
It understands a good part of the DOT language: clusters, record and HTML-like labels, ports, rankdir, minlen, concentrate for merged edges, and colors by X11 name.
The graph layouting can be used as a command-line tool and as a library.
To install the command-line tool:
go install github.com/loov/layout/cmd/glay@latest
To install the package:
go get github.com/loov/layout
package main
import (
"log"
"os"
"github.com/loov/layout"
"github.com/loov/layout/format/svg"
)
func main() {
graph := layout.NewDigraph()
graph.Edge("checkout", "build")
graph.Edge("checkout", "lint")
graph.Edge("build", "unit")
graph.Edge("build", "integration")
graph.Edge("lint", "review").LineStyle = layout.Dashed
graph.Edge("unit", "review")
graph.Edge("integration", "review").Label = "slow"
approve := graph.Edge("review", "deploy")
approve.Label = "approve"
approve.LineColor = layout.RGB{G: 0x80}
graph.Node("review").Shape = layout.Box
graph.Node("deploy").FillColor = layout.RGB{R: 0x98, G: 0xFB, B: 0x98}
graph.Clusters = []*layout.Cluster{{
ID: "test", Label: "test",
Nodes: []*layout.Node{graph.Node("unit"), graph.Node("integration")},
LineColor: layout.RGB{B: 0xFF},
}}
l, err := layout.Hierarchical(graph, layout.Options{})
if err != nil {
log.Fatal(err)
}
svg.Write(os.Stdout, l)
}The same graph drawn for a terminal, laid out with layout.Options{ForText: true} and written with text.Write instead of svg.Write:
╭──────────╮
│ checkout │
╰─────┬───┬╯
│ ╰────╮
▼ ▼
╭───────╮ ╭──────╮
│ build │ │ lint │
╰┬──┬───╯ ╰────┬─╯
│ │ ┊
╭────────╯ │ ┊
│ │ ┊
┌ test ╂┈┈┈┈┈┈┈┈┈┈┈╂┈┈┈┈┈┈┈┈┐ ┊
┊ │ │ ┊ ┊
┊ ▼ ▼ ┊ ┊
┊ ╭──────╮ ╭─────────────╮ ┊ ┊
┊ │ unit │ │ integration │ ┊ ┊
┊ ╰───┬──╯ ╰──────┬──────╯ ┊ ┊
┊ │ │ ┊ ┊
└┈┈┈┈┈┈╂┈┈┈┈┈┈┈┈┈┈┈╂┈┈┈┈┈┈┈┈┘ ┊
│ │slow ┊
╰────────╮ │ ┌┈┈┈┈┈┈┈┈┘
│ │ ┊
▼ ▼ ▼
┌────────┐
│ review │
└────┬───┘
│approve
▼
╭────────╮
│ deploy │
╰────────╯
Other layouts and outputs:
layout.Force(graph, layout.ForceOptions{})— force-directed layout for undirected or cyclic graphs.format/json— plain coordinates for drawing the graph elsewhere.
The same is available from the command line: glay -l force -q fast -t txt|ans|json|svg|dot|graphml input.dot; colored text takes -colors 16|truecolor and -bg color.