scale

package
v0.0.0-...-f10218a Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2021 License: BSD-3-Clause Imports: 2 Imported by: 2

Documentation

Overview

Package scale provides abstractions for scales that map from one domain to another and provide methods for indicating human-readable intervals in the input domain. The most common type of scale is a quantitative scale, such as a linear or log scale, which is captured by the Quantitative interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Linear

type Linear struct {
	// Min and Max specify the lower and upper bounds of the input
	// domain. The input domain [Min, Max] will be linearly mapped
	// to the output range [0, 1].
	Min, Max float64

	// Base specifies a base for computing ticks. Ticks will be
	// placed at powers of Base; that is at n*Base^l for n ∈ ℤ and
	// some integer tick level l. As a special case, a base of 0
	// alternates between ticks at n*10^⌊l/2⌋ and ticks at
	// 5n*10^⌊l/2⌋.
	Base int

	// If Clamp is true, the input is clamped to [Min, Max].
	Clamp bool
}

func (Linear) CountTicks

func (s Linear) CountTicks(level int) int

CountTicks returns the number of ticks in [s.Min, s.Max] at the given tick level.

func (Linear) Map

func (s Linear) Map(x float64) float64

func (*Linear) Nice

func (s *Linear) Nice(o TickOptions)

func (*Linear) SetClamp

func (s *Linear) SetClamp(clamp bool)

func (Linear) Ticks

func (s Linear) Ticks(o TickOptions) (major, minor []float64)

func (Linear) TicksAtLevel

func (s Linear) TicksAtLevel(level int) interface{}

TicksAtLevel returns the tick locations in [s.Min, s.Max] as a []float64 at the given tick level in ascending order.

func (Linear) Unmap

func (s Linear) Unmap(y float64) float64

type Log

type Log struct {

	// Min and Max specify the lower and upper bounds of the input
	// domain. The input range [Min, Max] will be mapped to the
	// output range [0, 1]. The range [Min, Max] must not include
	// 0.
	Min, Max float64

	// Base specifies the base of the logarithm for computing
	// ticks. Ticks will be placed at Base^((2^l)*n) for tick
	// level l ∈ ℕ and n ∈ ℤ. Typically l is 0, in which case this
	// is simply Base^n.
	Base int

	// If Clamp is true, the input is clamped to [Min, Max].
	Clamp bool
	// contains filtered or unexported fields
}

func NewLog

func NewLog(min, max float64, base int) (Log, error)

NewLog constructs a Log scale. If the arguments are out of range, it returns a RangeErr.

func (*Log) CountTicks

func (s *Log) CountTicks(level int) int

func (Log) Map

func (s Log) Map(x float64) float64

func (*Log) Nice

func (s *Log) Nice(o TickOptions)

func (*Log) SetClamp

func (s *Log) SetClamp(clamp bool)

func (Log) Ticks

func (s Log) Ticks(o TickOptions) (major, minor []float64)

func (*Log) TicksAtLevel

func (s *Log) TicksAtLevel(level int) interface{}

func (Log) Unmap

func (s Log) Unmap(y float64) float64

type QQ

type QQ struct {
	Src, Dest Quantitative
}

A QQ maps from a source Quantitative scale to a destination Quantitative scale.

func (QQ) Map

func (q QQ) Map(x float64) float64

Map maps from a value x in the source scale's input domain to a value y in the destination scale's input domain.

func (QQ) Unmap

func (q QQ) Unmap(x float64) float64

Unmap maps from a value y in the destination scale's input domain to a value x in the source scale's input domain.

type Quantitative

type Quantitative interface {
	// Map maps from a value x in the input domain to [0, 1]. If x
	// is outside the input domain and clamping is enabled, x will
	// first be clamped to the input domain.
	Map(x float64) float64

	// Unmap is the inverse of Map. That is, if x is in the input
	// domain or clamping is disabled, x = Unmap(Map(x)). If
	// clamping is enabled and y is outside [0,1], the results are
	// undefined.
	Unmap(y float64) float64

	// SetClamp sets the clamping mode of this scale.
	SetClamp(bool)

	// Ticks returns major and minor ticks that satisfy the
	// constraints given by o. These ticks will have "nice" values
	// within the input domain. Both arrays are sorted in
	// ascending order and minor includes ticks in major.
	Ticks(o TickOptions) (major, minor []float64)

	// Nice expands the input domain of this scale to "nice"
	// values for covering the input domain satisfying the
	// constraints given by o. After calling Nice(o), the first
	// and last major ticks returned by Ticks(o) will equal the
	// lower and upper bounds of the input domain.
	Nice(o TickOptions)

	// A Quantitative scale is also a Ticker.
	Ticker
}

A Quantative scale is an invertible function from some continuous input domain to an output range of [0, 1].

type RangeErr

type RangeErr string

RangeErr is an error that indicates some argument or value is out of range.

func (RangeErr) Error

func (r RangeErr) Error() string

type TickOptions

type TickOptions struct {
	// Max is the maximum number of major ticks to return.
	Max int

	// MinLevel and MaxLevel are the minimum and maximum tick
	// levels to accept, respectively. If they are both 0, there is
	// no limit on acceptable tick levels.
	MinLevel, MaxLevel int
}

TickOptions specifies constraints for constructing scale ticks.

A Ticks method will return the ticks at the lowest level (largest number of ticks) that satisfies all of the constraints. The exact meaning of the tick level differs between scale types, but for all scales higher tick levels result in ticks that are further apart (fewer ticks in a given interval). In general, the minor ticks are the ticks from one level below the major ticks.

func (*TickOptions) FindLevel

func (o *TickOptions) FindLevel(ticker Ticker, guess int) (int, bool)

FindLevel returns the lowest level that satisfies the constraints given by o:

* ticker.CountTicks(level) <= o.Max

* o.MinLevel <= level <= o.MaxLevel (if MinLevel and MaxLevel != 0).

If the constraints cannot be satisfied, it returns 0, false.

guess is the level to start the optimization at.

type Ticker

type Ticker interface {
	// CountTicks returns the number of ticks at level in this
	// scale's input range. This is equivalent to
	// len(TicksAtLevel(level)), but should be much more
	// efficient. CountTicks is a weakly monotonically decreasing
	// function of level.
	CountTicks(level int) int

	// TicksAtLevel returns a slice of "nice" tick values in
	// increasing order at level in this scale's input range.
	// Typically, TicksAtLevel(l+1) is a subset of
	// TicksAtLevel(l). That is, higher levels remove ticks from
	// lower levels.
	TicksAtLevel(level int) interface{}
}

A Ticker computes tick marks for a scale. The "level" of the ticks controls how many ticks there are and how closely they are spaced. Higher levels have fewer ticks, while lower levels have more ticks. For example, on a numerical scale, one could have ticks at every n*(10^level).

Jump to

Keyboard shortcuts

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