graphviz

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: MIT Imports: 8 Imported by: 0

README

go-graphviz Go GoDoc

Go bindings for Graphviz

Features

Graphviz version is here

  • Pure Go Library
  • No need to install Graphviz library ( brew install graphviz or apt-get install graphviz )
    • The Graphviz library has been converted to WebAssembly (WASM) and embedded it, so it works consistently across all environments
  • Supports encoding/decoding for DOT language
  • Supports custom renderer for custom format
  • Supports setting graph properties in a type-safe manner

Supported Layout

circo dot fdp neato nop nop1 nop2 osage patchwork sfdp twopi

Supported Format

dot svg png jpg

The above are the formats supported by default. You can also add custom formats.

Installation

$ go get github.com/oneid/go-graphviz

Synopsis

1. Write DOT Graph in Go

package main

import (
  "bytes"
  "context"
  "fmt"
  "log"

  "github.com/oneid/go-graphviz"
)

func main() {
  ctx := context.Background()
  g, err := graphviz.New(ctx)
  if err != nil { panic(err )}

  graph, err := g.Graph()
  if err != nil { panic(err) }
  defer func() {
    if err := graph.Close(); err != nil { panic(err) }
    g.Close()
  }()
  n, err := graph.CreateNodeByName("n")
  if err != nil { panic(err) }

  m, err := graph.CreateNodeByName("m")
  if err != nil { panic(err) }

  e, err := graph.CreateEdgeByName("e", n, m)
  if err != nil { panic(err) }
  e.SetLabel("e")

  var buf bytes.Buffer
  if err := g.Render(ctx, graph, "dot", &buf); err != nil {
    log.Fatal(err)
  }
  fmt.Println(buf.String())
}

2. Parse DOT Graph

path := "/path/to/dot.gv"
b, err := os.ReadFile(path)
if err != nil { panic(err) }
graph, err := graphviz.ParseBytes(b)

3. Render Graph

ctx := context.Background()
g, err := graphviz.New(ctx)
if err != nil { panic(err) }

graph, err := g.Graph()
if err != nil { panic(err) }

// create your graph

// 1. write encoded PNG data to buffer
var buf bytes.Buffer
if err := g.Render(ctx, graph, graphviz.PNG, &buf); err != nil { panic(err) }

// 2. get as image.Image instance
image, err := g.RenderImage(ctx, graph)
if err != nil { panic(err) }

// 3. write to file directly
if err := g.RenderFilename(ctx, graph, graphviz.PNG, "/path/to/graph.png"); err != nil { panic(err) }

Tool

dot

Installation
$ go install github.com/oneid/go-graphviz/cmd/dot@latest
Usage
Usage:
  dot [OPTIONS]

Application Options:
  -T=         specify output format ( currently supported: dot svg png jpg ) (default: dot)
  -K=         specify layout engine ( currently supported: circo dot fdp neato nop nop1 nop2 osage patchwork sfdp twopi )
  -o=         specify output file name

Help Options:
  -h, --help  Show this help message

How it works

  1. Generates bindings between Go and C from Protocol Buffers file.
  2. Builds graphviz.wasm on the docker container.
  3. Uses Graphviz functionality from a sub-packages ( cdt cgraph gvc ) via the internal/wasm package.
  4. graphviz package provides facade interface for all sub packages.

License

MIT

This library embeds and uses graphviz.wasm, which is generated based on the original source code of Graphviz. Therefore, the graphviz.wasm follows the license adopted by Graphviz ( Eclipse Public License ).

Documentation

Index

Constants

View Source
const (
	NormalArrow   = cgraph.NormalArrow
	InvArrow      = cgraph.InvArrow
	DotArrow      = cgraph.DotArrow
	InvDotArrow   = cgraph.InvDotArrow
	ODotArrow     = cgraph.ODotArrow
	InvODotArrow  = cgraph.InvODotArrow
	NoneArrow     = cgraph.NoneArrow
	TeeArrow      = cgraph.TeeArrow
	EmptyArrow    = cgraph.EmptyArrow
	InvEmptyArrow = cgraph.InvEmptyArrow
	DiamondArrow  = cgraph.DiamondArrow
	ODiamondArrow = cgraph.ODiamondArrow
	EDiamondArrow = cgraph.EDiamondArrow
	CrowArrow     = cgraph.CrowArrow
	BoxArrow      = cgraph.BoxArrow
	OBoxArrow     = cgraph.OBoxArrow
	OpenArrow     = cgraph.OpenArrow
	HalfOpenArrow = cgraph.HalfOpenArrow
	VeeArrow      = cgraph.VeeArrow
)

const variables from cgraph package.

View Source
const (
	LocalCluster  = cgraph.LocalCluster
	GlobalCluster = cgraph.GlobalCluster
	NoneCluster   = cgraph.NoneCluster
)
View Source
const (
	ForwardDir = cgraph.ForwardDir
	BackDir    = cgraph.BackDir
	BothDir    = cgraph.BothDir
	NoneDir    = cgraph.NoneDir
)
View Source
const (
	TopLeftPos        = cgraph.TopLeftPos
	TopCenteredPos    = cgraph.TopCenteredPos
	TopRightPos       = cgraph.TopRightPos
	MiddleLeftPos     = cgraph.MiddleLeftPos
	MiddleCenteredPos = cgraph.MiddleCenteredPos
	BottomLeftPos     = cgraph.BottomLeftPos
	BottomCenteredPos = cgraph.BottomCenteredPos
	BottomRightPos    = cgraph.BottomRightPos
)
View Source
const (
	LeftJust     = cgraph.LeftJust
	CenteredJust = cgraph.CenteredJust
	RightJust    = cgraph.RightJust
)
View Source
const (
	TopLocation      = cgraph.TopLocation
	CenteredLocation = cgraph.CenteredLocation
	BottomLocation   = cgraph.BottomLocation
)
View Source
const (
	MajorMode  = cgraph.MajorMode
	KKMode     = cgraph.KKMode
	HierMode   = cgraph.HierMode
	IpsepMode  = cgraph.IpsepMode
	SpringMode = cgraph.SpringMode
	MaxentMode = cgraph.MaxentMode
)
View Source
const (
	ShortPathModel = cgraph.ShortPathModel
	CircuitModel   = cgraph.CircuitModel
	SubsetModel    = cgraph.SubsetModel
	MdsModel       = cgraph.MdsModel
)
View Source
const (
	OutOrdering = cgraph.OutOrdering
	InOrdering  = cgraph.InOrdering
)
View Source
const (
	BreadthFirst = cgraph.BreadthFirst
	NodesFirst   = cgraph.NodesFirst
	EdgesFirst   = cgraph.EdgesFirst
)
View Source
const (
	NodePack    = cgraph.NodePack
	ClusterPack = cgraph.ClusterPack
	GraphPack   = cgraph.GraphPack
)
View Source
const (
	BLDir = cgraph.BLDir
	BRDir = cgraph.BRDir
	TLDir = cgraph.TLDir
	TRDir = cgraph.TRDir
	RBDir = cgraph.RBDir
	RTDir = cgraph.RTDir
	LBDir = cgraph.LBDir
	LTDir = cgraph.LTDir
)
View Source
const (
	NormalQuad = cgraph.NormalQuad
	FastQuad   = cgraph.FastQuad
	NoneQuad   = cgraph.NoneQuad
)
View Source
const (
	TBRank = cgraph.TBRank
	LRRank = cgraph.LRRank
	BTRank = cgraph.BTRank
	RLRank = cgraph.RLRank
)
View Source
const (
	FillRatio     = cgraph.FillRatio
	CompressRatio = cgraph.CompressRatio
	ExpandRatio   = cgraph.ExpandRatio
	AutoRatio     = cgraph.AutoRatio
)
View Source
const (
	BoxShape             = cgraph.BoxShape
	PolygonShape         = cgraph.PolygonShape
	EllipseShape         = cgraph.EllipseShape
	OvalShape            = cgraph.OvalShape
	CircleShape          = cgraph.CircleShape
	PointShape           = cgraph.PointShape
	EggShape             = cgraph.EggShape
	TriangleShape        = cgraph.TriangleShape
	PlainTextShape       = cgraph.PlainTextShape
	PlainShape           = cgraph.PlainShape
	DiamondShape         = cgraph.DiamondShape
	TrapeziumShape       = cgraph.TrapeziumShape
	ParallelogramShape   = cgraph.ParallelogramShape
	HouseShape           = cgraph.HouseShape
	PentagonShape        = cgraph.PentagonShape
	HexagonShape         = cgraph.HexagonShape
	SeptagonShape        = cgraph.SeptagonShape
	OctagonShape         = cgraph.OctagonShape
	DoubleCircleShape    = cgraph.DoubleCircleShape
	DoubleOctagonShape   = cgraph.DoubleOctagonShape
	TripleOctagonShape   = cgraph.TripleOctagonShape
	InvTriangleShape     = cgraph.InvTriangleShape
	InvTrapeziumShape    = cgraph.InvTrapeziumShape
	InvHouseShape        = cgraph.InvHouseShape
	MdiamondShape        = cgraph.MdiamondShape
	MsquareShape         = cgraph.MsquareShape
	McircleShape         = cgraph.McircleShape
	RectShape            = cgraph.RectShape
	RectangleShape       = cgraph.RectangleShape
	SquareShape          = cgraph.SquareShape
	StarShape            = cgraph.StarShape
	NoneShape            = cgraph.NoneShape
	UnderlineShape       = cgraph.UnderlineShape
	CylinderShape        = cgraph.CylinderShape
	NoteShape            = cgraph.NoteShape
	TabShape             = cgraph.TabShape
	FolderShape          = cgraph.FolderShape
	Box3DShape           = cgraph.Box3DShape
	ComponentShape       = cgraph.ComponentShape
	PromoterShape        = cgraph.PromoterShape
	CdsShape             = cgraph.CdsShape
	TerminatorShape      = cgraph.TerminatorShape
	UtrShape             = cgraph.UtrShape
	PrimersiteShape      = cgraph.PrimersiteShape
	RestrictionSiteShape = cgraph.RestrictionSiteShape
	FivePoverHangShape   = cgraph.FivePoverHangShape
	ThreePoverHangShape  = cgraph.ThreePoverHangShape
	NoverHangShape       = cgraph.NoverHangShape
	AssemblyShape        = cgraph.AssemblyShape
	SignatureShape       = cgraph.SignatureShape
	InsulatorShape       = cgraph.InsulatorShape
	RibositeShape        = cgraph.RibositeShape
	RnastabShape         = cgraph.RnastabShape
	ProteasesiteShape    = cgraph.ProteasesiteShape
	ProteinstabShape     = cgraph.ProteinstabShape
	RPromoterShape       = cgraph.RPromoterShape
	RArrowShape          = cgraph.RArrowShape
	LArrowShape          = cgraph.LArrowShape
	LPromoterShape       = cgraph.LPromoterShape
)
View Source
const (
	NoneSmooth      = cgraph.NoneSmooth
	AvgDistSmooth   = cgraph.AvgDistSmooth
	GraphDistSmooth = cgraph.GraphDistSmooth
	PowerDistSmooth = cgraph.PowerDistSmooth
	RngSmooth       = cgraph.RngSmooth
	SprintSmooth    = cgraph.SprintSmooth
	TriangleSmooth  = cgraph.TriangleSmooth
)
View Source
const (
	RegularStart = cgraph.RegularStart
	SelfStart    = cgraph.SelfStart
	RandomStart  = cgraph.RandomStart
)
View Source
const (
	SolidGraphStyle   = cgraph.SolidGraphStyle
	DashedGraphStyle  = cgraph.DashedGraphStyle
	DottedGraphStyle  = cgraph.DottedGraphStyle
	BoldGraphStyle    = cgraph.BoldGraphStyle
	RoundedGraphStyle = cgraph.RoundedGraphStyle
	FilledGraphStyle  = cgraph.FilledGraphStyle
	StripedGraphStyle = cgraph.StripedGraphStyle
)
View Source
const (
	SolidNodeStyle     = cgraph.SolidNodeStyle
	DashedNodeStyle    = cgraph.DashedNodeStyle
	DottedNodeStyle    = cgraph.DottedNodeStyle
	BoldNodeStyle      = cgraph.BoldNodeStyle
	RoundedNodeStyle   = cgraph.RoundedNodeStyle
	DiagonalsNodeStyle = cgraph.DiagonalsNodeStyle
	FilledNodeStyle    = cgraph.FilledNodeStyle
	StripedNodeStyle   = cgraph.StripedNodeStyle
	WedgedNodeStyle    = cgraph.WedgedNodeStyle
)
View Source
const (
	SolidEdgeStyle  = cgraph.SolidEdgeStyle
	DashedEdgeStyle = cgraph.DashedEdgeStyle
	DottedEdgeStyle = cgraph.DottedEdgeStyle
	BoldEdgeStyle   = cgraph.BoldEdgeStyle
)

Variables

View Source
var (
	Directed         = cgraph.Directed
	StrictDirected   = cgraph.StrictDirected
	UnDirected       = cgraph.UnDirected
	StrictUnDirected = cgraph.StrictUnDirected
)

variables from cgraph package.

View Source
var (
	ParseFile  = cgraph.ParseFile
	ParseBytes = cgraph.ParseBytes
)

functions from cgraph package.

View Source
var (
	SetFontLoader   = gvc.SetFontLoader
	DefaultPlugins  = gvc.DefaultPlugins
	DeviceQuality   = gvc.WithDeviceQuality
	DeviceFeatures  = gvc.WithDeviceFeatures
	DeviceDPI       = gvc.WithDeviceDPI
	NewDevicePlugin = gvc.NewDevicePlugin
	PNGDevicePlugin = gvc.PNGDevicePlugin
	JPGDevicePlugin = gvc.JPGDevicePlugin
	RenderQuality   = gvc.WithRenderQuality
	RenderFeatures  = gvc.WithRenderFeatures
	RenderColorType = gvc.WithRenderColorType
	RenderPAD       = gvc.WithRenderPAD
	NewRenderPlugin = gvc.NewRenderPlugin
	PNGRenderPlugin = gvc.PNGRenderPlugin
	JPGRenderPlugin = gvc.JPGRenderPlugin
)

functions from gvc package.

Functions

func SetFileSystem

func SetFileSystem(fs fs.FS)

Types

type ArrowType

type ArrowType = cgraph.ArrowType

types from cgraph package.

type Attribute

type Attribute = cgraph.Attr

types from cgraph package.

type CallbackStack

type CallbackStack = cgraph.CallbackStack

types from cgraph package.

type ClientDiscipline

type ClientDiscipline = cgraph.Disc

types from cgraph package.

type ClusterMode

type ClusterMode = cgraph.ClusterMode

types from cgraph package.

type Color

type Color = gvc.Color

types from gvc package.

type ColorType

type ColorType = gvc.ColorType

types from gvc package.

type CommonFields

type CommonFields = cgraph.CommonFields

types from cgraph package.

type Context

type Context = gvc.Context

types from gvc package.

type DataDict

type DataDict = cgraph.DataDict

types from cgraph package.

type DefaultRenderEngine

type DefaultRenderEngine = gvc.DefaultRenderEngine

types from gvc package.

type DeviceFeature

type DeviceFeature = gvc.DeviceFeature

types from gvc package.

type DevicePlugin

type DevicePlugin = gvc.DevicePlugin

types from gvc package.

type DevicePluginOption

type DevicePluginOption = gvc.DevicePluginOption

types from gvc package.

type Dict

type Dict = cdt.Dict

types from cdt package.

type DictData

type DictData = cdt.Data

types from cdt package.

type DictDisc

type DictDisc = cdt.Disc

types from cdt package.

type DictHold

type DictHold = cdt.Hold

types from cdt package.

type DictLink = cdt.Link

types from cdt package.

type DictMethod

type DictMethod = cdt.Method

types from cdt package.

type DictStat

type DictStat = cdt.Stat

types from cdt package.

type DirType

type DirType = cgraph.DirType

types from cgraph package.

type Edge

type Edge = cgraph.Edge

types from cgraph package.

type EdgeStyle

type EdgeStyle = cgraph.EdgeStyle

types from cgraph package.

type FillType

type FillType = gvc.FillType

types from gvc package.

type Format

type Format string
const (
	XDOT Format = "dot"
	SVG  Format = "svg"
	PNG  Format = "png"
	JPG  Format = "jpg"
)

type Graph

type Graph = cgraph.Graph

types from cgraph package.

type GraphDescriptor

type GraphDescriptor = cgraph.Desc

types from cgraph package.

type GraphOption

type GraphOption func(g *Graphviz)

func WithDirectedType

func WithDirectedType(desc *GraphDescriptor) GraphOption

func WithName

func WithName(name string) GraphOption

type GraphStyle

type GraphStyle = cgraph.GraphStyle

types from cgraph package.

type Graphviz

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

func New

func New(ctx context.Context) (*Graphviz, error)

func NewWithPlugins

func NewWithPlugins(ctx context.Context, plugins ...Plugin) (*Graphviz, error)

func (*Graphviz) Close

func (g *Graphviz) Close() error

func (*Graphviz) Graph

func (g *Graphviz) Graph(option ...GraphOption) (*Graph, error)

func (*Graphviz) Render

func (g *Graphviz) Render(ctx context.Context, graph *Graph, format Format, w io.Writer) (e error)

func (*Graphviz) RenderFilename

func (g *Graphviz) RenderFilename(ctx context.Context, graph *Graph, format Format, path string) (e error)

func (*Graphviz) RenderImage

func (g *Graphviz) RenderImage(ctx context.Context, graph *Graph) (img image.Image, e error)

func (*Graphviz) SetLayout

func (g *Graphviz) SetLayout(layout Layout) *Graphviz

type ID

type ID = cgraph.ID

types from cgraph package.

type ImagePos

type ImagePos = cgraph.ImagePos

types from cgraph package.

type Job

type Job = gvc.Job

types from gvc package.

type JustType

type JustType = cgraph.JustType

types from cgraph package.

type LabelLocation

type LabelLocation = cgraph.LabelLocation

types from cgraph package.

type LabelType

type LabelType = gvc.LabelType

types from gvc package.

type Layout

type Layout string
const (
	CIRCO     Layout = "circo"
	DOT       Layout = "dot"
	FDP       Layout = "fdp"
	NEATO     Layout = "neato"
	NOP       Layout = "nop"
	NOP1      Layout = "nop1"
	NOP2      Layout = "nop2"
	OSAGE     Layout = "osage"
	PATCHWORK Layout = "patchwork"
	SFDP      Layout = "sfdp"
	TWOPI     Layout = "twopi"
)

type ModeType

type ModeType = cgraph.ModeType

types from cgraph package.

type ModelType

type ModelType = cgraph.ModelType

types from cgraph package.

type Node

type Node = cgraph.Node

types from cgraph package.

type NodeStyle

type NodeStyle = cgraph.NodeStyle

types from cgraph package.

type Object

type Object = cgraph.Object

types from cgraph package.

type ObjectState

type ObjectState = gvc.ObjectState

types from gvc package.

type ObjectTag

type ObjectTag = cgraph.ObjectTag

types from cgraph package.

type OrderingType

type OrderingType = cgraph.OrderingType

types from cgraph package.

type OutputMode

type OutputMode = cgraph.OutputMode

types from cgraph package.

type PackMode

type PackMode = cgraph.PackMode

types from cgraph package.

type PageDir

type PageDir = cgraph.PageDir

types from cgraph package.

type PenType

type PenType = gvc.PenType

types from gvc package.

type Plugin

type Plugin = gvc.Plugin

types from gvc package.

type PointFloat

type PointFloat = gvc.PointFloat

types from gvc package.

type PostScriptAlias

type PostScriptAlias = gvc.PostScriptAlias

types from gvc package.

type QuadType

type QuadType = cgraph.QuadType

types from cgraph package.

type RankDir

type RankDir = cgraph.RankDir

types from cgraph package.

type RatioType

type RatioType = cgraph.RatioType

types from cgraph package.

type Record

type Record = cgraph.Record

types from cgraph package.

type RenderEngine

type RenderEngine = gvc.RenderEngine

types from gvc package.

type RenderFeature

type RenderFeature = gvc.RenderFeature

types from gvc package.

type RenderPlugin

type RenderPlugin = gvc.RenderPlugin

types from gvc package.

type RenderPluginOption

type RenderPluginOption = gvc.RenderPluginOption

types from gvc package.

type Scale

type Scale = gvc.Scale

types from gvc package.

type Shape

type Shape = cgraph.Shape

types from cgraph package.

type SmoothType

type SmoothType = cgraph.SmoothType

types from cgraph package.

type StartType

type StartType = cgraph.StartType

types from cgraph package.

type State

type State = cgraph.State

types from cgraph package.

type SubNode

type SubNode = cgraph.SubNode

types from cgraph package.

type Symbol

type Symbol = cgraph.Symbol

types from cgraph package.

type Tag

type Tag = cgraph.Tag

types from cgraph package.

type TextFont

type TextFont = gvc.TextFont

types from gvc package.

type TextSpan

type TextSpan = gvc.TextSpan

types from gvc package.

type Translation

type Translation = gvc.Translation

types from gvc package.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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