Documentation
¶
Overview ¶
Package scale holds the scales of Vega, which map values of the data to values of a chart, such as positions and colors, and the ticks of each scale (D6). The view resolves the domain and the range of a scale from the spec, and this package builds the scale from them.
Index ¶
- Constants
- func Bins(s Scale) []float64
- func Discrete(typ string) bool
- func EndTicks(lo, hi, count float64, most int) ([]float64, float64)
- func IntervalTicks(t0, t1 time.Time, unit string, step int) ([]time.Time, bool)
- func LogLabels(s Scale, count float64) func(float64) bool
- func Nice(lo, hi, count float64) (float64, float64)
- func Sample(at func(float64) string, count int) []any
- func Step(s Scale) float64
- func Thresholds(s Scale) []float64
- func TickIncrement(lo, hi, count float64) float64
- func TickStep(lo, hi, count float64) float64
- func Ticks(lo, hi, count float64) []float64
- func TimeTickUnit(t0, t1 time.Time, count float64) (string, int)
- func TimeTicks(t0, t1 time.Time, count float64) []time.Time
- type Error
- type Options
- type Scale
- type Scheme
Constants ¶
const Trading = "zuzu.trading"
Trading is the type of the scale of D30 that places the days of its domain at even steps, so that a day with no trade leaves no gap.
Variables ¶
This section is empty.
Functions ¶
func Bins ¶ added in v0.8.0
Bins returns the edges of the bins of a bin-ordinal scale, both ends included, or nil for any other scale.
func EndTicks ¶ added in v0.8.0
EndTicks returns ticks from lo to hi whose first tick is lo and whose last tick is hi, near count, and no more than most or one more than count, so that both ends of a nice domain get a tick (D46). It also returns the step whose precision the labels of the ticks need, which is finer than the step between the ticks for a step such as 2.5. It returns nil when no round step lands on both ends.
EndTicks takes the ticks of Ticks when they land on both ends and are no more than most. Then it takes a step of three ticks or more, and at most most, whose multiples from zero land on both ends, such as 0, 15 and 30 for a domain from 0 to 30. Then, for a nice domain, it takes such a step that lands on both ends from lo, such as 50, 200, 350 and 500 for a domain from 50 to 500. A domain is nice when Nice with a count of 10, as a scale of Vega makes it nice by default, leaves it as it is. Then it takes the ticks of Ticks when they land on both ends, though they are more than most. Last, it takes the two ends alone, unless count asks for three ticks or more and Ticks gives three or more, when it returns nil. Of two steps from zero, it takes the one whose number of ticks is nearer count, then the rounder one. Of two steps from lo, whose labels are less round, it takes the one with three ticks or more, then the rounder one, then the one whose number of ticks is nearer count.
func IntervalTicks ¶
IntervalTicks returns the times from t0 to t1, inclusive, at the start of every step of a unit, as a tickCount of Vega such as {"interval": "week", "step": 2} asks for. A day and a unit below it count from the start of the next unit up, as d3 counts them, and a week counts from the first tick. It returns false for a unit that Vega does not name.
func LogLabels ¶ added in v0.7.2
LogLabels returns a function that reports whether a tick of a log scale takes a label, or nil for a scale that is not log. Between two powers of the base, a log scale has a tick at each multiple of the lower power. If there are more ticks than count asks for, only the multiples up to a limit take a label, such as 1, 2 and 3 times each power. The limit is the base times count over the number of ticks, and at least 1, as in d3.
func Sample ¶ added in v0.8.0
Sample returns count colors of a continuous scheme, at 1/(count+1), 2/(count+1) and so on, so that neither end of the scheme is among them. This is how Vega takes the colors of a discrete scale from a continuous scheme. at maps t, from 0 to 1, to a color.
func Thresholds ¶
Thresholds returns the values where a discretizing scale changes from one value of its range to the next.
func TickIncrement ¶
TickIncrement returns the step of about count ticks from lo to hi: 1, 2 or 5 times a power of ten. A step below 1 is returned as the negative of its inverse, such as -10 for 0.1, so that it stays exact.
func TickStep ¶
TickStep returns the step between the ticks that Ticks returns, as a positive number.
func TimeTickUnit ¶
TimeTickUnit returns the unit and the step of the ticks that TimeTicks returns, so that an axis can choose a format for them.
Types ¶
type Error ¶
type Error string
Error is an error of the package scale.
const ErrType Error = "unsupported scale type"
ErrType is the error of a scale type that the package does not have.
type Options ¶
type Options struct {
Type string
Domain []any
// Range holds numbers or colors.
Range []any
// RangeStep is the step of a band or a point scale whose range the
// spec gives as a step. Range is then ignored.
RangeStep float64
// DomainMin, DomainMax and DomainMid override the domain, where they
// are not NaN.
DomainMin, DomainMax, DomainMid float64
Reverse bool
Round bool
Clamp bool
// Zero includes zero in the domain of a quantitative scale. Nil takes
// the default: true for linear, pow and sqrt.
Zero *bool
// Nice extends the domain to round values. NiceCount is the number of
// ticks to make it nice for, and NiceInterval a time interval.
Nice bool
NiceCount float64
NiceInterval string
// Padding is the padding of a band or a point scale, or the padding in
// pixels of a quantitative scale.
Padding float64
PaddingInner, PaddingOuter float64
HasPaddingInner bool
HasPaddingOuter bool
Align float64
Exponent float64
Base float64
Constant float64
// Interpolate is the color space of a color range: rgb, hcl, lab or
// hsl.
Interpolate string
// Scheme says that the range is a color scheme.
Scheme bool
// Interpolator maps a fraction of the domain, from 0 to 1, to a color.
// A continuous scale of colors that is sequential or diverging maps
// through it in place of Range, as a continuous scheme of Vega does.
// Any other scale uses Range (D46).
Interpolator func(float64) string
// Bins are the edges of the bins of a bin-ordinal scale.
Bins []float64
// Location is the time zone of a time scale.
Location *time.Location
}
Options are the settings of a scale, from the spec.
type Scale ¶
type Scale interface {
// Type is the type of the scale, such as linear or band.
Type() string
// Map maps a value of the domain to the range. A continuous scale with
// a range of numbers returns a float64, and with a range of colors a
// color string.
Map(v any) any
// Invert maps a value of the range back to the domain, where the scale
// can.
Invert(v float64) any
// Domain returns the domain.
Domain() []any
// Range returns the range.
Range() []any
// Bandwidth is the width of a band, for a band scale, and 0 for any
// other.
Bandwidth() float64
// Ticks returns about count values of the domain for an axis or a
// legend.
Ticks(count float64) []any
}
Scale maps a value of its domain to a value of its range.
type Scheme ¶ added in v0.8.0
type Scheme struct {
// Colors are the colors of the scheme.
Colors []string
// Continuous says that Colors are stops spread evenly from 0 to 1, and
// that the scheme interpolates between them, as viridis does. Else the
// scheme is a list of discrete colors, as tableau10 is.
Continuous bool
// Interpolate is the color space that a continuous scheme interpolates
// in, as Options.Interpolate names it. A named scheme of Vega
// interpolates in RGB, which "" also names.
Interpolate string
}
Scheme is a color scheme of Vega, such as viridis or tableau10, or a list of colors that a spec gives as its scheme (D46).
func NamedScheme ¶ added in v0.8.0
NamedScheme returns the scheme of Vega with a name in any case, such as viridis or tableau10, and whether Vega has it.