Documentation
¶
Overview ¶
Package dot is a helper for producing graphs in DOT format (graphviz). It offers Graph,Node and Edge objects to construct simple and complex (e.g. nested) graphs.
Copyright (c) Ernest Micklei. MIT License
Index ¶
- Constants
- Variables
- func MermaidFlowchart(g *Graph, orientation int) string
- func MermaidGraph(g *Graph, orientation int) string
- type AttributesMap
- type ClusterOption
- type Edge
- func (e Edge) Attr(key string, value interface{}) Edge
- func (e Edge) Bold() Edge
- func (e Edge) Dashed() Edge
- func (e Edge) Dotted() Edge
- func (e Edge) Edge(to Node, labels ...string) Edge
- func (e Edge) EdgesTo(to Node) []Edge
- func (e Edge) From() Node
- func (e Edge) GetAttr(name string) interface{}
- func (e Edge) GetAttributes() map[string]interface{}
- func (e Edge) Label(value interface{}) Edge
- func (e Edge) ReverseEdge(from Node, labels ...string) Edge
- func (e Edge) Solid() Edge
- func (e Edge) To() Node
- type Graph
- func (g *Graph) AddToSameRank(group string, nodes ...Node)
- func (g *Graph) DeepCopy() *Graph
- func (g *Graph) DeleteNode(id string) bool
- func (g *Graph) Edge(fromNode, toNode Node, labels ...string) Edge
- func (g *Graph) EdgeInitializer(callback func(e Edge))
- func (g *Graph) EdgeWithPorts(fromNode, toNode Node, fromNodePort, toNodePort string, labels ...string) Edge
- func (g *Graph) EdgesMap() map[string][]Edge
- func (g *Graph) FindEdges(fromNode, toNode Node) (found []Edge)
- func (g *Graph) FindNodeById(id string) (foundNode Node, found bool)
- func (g *Graph) FindNodeWithLabel(label string) (Node, bool)
- func (g *Graph) FindNodes() (nodes []Node)
- func (g *Graph) FindSubgraph(id string) (*Graph, bool)
- func (g *Graph) GetID() string
- func (g *Graph) HasNode(n Node) bool
- func (g *Graph) ID(newID string) *Graph
- func (g *Graph) IndentedWrite(w *IndentWriter)
- func (g *Graph) IsDirected() bool
- func (g *Graph) Label(label string) *Graph
- func (g *Graph) Node(id string) Node
- func (g *Graph) NodeInitializer(callback func(n Node))
- func (g *Graph) Root() *Graph
- func (g *Graph) String() string
- func (g *Graph) Subgraph(id string, options ...GraphOption) *Graph
- func (g *Graph) VisitNodes(callback func(node Node) (done bool))
- func (g *Graph) Write(w io.Writer)
- type GraphOption
- type GraphTypeOption
- type HTML
- type IndentWriter
- func (i *IndentWriter) BackIndent()
- func (i *IndentWriter) Indent()
- func (i *IndentWriter) IndentWhile(block func())
- func (i *IndentWriter) NewLine()
- func (i *IndentWriter) NewLineIndentWhile(block func())
- func (i *IndentWriter) Write(data []byte) (n int, err error)
- func (i *IndentWriter) WriteString(s string) (n int, err error)
- type Literal
- type Node
- func (n Node) Attr(label string, value interface{}) Node
- func (n Node) BidirectionalEdge(toAndFromNode Node) []Edge
- func (n Node) Box() Node
- func (n Node) Edge(toNode Node, labels ...string) Edge
- func (n Node) EdgesTo(toNode Node) []Edge
- func (n Node) GetAttr(name string) interface{}
- func (n Node) ID() string
- func (n Node) Label(label string) Node
- func (n Node) NewRecordBuilder() *recordBuilder
- func (n Node) ReverseEdge(fromNode Node, labels ...string) Edge
Examples ¶
Constants ¶
const ( MermaidTopToBottom = iota MermaidTopDown MermaidBottomToTop MermaidRightToLeft MermaidLeftToRight )
Variables ¶
var ( Strict = GraphTypeOption{"strict"} // only for graph and digraph, not for subgraph Undirected = GraphTypeOption{"graph"} Directed = GraphTypeOption{"digraph"} Sub = GraphTypeOption{"subgraph"} )
var ( MermaidShapeRound = shape{"(", ")"} MermaidShapeStadium = shape{"([", "])"} MermaidShapeSubroutine = shape{"[[", "]]"} MermaidShapeCylinder = shape{"[(", ")]"} MermaidShapeCirle = shape{"((", "))"} // Deprecated: use MermaidShapeCircle instead MermaidShapeCircle = shape{"((", "))"} MermaidShapeAsymmetric = shape{">", "]"} MermaidShapeRhombus = shape{"{", "}"} MermaidShapeTrapezoid = shape{"[/", "\\]"} MermaidShapeTrapezoidAlt = shape{"[\\", "/]"} MermaidShapeHexagon = shape{"{{", "}}"} MermaidShapeParallelogram = shape{"[/", "/]"} MermaidShapeParallelogramAlt = shape{"[\\", "\\]"} )
Functions ¶
func MermaidFlowchart ¶ added in v1.1.0
func MermaidGraph ¶ added in v1.1.0
Types ¶
type AttributesMap ¶
type AttributesMap struct {
// contains filtered or unexported fields
}
AttributesMap holds attribute=value pairs.
func (AttributesMap) Attr ¶
func (a AttributesMap) Attr(label string, value interface{})
Attr sets the value for an attribute (unless empty).
func (AttributesMap) Attrs ¶ added in v0.14.0
func (a AttributesMap) Attrs(labelvalues ...interface{})
Attrs sets multiple values for attributes (unless empty) taking a label,value list E.g Attrs("style","filled","fillcolor","red")
func (AttributesMap) Delete ¶ added in v0.10.2
func (a AttributesMap) Delete(key string)
Delete removes the attribute value at key, if any
func (*AttributesMap) GetAttributes ¶ added in v1.7.0
func (am *AttributesMap) GetAttributes() map[string]interface{}
GetAttributes returns a copy of the attributes.
func (AttributesMap) Value ¶
func (a AttributesMap) Value(label string) interface{}
Value return the value added for this label.
type ClusterOption ¶
type ClusterOption struct{}
func (ClusterOption) Apply ¶
func (o ClusterOption) Apply(g *Graph)
type Edge ¶
type Edge struct { AttributesMap // contains filtered or unexported fields }
Edge represents a graph edge between two Nodes.
func (Edge) Edge ¶
Edge returns a new Edge between the "to" node of this Edge and the argument Node.
func (Edge) EdgesTo ¶
EdgesTo returns all existing edges between the "to" Node of the Edge and the argument Node.
func (Edge) GetAttr ¶ added in v1.1.0
GetAttr returns the value stored by a name. Returns nil if missing.
func (Edge) GetAttributes ¶ added in v1.7.0
Returns a copy of the attributes for this edge.
func (Edge) ReverseEdge ¶ added in v1.1.0
ReverseEdge returns a new Edge between the "from" node of this Edge and the argument Node.
type Graph ¶
type Graph struct { AttributesMap // contains filtered or unexported fields }
Graph represents a dot graph with nodes and edges.
func NewGraph ¶
func NewGraph(options ...GraphOption) *Graph
NewGraph return a new initialized Graph.
func (*Graph) AddToSameRank ¶
AddToSameRank adds the given nodes to the specified rank group, forcing them to be rendered in the same row
func (*Graph) DeepCopy ¶ added in v1.8.0
DeepCopy creates a deep copy of a Graph, including all nodes, edges, subgraphs & attributes
func (*Graph) DeleteNode ¶ added in v0.16.0
DeleteNode deletes a node and all the edges associated to the node Returns false if the node wasn't found, true otherwise
func (*Graph) Edge ¶
Edge creates a new edge between two nodes. Nodes can have multiple edges to the same other node (or itself). If one or more labels are given then the "label" attribute is set to the edge.
func (*Graph) EdgeInitializer ¶ added in v0.15.0
EdgeInitializer sets a function that is called (if not nil) when an Edge is implicitly created.
func (*Graph) EdgeWithPorts ¶ added in v1.0.0
func (g *Graph) EdgeWithPorts(fromNode, toNode Node, fromNodePort, toNodePort string, labels ...string) Edge
EdgeWithPorts creates a new edge between two nodes with ports. Other functionality are the same
func (*Graph) FindEdges ¶
FindEdges finds all edges in the graph that go from the fromNode to the toNode. Otherwise, returns an empty slice.
func (*Graph) FindNodeById ¶ added in v0.11.0
FindNodeById return node by id
func (*Graph) FindNodeWithLabel ¶ added in v1.3.0
func (*Graph) FindSubgraph ¶ added in v0.13.0
FindSubgraph returns the subgraph of the graph or one from its parents.
func (*Graph) HasNode ¶ added in v1.4.0
HasNode returns whether the node was created in this graph (does not look for it in subgraphs).
func (*Graph) IndentedWrite ¶
func (g *Graph) IndentedWrite(w *IndentWriter)
IndentedWrite write the graph to a writer using simple TAB indentation.
func (*Graph) IsDirected ¶ added in v1.1.0
IsDirected returns info about the graph type
func (*Graph) Node ¶
Node returns the node created with this id or creates a new node if absent. The node will have a label attribute with the id as its value. Use Label() to overwrite this. This method can be used as both a constructor and accessor. not thread safe!
func (*Graph) NodeInitializer ¶ added in v0.15.0
NodeInitializer sets a function that is called (if not nil) when a Node is implicitly created.
func (*Graph) Subgraph ¶
func (g *Graph) Subgraph(id string, options ...GraphOption) *Graph
Subgraph returns the Graph with the given id ; creates one if absent. The label attribute is also set to the id ; use Label() to overwrite it.
func (*Graph) VisitNodes ¶ added in v0.11.0
VisitNodes visits all nodes recursively
type GraphOption ¶
type GraphOption interface {
Apply(*Graph)
}
type GraphTypeOption ¶
type GraphTypeOption struct {
Name string
}
func (GraphTypeOption) Apply ¶
func (o GraphTypeOption) Apply(g *Graph)
type HTML ¶
type HTML string
HTML renders the provided content as graphviz HTML. Use of this type is only valid for some attributes, like the 'label' attribute.
type IndentWriter ¶
type IndentWriter struct {
// contains filtered or unexported fields
}
IndentWriter decorates an io.Writer to insert leading TAB \t character per line
func NewIndentWriter ¶
func NewIndentWriter(w io.Writer) *IndentWriter
NewIndentWriter returns a new IndentWriter with indent level 0.
func (*IndentWriter) BackIndent ¶
func (i *IndentWriter) BackIndent()
BackIndent drops the level with one.
func (*IndentWriter) Indent ¶
func (i *IndentWriter) Indent()
Indent raises the level and writes the extra \t (TAB) character.
func (*IndentWriter) IndentWhile ¶
func (i *IndentWriter) IndentWhile(block func())
IndentWhile call the blocks after an indent and will restore that indent afterward.
func (*IndentWriter) NewLine ¶
func (i *IndentWriter) NewLine()
NewLine writes the new line and a number of tab \t characters that matches the level count.
func (*IndentWriter) NewLineIndentWhile ¶
func (i *IndentWriter) NewLineIndentWhile(block func())
NewLineIndentWhile is a variation of IndentWhile that produces extra newlines.
func (*IndentWriter) Write ¶
func (i *IndentWriter) Write(data []byte) (n int, err error)
Write makes it an io.Writer
func (*IndentWriter) WriteString ¶
func (i *IndentWriter) WriteString(s string) (n int, err error)
WriteString is a convenient Write.
type Literal ¶
type Literal string
Literal renders the provided value as is, without adding enclosing quotes, escaping newlines, quotations marks or any other characters. For example:
node.Attr("label", Literal(`"left-justified text\l"`))
allows you to left-justify the label (due to the \l at the end). The caller is responsible for enclosing the value in quotes and for proper escaping of special characters.
type Node ¶
type Node struct { AttributesMap // contains filtered or unexported fields }
Node represents a dot Node.
func (Node) BidirectionalEdge ¶ added in v1.2.0
BidirectionalEdge adds two edges, marks the first as invisible and the second with direction "both". Returns both edges.
func (Node) GetAttr ¶ added in v1.1.0
GetAttr returns the value stored by a name. Returns nil if missing.
func (Node) NewRecordBuilder ¶ added in v1.6.0
func (n Node) NewRecordBuilder() *recordBuilder
NewRecordBuilder returns a new recordBuilder for setting the attributes for a record-shaped node. Call Build() on the builder to set the label and shape.
Example ¶
https://graphviz.org/doc/info/shapes.html#record
digraph structs { node [shape=record]; struct1 [label="<f0> left|<f1> mid\ dle|<f2> right"]; struct2 [label="<f0> one|<f1> two"]; struct3 [label="hello\nworld |{ b |{c|<here> d|e}| f}| g | h"]; struct1:f1 -> struct2:f0; struct1:f2 -> struct3:here; }
g := NewGraph(Directed) r1 := g.Node("struct1").NewRecordBuilder() r1.FieldWithId("left", "f0") r1.FieldWithId("mid\dle", "f1") r1.FieldWithId("right", "f2") r1.Build() r2 := g.Node("struct2").NewRecordBuilder() r2.FieldWithId("one", "f0") r2.Build() r3 := g.Node("struct3").NewRecordBuilder() r3.Field("hello\world") r3.Nesting(func() { r3.Field("b") r3.Nesting(func() { r3.Field("c") r3.FieldWithId("d", "here") r3.Field("e") }) r3.Field("f") }) r3.Field("g") r3.Field("h") r3.Build() g.EdgeWithPorts(g.Node("struct1"), g.Node("struct2"), "f1", "f0") g.EdgeWithPorts(g.Node("struct1"), g.Node("struct3"), "f2", "here") fmt.Println(flatten(g.String()))
Output: digraph {n1[label="<f0> left|<f1> mid\dle|<f2> right",shape="record"];n2[label="<f0> one",shape="record"];n3[label="hello\world|{b|{c|<here> d|e}|f}|g|h",shape="record"];n1:f1->n2:f0;n1:f2->n3:here;}