| 1 |
package chart |
| 2 |
|
| 3 |
|
| 4 |
type GridLineProvider interface { |
| 5 |
GetGridLines(ticks []Tick, isVertical bool, majorStyle, minorStyle Style) []GridLine |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
type GridLine struct { |
| 10 |
IsMinor bool |
| 11 |
Style Style |
| 12 |
Value float64 |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
func (gl GridLine) Major() bool { |
| 17 |
return !gl.IsMinor |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
func (gl GridLine) Minor() bool { |
| 22 |
return gl.IsMinor |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
func (gl GridLine) Render(r Renderer, canvasBox Box, ra Range, isVertical bool, defaults Style) { |
| 27 |
r.SetStrokeColor(gl.Style.GetStrokeColor(defaults.GetStrokeColor())) |
| 28 |
r.SetStrokeWidth(gl.Style.GetStrokeWidth(defaults.GetStrokeWidth())) |
| 29 |
r.SetStrokeDashArray(gl.Style.GetStrokeDashArray(defaults.GetStrokeDashArray())) |
| 30 |
|
| 31 |
if isVertical { |
| 32 |
lineLeft := canvasBox.Left + ra.Translate(gl.Value) |
| 33 |
lineBottom := canvasBox.Bottom |
| 34 |
lineTop := canvasBox.Top |
| 35 |
|
| 36 |
r.MoveTo(lineLeft, lineBottom) |
| 37 |
r.LineTo(lineLeft, lineTop) |
| 38 |
r.Stroke() |
| 39 |
} else { |
| 40 |
lineLeft := canvasBox.Left |
| 41 |
lineRight := canvasBox.Right |
| 42 |
lineHeight := canvasBox.Bottom - ra.Translate(gl.Value) |
| 43 |
|
| 44 |
r.MoveTo(lineLeft, lineHeight) |
| 45 |
r.LineTo(lineRight, lineHeight) |
| 46 |
r.Stroke() |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
func GenerateGridLines(ticks []Tick, majorStyle, minorStyle Style) []GridLine { |
| 52 |
var gl []GridLine |
| 53 |
isMinor := false |
| 54 |
|
| 55 |
if len(ticks) < 3 { |
| 56 |
return gl |
| 57 |
} |
| 58 |
|
| 59 |
for _, t := range ticks[1 : len(ticks)-1] { |
| 60 |
s := majorStyle |
| 61 |
if isMinor { |
| 62 |
s = minorStyle |
| 63 |
} |
| 64 |
gl = append(gl, GridLine{ |
| 65 |
Style: s, |
| 66 |
IsMinor: isMinor, |
| 67 |
Value: t.Value, |
| 68 |
}) |
| 69 |
isMinor = !isMinor |
| 70 |
} |
| 71 |
return gl |
| 72 |
} |