Go bindings for Graphviz
A fork of github.com/goccy/go-graphviz,
maintained by forkcloser. The library is the same: Graphviz compiled to
WebAssembly, run on wazero, no C toolchain and no system Graphviz. The
upstream dot command is not part of this module; forkcloser ships its own,
github.com/forkcloser/dot, as a static
binary with every layout and format of the library.
What this fork fixes and changes that upstream does not have is the table in
UPSTREAM.md, beside the fork point and what was taken from
upstream since.
The embedded Graphviz is the version of the graphviz entry in pins.yaml; graphviz.Version() asks the module itself.
- Pure Go Library
- No need to install Graphviz library (
orbrew install graphviz)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
circo dot fdp neato nop nop1 nop2 osage patchwork sfdp twopi
The embedded Graphviz is built without a triangulation library (no HAVE_GTS)
and without the IPSEP solver (no IPSEPCOLA), as the hand-written
config.h has it. A dot built with both, as
Homebrew's is, lays these out differently:
overlap=falseremoves overlaps by the Voronoi method, where such adotuses prism, so the layout is another. This holds forneato,fdpandsfdp.overlap=prism, with or without its number, is ignored with the warningOverlap value "prism" unsupported - ignored, and does whatoverlap=falsedoes.sfdpremoves no overlaps unless told to: its default is prism in such adotand nothing here, so its nodes can overlap.overlap=false,overlap=scaleandoverlap=voronoiremove them.mode=ipsepis ignored byneatowith the warningIllegal value ipsep for attribute "mode", and the layout is the default mode's.
sfdp also places its nodes elsewhere than a native dot does for the same
graph, overlap removal aside: Graphviz's sfdp starts from positions drawn from
the C library's rand(), and the module carries its own C library. The layout
is the same from one run to the next.
dot svg png jpg
The above are the formats supported by default. You can also add custom formats.
Labels are measured for the layout and drawn with the fonts installed on the
machine, as Graphviz's own tools do, so text fits its node in every format, and
the same graph can lay out slightly differently on two machines with different
fonts, or against a native Graphviz on the same machine, whose pango plugin
measures the same font through a different text engine (a node a point wider
or narrower). A font name resolves the way Graphviz resolves it: a PostScript name such
as Times-Roman or Helvetica-Bold, a family with style words (DejaVu Sans Bold, Arial:italic), or a list of them, looked up among the installed fonts by
name, with the metric-compatible substitutes Graphviz knows (Arial for Helvetica,
the Liberation and TeX Gyre faces). Where nothing installed answers, the embedded
Go fonts stand in, in regular, bold, italic and monospace cuts. Those cover
Latin, Greek and Cyrillic: a label in another script, CJK among them, draws as
boxes on a machine with no font for it, unless a FontLoader (SetFontLoader)
supplies one.
A font can also be named by the path of its file (fontname="/fonts/face.ttf").
The file is read from the file system SetFileSystem names, as a node image
is, and from the machine when none is named.
Fonts are read from the platform's directories: on macOS ~/Library/Fonts,
/Library/Fonts and /System/Library/Fonts; on Windows the system and the
user's Fonts folders; on Linux and the BSDs ~/.fonts, then fonts under
XDG_DATA_HOME (~/.local/share/fonts when it is unset) and under each
XDG_DATA_DIRS entry (/usr/local/share/fonts and /usr/share/fonts when it
is unset); on Android /system/fonts.
$ go get github.com/forkcloser/go-graphvizpackage main
import (
"bytes"
"context"
"fmt"
"log"
"github.com/forkcloser/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, graphviz.GV, &buf); err != nil {
log.Fatal(err)
}
fmt.Println(buf.String())
}path := "/path/to/dot.gv"
b, err := os.ReadFile(path)
if err != nil { panic(err) }
graph, err := graphviz.ParseBytes(b)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) }Every task runs through just: just lint, just test,
just fix, just wasm, just bindings; just --list shows the rest. Tools are
pinned by aqua and the shared conventions come
from limen (see AGENTS.md).
just test first fetches Graphviz's own dot of the release the module is
built from (just oracle: a build by testbin,
pinned and verified per platform in pins.yaml), and the tests
compare the module's output with it. Under just test a missing or mismatched
dot fails; a bare go test uses a dot on PATH of the same release, or
skips those tests.
- Generates bindings between Go and C from Protocol Buffers file with
just bindings. - Builds
graphviz.wasmwithjust wasm(internal/wasm/build/build.sh): Graphviz, expat and the C bridge compiled towasm32-wasip1by a pinned wasi-sdk and shrunk by a pinned binaryen, every input fetched by version and checked against the digests in pins.yaml. No container and noconfigurerun: the build is reproducible, and CI rebuilds the committed blob on Linux and macOS and compares the bytes. - Uses Graphviz functionality from the sub-packages
cgraphandgvcvia theinternal/wasmpackage. graphvizpackage provides facade interface for all sub packages.
Graphviz runs as one WebAssembly module for the process. If a graph makes it
trap (a stack it overflows, an access outside its memory), that call and every
call after it return gvc.ErrTrapped (a getter, which has no error to return,
its zero value), and the process has to be restarted to render again: a trap leaves Graphviz's state half-updated, so no call is let in
to draw from it, and no fresh module takes its place, since every graph and
context a caller holds lives in the one that trapped.
A render during which Graphviz reports an error and goes on, a record or an
HTML label it cannot read for one, is written whole, and Render then returns
that error, a gvc.ErrReported: what dot does when it writes its output and
exits 1. RenderImage returns its image with it, and RenderFilename has
written its file. Any other error of Graphviz's is a render that failed, with
nothing written.
What Graphviz only warns of goes to the writer SetWarningWriter names.
MIT, see LICENSE.
internal/wasm/graphviz.wasm is built from Graphviz and expat at the versions
pins.yaml names: Graphviz is under the Eclipse Public License 2.0
(LICENSE-graphviz), expat under the MIT license (LICENSE-expat).
