...

Source file src/gonum.org/v1/plot/legend_example_test.go

Documentation: gonum.org/v1/plot

     1  // Copyright ©2018 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 plot_test
     6  
     7  import (
     8  	"image/color"
     9  	"os"
    10  
    11  	"gonum.org/v1/plot"
    12  	"gonum.org/v1/plot/vg"
    13  	"gonum.org/v1/plot/vg/draw"
    14  	"gonum.org/v1/plot/vg/vgimg"
    15  )
    16  
    17  type exampleThumbnailer struct {
    18  	color.Color
    19  }
    20  
    21  // Thumbnail fulfills the plot.Thumbnailer interface.
    22  func (et exampleThumbnailer) Thumbnail(c *draw.Canvas) {
    23  	pts := []vg.Point{
    24  		{X: c.Min.X, Y: c.Min.Y},
    25  		{X: c.Min.X, Y: c.Max.Y},
    26  		{X: c.Max.X, Y: c.Max.Y},
    27  		{X: c.Max.X, Y: c.Min.Y},
    28  	}
    29  	poly := c.ClipPolygonY(pts)
    30  	c.FillPolygon(et.Color, poly)
    31  
    32  	pts = append(pts, vg.Point{X: c.Min.X, Y: c.Min.Y})
    33  	outline := c.ClipLinesY(pts)
    34  	c.StrokeLines(draw.LineStyle{
    35  		Color: color.Black,
    36  		Width: vg.Points(1),
    37  	}, outline...)
    38  }
    39  
    40  // This example creates a some standalone legends with borders around them.
    41  func ExampleLegend_standalone() {
    42  	c := vgimg.New(vg.Points(120), vg.Points(100))
    43  	dc := draw.New(c)
    44  
    45  	// These example thumbnailers could be replaced with any of Plotters
    46  	// in the plotter subpackage.
    47  	red := exampleThumbnailer{Color: color.NRGBA{R: 255, A: 255}}
    48  	green := exampleThumbnailer{Color: color.NRGBA{G: 255, A: 255}}
    49  	blue := exampleThumbnailer{Color: color.NRGBA{B: 255, A: 255}}
    50  
    51  	l := plot.NewLegend()
    52  	l.Add("red", red)
    53  	l.Add("green", green)
    54  	l.Add("blue", blue)
    55  	l.Padding = vg.Millimeter
    56  	l.YPosition = draw.PosCenter
    57  
    58  	// purpleRectangle draws a purple rectangle around the given Legend.
    59  	purpleRectangle := func(l plot.Legend) {
    60  		r := l.Rectangle(dc)
    61  		dc.StrokeLines(draw.LineStyle{
    62  			Color: color.NRGBA{R: 255, B: 255, A: 255},
    63  			Width: vg.Points(1),
    64  		}, []vg.Point{
    65  			{X: r.Min.X, Y: r.Min.Y}, {X: r.Min.X, Y: r.Max.Y}, {X: r.Max.X, Y: r.Max.Y},
    66  			{X: r.Max.X, Y: r.Min.Y}, {X: r.Min.X, Y: r.Min.Y},
    67  		})
    68  	}
    69  
    70  	l.Draw(dc)
    71  	purpleRectangle(l)
    72  
    73  	l.Left = true
    74  	l.Draw(dc)
    75  	purpleRectangle(l)
    76  
    77  	l.Top = true
    78  	l.Draw(dc)
    79  	purpleRectangle(l)
    80  
    81  	l.Left = false
    82  	l.Draw(dc)
    83  	purpleRectangle(l)
    84  
    85  	w, err := os.Create("testdata/legend_standalone.png")
    86  	if err != nil {
    87  		panic(err)
    88  	}
    89  	defer w.Close()
    90  
    91  	png := vgimg.PngCanvas{Canvas: c}
    92  	if _, err := png.WriteTo(w); err != nil {
    93  		panic(err)
    94  	}
    95  }
    96  

View as plain text