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 ¶
- func FromVega(spec *vega.Spec, opts ...Option) (*Chart, Report, error)
- func FromVegaLite(spec *vegalite.Spec, opts ...Option) (*Chart, Report, error)
- func Parse(src []byte, opts ...Option) (*Chart, Report, error)
- type Chart
- func (c *Chart) Image(ctx context.Context, opts ...RenderOption) (*image.RGBA, Report, error)
- func (c *Chart) NewView(ctx context.Context, opts ...RenderOption) (*View, Report, error)
- func (c *Chart) Render(ctx context.Context, opts ...RenderOption) (*scene.Scene, Report, error)
- func (c *Chart) SVG(ctx context.Context, w io.Writer, opts ...RenderOption) (Report, error)
- func (c *Chart) Spec() *vega.Spec
- type Dialect
- type Error
- type IconLoader
- type IconLoaderFunc
- type IconRequest
- type LoadRequest
- type Loader
- type LoaderFunc
- type Option
- type RenderOption
- func WithBackground(c color.Color) RenderOption
- func WithIconLoader(l IconLoader) RenderOption
- func WithIcons(name string, p *icon.Pack) RenderOption
- func WithLoader(l Loader) RenderOption
- func WithLocale(l format.Locale) RenderOption
- func WithLocation(loc *time.Location) RenderOption
- func WithMode(m theme.Mode) RenderOption
- func WithRandom(fn func() rand.Source) RenderOption
- func WithRenderLogger(l *slog.Logger) RenderOption
- func WithResultSet(name string, rs ResultSet) RenderOption
- func WithScale(scale float64) RenderOption
- func WithScheme(s theme.Scheme) RenderOption
- func WithSize(width, height float64) RenderOption
- func WithValues(name string, rows []map[string]any) RenderOption
- type Report
- type ResultSet
- type SharedOption
- type View
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FromVega ¶
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 ¶
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.
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 ¶
Image renders the chart and draws it with the direct renderer of okage (D11). The text draws in the bundled IBM Plex Sans (D16), the fonts of WithEmoji and the font of a look drawn by hand of WithLookFont, 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.
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
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" // ErrNoNow is the error of an expression that calls now() when the // parse has no WithNow, in the strict mode. In the loose mode, now() // gives 0 and the report names the expression once (D38). ErrNoNow Error = "no time of the render" // ErrNoLookFont is the warning of a look drawn by hand when the render // has no font of the look, which WithLookFont gives. The look draws its // text in IBM Plex Sans, so nothing is dropped, and the report holds // the warning in either mode, never an error (D40). ErrNoLookFont Error = "no font of the look" )
The errors of the package. The errors of a spec are in the package vega.
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.
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 interface {
// contains filtered or unexported methods
}
Option is a setting of Parse.
func WithDialect ¶
WithDialect sets the dialect of a spec that has no $schema.
func WithFunction ¶
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 WithMath ¶ added in v0.7.5
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 WithNow ¶ added in v0.8.0
WithNow gives a parse the time of the render (D38). A gantt of Mermaid with the time draws the marker of today, and a date of its dateFormat that has no day, such as HH:mm, takes the day of the time, as in Mermaid. The gantt takes the day and the hour that the time has in its own time zone. The function now() of an expression gives the time, in milliseconds since 1970.
It is an option of Parse, because a gantt lays out when it compiles. The chart keeps the time, and each render of it uses the time. Without the time, a gantt draws no marker and puts a date with no day on 1970-01-01, and now() gives 0, so that the same spec gives the same bytes. The report names each expression that calls now() with no time, and in the strict mode such a call is an error that wraps ErrNoNow. The zero time is no time.
func WithStrict ¶
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 interface {
// contains filtered or unexported methods
}
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 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 ¶
Report lists what zuzu did not draw in the loose mode, and what it warns of in either mode (D26). A warning, such as an issue that wraps ErrNoLookFont, draws in full in another way, so the strict mode reports it and does not turn it into an error.
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 SharedOption ¶ added in v0.8.0
type SharedOption interface {
Option
RenderOption
}
SharedOption is a setting of both Parse and a render, such as WithEmoji.
func WithEmoji ¶ added in v0.6.0
func WithEmoji(f *text.Fallback) SharedOption
WithEmoji gives a parse or 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, report, err := zuzu.Parse(src, zuzu.WithEmoji(emoji.Font())) img, report, err := 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.
It is an option of Parse too, because some kinds of Mermaid, such as a sequence, a pie or a mindmap, lay out their labels when they compile (D38). The chart keeps the font, and each render of it that gives no font of its own uses the font. When both give a font, the font of the render wins.
func WithLayout ¶ added in v0.8.0
func WithLayout(name string) SharedOption
WithLayout forces a layout on each graph of the chart, by its name (D45 part 4): layered for zuzu.layered, dagre for zuzu.dagre, elk for the layered layout of ELK, or the name of another algorithm of ELK that Mermaid 12.3 has, such as elk.stress, elk.force, elk.mrtree, elk.sporeOverlap, elk.box or elk.rectpacking. A name of ELK needs the engine named elk of WithLayoutEngine, and another name that is not built in needs the engine of WithLayoutEngine with that name. The engine gets the name as graph.LayoutInput.Algorithm.
The option of the caller wins over the layout that the source asks for, as WithLook does. A forced layout with no engine is an issue, and an error in the strict mode, and the graph then lays out as though nothing forced it. The issue comes from the first graph that lays out, so a chart with no graph reports nothing.
It is an option of Parse too. The chart keeps the layout of Parse, and a render that forces no layout of its own lays out with it. The layout of the render wins over the layout of Parse, and an empty name in a render forces no layout, not even the layout of Parse.
func WithLayoutEngine ¶ added in v0.8.0
func WithLayoutEngine(e graph.Engine) SharedOption
WithLayoutEngine makes a layout engine available to zuzu, under its name (D45 part 3). It does not force the engine on a graph. A graph lays out with the engine when WithLayout or the spec names it, or when the source asks for its layout. The module github.com/xo/zuzu/elk gives the engine of ELK, named elk:
c, report, err := zuzu.Parse(src, zuzu.WithLayoutEngine(elk.Layout()))
A diagram of Mermaid that sets no layout, in a kind whose default layout in Mermaid 12.3 is ELK, then draws with ELK, and so does a diagram with layout: elk or a layout whose name starts with elk., such as elk.stress. Without the engine of ELK, such a diagram draws with zuzu.layered, with no issue (D32). The built in engines, graph.Layered and graph.Dagre, need no option.
It is an option of Parse too, as WithLook is. The chart keeps the engines of Parse, and a render can make more available. When the render and the parse each give an engine with the same name, the engine of the render wins. A nil engine makes nothing available.
func WithLook ¶ added in v0.8.0
func WithLook(l theme.Look) SharedOption
WithLook sets the look, such as theme.LookExcalidraw, which draws each mark as if by hand (D39, D40). It wins over the look that the spec asks for with zuzu.look in its config, such as look: handDrawn of a Mermaid diagram. A look that zuzu does not have is an issue, and draws in the standard look.
It is an option of Parse too, because some kinds of Mermaid lay out their labels when they compile, and a look measures its text in its own font (D40 part 1). The chart keeps the look, and each render of it that gives no look of its own uses the look. When both give a look, the look of the render wins.
func WithLookFont ¶ added in v0.8.0
func WithLookFont(l theme.Look, f *text.Fallback) SharedOption
WithLookFont gives a parse or a render the font of a look drawn by hand. The package text/excalifont gives the font of the look like Excalidraw, Excalifont:
font := zuzu.WithLookFont(theme.LookExcalidraw, excalifont.Font()) c, report, err := zuzu.Parse(src, font) img, report, err := c.Image(ctx, zuzu.WithLook(theme.LookExcalidraw), font)
The look measures its text in the font, with IBM Plex Sans for the characters that the font does not have, so the layout makes room for it (D40 part 1). The SVG names its family first, and the PNG of Image draws with it. Without the font, the look draws its text in IBM Plex Sans, and the report warns of it with ErrNoLookFont, in the strict mode too. A program that does not import text/excalifont does not hold the font. The standard look draws in IBM Plex Sans, and takes no font.
It is an option of Parse too, as WithLook is. The chart keeps the font, and each render of it that gives no font for the look uses the font. When both give a font, the font of the render wins.
type View ¶
type View struct {
// contains filtered or unexported fields
}
View holds the state of one render. It is not safe for concurrent use.
Source Files
¶
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. |
|
dagre
Package dagro lays out a directed graph in ranks, as dagre 3.1.1 does.
|
Package dagro lays out a directed graph in ranks, as dagre 3.1.1 does. |
|
rough
Package rough draws lines, curves, shapes and their fills as if by hand.
|
Package rough draws lines, curves, shapes and their fills as if by hand. |
|
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. |
|
sketch
Package sketch turns a scene into the same scene drawn as if by hand, with the copy of rough.js in internal/rough (D39, D40).
|
Package sketch turns a scene into the same scene drawn as if by hand, with the copy of rough.js in internal/rough (D39, D40). |
|
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). |