graphviz

package module
v0.2.9 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2024 License: MIT Imports: 8 Imported by: 181

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/goccy/go-graphviz

Synopsis

1. Write DOT Graph in Go

package main

import (
  "bytes"
  "fmt"
  "log"

  "github.com/goccy/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/goccy/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 added in v0.2.3

func SetFileSystem(fs fs.FS)

Types

type ArrowType added in v0.2.0

type ArrowType = cgraph.ArrowType

types from cgraph package.

type Attribute added in v0.2.0

type Attribute = cgraph.Attr

types from cgraph package.

type CallbackStack added in v0.2.0

type CallbackStack = cgraph.CallbackStack

types from cgraph package.

type ClientDiscipline added in v0.2.0

type ClientDiscipline = cgraph.Disc

types from cgraph package.

type ClusterMode added in v0.2.0

type ClusterMode = cgraph.ClusterMode

types from cgraph package.

type Color added in v0.2.0

type Color = gvc.Color

types from gvc package.

type ColorType added in v0.2.0

type ColorType = gvc.ColorType

types from gvc package.

type CommonFields added in v0.2.0

type CommonFields = cgraph.CommonFields

types from cgraph package.

type Context added in v0.2.0

type Context = gvc.Context

types from gvc package.

type DataDict added in v0.2.0

type DataDict = cgraph.DataDict

types from cgraph package.

type DefaultRenderEngine added in v0.2.0

type DefaultRenderEngine = gvc.DefaultRenderEngine

types from gvc package.

type DeviceFeature added in v0.2.0

type DeviceFeature = gvc.DeviceFeature

types from gvc package.

type DevicePlugin added in v0.2.0

type DevicePlugin = gvc.DevicePlugin

types from gvc package.

type DevicePluginOption added in v0.2.0

type DevicePluginOption = gvc.DevicePluginOption

types from gvc package.

type Dict added in v0.2.0

type Dict = cdt.Dict

types from cdt package.

type DictData added in v0.2.0

type DictData = cdt.Data

types from cdt package.

type DictDisc added in v0.2.0

type DictDisc = cdt.Disc

types from cdt package.

type DictHold added in v0.2.0

type DictHold = cdt.Hold

types from cdt package.

type DictLink = cdt.Link

types from cdt package.

type DictMethod added in v0.2.0

type DictMethod = cdt.Method

types from cdt package.

type DictStat added in v0.2.0

type DictStat = cdt.Stat

types from cdt package.

type DirType added in v0.2.0

type DirType = cgraph.DirType

types from cgraph package.

type Edge added in v0.2.0

type Edge = cgraph.Edge

types from cgraph package.

type EdgeStyle added in v0.2.0

type EdgeStyle = cgraph.EdgeStyle

types from cgraph package.

type FillType added in v0.2.0

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 added in v0.2.0

type Graph = cgraph.Graph

types from cgraph package.

type GraphDescriptor added in v0.2.0

type GraphDescriptor = cgraph.Desc

types from cgraph package.

type GraphOption

type GraphOption func(g *Graphviz)

func WithDirectedType added in v0.2.0

func WithDirectedType(desc *GraphDescriptor) GraphOption

func WithName added in v0.2.0

func WithName(name string) GraphOption

type GraphStyle added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

type ID = cgraph.ID

types from cgraph package.

type ImagePos added in v0.2.0

type ImagePos = cgraph.ImagePos

types from cgraph package.

type Job added in v0.2.0

type Job = gvc.Job

types from gvc package.

type JustType added in v0.2.0

type JustType = cgraph.JustType

types from cgraph package.

type LabelLocation added in v0.2.0

type LabelLocation = cgraph.LabelLocation

types from cgraph package.

type LabelType added in v0.2.0

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 added in v0.2.0

type ModeType = cgraph.ModeType

types from cgraph package.

type ModelType added in v0.2.0

type ModelType = cgraph.ModelType

types from cgraph package.

type Node added in v0.2.0

type Node = cgraph.Node

types from cgraph package.

type NodeStyle added in v0.2.0

type NodeStyle = cgraph.NodeStyle

types from cgraph package.

type Object added in v0.2.0

type Object = cgraph.Object

types from cgraph package.

type ObjectState added in v0.2.0

type ObjectState = gvc.ObjectState

types from gvc package.

type ObjectTag added in v0.2.0

type ObjectTag = cgraph.ObjectTag

types from cgraph package.

type OrderingType added in v0.2.0

type OrderingType = cgraph.OrderingType

types from cgraph package.

type OutputMode added in v0.2.0

type OutputMode = cgraph.OutputMode

types from cgraph package.

type PackMode added in v0.2.0

type PackMode = cgraph.PackMode

types from cgraph package.

type PageDir added in v0.2.0

type PageDir = cgraph.PageDir

types from cgraph package.

type PenType added in v0.2.0

type PenType = gvc.PenType

types from gvc package.

type Plugin added in v0.2.0

type Plugin = gvc.Plugin

types from gvc package.

type PointFloat added in v0.2.0

type PointFloat = gvc.PointFloat

types from gvc package.

type PostScriptAlias added in v0.2.0

type PostScriptAlias = gvc.PostScriptAlias

types from gvc package.

type QuadType added in v0.2.0

type QuadType = cgraph.QuadType

types from cgraph package.

type RankDir added in v0.2.0

type RankDir = cgraph.RankDir

types from cgraph package.

type RatioType added in v0.2.0

type RatioType = cgraph.RatioType

types from cgraph package.

type Record added in v0.2.0

type Record = cgraph.Record

types from cgraph package.

type RenderEngine added in v0.2.0

type RenderEngine = gvc.RenderEngine

types from gvc package.

type RenderFeature added in v0.2.0

type RenderFeature = gvc.RenderFeature

types from gvc package.

type RenderPlugin added in v0.2.0

type RenderPlugin = gvc.RenderPlugin

types from gvc package.

type RenderPluginOption added in v0.2.0

type RenderPluginOption = gvc.RenderPluginOption

types from gvc package.

type Scale added in v0.2.0

type Scale = gvc.Scale

types from gvc package.

type Shape added in v0.2.0

type Shape = cgraph.Shape

types from cgraph package.

type SmoothType added in v0.2.0

type SmoothType = cgraph.SmoothType

types from cgraph package.

type StartType added in v0.2.0

type StartType = cgraph.StartType

types from cgraph package.

type State added in v0.2.0

type State = cgraph.State

types from cgraph package.

type SubNode added in v0.2.0

type SubNode = cgraph.SubNode

types from cgraph package.

type Symbol added in v0.2.0

type Symbol = cgraph.Symbol

types from cgraph package.

type Tag added in v0.2.0

type Tag = cgraph.Tag

types from cgraph package.

type TextFont added in v0.2.0

type TextFont = gvc.TextFont

types from gvc package.

type TextSpan added in v0.2.0

type TextSpan = gvc.TextSpan

types from gvc package.

type Translation added in v0.2.0

type Translation = gvc.Translation

types from gvc package.

Directories

Path Synopsis
cmd
dot Module
internal

Jump to

Keyboard shortcuts

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