graphviz

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

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

Go to latest
Published: Jun 12, 2024 License: MIT Imports: 5 Imported by: 0

README

go-graphviz Go GoDoc

Go bindings for Graphviz ( port of version 2.40.1 )

Features

  • No need to install Graphviz library ( brew install graphviz or apt-get install graphviz )
  • Supports parsing for DOT language
  • Supports rendering graph in pure Go
  • Supports switch renderer to your own
  • Supports type safed property setting
  • gvc cgraph cdt are available as sub package

Currently supported Layout

circo dot fdp neato nop nop1 nop2 osage patchwork sfdp twopi

Currently supported format

dot svg png jpg

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() {
  g := graphviz.New()
  graph, err := g.Graph()
  if err != nil {
    log.Fatal(err)
  }
  defer func() {
    if err := graph.Close(); err != nil {
      log.Fatal(err)
    }
    g.Close()
  }()
  n, err := graph.CreateNode("n")
  if err != nil {
    log.Fatal(err)
  }
  m, err := graph.CreateNode("m")
  if err != nil {
    log.Fatal(err)
  }
  e, err := graph.CreateEdge("e", n, m)
  if err != nil {
    log.Fatal(err)
  }
  e.SetLabel("e")
  var buf bytes.Buffer
  if err := g.Render(graph, "dot", &buf); err != nil {
    log.Fatal(err)
  }
  fmt.Println(buf.String())
}

2. Parse DOT Graph

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

3. Render Graph

g := graphviz.New()
graph, err := g.Graph()
if err != nil {
  log.Fatal(err)
}

// create your graph

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

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

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

Tool

dot

Installation
$ go get github.com/goccy/go-graphviz/cmd/dot
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

go-graphviz has four layers.

  1. graphviz package provides facade interface for manipulating all features of graphviz library
  2. gvc cgraph cdt are sub packages ( FYI: C library section in https://www.graphviz.org/documentation )
  3. internal/ccall package provides bridge interface between Go and C
  4. go-graphviz includes full graphviz sources

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Directed         = func(g *Graphviz) { g.dir = cgraph.Directed }
	StrictDirected   = func(g *Graphviz) { g.dir = cgraph.StrictDirected }
	UnDirected       = func(g *Graphviz) { g.dir = cgraph.UnDirected }
	StrictUnDirected = func(g *Graphviz) { g.dir = cgraph.StrictUnDirected }
)

Functions

func ParseBytes

func ParseBytes(bytes []byte) (*cgraph.Graph, error)

func ParseFile

func ParseFile(path string) (*cgraph.Graph, error)

Types

type Format

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

type GraphOption

type GraphOption func(g *Graphviz)

func Name

func Name(name string) GraphOption

type Graphviz

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

func New

func New() *Graphviz

func (*Graphviz) Close

func (g *Graphviz) Close() error

func (*Graphviz) Graph

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

func (*Graphviz) Render

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

func (*Graphviz) RenderFilename

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

func (*Graphviz) RenderImage

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

func (*Graphviz) SetFontFace

func (g *Graphviz) SetFontFace(callback func(size float64) (font.Face, error))

func (*Graphviz) SetLayout

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

func (*Graphviz) SetRenderer

func (g *Graphviz) SetRenderer(format Format, renderer gvc.Renderer)

type Layout

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

Directories

Path Synopsis
cmd
dot
internal

Jump to

Keyboard shortcuts

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