Skip to content
This repository has been archived by the owner on Aug 23, 2024. It is now read-only.

Commit

Permalink
fixing things with generated ticks.
Browse files Browse the repository at this point in the history
  • Loading branch information
wcharczuk committed Jul 30, 2016
1 parent 457fefb commit 0fe9f53
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 42 deletions.
72 changes: 34 additions & 38 deletions tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,51 +31,47 @@ func (t Ticks) Less(i, j int) bool {
return t[i].Value < t[j].Value
}

// GenerateContinuousTicksWithStep generates a set of ticks.
func GenerateContinuousTicksWithStep(ra Range, step float64, vf ValueFormatter, includeMax bool) []Tick {
// GenerateContinuousTicks generates a set of ticks.
func GenerateContinuousTicks(r Renderer, ra Range, isVertical bool, style Style, vf ValueFormatter) []Tick {
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
}
ticks = append(ticks, Tick{
Value: min,
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{
Value: ra.GetMax(),
Label: vf(ra.GetMax()),
Value: tickValue,
Label: vf(tickValue),
})
}
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)
}
ticks = append(ticks, Tick{
Value: max,
Label: vf(max),
})

// 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)
return ticks
}
4 changes: 2 additions & 2 deletions xaxis.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (xa XAxis) GetTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter
if tp, isTickProvider := ra.(TicksProvider); isTickProvider {
return tp.GetTicks(vf)
}
step := CalculateContinuousTickStep(r, ra, false, xa.Style.InheritFrom(defaults), vf)
return GenerateContinuousTicksWithStep(ra, step, vf, xa.TickPosition == TickPositionBetweenTicks)
tickStyle := xa.Style.InheritFrom(defaults)
return GenerateContinuousTicks(r, ra, false, tickStyle, vf)
}

// GetGridLines returns the gridlines for the axis.
Expand Down
4 changes: 2 additions & 2 deletions yaxis.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (ya YAxis) GetTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter
if tp, isTickProvider := ra.(TicksProvider); isTickProvider {
return tp.GetTicks(vf)
}
step := CalculateContinuousTickStep(r, ra, true, ya.Style.InheritFrom(defaults), vf)
return GenerateContinuousTicksWithStep(ra, step, vf, true)
tickStyle := ya.Style.InheritFrom(defaults)
return GenerateContinuousTicks(r, ra, true, tickStyle, vf)
}

// GetGridLines returns the gridlines for the axis.
Expand Down

0 comments on commit 0fe9f53

Please sign in to comment.