scales

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BandScale

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

BandScale implements a categorical scale with bands. Maps discrete domain values (categories) to continuous range bands. Used for bar charts, where each category gets a band with optional padding.

Ranges use units.Length to support relative units (%, px, em, etc.).

Example:

scale := NewBandScale([]string{"A", "B", "C"}, [2]units.Length{units.Px(0), units.Px(300)})
scale.Apply("A") // Returns units.Px(0) (start of first band)
scale.Apply("B") // Returns units.Px(100) (start of second band)
scale.Bandwidth() // Returns units.Px(100) (width of each band)

func NewBandScale

func NewBandScale(domain []string, range_ [2]units.Length) *BandScale

NewBandScale creates a new band scale

func (*BandScale) Align

func (s *BandScale) Align(align float64) *BandScale

Align sets the alignment within range (0-1, 0.5 = center)

func (*BandScale) Apply

func (s *BandScale) Apply(value interface{}) units.Length

Apply maps a domain value to a range value (band start position)

func (*BandScale) ApplyValue

func (s *BandScale) ApplyValue(value interface{}) float64

ApplyValue maps a domain value to a normalized value (index / domain size)

func (*BandScale) Bandwidth

func (s *BandScale) Bandwidth() units.Length

Bandwidth returns the width of each band

func (*BandScale) Clone

func (s *BandScale) Clone() Scale

Clone creates a copy of this scale

func (*BandScale) Domain

func (s *BandScale) Domain() interface{}

Domain returns the input domain

func (*BandScale) Index

func (s *BandScale) Index(value string) int

Index returns the index of a value in the domain

func (*BandScale) Padding

func (s *BandScale) Padding(padding float64) *BandScale

Padding sets the outer padding (0-1)

func (*BandScale) PaddingInner

func (s *BandScale) PaddingInner(padding float64) *BandScale

PaddingInner sets the inner padding between bands (0-1)

func (*BandScale) PaddingOuter

func (s *BandScale) PaddingOuter(padding float64) *BandScale

PaddingOuter sets the outer padding at edges (0-1)

func (*BandScale) Range

func (s *BandScale) Range() [2]units.Length

Range returns the output range

func (*BandScale) Round

func (s *BandScale) Round(round bool) *BandScale

Round enables/disables rounding band positions to integers

func (*BandScale) Step

func (s *BandScale) Step() units.Length

Step returns the step size (band + padding)

func (*BandScale) Type

func (s *BandScale) Type() ScaleType

Type returns the scale type

func (*BandScale) Values

func (s *BandScale) Values() []string

Values returns all domain values

func (*BandScale) WithDomain

func (s *BandScale) WithDomain(domain []string) *BandScale

WithDomain sets a new domain

func (*BandScale) WithRange

func (s *BandScale) WithRange(range_ [2]units.Length) *BandScale

WithRange sets a new range

type BandScaleOptions

type BandScaleOptions struct {
	Padding      float64 // Outer padding (0-1)
	PaddingInner float64 // Inner padding between bands (0-1)
	PaddingOuter float64 // Outer padding at edges (0-1)
	Align        float64 // Alignment within range (0-1, 0.5 = center)
	Round        bool    // Round band positions to integers
}

BandScaleOptions configures BandScale behavior

type CategoricalColorScale

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

CategoricalColorScale maps discrete categories to distinct colors. Each category gets its own color, with cycling for domains larger than the color range.

Example:

colors := []color.Color{
  color.RGB(1, 0, 0), // Red
  color.RGB(0, 1, 0), // Green
  color.RGB(0, 0, 1), // Blue
}
scale := NewCategoricalColorScale(
  []string{"Group A", "Group B", "Group C"},
  colors,
)
scale.ApplyColor("Group A") // Returns red

Common uses: categorical data, group comparisons, qualitative differences

func NewCategoricalColorScale

func NewCategoricalColorScale(domain []string, colors []color.Color) *CategoricalColorScale

NewCategoricalColorScale creates a new categorical color scale

func (*CategoricalColorScale) Apply

func (s *CategoricalColorScale) Apply(value interface{}) units.Length

Apply maps a domain value (dummy for Scale interface)

func (*CategoricalColorScale) ApplyColor

func (s *CategoricalColorScale) ApplyColor(value interface{}) color.Color

ApplyColor maps a category to a color

func (*CategoricalColorScale) ApplyValue

func (s *CategoricalColorScale) ApplyValue(value interface{}) float64

ApplyValue maps a category to its index (normalized)

func (*CategoricalColorScale) Clone

func (s *CategoricalColorScale) Clone() Scale

Clone creates a copy of this scale

func (*CategoricalColorScale) Colors

func (s *CategoricalColorScale) Colors() []color.Color

Colors returns all colors in the scale

func (*CategoricalColorScale) Domain

func (s *CategoricalColorScale) Domain() interface{}

Domain returns the input domain

func (*CategoricalColorScale) Range

func (s *CategoricalColorScale) Range() [2]units.Length

Range returns a dummy range

func (*CategoricalColorScale) Type

func (s *CategoricalColorScale) Type() ScaleType

Type returns the scale type

func (*CategoricalColorScale) Unknown

Unknown sets the color for unknown categories

type CategoricalScale

type CategoricalScale interface {
	Scale

	// Values returns all domain values
	Values() []string

	// Index returns the index of a value in the domain
	Index(value string) int
}

CategoricalScale extends Scale for categorical scales (Band, Point, Ordinal)

type ColorInterpolatorFunc

type ColorInterpolatorFunc func(t float64) color.Color

ColorInterpolatorFunc is a function that interpolates colors

type ColorScale

type ColorScale interface {
	Scale

	// ApplyColor maps a domain value to a color
	ApplyColor(value interface{}) color.Color

	// Interpolator returns the color interpolation function
	Interpolator() func(t float64) color.Color
}

ColorScale maps domain values to colors

type ContinuousScale

type ContinuousScale interface {
	Scale

	// Invert maps a range value back to a domain value
	Invert(value units.Length) float64

	// InvertValue maps a normalized value (0-1) back to a domain value
	InvertValue(t float64) float64

	// Ticks generates nice tick values for axes
	Ticks(count int) []float64

	// Nice rounds the domain to nice numbers
	Nice(count int) ContinuousScale

	// Clamp enables/disables clamping output to range
	Clamp(enabled bool) ContinuousScale
}

ContinuousScale extends Scale for continuous numeric scales

type DivergingColorScale

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

DivergingColorScale maps a continuous domain to a diverging color gradient. Diverging scales use two distinct hues meeting at a neutral midpoint, ideal for data with a natural center or comparing deviations from a reference.

Example:

scale := NewDivergingColorScale(
  [2]float64{-100, 100},
  color.RGB(0, 0, 1), // Blue (negative)
  color.RGB(1, 1, 1), // White (neutral)
  color.RGB(1, 0, 0), // Red (positive)
)
scale.Apply(-50) // Returns light blue
scale.Apply(0)   // Returns white
scale.Apply(50)  // Returns light red

Common uses: temperature anomalies, profit/loss, before/after comparisons

func NewDivergingColorScale

func NewDivergingColorScale(domain [2]float64, start, mid, end color.Color) *DivergingColorScale

NewDivergingColorScale creates a new diverging color scale

func (*DivergingColorScale) Apply

func (s *DivergingColorScale) Apply(value interface{}) units.Length

Apply maps a domain value to a color (dummy for Scale interface)

func (*DivergingColorScale) ApplyColor

func (s *DivergingColorScale) ApplyColor(value interface{}) color.Color

ApplyColor maps a domain value to a color

func (*DivergingColorScale) ApplyValue

func (s *DivergingColorScale) ApplyValue(value interface{}) float64

ApplyValue maps a domain value to a normalized value

func (*DivergingColorScale) Clamp

func (s *DivergingColorScale) Clamp(enabled bool) *DivergingColorScale

Clamp enables/disables clamping

func (*DivergingColorScale) Clone

func (s *DivergingColorScale) Clone() Scale

Clone creates a copy of this scale

func (*DivergingColorScale) Domain

func (s *DivergingColorScale) Domain() interface{}

Domain returns the input domain

func (*DivergingColorScale) Interpolate

Interpolate sets a custom interpolation function

func (*DivergingColorScale) Midpoint

func (s *DivergingColorScale) Midpoint(value float64) *DivergingColorScale

Midpoint sets the domain value for the midpoint color

func (*DivergingColorScale) Range

func (s *DivergingColorScale) Range() [2]units.Length

Range returns a dummy range

func (*DivergingColorScale) Samples

func (s *DivergingColorScale) Samples(n int) []color.Color

Samples generates n evenly-spaced color samples

func (*DivergingColorScale) Space

Space sets the color interpolation space

func (*DivergingColorScale) Type

func (s *DivergingColorScale) Type() ScaleType

Type returns the scale type

type InterpolatorFunc

type InterpolatorFunc func(t float64) float64

InterpolatorFunc is a function that interpolates between two values

type LinearScale

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

LinearScale implements a continuous linear scale. Maps a continuous domain [d0, d1] to a continuous range [r0, r1] using linear interpolation.

Ranges use units.Length to support relative units (%, px, em, etc.).

Example:

scale := NewLinearScale([2]float64{0, 100}, [2]units.Length{units.Px(0), units.Px(500)})
scale.Apply(50) // Returns units.Px(250)
scale.Invert(units.Px(250)) // Returns 50

func NewLinearScale

func NewLinearScale(domain [2]float64, range_ [2]units.Length) *LinearScale

NewLinearScale creates a new linear scale

func (*LinearScale) Apply

func (s *LinearScale) Apply(value interface{}) units.Length

Apply maps a domain value to a range value

func (*LinearScale) ApplyValue

func (s *LinearScale) ApplyValue(value interface{}) float64

ApplyValue maps a domain value to a normalized value (0-1 interpolation factor)

func (*LinearScale) Clamp

func (s *LinearScale) Clamp(enabled bool) ContinuousScale

Clamp enables/disables clamping output to range

func (*LinearScale) Clone

func (s *LinearScale) Clone() Scale

Clone creates a copy of this scale

func (*LinearScale) Domain

func (s *LinearScale) Domain() interface{}

Domain returns the input domain

func (*LinearScale) Invert

func (s *LinearScale) Invert(value units.Length) float64

Invert maps a range value back to a domain value

func (*LinearScale) InvertValue

func (s *LinearScale) InvertValue(t float64) float64

InvertValue maps a normalized value (0-1) back to a domain value

func (*LinearScale) Nice

func (s *LinearScale) Nice(count int) ContinuousScale

Nice rounds the domain to nice numbers

func (*LinearScale) Range

func (s *LinearScale) Range() [2]units.Length

Range returns the output range

func (*LinearScale) Ticks

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

Ticks generates nice tick values for axes

func (*LinearScale) Type

func (s *LinearScale) Type() ScaleType

Type returns the scale type

func (*LinearScale) WithDomain

func (s *LinearScale) WithDomain(domain [2]float64) *LinearScale

WithDomain sets a new domain

func (*LinearScale) WithRange

func (s *LinearScale) WithRange(range_ [2]units.Length) *LinearScale

WithRange sets a new range

type LogScale

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

LogScale implements a continuous logarithmic scale. Maps a continuous domain [d0, d1] to a continuous range [r0, r1] using logarithmic interpolation.

Ranges use units.Length to support relative units (%, px, em, etc.).

Example:

scale := NewLogScale([2]float64{1, 1000}, [2]units.Length{units.Px(0), units.Px(500)})
scale.Apply(1)    // Returns units.Px(0)
scale.Apply(10)   // Returns units.Px(166.67) - 1/3 of way on log scale
scale.Apply(100)  // Returns units.Px(333.33) - 2/3 of way
scale.Apply(1000) // Returns units.Px(500)

Log scales are useful for data spanning multiple orders of magnitude: - Population (1K to 1B) - Wealth/income distributions - Scientific measurements - Earthquake magnitudes

func NewLogScale

func NewLogScale(domain [2]float64, range_ [2]units.Length) *LogScale

NewLogScale creates a new log scale with base 10

func (*LogScale) Apply

func (s *LogScale) Apply(value interface{}) units.Length

Apply maps a domain value to a range value

func (*LogScale) ApplyValue

func (s *LogScale) ApplyValue(value interface{}) float64

ApplyValue maps a domain value to a normalized value (0-1 interpolation factor)

func (*LogScale) Base

func (s *LogScale) Base(base float64) *LogScale

Base sets the logarithm base

func (*LogScale) Clamp

func (s *LogScale) Clamp(enabled bool) ContinuousScale

Clamp enables/disables clamping output to range

func (*LogScale) Clone

func (s *LogScale) Clone() Scale

Clone creates a copy of this scale

func (*LogScale) Domain

func (s *LogScale) Domain() interface{}

Domain returns the input domain

func (*LogScale) Invert

func (s *LogScale) Invert(value units.Length) float64

Invert maps a range value back to a domain value

func (*LogScale) InvertValue

func (s *LogScale) InvertValue(t float64) float64

InvertValue maps a normalized value (0-1) back to a domain value

func (*LogScale) Nice

func (s *LogScale) Nice(count int) ContinuousScale

Nice rounds the domain to nice powers of the base

func (*LogScale) Range

func (s *LogScale) Range() [2]units.Length

Range returns the output range

func (*LogScale) Ticks

func (s *LogScale) Ticks(count int) []float64

Ticks generates nice tick values for axes (powers of base)

func (*LogScale) Type

func (s *LogScale) Type() ScaleType

Type returns the scale type

func (*LogScale) WithDomain

func (s *LogScale) WithDomain(domain [2]float64) *LogScale

WithDomain sets a new domain

func (*LogScale) WithRange

func (s *LogScale) WithRange(range_ [2]units.Length) *LogScale

WithRange sets a new range

type LogScaleOptions

type LogScaleOptions struct {
	Base  float64 // Logarithm base (default: 10)
	Clamp bool    // Clamp output to range
}

LogScaleOptions configures LogScale

type NiceOptions

type NiceOptions struct {
	Count int  // Desired number of ticks
	Floor bool // If true, floor the domain start
	Ceil  bool // If true, ceil the domain end
}

NiceOptions configures the Nice operation

type OrdinalScale

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

OrdinalScale implements a categorical scale that maps discrete domain values to discrete range values. Unlike BandScale (which creates bands), OrdinalScale maps each domain value to a specific output value.

Ranges use units.Length to support relative units (%, px, em, etc.).

Example:

scale := NewOrdinalScale(
  []string{"small", "medium", "large"},
  []units.Length{units.Px(10), units.Px(20), units.Px(30)},
)
scale.Apply("medium") // Returns units.Px(20)

Ordinal scales are commonly used for: - Categorical color mapping - Size mapping (small/medium/large) - Shape mapping (circle/square/triangle) - Any discrete categorical encoding

func NewOrdinalScale

func NewOrdinalScale(domain []string, range_ []units.Length) *OrdinalScale

NewOrdinalScale creates a new ordinal scale

func (*OrdinalScale) Apply

func (s *OrdinalScale) Apply(value interface{}) units.Length

Apply maps a domain value to a range value

func (*OrdinalScale) ApplyValue

func (s *OrdinalScale) ApplyValue(value interface{}) float64

ApplyValue maps a domain value to a normalized value For ordinal scales, this returns the index position normalized to [0, 1]

func (*OrdinalScale) Clone

func (s *OrdinalScale) Clone() Scale

Clone creates a copy of this scale

func (*OrdinalScale) Domain

func (s *OrdinalScale) Domain() interface{}

Domain returns the input domain

func (*OrdinalScale) Index

func (s *OrdinalScale) Index(value string) int

Index returns the index of a value in the domain

func (*OrdinalScale) Range

func (s *OrdinalScale) Range() [2]units.Length

Range returns the output range

func (*OrdinalScale) RangeValues

func (s *OrdinalScale) RangeValues() []units.Length

RangeValues returns all range values

func (*OrdinalScale) Type

func (s *OrdinalScale) Type() ScaleType

Type returns the scale type

func (*OrdinalScale) Unknown

func (s *OrdinalScale) Unknown(value units.Length) *OrdinalScale

Unknown sets the return value for unknown domain values

func (*OrdinalScale) Values

func (s *OrdinalScale) Values() []string

Values returns all domain values

func (*OrdinalScale) WithDomain

func (s *OrdinalScale) WithDomain(domain []string) *OrdinalScale

WithDomain sets a new domain

func (*OrdinalScale) WithRange

func (s *OrdinalScale) WithRange(range_ []units.Length) *OrdinalScale

WithRange sets a new range

type PointScale

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

PointScale implements a categorical scale that maps discrete domain values to evenly-spaced points. Unlike BandScale (which has bandwidth), PointScale maps to single positions with no width.

Ranges use units.Length to support relative units (%, px, em, etc.).

Example:

scale := NewPointScale(
  []string{"A", "B", "C"},
  [2]units.Length{units.Px(0), units.Px(300)},
)
scale.Apply("A") // Returns units.Px(0) (first point)
scale.Apply("B") // Returns units.Px(150) (middle point)
scale.Apply("C") // Returns units.Px(300) (last point)

Point scales are commonly used for: - Scatter plot categorical axes - Dot plots - Categorical axes in line charts - Any visualization needing evenly-spaced categorical positions

func NewPointScale

func NewPointScale(domain []string, range_ [2]units.Length) *PointScale

NewPointScale creates a new point scale

func (*PointScale) Align

func (s *PointScale) Align(align float64) *PointScale

Align sets the alignment within range (0-1, 0.5 = center)

func (*PointScale) Apply

func (s *PointScale) Apply(value interface{}) units.Length

Apply maps a domain value to a range value (point position)

func (*PointScale) ApplyValue

func (s *PointScale) ApplyValue(value interface{}) float64

ApplyValue maps a domain value to a normalized value (index / domain size)

func (*PointScale) Clone

func (s *PointScale) Clone() Scale

Clone creates a copy of this scale

func (*PointScale) Domain

func (s *PointScale) Domain() interface{}

Domain returns the input domain

func (*PointScale) Index

func (s *PointScale) Index(value string) int

Index returns the index of a value in the domain

func (*PointScale) Padding

func (s *PointScale) Padding(padding float64) *PointScale

Padding sets the outer padding (0-1)

func (*PointScale) Range

func (s *PointScale) Range() [2]units.Length

Range returns the output range

func (*PointScale) Round

func (s *PointScale) Round(round bool) *PointScale

Round enables/disables rounding point positions to integers

func (*PointScale) Step

func (s *PointScale) Step() units.Length

Step returns the step size (distance between points)

func (*PointScale) Type

func (s *PointScale) Type() ScaleType

Type returns the scale type

func (*PointScale) Values

func (s *PointScale) Values() []string

Values returns all domain values

func (*PointScale) WithDomain

func (s *PointScale) WithDomain(domain []string) *PointScale

WithDomain sets a new domain

func (*PointScale) WithRange

func (s *PointScale) WithRange(range_ [2]units.Length) *PointScale

WithRange sets a new range

type PowScale

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

PowScale implements a continuous power scale. Maps a continuous domain [d0, d1] to a continuous range [r0, r1] using power transformation (value^exponent).

Ranges use units.Length to support relative units (%, px, em, etc.).

Example:

scale := NewPowScale([2]float64{0, 100}, [2]units.Length{units.Px(0), units.Px(500)})
scale.Exponent(2) // Square transformation
scale.Apply(0)   // Returns units.Px(0)
scale.Apply(50)  // Returns units.Px(125) - (50/100)^2 * 500
scale.Apply(100) // Returns units.Px(500)

Power scales are useful for: - Area scales (exponent = 0.5 for sqrt, radius → area) - Volume scales (exponent = 1/3 for cube root) - Custom non-linear transformations - Emphasizing small or large values

func NewPowScale

func NewPowScale(domain [2]float64, range_ [2]units.Length) *PowScale

NewPowScale creates a new power scale with exponent 1 (linear)

func NewSqrtScale

func NewSqrtScale(domain [2]float64, range_ [2]units.Length) *PowScale

NewSqrtScale creates a new square root scale (exponent = 0.5)

func (*PowScale) Apply

func (s *PowScale) Apply(value interface{}) units.Length

Apply maps a domain value to a range value

func (*PowScale) ApplyValue

func (s *PowScale) ApplyValue(value interface{}) float64

ApplyValue maps a domain value to a normalized value (0-1 interpolation factor)

func (*PowScale) Clamp

func (s *PowScale) Clamp(enabled bool) ContinuousScale

Clamp enables/disables clamping output to range

func (*PowScale) Clone

func (s *PowScale) Clone() Scale

Clone creates a copy of this scale

func (*PowScale) Domain

func (s *PowScale) Domain() interface{}

Domain returns the input domain

func (*PowScale) Exponent

func (s *PowScale) Exponent(exponent float64) *PowScale

Exponent sets the power exponent

func (*PowScale) Invert

func (s *PowScale) Invert(value units.Length) float64

Invert maps a range value back to a domain value

func (*PowScale) InvertValue

func (s *PowScale) InvertValue(t float64) float64

InvertValue maps a normalized value (0-1) back to a domain value

func (*PowScale) Nice

func (s *PowScale) Nice(count int) ContinuousScale

Nice rounds the domain to nice numbers For power scales, we use the same nice logic as linear scales

func (*PowScale) Range

func (s *PowScale) Range() [2]units.Length

Range returns the output range

func (*PowScale) Ticks

func (s *PowScale) Ticks(count int) []float64

Ticks generates nice tick values for axes For power scales, we generate linear ticks (not transformed)

func (*PowScale) Type

func (s *PowScale) Type() ScaleType

Type returns the scale type

func (*PowScale) WithDomain

func (s *PowScale) WithDomain(domain [2]float64) *PowScale

WithDomain sets a new domain

func (*PowScale) WithRange

func (s *PowScale) WithRange(range_ [2]units.Length) *PowScale

WithRange sets a new range

type PowScaleOptions

type PowScaleOptions struct {
	Exponent float64 // Power exponent (default: 1)
	Clamp    bool    // Clamp output to range
}

PowScaleOptions configures PowScale

type QuantizeScaleOptions

type QuantizeScaleOptions struct {
	Thresholds []float64 // Explicit threshold values
	Nice       bool      // Round thresholds to nice numbers
}

QuantizeScaleOptions configures QuantizeScale

type Scale

type Scale interface {
	// Apply maps a domain value to a range value
	Apply(value interface{}) units.Length

	// ApplyValue maps a domain value to a normalized value (0-1 interpolation factor)
	ApplyValue(value interface{}) float64

	// Domain returns the input domain
	Domain() interface{}

	// Range returns the output range
	Range() [2]units.Length

	// Type returns the scale type
	Type() ScaleType

	// Clone creates a copy of this scale
	Clone() Scale
}

Scale is the universal interface for all scale types. Scales map from a domain (input data space) to a range (output visual space).

Ranges use units.Length to support relative units (%, px, em, etc.). Values stay abstract until final rendering, minimizing conversion errors.

Inspired by D3 scales and Observable Plot scales.

type ScaleConfig

type ScaleConfig struct {
	Type         ScaleType
	Domain       interface{}     // Type depends on scale type
	Range        [2]units.Length // Output range with units
	Clamp        bool
	Nice         bool
	NiceCount    int
	Round        bool
	Padding      float64
	PaddingInner float64
	PaddingOuter float64
	Align        float64
	Base         float64      // For log scales
	Exponent     float64      // For pow scales
	Unknown      units.Length // For ordinal scales
	Interpolator interface{}  // InterpolatorFunc or ColorInterpolatorFunc
}

ScaleConfig is a universal configuration for creating scales

func DefaultScaleConfig

func DefaultScaleConfig() ScaleConfig

DefaultScaleConfig returns default configuration

type ScaleType

type ScaleType int

ScaleType identifies the type of scale

const (
	ScaleTypeLinear ScaleType = iota
	ScaleTypeLog
	ScaleTypePow
	ScaleTypeSqrt
	ScaleTypeOrdinal
	ScaleTypeBand
	ScaleTypePoint
	ScaleTypeTime
	ScaleTypeQuantize
	ScaleTypeQuantile
	ScaleTypeThreshold
	ScaleTypeIdentity
	ScaleTypeSequential
	ScaleTypeDiverging
)

func (ScaleType) String

func (t ScaleType) String() string

String returns the scale type name

type SequentialColorScale

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

SequentialColorScale maps a continuous domain to a sequential color gradient. Sequential scales use a single hue progressing from light to dark, ideal for representing magnitude or intensity.

Example:

scale := NewSequentialColorScale(
  [2]float64{0, 100},
  color.RGB(1, 1, 1), // White
  color.RGB(0, 0, 1), // Blue
)
scale.Apply(50) // Returns middle blue color

Common uses: heatmaps, choropleth maps, magnitude encoding

func NewSequentialColorScale

func NewSequentialColorScale(domain [2]float64, start, end color.Color) *SequentialColorScale

NewSequentialColorScale creates a new sequential color scale

func (*SequentialColorScale) Apply

func (s *SequentialColorScale) Apply(value interface{}) units.Length

Apply maps a domain value to a color

func (*SequentialColorScale) ApplyColor

func (s *SequentialColorScale) ApplyColor(value interface{}) color.Color

ApplyColor maps a domain value to a color

func (*SequentialColorScale) ApplyValue

func (s *SequentialColorScale) ApplyValue(value interface{}) float64

ApplyValue maps a domain value to a normalized value (0-1 interpolation factor)

func (*SequentialColorScale) Clamp

func (s *SequentialColorScale) Clamp(enabled bool) *SequentialColorScale

Clamp enables/disables clamping output to range

func (*SequentialColorScale) Clone

func (s *SequentialColorScale) Clone() Scale

Clone creates a copy of this scale

func (*SequentialColorScale) Domain

func (s *SequentialColorScale) Domain() interface{}

Domain returns the input domain

func (*SequentialColorScale) Interpolate

Interpolate sets a custom interpolation function

func (*SequentialColorScale) Range

func (s *SequentialColorScale) Range() [2]units.Length

Range returns a dummy range for Scale interface compatibility

func (*SequentialColorScale) Samples

func (s *SequentialColorScale) Samples(n int) []color.Color

Samples generates n evenly-spaced color samples

func (*SequentialColorScale) Space

Space sets the color interpolation space

func (*SequentialColorScale) Type

func (s *SequentialColorScale) Type() ScaleType

Type returns the scale type

type SequentialScaleOptions

type SequentialScaleOptions struct {
	Interpolator ColorInterpolatorFunc // Color interpolation function
	Clamp        bool                  // Clamp output
}

SequentialScaleOptions configures SequentialScale

type TickOptions

type TickOptions struct {
	Count  int       // Desired number of ticks
	Format string    // Format string for tick labels
	Values []float64 // Explicit tick values (overrides Count)
}

TickOptions configures tick generation

type TimeInterval

type TimeInterval int

TimeInterval represents time intervals for TimeScale

const (
	TimeIntervalMillisecond TimeInterval = iota
	TimeIntervalSecond
	TimeIntervalMinute
	TimeIntervalHour
	TimeIntervalDay
	TimeIntervalWeek
	TimeIntervalMonth
	TimeIntervalYear
)

type TimeScale

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

TimeScale implements a continuous scale for temporal data. Maps a time domain [t0, t1] to a continuous range [r0, r1] using linear interpolation.

Ranges use units.Length to support relative units (%, px, em, etc.).

Example:

start := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
end := time.Date(2024, 12, 31, 0, 0, 0, 0, time.UTC)
scale := NewTimeScale(
  [2]time.Time{start, end},
  [2]units.Length{units.Px(0), units.Px(500)},
)
scale.Apply(time.Date(2024, 7, 1, 0, 0, 0, 0, time.UTC)) // Mid-year position

func NewTimeScale

func NewTimeScale(domain [2]time.Time, range_ [2]units.Length) *TimeScale

NewTimeScale creates a new time scale

func (*TimeScale) Apply

func (s *TimeScale) Apply(value interface{}) units.Length

Apply maps a domain value to a range value

func (*TimeScale) ApplyValue

func (s *TimeScale) ApplyValue(value interface{}) float64

ApplyValue maps a domain value to a normalized value (0-1 interpolation factor)

func (*TimeScale) Clamp

func (s *TimeScale) Clamp(enabled bool) *TimeScale

Clamp enables/disables clamping output to range

func (*TimeScale) Clone

func (s *TimeScale) Clone() Scale

Clone creates a copy of this scale

func (*TimeScale) Domain

func (s *TimeScale) Domain() interface{}

Domain returns the input domain

func (*TimeScale) Invert

func (s *TimeScale) Invert(value units.Length) time.Time

Invert maps a range value back to a domain value

func (*TimeScale) InvertValue

func (s *TimeScale) InvertValue(t float64) time.Time

InvertValue maps a normalized value (0-1) back to a domain value

func (*TimeScale) Nice

func (s *TimeScale) Nice(interval TimeInterval) *TimeScale

Nice rounds the domain to nice time boundaries

func (*TimeScale) Range

func (s *TimeScale) Range() [2]units.Length

Range returns the output range

func (*TimeScale) Ticks

func (s *TimeScale) Ticks(count int) []time.Time

Ticks generates nice tick values for axes

func (*TimeScale) Type

func (s *TimeScale) Type() ScaleType

Type returns the scale type

func (*TimeScale) WithDomain

func (s *TimeScale) WithDomain(domain [2]time.Time) *TimeScale

WithDomain sets a new domain

func (*TimeScale) WithRange

func (s *TimeScale) WithRange(range_ [2]units.Length) *TimeScale

WithRange sets a new range

type TimeTickOptions

type TimeTickOptions struct {
	Interval TimeInterval // Interval for ticks (year, month, day, etc.)
	Count    int          // Desired number of ticks
	Format   string       // Time format string
}

TimeTickOptions configures time tick generation

type TimeValue

type TimeValue struct {
	Time time.Time
}

TimeValue wraps a time.Time for use in scales

func (TimeValue) Value

func (tv TimeValue) Value() float64

Value returns the Unix timestamp in seconds

Jump to

Keyboard shortcuts

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