Documentation
¶
Overview ¶
Package refract turns one declarative chart specification into any output you need — SVG and PDF today, raster now, GPU and browser through additional backends — from the same model, with the same geometry.
The core module is pure Go and depends on nothing but the standard library. Both vector emitters are built in and need no rendering engine and no font stack, so a server that wants a chart as SVG or a report generator that wants one as PDF links nothing native and nothing young. Raster output lives in a separate module, github.com/timzifer/refract/backend/gg, which is still CGO-free.
Shape of the API ¶
Build a plot, give it scales, add layers, render it to a target:
src := refract.Float64Columns(map[string][]float64{"t": times, "y": values})
p := refract.New(
refract.Theme(theme.Dark),
refract.Size(800, 500),
refract.Title("Signal"),
)
p.X(scale.Time())
p.Y(scale.Linear(scale.Nice()))
p.Add(geom.Line(src, geom.X("t"), geom.Y("y"), geom.Color(palette.Blue)))
err := p.Render(refract.SVG("signal.svg"))
Scales cover linear, time, log, symlog and ordinal/categorical axes; geoms cover lines, scatters, bars, areas, steps and boxplots. A mark's colour can come from the data through scale.Sequential or scale.Diverging and geom.ColorBy, which contributes a colourbar beside the plot.
Annotations ¶
geom.HLine, geom.VLine, geom.HBand, geom.VBand, geom.Segment, geom.Region and geom.Note add the marks that are not data — a threshold, a shaded window, a label pointing at what happened. They take values rather than a data source.
Many panels ¶
Plot.Facet splits one plot into small multiples, one panel per value of a column; NewGrid puts several different plots on one canvas. Both lay their panels out with the same solver, so the axes line up either way.
p.Facet(facet.Wrap("region", facet.Columns(3)))
Status ¶
Pre-alpha. Every release below v1.0.0 may contain breaking changes without a deprecation cycle. See CONCEPT.md for the design and the roadmap.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrEmptyGrid = errors.New("refract: grid has no plots")
ErrEmptyGrid reports a render of a grid with no plots in it.
var ErrNoLayers = errors.New("refract: plot has no layers and no scales")
ErrNoLayers reports a render of a plot with nothing in it. Rendering empty axes is a legitimate thing to want, so this is only returned when there is also no scale configured — that combination is always a mistake.
Functions ¶
func NewTable ¶
NewTable returns an empty table that can mix numeric and time columns. See data.NewTable.
Types ¶
type Grid ¶ added in v0.3.0
type Grid struct {
// contains filtered or unexported fields
}
Grid renders several plots together in one image, with their axes aligned.
It is the other half of the multi-panel story: Plot.Facet splits one plot by a column, and a Grid puts different plots side by side. Both go through the same solver, so the panels line up either way.
g := refract.NewGrid(2, refract.GridSize(900, 600), refract.GridTitle("Fleet"))
g.Add(latency, throughput, errors, saturation)
err := g.Render(refract.SVG("fleet.svg"))
A member plot contributes its layers, its scales and its title, which becomes the label above its panel. The canvas is the grid's: its size, theme, chart title and axis titles are the ones used, and a member plot's own size, theme and axis titles are not. That is the price of one image — two panels cannot disagree about the colour of the paper they are printed on.
func NewGrid ¶ added in v0.3.0
func NewGrid(cols int, opts ...GridOption) *Grid
NewGrid creates a grid that flows plots into rows of cols panels.
type GridOption ¶ added in v0.3.0
type GridOption func(*Grid)
GridOption configures a Grid at construction.
func GridAxisTitles ¶ added in v0.3.0
func GridAxisTitles(x, y string) GridOption
GridAxisTitles labels the shared axes, once for the grid. Panels keep their own scales; these name what those scales measure.
func GridDPR ¶ added in v0.3.0
func GridDPR(r float64) GridOption
GridDPR sets the device pixel ratio. See DPR.
func GridLegend ¶ added in v0.3.0
func GridLegend(show bool) GridOption
GridLegend forces the legend on or off. By default it appears when any panel would have shown one.
func GridSize ¶ added in v0.3.0
func GridSize(w, h int) GridOption
GridSize sets the output size in device-independent pixels. The default is 900x600, which is a grid's worth rather than a single chart's.
func GridTheme ¶ added in v0.3.0
func GridTheme(t themepkg.Theme) GridOption
GridTheme sets the visual tokens for the whole grid.
func GridTitle ¶ added in v0.3.0
func GridTitle(s string) GridOption
GridTitle sets the title above the grid.
type Option ¶
type Option func(*Plot)
Option configures a Plot at construction.
func DPR ¶
DPR sets the device pixel ratio. Backends that rasterize multiply the pixel buffer by it; coordinates stay in device-independent units either way. The default is 1.
func Legend ¶
Legend forces the legend on or off. By default a legend appears once a plot has more than one layer: one series does not need to be told apart from anything.
type Plot ¶
type Plot struct {
// contains filtered or unexported fields
}
Plot is a chart specification: size, theme, scales and layers.
A Plot is not safe for concurrent modification. Rendering the same Plot twice is supported and produces the same result, provided the underlying data has not changed.
func (*Plot) Facet ¶ added in v0.3.0
Facet splits the plot into small multiples, one panel per value of a column. See facet.Wrap and facet.Grid.
p.Facet(facet.Wrap("region", facet.Columns(3)))
Passing nil turns faceting back off.
func (*Plot) Render ¶
Render draws the plot into t.
It opens the target, lowers the chart into the backend it returns, flushes, and closes the target — so a file target has a complete file on disk when Render returns nil.
func (*Plot) X ¶
X sets the horizontal scale. The default is scale.Linear with nicing.
type Source ¶
Source is a columnar data source. See package data.
func Float64Columns ¶
Float64Columns builds a Source over numeric columns, borrowing the slices. See data.Float64Columns.
type Target ¶
Target is a render destination. See package ir.
func PDF ¶ added in v0.3.0
PDF returns a target writing a PDF document to the named file.
Like SVG, this is a zero-dependency path: the emitter is in backend/pdf and uses nothing but the standard library. The page is one PDF point per device-independent pixel, so a chart sized 800x500 is an 800x500pt page.
Directories
¶
| Path | Synopsis |
|---|---|
|
arrow
module
|
|
|
backend
|
|
|
pdf
Package pdf renders a chart to PDF using nothing but the standard library.
|
Package pdf renders a chart to PDF using nothing but the standard library. |
|
svg
Package svg is refract's built-in, zero-dependency SVG backend.
|
Package svg is refract's built-in, zero-dependency SVG backend. |
|
gg
module
|
|
|
gg/gpu
module
|
|
|
window
module
|
|
|
Package data is refract's data layer: columnar, batch-oriented access to a table of values.
|
Package data is refract's data layer: columnar, batch-oriented access to a table of values. |
|
examples
|
|
|
categories
command
Command categories renders the categorical chart from the README.
|
Command categories renders the categorical chart from the README. |
|
dashboard
command
Command dashboard renders the v0.3 additions: small multiples, annotations, a colourbar, a grid of subplots, and PDF output.
|
Command dashboard renders the v0.3 additions: small multiples, annotations, a colourbar, a grid of subplots, and PDF output. |
|
signal
command
Command signal renders the chart from CONCEPT.md §13.
|
Command signal renders the chart from CONCEPT.md §13. |
|
Package facet splits one chart into small multiples.
|
Package facet splits one chart into small multiples. |
|
Package geom holds the visual marks a chart is made of.
|
Package geom holds the visual marks a chart is made of. |
|
internal
|
|
|
fontmetrics
Package fontmetrics answers "how wide is this string" using nothing but the standard library.
|
Package fontmetrics answers "how wide is this string" using nothing but the standard library. |
|
irtest
Package irtest provides a recording ir.Backend for tests.
|
Package irtest provides a recording ir.Backend for tests. |
|
markers
Package markers builds the outline of a scatter marker.
|
Package markers builds the outline of a scatter marker. |
|
svgdiff
Package svgdiff compares two SVG documents as drawings rather than as bytes.
|
Package svgdiff compares two SVG documents as drawings rather than as bytes. |
|
Package ir defines refract's intermediate representation: a small, backend-agnostic scene description, plus the Backend interface every renderer implements.
|
Package ir defines refract's intermediate representation: a small, backend-agnostic scene description, plus the Backend interface every renderer implements. |
|
Package layout decides where the plot area, titles and guides go.
|
Package layout decides where the plot area, titles and guides go. |
|
Package palette provides colours and colour sequences for charts.
|
Package palette provides colours and colour sequences for charts. |
|
Package render lowers a resolved chart into IR.
|
Package render lowers a resolved chart into IR. |
|
Package scale maps data values onto visual positions and generates the ticks that label them.
|
Package scale maps data values onto visual positions and generates the ticks that label them. |
|
Package theme holds the visual tokens a chart is drawn with: colours, fonts, sizes and spacings.
|
Package theme holds the visual tokens a chart is drawn with: colours, fonts, sizes and spacings. |











