2016-07-10 04:11:47 -04:00
|
|
|
package chart
|
|
|
|
|
2016-07-23 18:35:49 -04:00
|
|
|
import "math"
|
2016-07-13 14:50:22 -04:00
|
|
|
|
2016-07-23 18:35:49 -04:00
|
|
|
// TicksProvider is a type that provides ticks.
|
|
|
|
type TicksProvider interface {
|
|
|
|
GetTicks(vf ValueFormatter) []Tick
|
2016-07-11 02:06:14 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2016-07-23 18:35:49 -04:00
|
|
|
|
|
|
|
// GenerateContinuousTicksWithStep generates a set of ticks.
|
2016-07-30 12:35:44 -04:00
|
|
|
func GenerateContinuousTicksWithStep(ra Range, step float64, vf ValueFormatter, includeMax bool) []Tick {
|
2016-07-23 18:35:49 -04:00
|
|
|
var ticks []Tick
|
|
|
|
min, max := ra.GetMin(), ra.GetMax()
|
|
|
|
for cursor := min; cursor <= max; cursor += step {
|
|
|
|
ticks = append(ticks, Tick{
|
|
|
|
Value: cursor,
|
|
|
|
Label: vf(cursor),
|
|
|
|
})
|
|
|
|
|
|
|
|
// this guard is in place in case step is super, super small.
|
|
|
|
if len(ticks) > DefaultTickCountSanityCheck {
|
|
|
|
return ticks
|
|
|
|
}
|
|
|
|
}
|
2016-07-30 12:35:44 -04:00
|
|
|
if includeMax {
|
|
|
|
ticks = append(ticks, Tick{
|
|
|
|
Value: ra.GetMax(),
|
|
|
|
Label: vf(ra.GetMax()),
|
|
|
|
})
|
|
|
|
}
|
2016-07-23 18:35:49 -04:00
|
|
|
return ticks
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalculateContinuousTickStep calculates the continous range interval between ticks.
|
|
|
|
func CalculateContinuousTickStep(r Renderer, ra Range, isVertical bool, style Style, vf ValueFormatter) float64 {
|
|
|
|
r.SetFont(style.GetFont())
|
|
|
|
r.SetFontSize(style.GetFontSize())
|
|
|
|
if isVertical {
|
|
|
|
label := vf(ra.GetMin())
|
|
|
|
tb := r.MeasureText(label)
|
|
|
|
count := int(math.Ceil(float64(ra.GetDomain()) / float64(tb.Height()+DefaultMinimumTickVerticalSpacing)))
|
|
|
|
return ra.GetDelta() / float64(count)
|
|
|
|
}
|
|
|
|
|
|
|
|
// take a cut at determining the 'widest' value.
|
|
|
|
l0 := vf(ra.GetMin())
|
|
|
|
ln := vf(ra.GetMax())
|
|
|
|
ll := l0
|
|
|
|
if len(ln) > len(l0) {
|
|
|
|
ll = ln
|
|
|
|
}
|
|
|
|
llb := r.MeasureText(ll)
|
|
|
|
textWidth := llb.Width()
|
|
|
|
width := textWidth + DefaultMinimumTickHorizontalSpacing
|
|
|
|
count := int(math.Ceil(float64(ra.GetDomain()) / float64(width)))
|
|
|
|
return ra.GetDelta() / float64(count)
|
|
|
|
}
|