2016-07-10 10:11:47 +02:00
|
|
|
package chart
|
|
|
|
|
2016-07-10 19:43:04 +02:00
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
2016-07-10 10:11:47 +02:00
|
|
|
// YAxis is a veritcal rule of the range.
|
|
|
|
// There can be (2) y-axes; a primary and secondary.
|
|
|
|
type YAxis struct {
|
2016-07-10 19:43:04 +02:00
|
|
|
Name string
|
|
|
|
Style Style
|
|
|
|
ValueFormatter ValueFormatter
|
|
|
|
Range Range
|
|
|
|
Ticks []Tick
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetName returns the name.
|
|
|
|
func (ya YAxis) GetName() string {
|
|
|
|
return ya.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStyle returns the style.
|
|
|
|
func (ya YAxis) GetStyle() Style {
|
|
|
|
return ya.Style
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTicks returns the ticks for a series. It coalesces between user provided ticks and
|
|
|
|
// generated ticks.
|
|
|
|
func (ya YAxis) GetTicks(r Renderer, ra Range, vf ValueFormatter) []Tick {
|
|
|
|
if len(ya.Ticks) > 0 {
|
|
|
|
return ya.Ticks
|
|
|
|
}
|
|
|
|
return ya.generateTicks(r, ra, vf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ya YAxis) generateTicks(r Renderer, ra Range, vf ValueFormatter) []Tick {
|
|
|
|
step := ya.getTickStep(r, ra, vf)
|
|
|
|
return ya.generateTicksWithStep(ra, step, vf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ya YAxis) getTickCount(r Renderer, ra Range, vf ValueFormatter) int {
|
|
|
|
textHeight := int(ya.Style.GetFontSize(DefaultFontSize))
|
|
|
|
height := textHeight + DefaultMinimumTickVerticalSpacing
|
|
|
|
count := int(math.Ceil(float64(ra.Domain) / float64(height)))
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ya YAxis) getTickStep(r Renderer, ra Range, vf ValueFormatter) float64 {
|
|
|
|
tickCount := ya.getTickCount(r, ra, vf)
|
|
|
|
return ra.Delta() / float64(tickCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ya YAxis) generateTicksWithStep(ra Range, step float64, vf ValueFormatter) []Tick {
|
|
|
|
var ticks []Tick
|
|
|
|
for cursor := ra.Min; cursor < ra.Max; cursor += step {
|
|
|
|
ticks = append(ticks, Tick{
|
|
|
|
Value: cursor,
|
|
|
|
Label: vf(cursor),
|
|
|
|
})
|
2016-07-11 02:19:44 +02:00
|
|
|
if len(ticks) == 20 {
|
|
|
|
return ticks
|
|
|
|
}
|
2016-07-10 19:43:04 +02:00
|
|
|
}
|
|
|
|
return ticks
|
2016-07-10 10:11:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Render renders the axis.
|
2016-07-10 19:43:04 +02:00
|
|
|
func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, axisType YAxisType, ticks []Tick) {
|
2016-07-10 20:19:56 +02:00
|
|
|
r.SetStrokeColor(ya.Style.GetStrokeColor(DefaultAxisColor))
|
|
|
|
r.SetStrokeWidth(ya.Style.GetStrokeWidth(DefaultAxisLineWidth))
|
2016-07-11 03:09:41 +02:00
|
|
|
|
|
|
|
fontColor := ya.Style.GetFontColor(DefaultAxisColor)
|
|
|
|
r.SetFontColor(fontColor)
|
2016-07-10 20:19:56 +02:00
|
|
|
fontSize := ya.Style.GetFontSize(DefaultFontSize)
|
|
|
|
r.SetFontSize(fontSize)
|
|
|
|
|
|
|
|
sort.Sort(Ticks(ticks))
|
|
|
|
|
2016-07-10 19:43:04 +02:00
|
|
|
var lx int
|
2016-07-10 10:11:47 +02:00
|
|
|
var tx int
|
|
|
|
if axisType == YAxisPrimary {
|
2016-07-10 19:43:04 +02:00
|
|
|
lx = canvasBox.Right
|
2016-07-10 10:11:47 +02:00
|
|
|
tx = canvasBox.Right + DefaultYAxisMargin
|
2016-07-10 20:19:56 +02:00
|
|
|
|
|
|
|
r.MoveTo(lx, canvasBox.Bottom)
|
|
|
|
r.LineTo(lx, canvasBox.Top)
|
|
|
|
r.Stroke()
|
|
|
|
|
|
|
|
for _, t := range ticks {
|
|
|
|
v := t.Value
|
|
|
|
ly := ra.Translate(v) + canvasBox.Top
|
|
|
|
|
|
|
|
th := int(fontSize) >> 1
|
|
|
|
ty := ly + th
|
|
|
|
|
|
|
|
r.Text(t.Label, tx, ty)
|
|
|
|
|
|
|
|
r.MoveTo(lx, ly)
|
|
|
|
r.LineTo(lx+DefaultVerticalTickWidth, ly)
|
|
|
|
r.Stroke()
|
|
|
|
}
|
2016-07-10 10:11:47 +02:00
|
|
|
} else if axisType == YAxisSecondary {
|
2016-07-10 19:43:04 +02:00
|
|
|
lx = canvasBox.Left
|
2016-07-10 10:11:47 +02:00
|
|
|
|
2016-07-10 20:19:56 +02:00
|
|
|
r.MoveTo(lx, canvasBox.Bottom)
|
|
|
|
r.LineTo(lx, canvasBox.Top)
|
|
|
|
r.Stroke()
|
2016-07-10 19:43:04 +02:00
|
|
|
|
2016-07-10 20:19:56 +02:00
|
|
|
for _, t := range ticks {
|
|
|
|
v := t.Value
|
|
|
|
ly := ra.Translate(v) + canvasBox.Top
|
2016-07-10 19:43:04 +02:00
|
|
|
|
2016-07-10 20:19:56 +02:00
|
|
|
ptw, _ := r.MeasureText(t.Label)
|
2016-07-10 10:11:47 +02:00
|
|
|
|
2016-07-10 20:19:56 +02:00
|
|
|
tw := ptw
|
|
|
|
th := int(fontSize)
|
2016-07-10 19:43:04 +02:00
|
|
|
|
2016-07-10 20:19:56 +02:00
|
|
|
tx = lx - (int(tw) + (DefaultYAxisMargin >> 1))
|
|
|
|
ty := ly + th>>1
|
2016-07-10 19:43:04 +02:00
|
|
|
|
2016-07-10 20:19:56 +02:00
|
|
|
r.Text(t.Label, tx, ty)
|
2016-07-10 19:43:04 +02:00
|
|
|
|
2016-07-10 20:19:56 +02:00
|
|
|
r.MoveTo(lx, ly)
|
|
|
|
r.LineTo(lx-DefaultVerticalTickWidth, ly)
|
|
|
|
r.Stroke()
|
|
|
|
}
|
2016-07-10 10:11:47 +02:00
|
|
|
}
|
2016-07-10 20:19:56 +02:00
|
|
|
|
2016-07-10 10:11:47 +02:00
|
|
|
}
|