...
1
2
3
4
5 package plotter
6
7 import (
8 "image/color"
9
10 "gonum.org/v1/plot"
11 "gonum.org/v1/plot/vg"
12 "gonum.org/v1/plot/vg/draw"
13 )
14
15 var (
16
17 DefaultGridLineStyle = draw.LineStyle{
18 Color: color.Gray{128},
19 Width: vg.Points(0.25),
20 }
21 )
22
23
24
25 type Grid struct {
26
27 Vertical draw.LineStyle
28
29
30 Horizontal draw.LineStyle
31 }
32
33
34
35 func NewGrid() *Grid {
36 return &Grid{
37 Vertical: DefaultGridLineStyle,
38 Horizontal: DefaultGridLineStyle,
39 }
40 }
41
42
43 func (g *Grid) Plot(c draw.Canvas, plt *plot.Plot) {
44 trX, trY := plt.Transforms(&c)
45
46 var (
47 ymin = c.Min.Y
48 ymax = c.Max.Y
49 xmin = c.Min.X
50 xmax = c.Max.X
51 )
52
53 if g.Vertical.Color == nil {
54 goto horiz
55 }
56 for _, tk := range plt.X.Tick.Marker.Ticks(plt.X.Min, plt.X.Max) {
57 if tk.IsMinor() {
58 continue
59 }
60 x := trX(tk.Value)
61 if x > xmax || x < xmin {
62 continue
63 }
64 c.StrokeLine2(g.Vertical, x, ymin, x, ymax)
65 }
66
67 horiz:
68 if g.Horizontal.Color == nil {
69 return
70 }
71 for _, tk := range plt.Y.Tick.Marker.Ticks(plt.Y.Min, plt.Y.Max) {
72 if tk.IsMinor() {
73 continue
74 }
75 y := trY(tk.Value)
76 if y > ymax || y < ymin {
77 continue
78 }
79 c.StrokeLine2(g.Horizontal, xmin, y, xmax, y)
80 }
81 }
82
View as plain text