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