This commit is contained in:
Will Charczuk 2016-07-11 18:48:51 -07:00
parent 01983f4e86
commit 9a5138b21d
17 changed files with 477 additions and 308 deletions

152
yaxis.go
View file

@ -8,8 +8,13 @@ import (
// YAxis is a veritcal rule of the range.
// There can be (2) y-axes; a primary and secondary.
type YAxis struct {
Name string
Style Style
Name string
Style Style
Zero GridLine
AxisType YAxisType
ValueFormatter ValueFormatter
Range Range
Ticks []Tick
@ -28,31 +33,81 @@ func (ya YAxis) GetStyle() 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 {
var ticks []Tick
if len(ya.Ticks) > 0 {
return ya.Ticks
ticks = ya.Ticks
} else {
ticks = ya.generateTicks(r, ra, vf)
}
return ya.generateTicks(r, ra, vf)
return ticks
}
func (ya YAxis) generateTicks(r Renderer, ra Range, vf ValueFormatter) []Tick {
step := ya.getTickStep(r, ra, vf)
return GenerateTicksWithStep(ra, step, vf)
ticks := GenerateTicksWithStep(ra, step, vf)
return ticks
}
func (ya YAxis) getTickStep(r Renderer, ra Range, vf ValueFormatter) float64 {
tickCount := ya.getTickCount(r, ra, vf)
return ra.Delta() / float64(tickCount)
step := ra.Delta() / float64(tickCount)
return step
}
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
return DefaultTickCount
}
// Measure returns the bounds of the axis.
func (ya YAxis) Measure(r Renderer, canvasBox Box, ra Range, ticks []Tick) Box {
defaultFont, _ := GetDefaultFont()
r.SetFont(ya.Style.GetFont(defaultFont))
r.SetFontSize(ya.Style.GetFontSize(DefaultFontSize))
sort.Sort(Ticks(ticks))
var tx int
if ya.AxisType == YAxisPrimary {
tx = canvasBox.Right + DefaultYAxisMargin
} else if ya.AxisType == YAxisSecondary {
tx = canvasBox.Left - DefaultYAxisMargin
}
var minx, maxx, miny, maxy = math.MaxInt32, 0, math.MaxInt32, 0
for _, t := range ticks {
v := t.Value
ly := canvasBox.Bottom - ra.Translate(v)
tb := r.MeasureText(t.Label)
finalTextX := tx
if ya.AxisType == YAxisSecondary {
finalTextX = tx - tb.Width
}
if ya.AxisType == YAxisPrimary {
minx = canvasBox.Right
maxx = MaxInt(maxx, tx+tb.Width)
} else if ya.AxisType == YAxisSecondary {
minx = MinInt(minx, finalTextX)
maxx = MaxInt(maxx, tx)
}
miny = MinInt(miny, ly-tb.Height>>1)
maxy = MaxInt(maxy, ly+tb.Height>>1)
}
return Box{
Top: miny,
Left: minx,
Right: maxx,
Bottom: maxy,
Width: maxx - minx,
Height: maxy - miny,
}
}
// Render renders the axis.
func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, axisType YAxisType, ticks []Tick) {
func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, ticks []Tick) {
r.SetStrokeColor(ya.Style.GetStrokeColor(DefaultAxisColor))
r.SetStrokeWidth(ya.Style.GetStrokeWidth(DefaultAxisLineWidth))
@ -65,51 +120,42 @@ func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, axisType YAxisType,
var lx int
var tx int
if axisType == YAxisPrimary {
if ya.AxisType == YAxisPrimary {
lx = canvasBox.Right
tx = canvasBox.Right + DefaultYAxisMargin
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
_, pth := r.MeasureText(t.Label)
ty := ly + pth>>1
r.Text(t.Label, tx, ty)
r.MoveTo(lx, ly)
r.LineTo(lx+DefaultVerticalTickWidth, ly)
r.Stroke()
}
} else if axisType == YAxisSecondary {
} else if ya.AxisType == YAxisSecondary {
lx = canvasBox.Left
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
ptw, pth := r.MeasureText(t.Label)
tw := ptw
tx = lx - (tw + DefaultYAxisMargin)
ty := ly + pth>>1
r.Text(t.Label, tx, ty)
r.MoveTo(lx, ly)
r.LineTo(lx-DefaultVerticalTickWidth, ly)
r.Stroke()
}
tx = canvasBox.Left - DefaultYAxisMargin
}
r.MoveTo(lx, canvasBox.Bottom)
r.LineTo(lx, canvasBox.Top)
r.Stroke()
for _, t := range ticks {
v := t.Value
ly := canvasBox.Bottom - ra.Translate(v)
tb := r.MeasureText(t.Label)
finalTextX := tx
finalTextY := ly + tb.Height>>1
if ya.AxisType == YAxisSecondary {
finalTextX = tx - tb.Width
}
r.Text(t.Label, finalTextX, finalTextY)
r.MoveTo(lx, ly)
if ya.AxisType == YAxisPrimary {
r.LineTo(lx+DefaultHorizontalTickWidth, ly)
} else if ya.AxisType == YAxisSecondary {
r.LineTo(lx-DefaultHorizontalTickWidth, ly)
}
r.Stroke()
}
if ya.Zero.Style.Show {
ya.Zero.Render(r, canvasBox, ra)
}
}