gridlines.

This commit is contained in:
Will Charczuk 2016-07-12 19:14:14 -07:00
parent 28f01842de
commit 249e9956d0
9 changed files with 141 additions and 15 deletions

View file

@ -12,6 +12,10 @@ type XAxis struct {
ValueFormatter ValueFormatter
Range Range
Ticks []Tick
GridLines []GridLine
GridMajorStyle Style
GridMinorStyle Style
}
// GetName returns the name.
@ -62,6 +66,41 @@ func (xa XAxis) getTickCount(r Renderer, ra Range, vf ValueFormatter) int {
return count
}
// GetGridLines returns the gridlines for the axis.
func (xa XAxis) GetGridLines(ticks []Tick) []GridLine {
if len(xa.GridLines) > 0 {
return xa.GridLines
}
return xa.generateGridLines(ticks)
}
func (xa XAxis) generateGridLines(ticks []Tick) []GridLine {
var gl []GridLine
isMinor := false
minorStyle := Style{
StrokeColor: DefaultGridLineColor.WithAlpha(100),
StrokeWidth: 1.0,
}
majorStyle := Style{
StrokeColor: DefaultGridLineColor,
StrokeWidth: 1.0,
}
for _, t := range ticks {
s := majorStyle
if isMinor {
s = minorStyle
}
gl = append(gl, GridLine{
Style: s,
IsMinor: isMinor,
IsVertical: true,
Value: t.Value,
})
isMinor = !isMinor
}
return gl
}
// Measure returns the bounds of the axis.
func (xa XAxis) Measure(r Renderer, canvasBox Box, ra Range, ticks []Tick) Box {
defaultFont, _ := GetDefaultFont()
@ -121,4 +160,13 @@ func (xa XAxis) Render(r Renderer, canvasBox Box, ra Range, ticks []Tick) {
r.LineTo(tx, canvasBox.Bottom+DefaultVerticalTickHeight)
r.Stroke()
}
if xa.GridMajorStyle.Show || xa.GridMinorStyle.Show {
for _, gl := range xa.GetGridLines(ticks) {
if (gl.IsMinor && xa.GridMinorStyle.Show) ||
(!gl.IsMinor && xa.GridMajorStyle.Show) {
gl.Render(r, canvasBox, ra)
}
}
}
}