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

@ -15,5 +15,6 @@ type Axis interface {
GetName() string GetName() string
GetStyle() Style GetStyle() Style
GetTicks(r Renderer, ra Range, vf ValueFormatter) []Tick GetTicks(r Renderer, ra Range, vf ValueFormatter) []Tick
GetGridLines(ticks []Tick) []GridLine
Render(c *Chart, r Renderer, canvasBox Box, ra Range, ticks []Tick) Render(c *Chart, r Renderer, canvasBox Box, ra Range, ticks []Tick)
} }

View File

@ -24,8 +24,9 @@ type Chart struct {
YAxis YAxis YAxis YAxis
YAxisSecondary YAxis YAxisSecondary YAxis
Font *truetype.Font Font *truetype.Font
Series []Series Series []Series
Elements []Renderable
} }
// GetDPI returns the dpi for the chart. // GetDPI returns the dpi for the chart.
@ -100,6 +101,10 @@ func (c Chart) Render(rp RendererProvider, w io.Writer) error {
} }
c.drawTitle(r) c.drawTitle(r)
for _, a := range c.Elements {
a(r, canvasBox)
}
return r.Save(w) return r.Save(w)
} }

View File

@ -82,6 +82,8 @@ var (
DefaultFillColor = drawing.Color{R: 0, G: 217, B: 116, A: 255} DefaultFillColor = drawing.Color{R: 0, G: 217, B: 116, A: 255}
// DefaultAnnotationFillColor is the default annotation background color. // DefaultAnnotationFillColor is the default annotation background color.
DefaultAnnotationFillColor = drawing.Color{R: 255, G: 255, B: 255, A: 255} DefaultAnnotationFillColor = drawing.Color{R: 255, G: 255, B: 255, A: 255}
// DefaultGridLineColor is the default grid line color.
DefaultGridLineColor = drawing.Color{R: 239, G: 239, B: 239, A: 255}
) )
var ( var (

View File

@ -30,14 +30,27 @@ func (gl GridLine) Horizontal() bool {
// Render renders the gridline // Render renders the gridline
func (gl GridLine) Render(r Renderer, canvasBox Box, ra Range) { func (gl GridLine) Render(r Renderer, canvasBox Box, ra Range) {
lineleft := canvasBox.Left if gl.IsVertical {
lineright := canvasBox.Right lineLeft := canvasBox.Left + ra.Translate(gl.Value)
lineheight := canvasBox.Bottom - ra.Translate(gl.Value) lineBottom := canvasBox.Bottom
lineTop := canvasBox.Top
r.SetStrokeColor(gl.Style.GetStrokeColor(DefaultAxisColor)) r.SetStrokeColor(gl.Style.GetStrokeColor(DefaultAxisColor))
r.SetStrokeWidth(gl.Style.GetStrokeWidth(DefaultAxisLineWidth)) r.SetStrokeWidth(gl.Style.GetStrokeWidth(DefaultAxisLineWidth))
r.MoveTo(lineleft, lineheight) r.MoveTo(lineLeft, lineBottom)
r.LineTo(lineright, lineheight) r.LineTo(lineLeft, lineTop)
r.Stroke() r.Stroke()
} else {
lineLeft := canvasBox.Left
lineRight := canvasBox.Right
lineHeight := canvasBox.Bottom - ra.Translate(gl.Value)
r.SetStrokeColor(gl.Style.GetStrokeColor(DefaultAxisColor))
r.SetStrokeWidth(gl.Style.GetStrokeWidth(DefaultAxisLineWidth))
r.MoveTo(lineLeft, lineHeight)
r.LineTo(lineRight, lineHeight)
r.Stroke()
}
} }

View File

@ -1,6 +1,4 @@
package chart package chart
// Renderable is a type that can be rendered onto a chart. // Renderable is a function that can be called to render custom elements on the chart.
type Renderable interface { type Renderable func(r Renderer, canvasBox Box)
Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style)
}

View File

@ -3,5 +3,5 @@ package chart
// Series is an alias to Renderable. // Series is an alias to Renderable.
type Series interface { type Series interface {
GetYAxis() YAxisType GetYAxis() YAxisType
Renderable Render(r Renderer, canvasBox Box, xrange, yrange Range, s Style)
} }

View File

@ -46,6 +46,12 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult {
Style: chart.Style{ Style: chart.Style{
Show: true, Show: true,
}, },
GridMajorStyle: chart.Style{
Show: true,
},
GridMinorStyle: chart.Style{
Show: true,
},
}, },
YAxis: chart.YAxis{ YAxis: chart.YAxis{
Style: chart.Style{ Style: chart.Style{
@ -57,6 +63,12 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult {
StrokeWidth: 1.0, StrokeWidth: 1.0,
}, },
}, },
GridMajorStyle: chart.Style{
Show: true,
},
GridMinorStyle: chart.Style{
Show: true,
},
}, },
Series: []chart.Series{ Series: []chart.Series{
chart.TimeSeries{ chart.TimeSeries{

View File

@ -12,6 +12,10 @@ type XAxis struct {
ValueFormatter ValueFormatter ValueFormatter ValueFormatter
Range Range Range Range
Ticks []Tick Ticks []Tick
GridLines []GridLine
GridMajorStyle Style
GridMinorStyle Style
} }
// GetName returns the name. // GetName returns the name.
@ -62,6 +66,41 @@ func (xa XAxis) getTickCount(r Renderer, ra Range, vf ValueFormatter) int {
return count 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. // Measure returns the bounds of the axis.
func (xa XAxis) Measure(r Renderer, canvasBox Box, ra Range, ticks []Tick) Box { func (xa XAxis) Measure(r Renderer, canvasBox Box, ra Range, ticks []Tick) Box {
defaultFont, _ := GetDefaultFont() 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.LineTo(tx, canvasBox.Bottom+DefaultVerticalTickHeight)
r.Stroke() 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)
}
}
}
} }

View File

@ -18,6 +18,10 @@ type YAxis struct {
ValueFormatter ValueFormatter ValueFormatter ValueFormatter
Range Range Range Range
Ticks []Tick Ticks []Tick
GridLines []GridLine
GridMajorStyle Style
GridMinorStyle Style
} }
// GetName returns the name. // GetName returns the name.
@ -62,6 +66,40 @@ func (ya YAxis) getTickCount(r Renderer, ra Range, vf ValueFormatter) int {
return int(math.Ceil(float64(ra.Domain) / float64(tb.Height()+DefaultMinimumTickVerticalSpacing))) return int(math.Ceil(float64(ra.Domain) / float64(tb.Height()+DefaultMinimumTickVerticalSpacing)))
} }
// GetGridLines returns the gridlines for the axis.
func (ya YAxis) GetGridLines(ticks []Tick) []GridLine {
if len(ya.GridLines) > 0 {
return ya.GridLines
}
return ya.generateGridLines(ticks)
}
func (ya YAxis) 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,
Value: t.Value,
})
isMinor = !isMinor
}
return gl
}
// Measure returns the bounds of the axis. // Measure returns the bounds of the axis.
func (ya YAxis) Measure(r Renderer, canvasBox Box, ra Range, ticks []Tick) Box { func (ya YAxis) Measure(r Renderer, canvasBox Box, ra Range, ticks []Tick) Box {
defaultFont, _ := GetDefaultFont() defaultFont, _ := GetDefaultFont()
@ -159,4 +197,13 @@ func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, ticks []Tick) {
if ya.Zero.Style.Show { if ya.Zero.Style.Show {
ya.Zero.Render(r, canvasBox, ra) ya.Zero.Render(r, canvasBox, ra)
} }
if ya.GridMajorStyle.Show || ya.GridMinorStyle.Show {
for _, gl := range ya.GetGridLines(ticks) {
if (gl.IsMinor && ya.GridMinorStyle.Show) ||
(!gl.IsMinor && ya.GridMajorStyle.Show) {
gl.Render(r, canvasBox, ra)
}
}
}
} }