Go package for writing descriptions using the Graphviz DOT language

Overview

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

Build Status Go Report Card GoDoc codecov

DOT language

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()) } ">
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

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

Chaining edges

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

A -> B -> C

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

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

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)

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

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

Issues
  • Set global attributes for edge or node

    Set global attributes for edge or node

    How to set node/edge attribute globally like in:

    digraph G
    {
        node[shape="point"];
        edge[arrowhead="odiamond"];
    
        A -> B
        ...
    }
    

    so that we don't have to set the attribute on each individual node/edge?

    opened by cakraww 8
  • Escape character issue?

    Escape character issue?

    https://github.com/emicklei/dot/blob/966008b949b7b1e69370ec0d95ec6fcd52ba0532/graph.go#L269

    Hi - I'm new to GoLang so I might be wrong. I've been trying to use the "\l" instruction (See page 8 here: https://www.graphviz.org/pdf/dotguide.pdf ).

    To get my code to compile I have to escape the "\l" like this "\l". I noticed that the output ended up with the escape being retained, meaning it would look this like in the resultant DOT file "\l" - which would then make the DOT file not be correct.

    I found that on line 368 of "graph.go", if I changed the second %q to a %s and added some quotes, then it worked. Like this:

    fmt.Fprintf(b, "%s="%s"", k, m[k])

    Like I say, I only started using GoLang yesterday, so there might be a better way to prevent the escape character from appearing.

    Thanks.

    opened by kevin-peirce 6
  • Mutually exclusive GraphTypeOption

    Mutually exclusive GraphTypeOption

    Currently the options such as strict and digraph are mutually exclusive

    https://github.com/emicklei/dot/blob/d7ab2356e483e86e86f12c8ca8e8e73dfcaa35ea/graph_options.go#L24-L26

    So something like this won't work

    g := dot.NewGraph(dot.Directed, dot.Strict)
    

    And this would fail to render from graphviz v2.38 from here

    since the output looks like

    strict  {
            n1[label="Hello"];
            n2[label="World"];
            n1->n2;
    }
    

    Code

    package main
    
    import (
    	"fmt"
    	"github.com/emicklei/dot"
    )
    
    func main() {
    	g := dot.NewGraph(dot.Directed, dot.Strict)
    	n1 := g.Node("Hello")
    	n2 := g.Node("World")
    
    	g.Edge(n1, n2)
    
    	fmt.Println(g.String())
    }
    
    opened by sudhackar 3
  • Using label as ID

    Using label as ID

    In DOT language, the ID is something apart from the label attribute. You are indexing the map by labels instead of by IDs.

    This makes us unable to create subgraphs with a same label (but different IDs).

    Solution

    Probably you should be indexing your g.subgraphs by g.ID instead of g.Attr("label")

    opened by saantiaguilera 3
  • Added a function to delete a Node and its associated Edges

    Added a function to delete a Node and its associated Edges

    Added a function and its corresponding Test. Tests pass 100%. I need it for my project, is it ok for you to integrate it in your lib ?

    A fellow golang dev, Best

    opened by EwenQuim 2
  • Introduce Literal attribute type

    Introduce Literal attribute type

    RawString allows clients to circumvent golang's escape logic.

    Before this commit, it was impossible to left- or right-justify text, as the escape sequences \l and \r were escaped to \l and \r by printf("%q").

    opened by luksa 2
  • Node attributes affect all nodes

    Node attributes affect all nodes

    Based on the example in the readme, I switched the order of defining n1 and n2. I expected n2 to be a box, while n1 remains as an oval, but actually both will be rendered as boxes.

    func main() {
    	g := dot.NewGraph(dot.Directed)
    	n2 := g.Node("testing a little").Box()
    	n1 := g.Node("coding")
    
    	g.Edge(n1, n2)
    	g.Edge(n2, n1, "back").Attr("color", "red")
    
    	fmt.Println(g.String())
    }
    
    opened by caesarxuchao 2
  • add edge connection to specific port of node functionality

    add edge connection to specific port of node functionality

    Feature: as described in Graphviz examples here https://renenyffenegger.ch/notes/tools/Graphviz/examples/index we can connect edge to specific node port But current module api are not able to do it

    I added some code for enable this ability

    opened by v-electrolux 1
  • Add support for HTML attributes.

    Add support for HTML attributes.

    Some attributes, like the label attribute, allow you to use HTML to style them. HTML strings are enclosed in angle brackets rather than quotes.

    This PR implements support for this.

    opened by twitchyliquid64 1
  • Tests are failing

    Tests are failing

    === RUN   TestEmpty
    --- PASS: TestEmpty (0.00s)
    === RUN   TestEmptyWithIDAndAttributes
    --- PASS: TestEmptyWithIDAndAttributes (0.00s)
    === RUN   TestTwoConnectedNodes
    --- FAIL: TestTwoConnectedNodes (0.00s)
        graph_test.go:34: got [digraph  {n1[label="A"];n2[label="B"];n1->n2;}] want [digraph  {node[label="A"] n1;node[label="B"] n2;n1->n2;}]
    === RUN   TestGraph_FindEdges
    --- PASS: TestGraph_FindEdges (0.00s)
    === RUN   TestSubgraph
    --- PASS: TestSubgraph (0.00s)
    === RUN   TestSubgraphClusterOption
    --- PASS: TestSubgraphClusterOption (0.00s)
    === RUN   TestEdgeLabel
    --- FAIL: TestEdgeLabel (0.00s)
        graph_test.go:72: got [digraph  {n1[label="n1"];n2[label="n2"];n1->n2[label="wat"];}] want [digraph  {node[label="n1"] n1;node[label="n2"] n2;n1->n2[label="wat"];}]
    === RUN   TestCluster
    --- PASS: TestCluster (0.00s)
    === RUN   TestIndentWriter
    --- PASS: TestIndentWriter (0.00s)
    FAIL
    FAIL	github.com/emicklei/dot	0.119s
    
    Process finished with exit code 1
    

    The generated charts seem to be valid though. So just forgot updating test cases in #2 ? I'd recommend integrating Travis CI to get early feedback if tests fail. It's available in the GitHub Marketplace.

    opened by matfax 1
  • feat: FindEdges and EdgesTo functions

    feat: FindEdges and EdgesTo functions

    Before that, I couldn't find a way to check if a graph already contains an edge or not. If you don't want duplicate edges, FindEdges gives the possibility to check beforehand if an edge is necessary or a duplicate.

    opened by matfax 1
Owner
Ernest Micklei
GCP Architect, Software Engineer, Google Developer Expert, Gopher, Dartian, Javanian, Smalltalker
Ernest Micklei
Slice graph uses graphviz in order to pretty print slices for you.

slicegraph Slice graph uses graphviz in order to make you understand what happens underneath your slices.

Jędrzej 34 Mar 7, 2022
HSM package provides a simple state chart library written in Go.

UML HSM HSM package provides a simple state chart library written in Go. Supported UML State Chart Features Feature Implemented Test case Simple state

Chris Castro 1 Apr 14, 2022
Ltree Visualizer - A golang library to visualize postgres ltree type data using DOT language and Graphviz

Ltree Visualizer A golang library to visualize postgres ltree type data using DOT language and Graphviz What is Ltree? Ltree is a data type which is u

Vasubabu Jinagam 8 Jun 12, 2022
Parses the Graphviz DOT language in golang

Parses the Graphviz DOT language and creates an interface, in golang, with which to easily create new and manipulate existing graphs which can be writ

Walter Schulze 484 Jun 25, 2022
Converts 'go mod graph' output into Graphviz's DOT language

modgv Converts 'go mod graph' output into GraphViz's DOT language. takes no options or arguments it reads the output generated by “go mod graph” on st

Luca Sepe 407 Jun 24, 2022
Parses the Graphviz DOT language in golang

Parses the Graphviz DOT language and creates an interface, in golang, with which to easily create new and manipulate existing graphs which can be writ

Walter Schulze 484 Jun 25, 2022
The kprobe package allows construction of dynamic struct based on kprobe event format descriptions.

The kprobe package allows construction of dynamic struct based on kprobe event format descriptions.

Dan Kortschak 4 Oct 27, 2021
Project to get Youtube video descriptions and search those videos as required

FamPayProject Project to get Youtube video descriptions and search those videos as required Prerequisities Postgres DB for persisting data Youtube Dat

null 0 Nov 5, 2021
brewfile-desc add descriptions of formulae to Brewfile.

brewfile-desc brewfile-desc add descriptions of formulae to Brewfile. Usage $ cat path/to/Brewfile tap "golangci/tap" tap "k1low/tap" tap "mas-cli/tap

Ken’ichiro Oyama 4 Mar 11, 2022
Monkey programming language project from 'Writing An Interpreter In Go'and 'Writing A Compiler In Go' Books

Monkey Monkey programming language ?? project from "Writing An Interpreter In Go

Amr Hesham 1 Dec 16, 2021
Visualize call graph of a Go program using Graphviz

go-callvis go-callvis is a development tool to help visualize call graph of a Go program using interactive view. Introduction The purpose of this tool

Ondrej Fabry 4.2k Jun 29, 2022
Visualize call graph of a Go program using Graphviz

go-callvis go-callvis is a development tool to help visualize call graph of a Go program using interactive view. Introduction The purpose of this tool

Ondrej Fabry 4.1k Jun 22, 2022
Visualize your Go data structures using graphviz

memviz How would you rather debug a data structure? "Pretty" printed Visual graph (*test.fib)(0xc04204a5a0)({ index: (int) 5, prev: (*test.fib)(0xc0

Bradley Kemp 1.3k Jun 15, 2022
GoLobby DotEnv is a lightweight package for loading dot env (.env) files into structs for Go projects

DotEnv GoLobby DotEnv is a lightweight package for loading dot env (.env) files into structs for Go projects Documentation Supported Versions It requi

GoLobby 12 Apr 5, 2022
Access and modify property values in deeply nested maps, using dot-separated paths

Dig lets you access and modify property values in deeply nested, unstructured maps, using dot-separated paths: source := make(map[string]interface{})

Preslav Rachev 12 May 7, 2022
Slice graph uses graphviz in order to pretty print slices for you.

slicegraph Slice graph uses graphviz in order to make you understand what happens underneath your slices.

Jędrzej 34 Mar 7, 2022
An ease to use finit state machine golang implementation.Turn any struct to a fsm with graphviz visualization supported.

go-fsm An ease to use finit state machine golang implementation.Turn any struct to a fsm with graphviz visualization supported. usage import github.co

FingerLiu 5 Dec 26, 2021
Decode / encode XML to/from map[string]interface{} (or JSON); extract values with dot-notation paths and wildcards. Replaces x2j and j2x packages.

mxj - to/from maps, XML and JSON Decode/encode XML to/from map[string]interface{} (or JSON) values, and extract/modify values from maps by key or key-

Charles Banning 521 Jun 28, 2022
Decode / encode XML to/from map[string]interface{} (or JSON); extract values with dot-notation paths and wildcards. Replaces x2j and j2x packages.

mxj - to/from maps, XML and JSON Decode/encode XML to/from map[string]interface{} (or JSON) values, and extract/modify values from maps by key or key-

Charles Banning 520 Jun 19, 2022
A tiny command line DNS client with support for UDP, DoT, DoH, and DoQ.

q A tiny command line DNS client with support for UDP, DoT, DoH, and DoQ. Usage q command line DNS client (https://github.com/natesales/q) Usage: q

Nate Sales 665 Jun 21, 2022
🔄 Maintain a local copy of you dot files

Dotup This command line is in charge of maintaining a local copy of you dot files from a <source> folder. It copies all the files in the <source> fold

Kevin Disneur 2 Oct 20, 2021
DNS/DoT to DoH proxy with load-balancing, fail-over and SSL certificate management

dns-proxy Configuration Variable Example Description TLS_DOMAIN my.duckdns.org Domain name without wildcards. Used to create wildcard certificate and

Dimitri Herzog 3 Nov 23, 2021
K8s-dotenv - Kubernetes Secrets and Configmaps to dot env file

k8s-dotenv A commandline tool to fetch, merge and convert secrets and config map

Sami Khan 3 Apr 22, 2022
The android-go project provides a platform for writing native Android apps in Go programming language.

android-go The android-go project aims to provide a platform (namely an SDK) for writing native Android apps in Go programming language. All things he

Max Kupriianov 972 Jun 16, 2022
Assembly syntax that makes you feel like you're writing code in a high-level language.

shasm Assembly syntax that makes you feel like you're writing code in a high-level language. Shasm is not an Assembler. Shasm simply compiles Shasm sy

Shoyaaa 14 Jun 5, 2021
Logger - Go language is interface-oriented to implement an asynchronous log writing program

logger日志库 1、安装 go get github.com/staryjie/[email protected] 2、使用 示例: package main import ( "github.com/staryjie/logger" "time" ) func initLogger(name,

StaryJie 0 Jan 4, 2022
Package kml provides convenince methods for creating and writing KML documents.

go-kml Package kml provides convenience methods for creating and writing KML documents. Key Features Simple API for building arbitrarily complex KML d

Tom Payne 67 May 31, 2022
Package raw enables reading and writing data at the device driver level for a network interface. MIT Licensed.

raw Package raw enables reading and writing data at the device driver level for a network interface. MIT Licensed. For more information about using ra

Matt Layher 421 Jun 15, 2022
Package for writing Nagios/Icinga/et cetera plugins in Go (golang)

nagiosplugin Package for writing Nagios/Icinga/et cetera plugins in Go (golang). Documentation See http://godoc.org/github.com/olorin/nagiosplugin. Us

Sharif Olorin 69 Apr 4, 2022