fixing things with generated ticks.

This commit is contained in:
Will Charczuk 2016-07-30 12:57:18 -07:00
parent 457fefbc6d
commit 0fe9f53806
3 changed files with 39 additions and 43 deletions

74
tick.go
View File

@ -31,51 +31,47 @@ func (t Ticks) Less(i, j int) bool {
return t[i].Value < t[j].Value return t[i].Value < t[j].Value
} }
// GenerateContinuousTicksWithStep generates a set of ticks. // GenerateContinuousTicks generates a set of ticks.
func GenerateContinuousTicksWithStep(ra Range, step float64, vf ValueFormatter, includeMax bool) []Tick { func GenerateContinuousTicks(r Renderer, ra Range, isVertical bool, style Style, vf ValueFormatter) []Tick {
var ticks []Tick var ticks []Tick
min, max := ra.GetMin(), ra.GetMax() 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. ticks = append(ticks, Tick{
if len(ticks) > DefaultTickCountSanityCheck { Value: min,
return ticks Label: vf(min),
} })
minLabel := vf(min)
style.GetTextOptions().WriteToRenderer(r)
labelBox := r.MeasureText(minLabel)
var tickSize int
if isVertical {
tickSize = labelBox.Height() + DefaultMinimumTickVerticalSpacing
} else {
tickSize = labelBox.Width() + DefaultMinimumTickHorizontalSpacing
} }
if includeMax {
domainRemainder := (ra.GetDomain()) - (tickSize * 2)
intermediateTickCount := int(math.Floor(float64(domainRemainder) / float64(tickSize)))
rangeDelta := math.Floor(max - min)
tickStep := rangeDelta / float64(intermediateTickCount)
roundTo := Math.GetRoundToForDelta(rangeDelta) / 10
for x := 1; x < intermediateTickCount; x++ {
tickValue := min + Math.RoundDown(tickStep*float64(x), roundTo)
ticks = append(ticks, Tick{ ticks = append(ticks, Tick{
Value: ra.GetMax(), Value: tickValue,
Label: vf(ra.GetMax()), Label: vf(tickValue),
}) })
} }
ticks = append(ticks, Tick{
Value: max,
Label: vf(max),
})
return ticks 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)
}

View File

@ -53,8 +53,8 @@ func (xa XAxis) GetTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter
if tp, isTickProvider := ra.(TicksProvider); isTickProvider { if tp, isTickProvider := ra.(TicksProvider); isTickProvider {
return tp.GetTicks(vf) return tp.GetTicks(vf)
} }
step := CalculateContinuousTickStep(r, ra, false, xa.Style.InheritFrom(defaults), vf) tickStyle := xa.Style.InheritFrom(defaults)
return GenerateContinuousTicksWithStep(ra, step, vf, xa.TickPosition == TickPositionBetweenTicks) return GenerateContinuousTicks(r, ra, false, tickStyle, vf)
} }
// GetGridLines returns the gridlines for the axis. // GetGridLines returns the gridlines for the axis.

View File

@ -47,8 +47,8 @@ func (ya YAxis) GetTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter
if tp, isTickProvider := ra.(TicksProvider); isTickProvider { if tp, isTickProvider := ra.(TicksProvider); isTickProvider {
return tp.GetTicks(vf) return tp.GetTicks(vf)
} }
step := CalculateContinuousTickStep(r, ra, true, ya.Style.InheritFrom(defaults), vf) tickStyle := ya.Style.InheritFrom(defaults)
return GenerateContinuousTicksWithStep(ra, step, vf, true) return GenerateContinuousTicks(r, ra, true, tickStyle, vf)
} }
// GetGridLines returns the gridlines for the axis. // GetGridLines returns the gridlines for the axis.