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

     1  // Copyright ©2020 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  	"testing"
     9  
    10  	"go-hep.org/x/hep/hplot"
    11  	"gonum.org/v1/plot"
    12  	"gonum.org/v1/plot/cmpimg"
    13  	"gonum.org/v1/plot/plotter"
    14  	"gonum.org/v1/plot/vg"
    15  	"gonum.org/v1/plot/vg/draw"
    16  )
    17  
    18  func TestLabel(t *testing.T) {
    19  	checkPlot(cmpimg.CheckPlot)(ExampleLabel, t, "label_plot.png")
    20  }
    21  
    22  func TestLabelPanic(t *testing.T) {
    23  	for _, tc := range []struct {
    24  		x, y float64
    25  		txt  string
    26  		opts []hplot.LabelOption
    27  		err  string
    28  	}{
    29  		{
    30  			x:    1.1,
    31  			txt:  "invalid-x",
    32  			opts: []hplot.LabelOption{hplot.WithLabelNormalized(true)},
    33  			err:  "hplot: normalized label x-position is outside [0,1]: 1.1",
    34  		},
    35  		{
    36  			y:    1.1,
    37  			txt:  "invalid-y",
    38  			opts: []hplot.LabelOption{hplot.WithLabelNormalized(true)},
    39  			err:  "hplot: normalized label y-position is outside [0,1]: 1.1",
    40  		},
    41  		{
    42  			x:    0.99,
    43  			y:    0,
    44  			txt:  "very long text in x",
    45  			opts: []hplot.LabelOption{hplot.WithLabelNormalized(true)},
    46  			err:  "hplot: label (0.99, 0) falls outside data canvas",
    47  		},
    48  		{
    49  			x:    0,
    50  			y:    0.99,
    51  			txt:  "very tall text in y\n1\n2\n",
    52  			opts: []hplot.LabelOption{hplot.WithLabelNormalized(true)},
    53  			err:  "hplot: label (0, 0.99) falls outside data canvas",
    54  		},
    55  	} {
    56  		t.Run(tc.txt, func(t *testing.T) {
    57  			defer func() {
    58  				e := recover()
    59  				if e == nil {
    60  					t.Fatalf("expected a panic %q", tc.err)
    61  				}
    62  				if got, want := e.(error).Error(), tc.err; got != want {
    63  					t.Fatalf("invalid panic message\ngot= %q\nwant=%q",
    64  						got, want,
    65  					)
    66  				}
    67  			}()
    68  
    69  			lbl := hplot.NewLabel(tc.x, tc.y, tc.txt, tc.opts...)
    70  
    71  			p := hplot.New()
    72  			p.X.Min = -10
    73  			p.X.Max = +10
    74  			p.Y.Min = -10
    75  			p.Y.Max = +10
    76  			p.Add(lbl)
    77  
    78  			const (
    79  				sz = 10 * vg.Centimeter
    80  			)
    81  			dc, err := draw.NewFormattedCanvas(sz, sz, "png")
    82  			if err != nil {
    83  				t.Fatalf("could not create draw canvas: %+v", err)
    84  			}
    85  
    86  			p.Draw(draw.New(dc))
    87  		})
    88  	}
    89  }
    90  
    91  func TestLabelWithLog(t *testing.T) {
    92  	checkPlot(cmpimg.CheckPlot)(func() {
    93  
    94  		// Creating a new plot
    95  		p := hplot.New()
    96  		p.Title.Text = "Plot labels"
    97  		p.X.Min = 1
    98  		p.X.Max = 110
    99  		p.Y.Min = 1
   100  		p.Y.Max = 110
   101  
   102  		p.X.Scale = plot.LogScale{}
   103  		p.Y.Scale = plot.LogScale{}
   104  
   105  		p.Add(hplot.NewLabel(
   106  			0.5, 0.5,
   107  			"(0.5,0.5)\nMy Label",
   108  			hplot.WithLabelNormalized(true),
   109  		))
   110  
   111  		p.Add(hplot.NewLabel(
   112  			0.95, 0.95,
   113  			"(0.95,0.95)\nAuto-adjust",
   114  			hplot.WithLabelNormalized(true),
   115  			hplot.WithLabelAutoAdjust(true),
   116  		))
   117  
   118  		p.Add(plotter.NewGlyphBoxes())
   119  		p.Add(hplot.NewGrid())
   120  
   121  		// Save the plot to a PNG file.
   122  		err := p.Save(15*vg.Centimeter, -1, "testdata/label_log_plot.png")
   123  		if err != nil {
   124  			t.Fatalf("error saving plot: %v\n", err)
   125  		}
   126  
   127  	}, t, "label_log_plot.png")
   128  }