-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathgrid.go
More file actions
81 lines (70 loc) · 1.71 KB
/
Copy pathgrid.go
File metadata and controls
81 lines (70 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright ©2015 The gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package plotter
import (
"image/color"
"github.com/gonum/plot"
"github.com/gonum/plot/vg"
"github.com/gonum/plot/vg/draw"
)
var (
// DefaultGridLineStyle is the default style for grid lines.
DefaultGridLineStyle = draw.LineStyle{
Color: color.Gray{128},
Width: vg.Points(0.25),
}
)
// Grid implements the plot.Plotter interface, drawing
// a set of grid lines at the major tick marks.
type Grid struct {
// Vertical is the style of the vertical lines.
Vertical draw.LineStyle
// Horizontal is the style of the horizontal lines.
Horizontal draw.LineStyle
}
// NewGrid returns a new grid with both vertical and
// horizontal lines using the default grid line style.
func NewGrid() *Grid {
return &Grid{
Vertical: DefaultGridLineStyle,
Horizontal: DefaultGridLineStyle,
}
}
// Plot implements the plot.Plotter interface.
func (g *Grid) Plot(c draw.Canvas, plt *plot.Plot) {
trX, trY := plt.Transforms(&c)
var (
ymin = c.Min.Y
ymax = c.Max.Y
xmin = c.Min.X
xmax = c.Max.X
)
if g.Vertical.Color == nil {
goto horiz
}
for _, tk := range plt.X.Tick.Marker.Ticks(plt.X.Min, plt.X.Max, plt.X.Tick.Format) {
if tk.IsMinor() {
continue
}
x := trX(tk.Value)
if x > xmax || x < xmin {
continue
}
c.StrokeLine2(g.Vertical, x, ymin, x, ymax)
}
horiz:
if g.Horizontal.Color == nil {
return
}
for _, tk := range plt.Y.Tick.Marker.Ticks(plt.Y.Min, plt.Y.Max, plt.Y.Tick.Format) {
if tk.IsMinor() {
continue
}
y := trY(tk.Value)
if y > ymax || y < ymin {
continue
}
c.StrokeLine2(g.Horizontal, xmin, y, xmax, y)
}
}