An open API service indexing awesome lists of open source software.

https://github.com/windler/dotgraph

Go package to create and render graphviz dot graphs
https://github.com/windler/dotgraph

dot go golang graphviz graphviz-dot png render

Last synced: 6 months ago
JSON representation

Go package to create and render graphviz dot graphs

Awesome Lists containing this project

README

          

[![Go Report Card](https://goreportcard.com/badge/github.com/windler/dotgraph)](https://goreportcard.com/report/github.com/windler/dotgraph) [![CircleCI](https://circleci.com/gh/windler/dotgraph.svg?style=svg)](https://circleci.com/gh/windler/dotgraph) [![codebeat badge](https://codebeat.co/badges/ea2758da-b3f2-4de0-a051-effee8d9d499)](https://codebeat.co/projects/github-com-windler-dotgraph-master)

# dotgraph
`dotgraph` is a package that lets you create and render [graphviz dot graphs](https://www.graphviz.org/).

## Installation
```bash
go get github.com/windler/dotgraph/graph
go get github.com/windler/dotgraph/renderer
```

If you want to render graphs make sure you have [graphviz](https://www.graphviz.org/) installed.

## Usage
### Creating a new graph
```go
graph := graph.New("my_graph")
```

### Adding nodes
```go
graph.AddNode("first node")
graph.AddNode("second node")
graph.AddNode("third node")
```
### Adding directed edges
```go
graph.AddDirectedEdge("first node", "second node", "edge label")
```

### Assiging dot graph attributes
You can apply any [dot attributes](https://graphviz.gitlab.io/_pages/doc/info/attrs.html).

#### Graph attributes
```go
graph.SetGraphOptions(dotgraph.DotGraphOptions{
"bgcolor": "#333333",
})
```

#### Global node attributes
```go
graph.SetNodeGraphOptions(dotgraph.DotGraphOptions{
"fillcolor": "#336699",
"style": "filled",
"fontcolor": "white",
"fontname": "Courier",
"shape": "rectangle",
})
```

#### Node attributes for nodes
If you want to assign attributes for nodes matching a pattern you can do the following:

```go
graph.AddNodeGraphPatternOptions("first", dotgraph.DotGraphOptions{
"shape": "oval",
})
```

#### Global edge attributes
```go
graph.SetEdgeGraphOptions(dotgraph.DotGraphOptions{
"arrowhead": "open",
"color": "white",
"fontcolor": "white",
"splines": "curved",
})
```

#### Edge attributes
If you want to assign attributes for an edge that references a certain node you can do the following:

```go
graph.AddEdgeGraphPatternOptions("first", dotgraph.DotGraphOptions{
"color": "black",
})
```

### Render a graph
```go
r := &renderer.PNGRenderer{
OutputFile: "/tmp/my_graph.png",
}

r.Render(graph.String())
```