...

Source file src/gonum.org/v1/plot/plotter/scatter.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  	"gonum.org/v1/plot"
     9  	"gonum.org/v1/plot/vg"
    10  	"gonum.org/v1/plot/vg/draw"
    11  )
    12  
    13  // Scatter implements the Plotter interface, drawing
    14  // a glyph for each of a set of points.
    15  type Scatter struct {
    16  	// XYs is a copy of the points for this scatter.
    17  	XYs
    18  
    19  	// GlyphStyleFunc, if not nil, specifies GlyphStyles
    20  	// for individual points
    21  	GlyphStyleFunc func(int) draw.GlyphStyle
    22  
    23  	// GlyphStyle is the style of the glyphs drawn
    24  	// at each point.
    25  	draw.GlyphStyle
    26  }
    27  
    28  // NewScatter returns a Scatter that uses the
    29  // default glyph style.
    30  func NewScatter(xys XYer) (*Scatter, error) {
    31  	data, err := CopyXYs(xys)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  	return &Scatter{
    36  		XYs:        data,
    37  		GlyphStyle: DefaultGlyphStyle,
    38  	}, err
    39  }
    40  
    41  // Plot draws the Scatter, implementing the plot.Plotter
    42  // interface.
    43  func (pts *Scatter) Plot(c draw.Canvas, plt *plot.Plot) {
    44  	trX, trY := plt.Transforms(&c)
    45  	glyph := func(i int) draw.GlyphStyle { return pts.GlyphStyle }
    46  	if pts.GlyphStyleFunc != nil {
    47  		glyph = pts.GlyphStyleFunc
    48  	}
    49  	for i, p := range pts.XYs {
    50  		c.DrawGlyph(glyph(i), vg.Point{X: trX(p.X), Y: trY(p.Y)})
    51  	}
    52  }
    53  
    54  // DataRange returns the minimum and maximum
    55  // x and y values, implementing the plot.DataRanger
    56  // interface.
    57  func (pts *Scatter) DataRange() (xmin, xmax, ymin, ymax float64) {
    58  	return XYRange(pts)
    59  }
    60  
    61  // GlyphBoxes returns a slice of plot.GlyphBoxes,
    62  // implementing the plot.GlyphBoxer interface.
    63  func (pts *Scatter) GlyphBoxes(plt *plot.Plot) []plot.GlyphBox {
    64  	glyph := func(i int) draw.GlyphStyle { return pts.GlyphStyle }
    65  	if pts.GlyphStyleFunc != nil {
    66  		glyph = pts.GlyphStyleFunc
    67  	}
    68  	bs := make([]plot.GlyphBox, len(pts.XYs))
    69  	for i, p := range pts.XYs {
    70  		bs[i].X = plt.X.Norm(p.X)
    71  		bs[i].Y = plt.Y.Norm(p.Y)
    72  		r := glyph(i).Radius
    73  		bs[i].Rectangle = vg.Rectangle{
    74  			Min: vg.Point{X: -r, Y: -r},
    75  			Max: vg.Point{X: +r, Y: +r},
    76  		}
    77  	}
    78  	return bs
    79  }
    80  
    81  // Thumbnail the thumbnail for the Scatter,
    82  // implementing the plot.Thumbnailer interface.
    83  func (pts *Scatter) Thumbnail(c *draw.Canvas) {
    84  	c.DrawGlyph(pts.GlyphStyle, c.Center())
    85  }
    86  

View as plain text