2016-07-10 04:11:47 -04:00
|
|
|
package chart
|
|
|
|
|
2016-07-11 02:06:14 -04:00
|
|
|
// GenerateTicksWithStep generates a set of ticks.
|
|
|
|
func GenerateTicksWithStep(ra Range, step float64, vf ValueFormatter) []Tick {
|
|
|
|
var ticks []Tick
|
2016-07-12 02:32:31 -04:00
|
|
|
min, max := ra.Min, ra.Max
|
2016-07-11 21:48:51 -04:00
|
|
|
for cursor := min; cursor <= max; cursor += step {
|
2016-07-11 02:06:14 -04:00
|
|
|
ticks = append(ticks, Tick{
|
|
|
|
Value: cursor,
|
|
|
|
Label: vf(cursor),
|
|
|
|
})
|
2016-07-13 14:50:22 -04:00
|
|
|
|
|
|
|
// this guard is in place in case step is super, super small.
|
|
|
|
if len(ticks) > DefaultTickCountSanityCheck {
|
|
|
|
return ticks
|
|
|
|
}
|
2016-07-11 02:06:14 -04:00
|
|
|
}
|
|
|
|
return ticks
|
|
|
|
}
|
|
|
|
|
2016-07-10 04:11:47 -04:00
|
|
|
// Tick represents a label on an axis.
|
|
|
|
type Tick struct {
|
|
|
|
Value float64
|
|
|
|
Label string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ticks is an array of ticks.
|
|
|
|
type Ticks []Tick
|
|
|
|
|
|
|
|
// Len returns the length of the ticks set.
|
|
|
|
func (t Ticks) Len() int {
|
|
|
|
return len(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap swaps two elements.
|
|
|
|
func (t Ticks) Swap(i, j int) {
|
|
|
|
t[i], t[j] = t[j], t[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Less returns if i's value is less than j's value.
|
|
|
|
func (t Ticks) Less(i, j int) bool {
|
|
|
|
return t[i].Value < t[j].Value
|
|
|
|
}
|