scale

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package scale holds the per-type scale implementations consumed by the encode stage. P06 ships the 8 canonical scale types: linear, log, pow, sqrt, time, band, point, ordinal. Each implementation satisfies the Scale interface declared here.

The package is leaf-y on purpose: it imports encode/scene (for the canonical ScaleType enum + Color) and errors, but nothing from encode/ itself. The dispatch lives in encode/scale.go so back-compat callers continue to use encode.ResolveScale.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToEpochMs

func ToEpochMs(v any) (float64, bool)

ToEpochMs converts a time-shaped value (time.Time, ISO-8601 string, numeric epoch ms) to float64 epoch ms.

func ToFloat

func ToFloat(v any) (float64, bool)

ToFloat coerces common Go numeric types to float64. Returns (0, false) for NaN / Inf / non-numeric inputs. Exported so the encoder + per-scale apply paths share one definition.

Types

type BandScale

type BandScale struct {
	Categories []string
	RangeMin   float64
	RangeMax   float64
	Padding    float64 // [0,1) inner padding (fraction of step)
}

BandScale is the categorical scale used by bar / rect marks. Each category gets a band of equal width; padding leaves an inner gap.

func (*BandScale) Apply

func (s *BandScale) Apply(value any) (float64, error)

Apply implements Scale. Returns the left edge of the band for the given category.

func (*BandScale) BandCenter

func (s *BandScale) BandCenter(cat string) (float64, error)

BandCenter returns the center x of the band for category cat.

func (*BandScale) BandWidth

func (s *BandScale) BandWidth() float64

BandWidth returns the pixel width of one band (post-padding).

func (*BandScale) Domain

func (s *BandScale) Domain() []any

Domain implements Scale.

func (*BandScale) Range

func (s *BandScale) Range() [2]float64

Range implements Scale.

func (*BandScale) Type

func (s *BandScale) Type() scene.ScaleType

Type implements Scale.

type BandSizer

type BandSizer interface {
	BandWidth() float64
}

BandSizer is the optional capability returning a band's pixel width. Implemented by BandScale; bar / rect mark encoders ask for it.

type LinearScale

type LinearScale struct {
	DomainMin float64
	DomainMax float64
	RangeMin  float64
	RangeMax  float64
}

LinearScale is the canonical quantitative scale: linear interpolation from [DomainMin, DomainMax] to [RangeMin, RangeMax]. Range may be inverted (RangeMin > RangeMax) for y-axes where data 0 sits at the bottom of the SVG.

func (*LinearScale) Apply

func (s *LinearScale) Apply(value any) (float64, error)

Apply implements Scale.

func (*LinearScale) Domain

func (s *LinearScale) Domain() []any

Domain implements Scale.

func (*LinearScale) Range

func (s *LinearScale) Range() [2]float64

Range implements Scale.

func (*LinearScale) Type

func (s *LinearScale) Type() scene.ScaleType

Type implements Scale.

type LogScale

type LogScale struct {
	Base      float64
	DomainMin float64
	DomainMax float64
	RangeMin  float64
	RangeMax  float64
}

LogScale interpolates log(value)/log(base) linearly into the pixel range. Domain must be strictly positive — non-positive values raise PRISM_SPEC_010 at resolve time.

func (*LogScale) Apply

func (s *LogScale) Apply(value any) (float64, error)

Apply implements Scale.

func (*LogScale) Domain

func (s *LogScale) Domain() []any

Domain implements Scale.

func (*LogScale) Range

func (s *LogScale) Range() [2]float64

Range implements Scale.

func (*LogScale) Type

func (s *LogScale) Type() scene.ScaleType

Type implements Scale.

type OrdinalScale

type OrdinalScale struct {
	Categories []string
	Positions  []float64 // same length as Categories
}

OrdinalScale maps discrete categories to explicit pixel positions. Used when the caller already knows where each category sits (typical for fixed-position labels, color-scheme indexing).

func (*OrdinalScale) Apply

func (s *OrdinalScale) Apply(value any) (float64, error)

Apply implements Scale.

func (*OrdinalScale) ApplyColor

func (s *OrdinalScale) ApplyColor(category string, palette []*scene.Color) *scene.Color

ApplyColor returns the palette entry indexed by the category's position in Categories (mod len(palette)). Returns nil when either category or palette is empty.

func (*OrdinalScale) Domain

func (s *OrdinalScale) Domain() []any

Domain implements Scale.

func (*OrdinalScale) Range

func (s *OrdinalScale) Range() [2]float64

Range implements Scale.

func (*OrdinalScale) Type

func (s *OrdinalScale) Type() scene.ScaleType

Type implements Scale.

type PointScale

type PointScale struct {
	Categories []string
	RangeMin   float64
	RangeMax   float64
	Padding    float64 // [0,1) outer padding (fraction of step)
}

PointScale places each category at the center of an evenly-divided step. Unlike BandScale it has no bandwidth — Apply returns the point itself, not a band's left edge. Used for line/point fixtures where the x-axis is categorical but the marks need a single coordinate.

func (*PointScale) Apply

func (s *PointScale) Apply(value any) (float64, error)

Apply implements Scale.

func (*PointScale) Domain

func (s *PointScale) Domain() []any

Domain implements Scale.

func (*PointScale) Range

func (s *PointScale) Range() [2]float64

Range implements Scale.

func (*PointScale) Type

func (s *PointScale) Type() scene.ScaleType

Type implements Scale.

type PowScale

type PowScale struct {
	Exp       float64
	DomainMin float64
	DomainMax float64
	RangeMin  float64
	RangeMax  float64
}

PowScale interpolates sign(v)*|v|^exp linearly into the pixel range. Default exp = 1 reduces to a linear scale. The signed-power shape handles negative inputs correctly (Vega-Lite parity).

func (*PowScale) Apply

func (s *PowScale) Apply(value any) (float64, error)

Apply implements Scale.

func (*PowScale) Domain

func (s *PowScale) Domain() []any

Domain implements Scale.

func (*PowScale) Range

func (s *PowScale) Range() [2]float64

Range implements Scale.

func (*PowScale) Type

func (s *PowScale) Type() scene.ScaleType

Type implements Scale.

type Scale

type Scale interface {
	// Apply resolves a data value to its pixel coordinate. Returns
	// PRISM_ENCODE_001 on type / category mismatches.
	Apply(value any) (float64, error)
	// Domain returns the resolved input domain. Cast on read.
	Domain() []any
	// Range returns the [min,max] pixel range the scale maps into.
	Range() [2]float64
	// Type returns the canonical scene.ScaleType for this scale.
	Type() scene.ScaleType
}

Scale resolves a single data value into a pixel coordinate. The concrete impls live one-per-file in this package.

type SqrtScale

type SqrtScale struct {
	Inner PowScale
}

SqrtScale is PowScale with exp=0.5 surfaced as its own type so scene.ScaleType reads correctly.

func (*SqrtScale) Apply

func (s *SqrtScale) Apply(value any) (float64, error)

Apply implements Scale.

func (*SqrtScale) Domain

func (s *SqrtScale) Domain() []any

Domain implements Scale.

func (*SqrtScale) Range

func (s *SqrtScale) Range() [2]float64

Range implements Scale.

func (*SqrtScale) Type

func (s *SqrtScale) Type() scene.ScaleType

Type implements Scale.

type TimeScale

type TimeScale struct {
	Linear *LinearScale
}

TimeScale wraps a LinearScale over epoch-ms. Apply accepts time.Time, ISO-8601 strings, and numeric epoch ms. Calendar-aware tick generation lives in encode/ticks_time.go.

func (*TimeScale) Apply

func (s *TimeScale) Apply(value any) (float64, error)

Apply implements Scale.

func (*TimeScale) Domain

func (s *TimeScale) Domain() []any

Domain implements Scale.

func (*TimeScale) Range

func (s *TimeScale) Range() [2]float64

Range implements Scale.

func (*TimeScale) Type

func (s *TimeScale) Type() scene.ScaleType

Type implements Scale.

Jump to

Keyboard shortcuts

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