scale

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package scale provides scale transformations that map data values to visual aesthetic values. Scales control axis ranges, color mappings, size mappings, and produce the guides (axes and legends) that make plots interpretable.

Continuous scales map a numeric domain [min, max] to a visual range. Discrete scales map categorical values to a set of visual values.

Index

Constants

This section is empty.

Variables

View Source
var Q = []float64{1, 5, 2, 2.5, 4, 3}

Q is the preference-ordered set of "nice" step multipliers. Order encodes simplicity: earlier values are preferred.

Functions

func FormatNumber

func FormatNumber(v float64) string

FormatNumber formats a tick value for display. Integers are shown without decimals, floats use compact notation.

func NiceSequence

func NiceSequence(lo, hi float64, n int) []float64

NiceSequence produces approximately n optimally placed tick positions between lo and hi using the Talbot-Lin-Hanrahan (2010) extended Wilkinson algorithm. See ticks.go for the full implementation.

Types

type BoundsSetter

type BoundsSetter interface {
	SetBounds(mn, mx float64)
}

BoundsSetter is an optional interface for scales that support manual bounds override. Both LinearScale and DiscreteScale implement this.

type DiscreteScale

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

DiscreteScale maps categorical string values to evenly-spaced [0, N-1] positions where N is the number of unique categories.

func Discrete

func Discrete() *DiscreteScale

Discrete returns a discrete (categorical) scale that maps string labels to evenly-spaced numeric positions. Categories are extracted during Train and sorted alphabetically for deterministic ordering.

func (*DiscreteScale) Bounds

func (s *DiscreteScale) Bounds() (float64, float64)

func (*DiscreteScale) Categories

func (s *DiscreteScale) Categories() []string

Categories returns the ordered category labels.

func (*DiscreteScale) Format

func (s *DiscreteScale) Format(v float64) string

func (*DiscreteScale) Inverse

func (s *DiscreteScale) Inverse(v float64) float64

func (*DiscreteScale) Map

func (s *DiscreteScale) Map(v float64) float64

func (*DiscreteScale) MapCategory

func (s *DiscreteScale) MapCategory(label string) float64

MapCategory maps a category label to its numeric position.

func (*DiscreteScale) SetBounds

func (s *DiscreteScale) SetBounds(mn, mx float64)

SetBounds overrides the domain bounds (used by pipeline padding logic).

func (*DiscreteScale) String

func (s *DiscreteScale) String() string

func (*DiscreteScale) Ticks

func (s *DiscreteScale) Ticks(n int) []float64

func (*DiscreteScale) Train

func (s *DiscreteScale) Train(col dataset.AnyColumn) error

Train extracts unique string values from the column. If the column is already numeric (e.g. after stat transforms), this is a no-op.

func (*DiscreteScale) TrainValues

func (s *DiscreteScale) TrainValues(labels []string)

TrainValues trains the scale from a pre-built slice of category labels.

type LinearScale

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

LinearScale is a standard linear continuous scale.

func (*LinearScale) Bounds

func (s *LinearScale) Bounds() (float64, float64)

func (*LinearScale) Format

func (s *LinearScale) Format(v float64) string

func (*LinearScale) Inverse

func (s *LinearScale) Inverse(v float64) float64

func (*LinearScale) Map

func (s *LinearScale) Map(v float64) float64

func (*LinearScale) SetBounds

func (s *LinearScale) SetBounds(mn, mx float64)

SetBounds manually overrides the scale domain. Used by the rendering pipeline for padding and forcing Y=0.

func (*LinearScale) String

func (s *LinearScale) String() string

func (*LinearScale) Ticks

func (s *LinearScale) Ticks(n int) []float64

func (*LinearScale) Train

func (s *LinearScale) Train(col dataset.AnyColumn) error

type Manager

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

Manager holds trained scales for all aesthetic channels.

func NewManager

func NewManager() *Manager

NewManager creates a Manager with default scales.

func (*Manager) Get

func (m *Manager) Get(channel string) Scale

Get retrieves the scale for a channel. Returns a linear scale as default.

func (*Manager) Set

func (m *Manager) Set(channel string, s Scale)

Set registers a scale for an aesthetic channel.

type Scale

type Scale interface {
	// Train updates the scale's domain from the given column data.
	Train(col dataset.AnyColumn) error

	// Map transforms a raw value to the [0,1] normalized scale.
	Map(v float64) float64

	// Inverse maps a [0,1] normalized value back to data space.
	Inverse(v float64) float64

	// Ticks generates n nicely spaced tick positions in data space.
	Ticks(n int) []float64

	// Format converts a data value to a display string.
	Format(v float64) string

	// Bounds returns the trained domain [min, max].
	Bounds() (float64, float64)

	// String returns a description.
	String() string
}

Scale defines the interface for all scale types.

func NewLinear

func NewLinear() Scale

NewLinear returns a standard linear scale.

func NewLog10

func NewLog10() Scale

NewLog10 returns a base-10 logarithmic scale.

func NewReverse

func NewReverse() Scale

NewReverse returns a scale that inverts the axis direction.

func NewSqrt

func NewSqrt() Scale

NewSqrt returns a square-root scale.

func Resolve

func Resolve(t Type) (Scale, error)

Resolve returns a Scale for the given type. Returns an error for unknown types.

type Type

type Type string

Type identifies a scale transformation.

const (
	Linear  Type = "linear"
	Log10   Type = "log10"
	Sqrt    Type = "sqrt"
	Reverse Type = "reverse"
)

Jump to

Keyboard shortcuts

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