zuzu

package module
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 46 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README


Unit Tests Go Reference Releases Discord Discussion

About

zuzu draws charts and diagrams in pure Go. A caller gives it a declarative spec, which says what to draw from which data, and zuzu writes the chart as SVG or as an image.

The spec follows Vega and Vega-Lite. A spec written for the Vega tools works with zuzu, as far as zuzu supports it. zuzu draws its own design, at the quality of nivo and visx, with a light and a dark mode and color schemes that swap. zuzu renders the SVG to an image with okage, a pure Go port of resvg.

zuzu also reads the DOT language of Graphviz and the syntax of Mermaid, and draws a diagram in the same look. Later, zuzu sends live charts to a web browser, and draws the results of a query for usql.

Status

Phases 0 to 7 of the plan are done. Phase 0, the repository, the tests of the documents, CI, the corpus and the oracle, is done. The scene graph, the themes, the SVG writer and the measurement of text with the bundled IBM Plex Sans exist (phases 1 and 2). zuzu reads a Vega spec and draws it (phase 3). On the 540 specs of the first target, its datasets, scales and marks match those of vega.js for 99 percent of the specs. zuzu compiles a Vega-Lite spec itself (phase 4), and its result matches for 98 percent of the 470 Vega-Lite specs. Phase 5 added the transforms of statistics, hierarchies, maps, contours and labels, and the transforms and the scale of zuzu for sankeys, chord diagrams, hex bins and trading days. Phase 6 added raster, which draws a chart with okage directly, without SVG, and Chart.Image. Phase 7 added the diagrams of DOT and Mermaid: a layered layout of graphs, the flowchart and nine charts of Mermaid, and icons, with four packs bundled. Since then zuzu also draws every other kind of Mermaid 12.3: the sequence, state, class, ER, requirement, block, swimlane, use case, agentflow, timeline, C4, venn, ishikawa, Cynefin, event model, ZenUML, kanban, packet and railroad diagrams, the mindmap, the architecture diagram, the gitGraph, the tree view and info. The package text/emoji draws an emoji in color. docs/DIAGRAMS.md lists what zuzu reads. docs/PLAN.md holds the plan, its phases and the questions that the maintainers have not answered.

chart, _, err := zuzu.Parse(src)
if err != nil {
	return err
}
report, err := chart.SVG(ctx, w, zuzu.WithLoader(zuzu.FSLoader(os.DirFS("data"))))

The first step was a survey of what other libraries do. Open docs/survey/compare.html to see the same real data drawn by zuzu and 14 other chart and diagram libraries, side by side, in a light and a dark theme. docs/survey/README.md says how to open it.

The second step was the look. Open docs/targets/index.html to see 33 target charts, from a line to a flowchart, in the light and the dark mode and in thirteen color schemes. They came before any code. The script in docs/targets/ draws them, and docs/targets/README.md says how to draw and open them.

Design

zuzu has four stages, and each stage hands the next one a value that can be written as JSON:

  1. A front end reads Vega, Vega-Lite, DOT or Mermaid, and produces a spec in the shape of a Vega spec.
  2. The runtime loads the data, runs the transforms and the expressions, builds the scales and lays out the axes and the legends.
  3. The result is a scene graph: a tree of shapes with their final positions, sizes and colors.
  4. A renderer writes the scene graph as SVG, draws it with okage, or later sends it to a browser.

The first target is static Vega, which is Vega without the events of a mouse or a keyboard, and the Vega-Lite that compiles to it. The corpus of 579 Vega-Lite specs and 87 Vega specs from vegagoja, with the output of vega.js for each, measures how far zuzu agrees with Vega.

Documents

Document What it holds
docs/PLAN.md the plan, the architecture, the phases and the open questions
docs/API.md the public API
docs/EXPRESSIONS.md the expression language, and the choices for scripting beyond it
docs/decisions/ every decision, one file each, with an index
docs/BACKLOG.md the work that is known and not done
docs/STYLE.md the rules and the tokens of the look, the modes and the color schemes
docs/DIAGRAMS.md the parts of DOT and of Mermaid that zuzu reads
docs/targets/ the target SVGs that show the look zuzu aims for
docs/survey/ the survey of other libraries, and the page that compares them
AGENTS.md the rules for a coding agent, which apply to a person too
CONTRIBUTING.md how to change zuzu

Contributing

Read CONTRIBUTING.md first. A change to the design is a decision first, and an open question goes to the maintainers.


Documentation

Overview

Package zuzu draws charts and diagrams from a declarative spec that follows Vega and Vega-Lite. It writes its own SVG, and okage renders the SVG to an image.

The plan is in docs/PLAN.md and the public API in docs/API.md.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromVega

func FromVega(spec *vega.Spec, opts ...Option) (*Chart, Report, error)

FromVega takes a Vega spec that a program built as Go values. It copies the spec, so a later change by the caller does not reach the Chart.

func FromVegaLite

func FromVegaLite(spec *vegalite.Spec, opts ...Option) (*Chart, Report, error)

FromVegaLite takes a Vega-Lite spec that a program built as Go values, and compiles it to Vega. The report names each part that zuzu did not compile, in the loose mode.

func Parse

func Parse(src []byte, opts ...Option) (*Chart, Report, error)

Parse reads a Vega or a Vega-Lite spec, or a diagram of DOT or Mermaid (D31). The $schema field decides between Vega and Vega-Lite, and the first word of a source finds a diagram. Another source needs WithDialect.

Types

type Chart

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

Chart is a parsed spec. It does not change after Parse, so it is safe for concurrent use.

func (*Chart) Image

func (c *Chart) Image(ctx context.Context, opts ...RenderOption) (*image.RGBA, Report, error)

Image renders the chart and draws it with the direct renderer of okage (D11). The text draws in the bundled IBM Plex Sans (D16), and the fonts of WithEmoji, and in no font of the system, so that a chart gives the same pixels on every machine. A character that no font has draws nothing (D32). The pixels are premultiplied, as image.RGBA expects.

func (*Chart) NewView

func (c *Chart) NewView(ctx context.Context, opts ...RenderOption) (*View, Report, error)

NewView runs the spec and keeps its state.

func (*Chart) Render

func (c *Chart) Render(ctx context.Context, opts ...RenderOption) (*scene.Scene, Report, error)

Render runs the spec and returns the scene graph.

func (*Chart) SVG

func (c *Chart) SVG(ctx context.Context, w io.Writer, opts ...RenderOption) (Report, error)

SVG renders the chart and writes it as SVG.

func (*Chart) Spec

func (c *Chart) Spec() *vega.Spec

Spec returns a copy of the Vega spec, to read or to debug.

type Dialect

type Dialect int

Dialect is the language of a spec.

const (
	// Vega is a Vega spec.
	Vega Dialect = iota + 1
	// VegaLite is a Vega-Lite spec.
	VegaLite
	// DOT is a graph in the DOT language of Graphviz (D31).
	DOT
	// Mermaid is a diagram of Mermaid (D31).
	Mermaid
)

The dialects.

func Sniff added in v0.2.0

func Sniff(prefix []byte) (Dialect, bool)

Sniff returns the dialect of a source from its first bytes, as a detector of a type of file does: DOT or Mermaid from the first word, and Vega or Vega-Lite from a $schema in the first bytes. It needs no more than the first 512 bytes, and returns false when they do not decide.

type Error

type Error string

Error is an error of the package zuzu.

const (
	// ErrUnknownDialect is the error of a spec whose $schema names neither
	// Vega nor Vega-Lite, and of a spec with no $schema and no WithDialect.
	ErrUnknownDialect Error = "unknown spec dialect"
	// ErrNoLoader is the error of a spec that names data by url, when the
	// render has no loader (D25).
	ErrNoLoader Error = "no loader for url data"
	// ErrCycle is the error of a signal, a dataset or a scale that depends
	// on itself.
	ErrCycle Error = "cycle in the spec"
	// ErrUnknownName is the error of a reference to a signal, a dataset or a
	// scale that the spec does not have.
	ErrUnknownName Error = "unknown name"
	// ErrNoGlyph is the error of a character of a text that no font of the
	// render has, in the strict mode. In the loose mode, the character
	// draws nothing and the report names it once (D32).
	ErrNoGlyph Error = "no font has the character"
)

The errors of the package. The errors of a spec are in the package vega.

func (Error) Error

func (err Error) Error() string

Error satisfies the error interface.

type IconLoader

type IconLoader interface {
	LoadIcon(ctx context.Context, req IconRequest) (io.ReadCloser, error)
}

IconLoader gives the icons that no pack of WithIcons holds (D31). zuzu asks it once for each icon of a render, by its prefix and its name, and it gives the bytes from wherever it chooses. zuzu itself reads no file and makes no request.

func FSIconLoader

func FSIconLoader(fsys fs.FS) IconLoader

FSIconLoader reads an icon from fsys: the SVG <prefix>/<name>.svg, or else the pack of Iconify <prefix>.json. The caller chooses fsys, so zuzu still reads only what the caller allows.

type IconLoaderFunc

type IconLoaderFunc func(context.Context, IconRequest) (io.ReadCloser, error)

IconLoaderFunc makes an IconLoader of a function.

func (IconLoaderFunc) LoadIcon

func (f IconLoaderFunc) LoadIcon(ctx context.Context, req IconRequest) (io.ReadCloser, error)

LoadIcon calls the function.

type IconRequest

type IconRequest struct {
	Prefix, Name string
}

IconRequest names an icon: aws and lambda, for aws:lambda.

type LoadRequest

type LoadRequest struct {
	// URL is the url of the spec, as written.
	URL string
	// Format is csv, tsv, json or topojson, from the spec.
	Format string
	// Name is the name of the dataset.
	Name string
}

LoadRequest says what a spec asks for.

type Loader

type Loader interface {
	Load(ctx context.Context, req LoadRequest) (io.ReadCloser, error)
}

Loader reads the data that a spec names by url (D25). zuzu reads no file and makes no request of its own.

func FSLoader

func FSLoader(fsys fs.FS) Loader

FSLoader reads each url as a path in fsys. The caller chooses fsys, so zuzu still reads only what the caller allows. A url with a scheme, such as https:, is an error.

type LoaderFunc

type LoaderFunc func(context.Context, LoadRequest) (io.ReadCloser, error)

LoaderFunc makes a Loader of a function.

func (LoaderFunc) Load

func (f LoaderFunc) Load(ctx context.Context, req LoadRequest) (io.ReadCloser, error)

Load calls the function.

type Option

type Option func(*options)

Option is a setting of Parse.

func WithDialect

func WithDialect(d Dialect) Option

WithDialect sets the dialect of a spec that has no $schema.

func WithFunction

func WithFunction(name string, fn expr.Func) Option

WithFunction adds a Go function that an expression can call by name. It takes the place of a built in function of the same name.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger sets where zuzu logs.

func WithMath added in v0.7.5

func WithMath(f *text.MathFont) Option

WithMath gives a parse a font of math, so that the math of KaTeX in a label of Mermaid, written as $$...$$, draws as math (D36). The package text/math gives the font, STIX Two Math:

c, report, err := zuzu.Parse(src, zuzu.WithMath(math.Font()))

It is an option of Parse, because some kinds of Mermaid lay out their labels when they compile. The chart keeps the font, and each render of it uses the font. Without the font, a label with math draws its source, and the report names it. A program that does not import text/math does not hold the font.

func WithStrict

func WithStrict(strict bool) Option

WithStrict sets the feature mode (D26). In the strict mode, a feature that zuzu does not support is an error. In the loose mode, the default, zuzu skips it and names it in the report.

type RenderOption

type RenderOption func(*renderOptions)

RenderOption is a setting of a render.

func WithBackground added in v0.2.0

func WithBackground(c color.Color) RenderOption

WithBackground sets the background of the chart, over the background of the theme and of the spec, and over zuzu.background in the config of the spec. A color with an alpha of 0 draws no background, and nil keeps the background of the theme and of the spec. The boxes under the labels of a diagram and the open arrowheads take an opaque color too, so that they match the background.

func WithEmoji added in v0.6.0

func WithEmoji(f *text.Fallback) RenderOption

WithEmoji gives a render a font for the emoji, so that an emoji draws in color (D32). The package text/emoji gives the font, Noto Color Emoji:

c.Image(ctx, zuzu.WithEmoji(emoji.Font()))

The text measures with the font, and the PNG of Image draws with it. The SVG names its family for a text with an emoji, and a viewer draws it in the color font that it has. Without the font, an emoji draws nothing, as a character that no font has. A program that does not import text/emoji does not hold the font.

func WithIconLoader

func WithIconLoader(l IconLoader) RenderOption

WithIconLoader sets how a render loads the icons that no pack holds.

func WithIcons

func WithIcons(name string, p *icon.Pack) RenderOption

WithIcons gives a pack of icons to a render, under a name that replaces the prefix of the pack, as Mermaid does (D31). A diagram names an icon of the pack as name:icon.

func WithLoader

func WithLoader(l Loader) RenderOption

WithLoader sets how the data of a url is read (D25).

func WithLocale

func WithLocale(l format.Locale) RenderOption

WithLocale sets the locale of numbers and dates (D27).

func WithLocation

func WithLocation(loc *time.Location) RenderOption

WithLocation sets the time zone of local time (D27). The default is the zone of the system.

func WithMode

func WithMode(m theme.Mode) RenderOption

WithMode sets the light or the dark mode (D14). It wins over the mode that the spec asks for with zuzu.mode in its config, such as the dark theme of a Mermaid diagram (D32).

func WithRandom

func WithRandom(fn func() rand.Source) RenderOption

WithRandom sets the function that makes the source of random numbers of a render (D28). Each render calls it once.

func WithRenderLogger

func WithRenderLogger(l *slog.Logger) RenderOption

WithRenderLogger sets where a render logs.

func WithResultSet

func WithResultSet(name string, rs ResultSet) RenderOption

WithResultSet gives the rows of a named dataset, read from the result of a query. zuzu reads it once, and the caller closes it.

func WithScale added in v0.3.0

func WithScale(scale float64) RenderOption

WithScale draws the image of Image at a scale, as a ratio of device pixels to the pixels of the chart, such as 2 for a screen of high density. The layout, the fonts and the size of the scene do not change, and the image has the size of the scene times the scale. The SVG of SVG has a viewBox, so a viewer scales it, and the scale does not change it. A scale that is not more than zero, or not finite, is an error of Image.

func WithScheme

func WithScheme(s theme.Scheme) RenderOption

WithScheme sets the color scheme (D14). It wins over the scheme that the spec asks for with zuzu.scheme in its config, such as the forest theme of a Mermaid diagram (D32).

func WithSize

func WithSize(width, height float64) RenderOption

WithSize sets the size of the chart, in place of its width and height.

func WithValues

func WithValues(name string, rows []map[string]any) RenderOption

WithValues gives the rows of a named dataset. They take the place of the values or the url of the dataset in the spec, and its transforms still run.

type Report

type Report = vega.Report

Report lists what zuzu did not draw in the loose mode, and what it warns of in either mode (D26).

type ResultSet

type ResultSet interface {
	Columns() ([]string, error)
	Next() bool
	Scan(dest ...any) error
	Err() error
}

ResultSet is the result of a query. *sql.Rows satisfies it.

type View

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

View holds the state of one render. It is not safe for concurrent use.

func (*View) Data

func (v *View) Data(name string) ([]map[string]any, error)

Data returns the rows of a dataset of the top level, after its transforms.

func (*View) Scale

func (v *View) Scale(name string) (scale.Scale, error)

Scale returns a scale of the top level.

func (*View) Scene

func (v *View) Scene() *scene.Scene

Scene returns the scene graph of the view.

func (*View) Signal

func (v *View) Signal(name string) (any, error)

Signal returns the value of a signal of the top level.

Directories

Path Synopsis
Package dataflow reads the data of a Vega spec and runs its transforms (D6).
Package dataflow reads the data of a Vega spec and runs its transforms (D6).
Package expr parses and evaluates the expression language of Vega (D9).
Package expr parses and evaluates the expression language of Vega (D9).
Package format formats numbers and times with the syntax of d3-format and d3-time-format, which Vega uses for its axes, its legends and the functions format and timeFormat of its expressions.
Package format formats numbers and times with the syntax of d3-format and d3-time-format, which Vega uses for its axes, its legends and the functions format and timeFormat of its expressions.
Package geo holds the cartographic projections of zuzu and the geographic paths that they draw.
Package geo holds the cartographic projections of zuzu and the geographic paths that they draw.
Package graph holds the model of a diagram, a graph of nodes, edges and clusters, and its layered layout (D18, D31).
Package graph holds the model of a diagram, a graph of nodes, edges and clusters, and its layered layout (D18, D31).
dot
Package dot reads the DOT language of Graphviz into a graph (D31).
Package dot reads the DOT language of Graphviz into a graph (D31).
mermaid
Package mermaid reads diagrams of Mermaid 12.3.0 (D31): the flowchart, the swimlane diagram, the state diagram, the class diagram, the entity relationship diagram, the requirement diagram, the agentflow diagram and the use case diagram into a graph, and the charts into specs of Vega-Lite and Vega.
Package mermaid reads diagrams of Mermaid 12.3.0 (D31): the flowchart, the swimlane diagram, the state diagram, the class diagram, the entity relationship diagram, the requirement diagram, the agentflow diagram and the use case diagram into a graph, and the charts into specs of Vega-Lite and Vega.
Package icon reads icons in the JSON format of Iconify, and in SVG, for the nodes of a diagram (D31).
Package icon reads icons in the JSON format of Iconify, and in SVG, for the nodes of a diagram (D31).
icons
carbon
Package carbon bundles the icons of Carbon, the design system of IBM, version 11.92.0, under Apache-2.0, as a pack of icons for the diagrams of zuzu (D31).
Package carbon bundles the icons of Carbon, the design system of IBM, version 11.92.0, under Apache-2.0, as a pack of icons for the diagrams of zuzu (D31).
fa
Package fa bundles the solid icons of Font Awesome Free, version 7.3.1, under CC BY 4.0, as a pack of icons for the diagrams of zuzu (D31).
Package fa bundles the solid icons of Font Awesome Free, version 7.3.1, under CC BY 4.0, as a pack of icons for the diagrams of zuzu (D31).
k8s
Package k8s bundles the icons of Kubernetes, from the pack of Iconify of 2026-10-20, under Apache-2.0, as a pack of icons for the diagrams of zuzu (D31).
Package k8s bundles the icons of Kubernetes, from the pack of Iconify of 2026-10-20, under Apache-2.0, as a pack of icons for the diagrams of zuzu (D31).
mdi
Package mdi bundles Material Design Icons of Pictogrammers, from the pack of Iconify of 2025-01-21, under Apache-2.0, as a pack of icons for the diagrams of zuzu (D31).
Package mdi bundles Material Design Icons of Pictogrammers, from the pack of Iconify of 2025-01-21, under Apache-2.0, as a pack of icons for the diagrams of zuzu (D31).
internal
color
Package color reads the colors of CSS and converts them between the color spaces of d3: RGB, HSL, CIELAB and HCL.
Package color reads the colors of CSS and converts them between the color spaces of d3: RGB, HSL, CIELAB and HCL.
shape
Package shape draws the geometry of the items of a scene as the path data of SVG: the curves of lines and areas, the symbols, the arcs, the trails and the rects with round corners.
Package shape draws the geometry of the items of a scene as the path data of SVG: the curves of lines and areas, the symbols, the arcs, the trails and the rects with round corners.
Package layout draws the guides of a chart in the look of zuzu: the axes, the legends and the titles (D12).
Package layout draws the guides of a chart in the look of zuzu: the axes, the legends and the titles (D12).
Package raster draws a scene graph as an image with the drawing of okage directly, without the round trip through SVG (D11).
Package raster draws a scene graph as an image with the drawing of okage directly, without the round trip through SVG (D11).
Package scale holds the scales of Vega, which map values of the data to values of a chart, such as positions and colors, and the ticks of each scale (D6).
Package scale holds the scales of Vega, which map values of the data to values of a chart, such as positions and colors, and the ticks of each scale (D6).
Package scene holds the scene graph of zuzu: the shapes of a chart with their final positions, sizes and colors (D8).
Package scene holds the scene graph of zuzu: the shapes of a chart with their final positions, sizes and colors (D8).
Package svg writes a scene graph as SVG (D10).
Package svg writes a scene graph as SVG (D10).
Package text measures text for the layout of zuzu (D11).
Package text measures text for the layout of zuzu (D11).
math
Package math gives STIX Two Math, a font of math under the SIL Open Font License 1.1, so that zuzu draws the math of KaTeX in a label of Mermaid (D36).
Package math gives STIX Two Math, a font of math under the SIL Open Font License 1.1, so that zuzu draws the math of KaTeX in a label of Mermaid (D36).
Package theme holds the themes of zuzu: a mode, light or dark, and a color scheme, which a caller swaps apart from the mode (D14).
Package theme holds the themes of zuzu: a mode, light or dark, and a color scheme, which a caller swaps apart from the mode (D14).
Package vega holds the Go types of a Vega spec, which it reads from JSON and writes back to JSON (D6, D21).
Package vega holds the Go types of a Vega spec, which it reads from JSON and writes back to JSON (D6, D21).
Package vegalite holds the Go types of a Vega-Lite spec and the compiler that turns it into a Vega spec (D6, D21).
Package vegalite holds the Go types of a Vega-Lite spec and the compiler that turns it into a Vega spec (D6, D21).

Jump to

Keyboard shortcuts

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