dot

package module
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2021 License: MIT Imports: 6 Imported by: 0

README

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

Go Report Card GoDoc

This is a modified fork of the great project made by Ernest Micklei.

DOT language

package main

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

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

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

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

	fmt.Println(g.String())
}

Output

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

Subgraphs

s := g.NewSubgraph()
s.Attr("label", "My Subgraph")
s.Attr("style","filled")

HTML and Literal values

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

Nodes Global Attributes

g := dot.NewGraph(dot.Directed)
g.NodeBaseAttrs().Attr("shape", "plaintext").Attr("color", "blue")
// Override shape for node `A`
n1 := g.Node(WithLabel("A")).Attr("shape", "box")

cluster example

di := dot.NewGraph(dot.Directed)
di.Attr("rankdir", "LR")
outside := di.Node(dot.WithLabel("Outside"))

// A
clusterA := di.NewSubgraph()
clusterA.Attr("label", "Cluster A")

insideOne := clusterA.Node(dot.WithLabel("one"))
insideTwo := clusterA.Node(dot.WithLabel("two"))

// B
clusterB := di.NewSubgraph()
clusterB.Attr("label", "Cluster B")

insideThree := clusterB.Node(dot.WithLabel("three"))
insideFour := clusterB.Node(dot.WithLabel("four"))

di.Edge(outside, insideFour)
di.Edge(insideFour, insideOne)
di.Edge(insideOne, insideTwo)
di.Edge(insideTwo, insideThree)
di.Edge(insideThree, outside)

About dot attributes

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

display your graph

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Strict defines a `strict` Graph type
	Strict = GraphTypeOption{"strict"}
	// Undirected defines a `graph` Graph type
	Undirected = GraphTypeOption{"graph"}
	// Directed defines a `digraph` Graph type
	Directed = GraphTypeOption{"digraph"}
	// Sub defines a `subgraph` Graph type
	Sub = GraphTypeOption{"subgraph"}
)

Functions

This section is empty.

Types

type Attribute added in v0.4.1

type Attribute func(*AttributesMap)

func WithLabel added in v0.4.1

func WithLabel(label string) Attribute

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{}) *AttributesMap

Attr sets the value for an attribute (unless empty).

func (*AttributesMap) Delete

func (a *AttributesMap) Delete(key string)

Delete removes the attribute value at key, if any

func (*AttributesMap) Value

func (a *AttributesMap) Value(label string) interface{}

Value return the value added for this label.

func (*AttributesMap) Write

func (a *AttributesMap) Write(wri io.Writer, mustBracket bool)

type ClusterOption

type ClusterOption struct{}

ClusterOption mark a graph as cluster

func (ClusterOption) Apply

func (o ClusterOption) Apply(g *Graph)

Apply enforces the Graph as cluster

type Edge

type Edge struct {
	AttributesMap
	// contains filtered or unexported fields
}

Edge represents a graph edge between two Nodes.

func (*Edge) Attrs added in v0.3.0

func (e *Edge) Attrs() *AttributesMap

Attrs returns the node attributes

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

func (g *Graph) AddToSameRank(group string, nodes ...Node)

AddToSameRank adds the given nodes to the specified rank group, forcing them to be rendered in the same row

func (*Graph) Edge

func (g *Graph) Edge(fromNode, toNode *Node, withAttrs ...func(*AttributesMap)) *Edge

Edge creates a new edge between two nodes. Eventually specify optional attributes using the `withAttrs` functions. Nodes can be have multiple edges to the same other node (or itself).

func (*Graph) FindEdges

func (g *Graph) FindEdges(fromNode, toNode Node) (found []Edge)

FindEdges finds all edges in the graph that go from the fromNode to the toNode. Otherwise, returns an empty slice.

func (*Graph) FindNodeByID

func (g *Graph) FindNodeByID(id string) (found *Node)

FindNodeByID returns a node by its identifier.

func (*Graph) FindNodeByLabel added in v0.3.0

func (g *Graph) FindNodeByLabel(label string) (found *Node)

FindNodeByLabel returns a node by its label.

func (*Graph) FindSubgraph

func (g *Graph) FindSubgraph(id string) (*Graph, bool)

FindSubgraph returns the subgraph of the graph or one from its parents.

func (*Graph) FindSubgraphByLabel added in v0.4.1

func (g *Graph) FindSubgraphByLabel(label string) (*Graph, bool)

FindSubgraphByLabel returns the subgraph of the graph or one from its parents.

func (*Graph) ID

func (g *Graph) ID(newID string) *Graph

ID sets the identifier of the graph.

func (*Graph) IndentedWrite

func (g *Graph) IndentedWrite(w *IndentWriter)

IndentedWrite write the graph to a writer using simple TAB indentation.

func (*Graph) Label

func (g *Graph) Label(label string) *Graph

Label sets the "label" attribute value.

func (*Graph) NewSubgraph added in v0.4.1

func (g *Graph) NewSubgraph() *Graph

Subgraph creates a new subgraph.

func (*Graph) Node

func (g *Graph) Node(withAttrs ...func(*AttributesMap)) *Node

Node creates a new node with an autogenerated identifier. Eventually specify optional attributes using the `withAttrs` functions. The node will have a label attribute with the id as its value. Use Label() to overwrite this.

func (*Graph) NodeBaseAttrs added in v0.4.1

func (g *Graph) NodeBaseAttrs() *AttributesMap

NodeBaseAttrs returns the node global attributes.

func (*Graph) NodeWithID added in v0.3.0

func (g *Graph) NodeWithID(id string, withAttrs ...func(*AttributesMap)) *Node

NodeWithID creates a new node with the specified identifier. Eventually specify optional attributes using the `withAttrs` functions. The node will have a label attribute with the id as its value. Use Label() to overwrite this.

func (*Graph) Root

func (g *Graph) Root() *Graph

Root returns the top-level graph if this was a subgraph.

func (*Graph) String

func (g *Graph) String() string

String returns the source in dot notation.

func (*Graph) VisitNodes

func (g *Graph) VisitNodes(callback func(node *Node) (done bool))

VisitNodes visits all nodes recursively

func (*Graph) Write

func (g *Graph) Write(w io.Writer)

type GraphOption

type GraphOption interface {
	Apply(*Graph)
}

GraphOption is a Graph configuration option.

type GraphTypeOption

type GraphTypeOption struct {
	Name string
}

GraphTypeOption sets the graph type

func (GraphTypeOption) Apply

func (o GraphTypeOption) Apply(g *Graph)

Apply enforces the Graph type

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 is a writer with indentation.

func NewIndentWriter

func NewIndentWriter(w io.Writer) *IndentWriter

NewIndentWriter returns a IndentWriter from a io.Writer.

func (*IndentWriter) BackIndent

func (i *IndentWriter) BackIndent()

BackIndent decrements the current indentation level.

func (*IndentWriter) Indent

func (i *IndentWriter) Indent()

Indent writes a tab `\t` to the writer

func (*IndentWriter) IndentWhile

func (i *IndentWriter) IndentWhile(block func())

IndentWhile writes an indented block.

func (*IndentWriter) NewLine

func (i *IndentWriter) NewLine()

NewLine add an indented new line.

func (*IndentWriter) NewLineIndentWhile

func (i *IndentWriter) NewLineIndentWhile(block func())

NewLineIndentWhile writes an indented block between new line chars `\n`.

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 writes an indented string

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) Attrs added in v0.3.0

func (n *Node) Attrs() *AttributesMap

Attrs returns the node attributes

func (*Node) ID added in v0.1.1

func (n *Node) ID() string

ID returns the Node identifier

func (*Node) Seq added in v0.1.1

func (n *Node) Seq() int

Seq returns the Node sequential number

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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