layout

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package layout decides where the plot area, titles and guides go.

Compute lays out a single Cartesian panel: axes on the left and bottom, an optional title above, and a column of guides — a legend, colourbars — to the right. Panels lays out a grid of them with their axes aligned, which is what subplots and faceting are made of.

Everything is sized by measuring the real text with the real backend, so an axis whose labels are wide gets a wide margin and nothing is ever clipped by a guessed constant.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chart

type Chart struct {
	// Canvas is the full drawing surface.
	Canvas ir.Rect
	// Theme supplies sizes, paddings and fonts.
	Theme theme.Theme

	// Title is the chart title, or "" for none.
	Title string
	// XTitle and YTitle are axis titles, or "" for none.
	XTitle, YTitle string

	// XLabels and YLabels are the tick label texts. Only their measured
	// extents matter here; their positions are the scales' business.
	XLabels, YLabels []string

	// LegendLabels are the series names. An empty slice suppresses the legend.
	LegendLabels []string

	// Colorbars are the continuous colour guides. Only the extents of their
	// titles and tick labels matter here; where the ticks fall is the colour
	// scale's business, and it needs the bar's rectangle to answer.
	Colorbars []Colorbar
}

Chart is everything layout needs to know about what will be drawn.

type Colorbar added in v0.3.0

type Colorbar struct {
	// Title names the quantity the ramp encodes, or "" for none.
	Title string
	// Ticks are the labels written beside the bar.
	Ticks []string
}

Colorbar is what layout needs to know about one continuous colour guide.

type Grid added in v0.3.0

type Grid struct {
	// Canvas is the full drawing surface.
	Canvas ir.Rect
	// Theme supplies sizes, paddings and fonts.
	Theme theme.Theme

	// Title is the chart title above the whole grid, or "" for none.
	Title string
	// XTitle and YTitle label the shared axes, once for the grid.
	XTitle, YTitle string

	// Rows and Cols are the shape of the grid.
	Rows, Cols int

	// Panels are the panels, in any order. A cell with no panel is a hole.
	Panels []Panel

	// LegendLabels and Colorbars are the guides for the grid as a whole.
	LegendLabels []string
	Colorbars    []Colorbar
}

Grid is a set of Cartesian panels laid out together with their axes aligned.

Alignment is the whole point and it is what makes this a constraint problem rather than a loop. Every panel in a column gets the same horizontal extent and every panel in a row the same vertical one, so a value at the same position means the same thing wherever the reader's eye lands. That is not achievable panel by panel: the width of the widest Y tick label in a column decides where every panel in that column starts, and the panels' common size then falls out of what is left.

type GridResult added in v0.3.0

type GridResult struct {
	// Areas are the panel rectangles, parallel to Grid.Panels.
	Areas []ir.Rect
	// Strips are the label bands above each panel, parallel to Grid.Panels
	// and empty for a panel with no strip.
	Strips []ir.Rect
	// RightStrips are the label bands beside each panel, parallel to
	// Grid.Panels and empty for a panel with none.
	RightStrips []ir.Rect

	// Region is the rectangle the panels and their gutters occupy together.
	// The chart title is centred on it and the guides sit beside it.
	Region ir.Rect

	// Title, XTitle and YTitle are the baseline anchors for the grid's own
	// titles, zero when there is none. YTitle is drawn rotated a quarter turn
	// anticlockwise.
	Title, XTitle, YTitle ir.Point

	// Legend and Colorbars are the guide boxes, as in [Result].
	Legend    ir.Rect
	Colorbars []ir.Rect

	// TickLabelPad is copied from the theme so the renderer does not re-derive
	// it.
	TickLabelPad float32
}

GridResult is where everything goes, in device space.

func Panels added in v0.3.0

func Panels(g Grid, m Measurer) GridResult

Panels lays out a grid.

The order of decisions matters and is the reason this is not four independent calculations: the guides' height depends on how tall the panel region is, the panel region's width depends on how wide the guides are, and both depend on the tick labels — which are known before any of it, because a scale can name its ticks from its domain alone.

type Measurer

type Measurer interface {
	Measure(run ir.TextRun) ir.TextMetrics
}

Measurer is the text-measurement capability layout needs. Every backend provides it; taking the narrow interface rather than the whole backend keeps layout unable to draw anything by accident.

type Panel added in v0.3.0

type Panel struct {
	// Row and Col place the panel.
	Row, Col int

	// Strip is the label written in a band above the panel, or "" for none.
	// It is what a facet is named by.
	Strip string
	// RightStrip is a label written in a band down the panel's right side,
	// reading top to bottom. A two-way facet names its rows this way.
	RightStrip string

	// XLabels and YLabels are the tick labels this panel writes. A panel that
	// shares an axis with the panel beside it leaves them empty and takes the
	// space anyway, so that the panels stay the same size.
	XLabels, YLabels []string
}

Panel is one Cartesian area within a grid.

type Result

type Result struct {
	// Plot is the data area. Scales map into it.
	Plot ir.Rect
	// Title is the baseline anchor for the chart title, horizontally centred
	// on the plot area. Zero if there is no title.
	Title ir.Point
	// XTitle is the baseline anchor for the X axis title, centred on the plot
	// area. Zero if there is none.
	XTitle ir.Point
	// YTitle is the anchor for the Y axis title, centred on the plot area and
	// meant to be drawn rotated a quarter turn anticlockwise. Zero if there is
	// none.
	YTitle ir.Point
	// Legend is the rectangle reserved for the legend, empty if there is none.
	Legend ir.Rect
	// Colorbars are the rectangles reserved for the colour guides, one per
	// entry in Chart.Colorbars and in the same order. Each covers the bar, its
	// tick labels and its title.
	Colorbars []ir.Rect
	// TickLabelPad is the distance from the axis to the near edge of a tick
	// label, copied from the theme so the renderer does not re-derive it.
	TickLabelPad float32
}

Result is where everything goes, in device space.

func Compute

func Compute(c Chart, m Measurer) Result

Compute lays out a single-panel chart.

It is Panels over a one-by-one grid, and it exists because that is the shape almost every chart has: one panel, its axes, a guide column. Going through the same solver is what keeps a lone chart and a facet of one from coming out differently.

Jump to

Keyboard shortcuts

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