scale

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package scale maps data values onto visual positions and generates the ticks that label them.

A Scale owns two things: the domain→range mapping, and the choice of tick positions and labels for that domain. Keeping both in one place is what lets a time axis label itself in calendar units while a linear axis labels itself with round numbers, without either the geom or the layout knowing which is which.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromNanos

func FromNanos(v float64) time.Time

FromNanos converts a time scale's domain value back to a time.

func Nanos

func Nanos(t time.Time) float64

Nanos converts a time to the float64 domain value a time scale uses.

Types

type LinearOption

type LinearOption func(*linear)

LinearOption configures a linear scale.

func Domain

func Domain(min, max float64) LinearOption

Domain pins the data domain explicitly, disabling training.

func Format

func Format(fn func(v float64) string) LinearOption

Format overrides tick label formatting.

func Nice

func Nice() LinearOption

Nice expands the domain outwards to the tick sequence's own bounds, so the axis starts and ends on a labelled tick rather than on the extreme data value. This is what most charts want and what CONCEPT.md §13 shows.

func Zero

func Zero() LinearOption

Zero forces the domain to include zero. Bar charts need this: a bar chart whose baseline is off-screen misleads.

type Scale

type Scale interface {
	// Train extends the scale's data domain to include vs. Values that are
	// NaN or infinite are ignored. Calling Train repeatedly accumulates.
	Train(vs ...float64)

	// SetRange sets the device-space output interval. lo may be greater than
	// hi, which is how a Y axis is flipped so that larger values are higher on
	// screen.
	SetRange(lo, hi float32)

	// Domain reports the current data domain, after any nicing.
	Domain() (min, max float64)

	// Map converts a data value to a device position. Values outside the
	// domain map outside the range; clipping is the caller's business.
	Map(v float64) float32

	// Invert converts a device position back to a data value. It is the
	// inverse of Map over the whole real line, not just the range.
	Invert(pos float32) float64

	// Ticks returns tick positions and labels, aiming for about want ticks.
	// The result is ordered ascending by Value.
	Ticks(want int) []Tick
}

Scale maps data values onto a device-space range.

A scale is trained on data, given a device range, and then queried. The order matters: Map and Ticks are only meaningful once both the domain and the range are set.

func Linear

func Linear(opts ...LinearOption) Scale

Linear returns a linear scale.

func Time

func Time(opts ...TimeOption) Scale

Time returns a time scale.

Its domain is carried as float64 Unix nanoseconds, so it satisfies the same Scale interface as every other scale and geoms need no special case. Use Nanos and FromNanos to convert.

type Tick

type Tick struct {
	// Value is the tick's position in data space.
	Value float64
	// Pos is the tick's position in device space, already mapped.
	Pos float32
	// Label is the formatted text for the tick. An empty label means a tick
	// mark and grid line are drawn but nothing is written.
	Label string
	// Minor marks a tick that subdivides the axis without a label.
	Minor bool
}

Tick is one labelled position on an axis.

type TimeOption

type TimeOption func(*timeScale)

TimeOption configures a time scale.

func In

func In(loc *time.Location) TimeOption

In sets the location used to compute calendar ticks and format labels. The default is time.UTC, so that a chart renders identically wherever it is built — a server rendering in the client's local zone is a bug, not a feature.

func TimeFormat

func TimeFormat(fn func(t time.Time, unit time.Duration) string) TimeOption

TimeFormat overrides tick label formatting. The unit the tick sequence settled on is passed so a caller can vary detail with zoom level.

Jump to

Keyboard shortcuts

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