...

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

Documentation: gonum.org/v1/plot/plotter

     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 plotter_test
     6  
     7  import (
     8  	"image/color"
     9  	"log"
    10  	"testing"
    11  
    12  	"golang.org/x/exp/rand"
    13  
    14  	"gonum.org/v1/plot"
    15  	"gonum.org/v1/plot/cmpimg"
    16  	"gonum.org/v1/plot/plotter"
    17  )
    18  
    19  // See https://github.com/gonum/plot/issues/488
    20  func clippedFilledLine() {
    21  	rnd := rand.New(rand.NewSource(1))
    22  
    23  	// randomPoints returns some random x, y points
    24  	// with some interesting kind of trend.
    25  	randomPoints := func(n int, x float64) plotter.XYs {
    26  		pts := make(plotter.XYs, n)
    27  		for i := range pts {
    28  			if i == 0 {
    29  				pts[i].X = x + rnd.Float64()
    30  			} else {
    31  				pts[i].X = pts[i-1].X + 0.5 + rnd.Float64()
    32  			}
    33  			pts[i].Y = -5. + 10*rnd.Float64()
    34  		}
    35  		return pts
    36  	}
    37  
    38  	p := plot.New()
    39  	p.Title.Text = "Filled Line Example"
    40  	p.X.Label.Text = "X"
    41  	p.Y.Label.Text = "Y"
    42  	p.Add(plotter.NewGrid())
    43  
    44  	filled, err := plotter.NewLine(randomPoints(4, 0))
    45  	if err != nil {
    46  		log.Panic(err)
    47  	}
    48  	filled.FillColor = color.RGBA{R: 196, G: 255, B: 196, A: 255}
    49  
    50  	p.Add(filled)
    51  	// testing clipping
    52  	p.X.Min, p.X.Max = 1, 3
    53  	p.Y.Max = -1
    54  
    55  	err = p.Save(200, 200, "testdata/clippedFilledLine.png")
    56  	if err != nil {
    57  		log.Panic(err)
    58  	}
    59  }
    60  
    61  func TestFilledLine(t *testing.T) {
    62  	cmpimg.CheckPlot(ExampleLine_filledLine, t, "filledLine.png")
    63  	cmpimg.CheckPlot(clippedFilledLine, t, "clippedFilledLine.png")
    64  }
    65  

View as plain text