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

70
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{ ticks = append(ticks, Tick{
Value: cursor, Value: min,
Label: vf(cursor), Label: vf(min),
}) })
// this guard is in place in case step is super, super small. minLabel := vf(min)
if len(ticks) > DefaultTickCountSanityCheck { style.GetTextOptions().WriteToRenderer(r)
return ticks labelBox := r.MeasureText(minLabel)
}
}
if includeMax {
ticks = append(ticks, Tick{
Value: ra.GetMax(),
Label: vf(ra.GetMax()),
})
}
return ticks
}
// CalculateContinuousTickStep calculates the continous range interval between ticks. var tickSize int
func CalculateContinuousTickStep(r Renderer, ra Range, isVertical bool, style Style, vf ValueFormatter) float64 {
r.SetFont(style.GetFont())
r.SetFontSize(style.GetFontSize())
if isVertical { if isVertical {
label := vf(ra.GetMin()) tickSize = labelBox.Height() + DefaultMinimumTickVerticalSpacing
tb := r.MeasureText(label) } else {
count := int(math.Ceil(float64(ra.GetDomain()) / float64(tb.Height()+DefaultMinimumTickVerticalSpacing))) tickSize = labelBox.Width() + DefaultMinimumTickHorizontalSpacing
return ra.GetDelta() / float64(count)
} }
// take a cut at determining the 'widest' value. domainRemainder := (ra.GetDomain()) - (tickSize * 2)
l0 := vf(ra.GetMin()) intermediateTickCount := int(math.Floor(float64(domainRemainder) / float64(tickSize)))
ln := vf(ra.GetMax())
ll := l0 rangeDelta := math.Floor(max - min)
if len(ln) > len(l0) { tickStep := rangeDelta / float64(intermediateTickCount)
ll = ln
roundTo := Math.GetRoundToForDelta(rangeDelta) / 10
for x := 1; x < intermediateTickCount; x++ {
tickValue := min + Math.RoundDown(tickStep*float64(x), roundTo)
ticks = append(ticks, Tick{
Value: tickValue,
Label: vf(tickValue),
})
} }
llb := r.MeasureText(ll)
textWidth := llb.Width() ticks = append(ticks, Tick{
width := textWidth + DefaultMinimumTickHorizontalSpacing Value: max,
count := int(math.Ceil(float64(ra.GetDomain()) / float64(width))) Label: vf(max),
return ra.GetDelta() / float64(count) })
return ticks
} }

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.