...

Source file src/gonum.org/v1/plot/plotter/grid.go

Documentation: gonum.org/v1/plot/plotter

     1  // Copyright ©2015 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     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  	// DefaultGridLineStyle is the default style for grid lines.
    17  	DefaultGridLineStyle = draw.LineStyle{
    18  		Color: color.Gray{128},
    19  		Width: vg.Points(0.25),
    20  	}
    21  )
    22  
    23  // Grid implements the plot.Plotter interface, drawing
    24  // a set of grid lines at the major tick marks.
    25  type Grid struct {
    26  	// Vertical is the style of the vertical lines.
    27  	Vertical draw.LineStyle
    28  
    29  	// Horizontal is the style of the horizontal lines.
    30  	Horizontal draw.LineStyle
    31  }
    32  
    33  // NewGrid returns a new grid with both vertical and
    34  // horizontal lines using the default grid line style.
    35  func NewGrid() *Grid {
    36  	return &Grid{
    37  		Vertical:   DefaultGridLineStyle,
    38  		Horizontal: DefaultGridLineStyle,
    39  	}
    40  }
    41  
    42  // Plot implements the plot.Plotter interface.
    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