go-chart/grid_line.go

73 lines
1.7 KiB
Go
Raw Normal View History

2016-07-12 03:48:51 +02:00
package chart
2016-07-24 00:35:49 +02:00
// GridLineProvider is a type that provides grid lines.
type GridLineProvider interface {
GetGridLines(ticks []Tick, isVertical bool, majorStyle, minorStyle Style) []GridLine
}
2016-07-12 03:48:51 +02:00
// GridLine is a line on a graph canvas.
type GridLine struct {
2016-08-12 05:42:25 +02:00
IsMinor bool
Style Style
Value float64
2016-07-12 03:48:51 +02:00
}
// Major returns if the gridline is a `major` line.
func (gl GridLine) Major() bool {
return !gl.IsMinor
}
// Minor returns if the gridline is a `minor` line.
func (gl GridLine) Minor() bool {
return gl.IsMinor
}
// Render renders the gridline
2016-08-12 05:42:25 +02:00
func (gl GridLine) Render(r Renderer, canvasBox Box, ra Range, isVertical bool, defaults Style) {
2016-07-25 05:27:19 +02:00
r.SetStrokeColor(gl.Style.GetStrokeColor(defaults.GetStrokeColor()))
r.SetStrokeWidth(gl.Style.GetStrokeWidth(defaults.GetStrokeWidth()))
r.SetStrokeDashArray(gl.Style.GetStrokeDashArray(defaults.GetStrokeDashArray()))
2016-08-12 05:42:25 +02:00
if isVertical {
2016-07-13 04:14:14 +02:00
lineLeft := canvasBox.Left + ra.Translate(gl.Value)
lineBottom := canvasBox.Bottom
lineTop := canvasBox.Top
r.MoveTo(lineLeft, lineBottom)
r.LineTo(lineLeft, lineTop)
r.Stroke()
} else {
lineLeft := canvasBox.Left
lineRight := canvasBox.Right
lineHeight := canvasBox.Bottom - ra.Translate(gl.Value)
r.MoveTo(lineLeft, lineHeight)
r.LineTo(lineRight, lineHeight)
r.Stroke()
}
2016-07-12 03:48:51 +02:00
}
2016-07-24 00:35:49 +02:00
// GenerateGridLines generates grid lines.
2016-08-12 05:42:25 +02:00
func GenerateGridLines(ticks []Tick, majorStyle, minorStyle Style) []GridLine {
2016-07-24 00:35:49 +02:00
var gl []GridLine
isMinor := false
if len(ticks) < 3 {
return gl
}
for _, t := range ticks[1 : len(ticks)-1] {
2016-07-24 00:35:49 +02:00
s := majorStyle
if isMinor {
s = minorStyle
}
gl = append(gl, GridLine{
2016-08-12 05:42:25 +02:00
Style: s,
IsMinor: isMinor,
Value: t.Value,
2016-07-24 00:35:49 +02:00
})
isMinor = !isMinor
}
return gl
}