graphviz

package module
v0.0.0-...-7802999 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2019 License: MIT Imports: 3 Imported by: 1

Documentation

Overview

Package graphviz provides an easy-to-use API for visualizing graphs using Graphviz (http://graphviz.org). It can generate graph descriptions in DOT language which can be made into a picture using various Graphviz tools such as dot, neato, ...

Example (LinkedList)

This example shows how Graph can be used to display a simple linked list. The output can be piped to the dot tool to generate an image.

package main

import (
	"os"

	"github.com/mzohreva/GoGraphviz/graphviz"
)

func main() {
	G := graphviz.Graph{}
	G.MakeDirected()
	n1 := G.AddNode("Hello")
	n2 := G.AddNode("World")
	n3 := G.AddNode("Hi")
	n4 := G.AddNode("NULL")
	G.AddEdge(n1, n2, "next")
	G.AddEdge(n2, n3, "next")
	G.AddEdge(n3, n4, "next")
	G.MakeSameRank(n1, n2, n3, n4)

	G.GraphAttribute(graphviz.NodeSep, "0.5")

	G.DefaultNodeAttribute(graphviz.Shape, graphviz.ShapeBox)
	G.DefaultNodeAttribute(graphviz.FontName, "Courier")
	G.DefaultNodeAttribute(graphviz.FontSize, "14")
	G.DefaultNodeAttribute(graphviz.Style, graphviz.StyleFilled+","+graphviz.StyleRounded)
	G.DefaultNodeAttribute(graphviz.FillColor, "yellow")

	G.NodeAttribute(n4, graphviz.Shape, graphviz.ShapeCircle)
	G.NodeAttribute(n4, graphviz.Style, graphviz.StyleDashed)

	G.DefaultEdgeAttribute(graphviz.FontName, "Courier")
	G.DefaultEdgeAttribute(graphviz.FontSize, "12")

	G.GenerateDOT(os.Stdout)
}
Output:

strict digraph {
  nodesep = "0.5";
  node [ shape = "box" ]
  node [ fontname = "Courier" ]
  node [ fontsize = "14" ]
  node [ style = "filled,rounded" ]
  node [ fillcolor = "yellow" ]
  edge [ fontname = "Courier" ]
  edge [ fontsize = "12" ]
  n0 [label="Hello"]
  n1 [label="World"]
  n2 [label="Hi"]
  n3 [label="NULL", shape="circle", style="dashed"]
  {rank=same; n0; n1; n2; n3; }
  n0 -> n1 [label="next"]
  n1 -> n2 [label="next"]
  n2 -> n3 [label="next"]
}

Index

Examples

Constants

View Source
const (
	Shape     = "shape"
	Style     = "style"
	Color     = "color"
	FillColor = "fillcolor"
	FontName  = "fontname"
	FontSize  = "fontsize"
	FontColor = "fontcolor"
	NodeSep   = "nodesep"
)

Common attribute names

View Source
const (
	ShapeBox       = "box"
	ShapePolygon   = "polygon"
	ShapeEllipse   = "ellipse"
	ShapeOval      = "oval"
	ShapeCircle    = "circle"
	ShapePoint     = "point"
	ShapeEgg       = "egg"
	ShapeTriangle  = "triangle"
	ShapeNone      = "none"
	ShapeDiamond   = "diamond"
	ShapeRectangle = "rectangle"
	ShapeSquare    = "square"
)

Common values for shape attribute

View Source
const (
	StyleSolid   = "solid"
	StyleDashed  = "dashed"
	StyleDotted  = "dotted"
	StyleBold    = "bold"
	StyleRounded = "rounded"
	StyleFilled  = "filled"
	StyleStriped = "striped"
)

Common values for style attribute

Variables

This section is empty.

Functions

This section is empty.

Types

type Graph

type Graph struct {
	// contains filtered or unexported fields
}

Graph represents a set of nodes, edges and attributes that can be translated to DOT language.

func (*Graph) AddEdge

func (g *Graph) AddEdge(from, to int, label string) int

AddEdge adds a new edge between the given nodes with the specified label and returns an id for the new edge.

func (*Graph) AddNode

func (g *Graph) AddNode(label string) int

AddNode adds a new node with the given label and returns its id.

func (*Graph) DefaultEdgeAttribute

func (g *Graph) DefaultEdgeAttribute(name, value string)

DefaultEdgeAttribute sets an attribute for all edges by default.

func (*Graph) DefaultNodeAttribute

func (g *Graph) DefaultNodeAttribute(name, value string)

DefaultNodeAttribute sets an attribute for all nodes by default.

func (*Graph) DrawMultipleEdges

func (g *Graph) DrawMultipleEdges()

DrawMultipleEdges causes multiple edges between same pair of nodes to be drawn separately. By default, for a given pair of nodes, only the edge that was last added to the graph is drawn.

func (*Graph) EdgeAttribute

func (g *Graph) EdgeAttribute(id int, name, value string)

EdgeAttribute sets an attribute for the specified edge.

func (Graph) GenerateDOT

func (g Graph) GenerateDOT(w io.Writer)

GenerateDOT generates the graph description in DOT language

func (Graph) GenerateImage

func (g Graph) GenerateImage(tool, filename, filetype string) error

GenerateImage runs a Graphviz tool such as dot or neato to generate an image of the graph. filetype can be any file type supported by the tool, e.g. png or svg

func (*Graph) GraphAttribute

func (g *Graph) GraphAttribute(name, value string)

GraphAttribute sets an attribute for the graph

func (*Graph) MakeDirected

func (g *Graph) MakeDirected()

MakeDirected makes the graph a directed graph. By default, a new graph is undirected

func (*Graph) MakeSameRank

func (g *Graph) MakeSameRank(node1, node2 int, others ...int)

MakeSameRank causes the specified nodes to be drawn on the same rank. Only effective when using the dot tool.

func (*Graph) NodeAttribute

func (g *Graph) NodeAttribute(id int, name, value string)

NodeAttribute sets an attribute for the specified node.

func (*Graph) SetTitle

func (g *Graph) SetTitle(title string)

SetTitle sets a title for the graph.

Jump to

Keyboard shortcuts

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