descending

This commit is contained in:
Will Charczuk 2017-01-10 13:50:17 -08:00
parent 78cbfa62bc
commit 98d3996b47
6 changed files with 127 additions and 18 deletions

52
tick.go
View file

@ -1,6 +1,10 @@
package chart
import "math"
import (
"fmt"
"math"
"strings"
)
// TicksProvider is a type that provides ticks.
type TicksProvider interface {
@ -31,6 +35,15 @@ func (t Ticks) Less(i, j int) bool {
return t[i].Value < t[j].Value
}
// String returns a string representation of the set of ticks.
func (t Ticks) String() string {
var values []string
for i, tick := range t {
values = append(values, fmt.Sprintf("[%d: %s]", i, tick.Label))
}
return strings.Join(values, ", ")
}
// GenerateContinuousTicks generates a set of ticks.
func GenerateContinuousTicks(r Renderer, ra Range, isVertical bool, style Style, vf ValueFormatter) []Tick {
if vf == nil {
@ -40,10 +53,17 @@ func GenerateContinuousTicks(r Renderer, ra Range, isVertical bool, style Style,
var ticks []Tick
min, max := ra.GetMin(), ra.GetMax()
ticks = append(ticks, Tick{
Value: min,
Label: vf(min),
})
if ra.IsDescending() {
ticks = append(ticks, Tick{
Value: max,
Label: vf(max),
})
} else {
ticks = append(ticks, Tick{
Value: min,
Label: vf(min),
})
}
minLabel := vf(min)
style.GetTextOptions().WriteToRenderer(r)
@ -67,17 +87,29 @@ func GenerateContinuousTicks(r Renderer, ra Range, isVertical bool, style Style,
intermediateTickCount = Math.MinInt(intermediateTickCount, 1<<10)
for x := 1; x < intermediateTickCount; x++ {
tickValue := min + Math.RoundUp(tickStep*float64(x), roundTo)
var tickValue float64
if ra.IsDescending() {
tickValue = max - Math.RoundUp(tickStep*float64(x), roundTo)
} else {
tickValue = min + Math.RoundUp(tickStep*float64(x), roundTo)
}
ticks = append(ticks, Tick{
Value: tickValue,
Label: vf(tickValue),
})
}
ticks = append(ticks, Tick{
Value: max,
Label: vf(max),
})
if ra.IsDescending() {
ticks = append(ticks, Tick{
Value: min,
Label: vf(min),
})
} else {
ticks = append(ticks, Tick{
Value: max,
Label: vf(max),
})
}
return ticks
}