MCPcopy Index your code
hub / github.com/emicklei/dot

github.com/emicklei/dot @v1.11.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.11.0 ↗ · + Follow
203 symbols 1,046 edges 19 files 97 documented · 48% 9 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

dot - little helper package in Go for the graphviz dot language

Go Go Report Card GoDoc codecov

DOT language

package main

import (
    "fmt"   
    "github.com/emicklei/dot"
)

// go run main.go | dot -Tpng  > test.png && open test.png

func main() {
    g := dot.NewGraph(dot.Directed)
    n1 := g.Node("coding")
    n2 := g.Node("testing a little").Box()

    g.Edge(n1, n2)
    g.Edge(n2, n1, "back").Attr("color", "red")

    fmt.Println(g.String())
}

Output

digraph {
    node [label="coding"]; n1;
    node [label="testing a little",shape="box"]; n2;
    n1 -> n2;
    n2 -> n1 [color="red", label="back"];
}

Chaining edges

g.Node("A").Edge(g.Node("B")).Edge(g.Node("C"))

A -> B -> C

g.Node("D").BidirectionalEdge(g.Node("E"))

D <-> E

Subgraphs

s := g.Subgraph("cluster")
s.Attr("style", "filled")

Initializers

g := dot.NewGraph(dot.Directed)
g.NodeInitializer(func(n dot.Node) {
    n.Attr("shape", "rectangle")
    n.Attr("fontname", "arial")
    n.Attr("style", "rounded,filled")
})

g.EdgeInitializer(func(e dot.Edge) {
    e.Attr("fontname", "arial")
    e.Attr("fontsize", "9")
    e.Attr("arrowsize", "0.8")
    e.Attr("arrowhead", "open")
})

HTML and Literal values

node.Attr("label", Literal(`"left-justified text\l"`))
graph.Attr("label", HTML("<B>Hi</B>"))

cluster example

di := dot.NewGraph(dot.Directed)
outside := di.Node("Outside")

// A
clusterA := di.Subgraph("Cluster A", dot.ClusterOption{})
insideOne := clusterA.Node("one")
insideTwo := clusterA.Node("two")

// B
clusterB := di.Subgraph("Cluster B", dot.ClusterOption{})
insideThree := clusterB.Node("three")
insideFour := clusterB.Node("four")

// edges
outside.Edge(insideFour).Edge(insideOne).Edge(insideTwo).Edge(insideThree).Edge(outside)

See also ext/Subsystem type for creating composition hierarchies.

record example

See record_test.go#ExampleNode_NewRecordBuilder.

About dot attributes

https://graphviz.gitlab.io/doc/info/attrs.html

display your graph

go run main.go | dot -Tpng  > test.png && open test.png

mermaid

Output a dot Graph using the mermaid syntax. Only Graph and Flowchart are supported. See MermaidGraph and MermaidFlowchart.

g := dot.NewGraph(dot.Directed)
...
fmt.Println(dot.MermaidGraph(g, dot.MermaidTopToBottom))

subgraphs in mermaid

flowchart LR;n8-->n3;subgraph one;n2("a1");n3("a2");n2-->n3;end;subgraph three;n8("c1");n9("c2");n8-->n9;end;subgraph two;n5("b1");n6("b2");n5-->n6;end;

mermaid specific attributes

attr type description
link Edge examples are {-->,-.->,--x,o--o}
shape Node examples are {MermaidShapeRound,MermaidShapeCircle,MermaidShapeTrapezoid}
style Node example is fill:#90EE90
animate Edge Attr("animate","true)
linkStyle Edge Attr("linkStyle","stroke:red")

extensions

See also package dot/dotx for types that can help in constructing complex graphs.

testing

go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out

(c) 2015-2023, http://ernestmicklei.com. MIT License.

Extension points exported contracts — how you extend this code

Connectable (Interface)
Connectable is a dot.Node or a *dotx.Composite [4 implementers]
dotx/composite.go
GraphOption (Interface)
(no doc) [4 implementers]
graph_options.go

Core symbols most depended-on inside this repo

Node
called by 133
graph.go
NewGraph
called by 74
graph.go
Attr
called by 62
dotx/composite.go
String
called by 43
graph.go
Edge
called by 29
node.go
Value
called by 21
attr.go
Subgraph
called by 18
graph.go
Edge
called by 17
graph.go

Shape

Method 94
Function 89
Struct 13
TypeAlias 5
Interface 2

Languages

Go100%

Modules by API surface

graph.go35 symbols
graph_test.go33 symbols
record.go17 symbols
edge.go15 symbols
node.go13 symbols
dotx/composite.go13 symbols
mermaid_test.go12 symbols
mermaid.go9 symbols
indent.go9 symbols
edge_test.go9 symbols
dotx/composite_test.go7 symbols
attr.go7 symbols

For agents

$ claude mcp add dot \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact