go-hep.org/x/hep@v0.38.1/hplot/line_test.go (about)

     1  // Copyright ©2019 The go-hep 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 hplot_test
     6  
     7  import (
     8  	"image/color"
     9  	"testing"
    10  
    11  	"go-hep.org/x/hep/hbook"
    12  	"go-hep.org/x/hep/hplot"
    13  	"gonum.org/v1/plot/cmpimg"
    14  	"gonum.org/v1/plot/vg"
    15  )
    16  
    17  func TestVLine(t *testing.T) {
    18  	checkPlot(cmpimg.CheckPlot)(ExampleVLine, t, "vline.png")
    19  }
    20  
    21  func TestHLine(t *testing.T) {
    22  	checkPlot(cmpimg.CheckPlot)(ExampleHLine, t, "hline.png")
    23  }
    24  
    25  func TestHLineOutOfPlot(t *testing.T) {
    26  	checkPlot(cmpimg.CheckPlot)(func() {
    27  		p := hplot.New()
    28  
    29  		pts := []hbook.Point2D{
    30  			{X: 1, Y: 1},
    31  			{X: 2, Y: 2},
    32  		}
    33  
    34  		s2d := hplot.NewS2D(hbook.NewS2D(pts...))
    35  		s2d.Color = color.RGBA{R: 255, A: 255}
    36  		s2d.GlyphStyle.Radius = vg.Points(4)
    37  
    38  		p.Add(
    39  			s2d, hplot.NewGrid(),
    40  			hplot.HLine(0, nil, color.Gray16{}),
    41  			hplot.HLine(3, color.Gray16{}, nil),
    42  		)
    43  
    44  		err := p.Save(5*vg.Centimeter, -1, "testdata/hline_out_of_plot.png")
    45  		if err != nil {
    46  			t.Fatalf("could not save plot: %+v", err)
    47  		}
    48  	}, t, "hline_out_of_plot.png")
    49  }
    50  
    51  func TestVLineOutOfPlot(t *testing.T) {
    52  	checkPlot(cmpimg.CheckPlot)(func() {
    53  		p := hplot.New()
    54  
    55  		pts := []hbook.Point2D{
    56  			{X: 1, Y: 1},
    57  			{X: 2, Y: 2},
    58  		}
    59  
    60  		s2d := hplot.NewS2D(hbook.NewS2D(pts...))
    61  		s2d.Color = color.RGBA{R: 255, A: 255}
    62  		s2d.GlyphStyle.Radius = vg.Points(4)
    63  
    64  		p.Add(
    65  			s2d, hplot.NewGrid(),
    66  			hplot.VLine(3, nil, color.Gray16{}),
    67  			hplot.VLine(0, color.Gray16{}, nil),
    68  		)
    69  
    70  		err := p.Save(5*vg.Centimeter, -1, "testdata/vline_out_of_plot.png")
    71  		if err != nil {
    72  			t.Fatalf("could not save plot: %+v", err)
    73  		}
    74  	}, t, "vline_out_of_plot.png")
    75  }
    76  
    77  func TestHVLineThumbnail(t *testing.T) {
    78  	checkPlot(cmpimg.CheckPlot)(func() {
    79  		p := hplot.New()
    80  		p.Title.Text = "hvlines"
    81  		p.X.Min = 0
    82  		p.X.Max = 10
    83  		p.Y.Min = 0
    84  		p.Y.Max = 10
    85  
    86  		var (
    87  			left   = color.Transparent
    88  			right  = color.Transparent
    89  			top    = color.Transparent
    90  			bottom = color.Transparent
    91  		)
    92  
    93  		l1 := hplot.VLine(2.5, left, nil)
    94  		l2 := hplot.VLine(5, nil, nil)
    95  		l3 := hplot.VLine(7.5, nil, right)
    96  		l4 := hplot.HLine(2.5, nil, bottom)
    97  		l5 := hplot.HLine(5, nil, nil)
    98  		l6 := hplot.HLine(7.5, top, nil)
    99  
   100  		p.Add(l1, l2, l3, l4, l5, l6)
   101  		p.Legend.Add("l1", l1)
   102  		p.Legend.Add("l2", l2)
   103  		p.Legend.Add("l3", l3)
   104  		p.Legend.Add("l4", l4)
   105  		p.Legend.Add("l5", l5)
   106  		p.Legend.Add("l6", l6)
   107  
   108  		err := p.Save(-1, -1, "testdata/hvline.png")
   109  		if err != nil {
   110  			t.Fatalf("could not save hvline: %+v", err)
   111  		}
   112  	}, t, "hvline.png")
   113  }