...
1
2
3
4
5 package vgtex_test
6
7 import (
8 "image/color"
9 "testing"
10
11 "gonum.org/v1/plot"
12 "gonum.org/v1/plot/cmpimg"
13 "gonum.org/v1/plot/plotter"
14 "gonum.org/v1/plot/vg"
15 )
16
17 func TestTexCanvas(t *testing.T) {
18 cmpimg.CheckPlot(Example, t, "scatter.tex")
19 }
20
21 func TestLineLatex(t *testing.T) {
22 test := func(fname string) func() {
23 return func() {
24 p := plot.New()
25 p.X.Min = -10
26 p.X.Max = +10
27 p.Y.Min = -10
28 p.Y.Max = +10
29
30 f1 := plotter.NewFunction(func(float64) float64 {
31 return -7
32 })
33 f1.LineStyle.Color = color.Black
34 f1.LineStyle.Width = 2
35 f1.LineStyle.Dashes = []vg.Length{2, 1}
36
37 f2 := plotter.NewFunction(func(float64) float64 {
38 return -1
39 })
40 f2.LineStyle.Color = color.RGBA{R: 255, A: 255}
41 f2.LineStyle.Width = 2
42 f2.LineStyle.Dashes = []vg.Length{4, 2}
43
44 f3 := plotter.NewFunction(func(float64) float64 {
45 return +7
46 })
47 f3.LineStyle.Color = color.Black
48 f3.LineStyle.Width = 2
49 f3.LineStyle.Dashes = []vg.Length{2, 1}
50
51 p.Add(f1, f2, f3)
52 p.Add(plotter.NewGrid())
53
54 const size = 5 * vg.Centimeter
55 err := p.Save(size, size, fname)
56 if err != nil {
57 t.Fatalf("error: %+v", err)
58 }
59 }
60 }
61 cmpimg.CheckPlot(test("testdata/linestyle.tex"), t, "linestyle.tex")
62 cmpimg.CheckPlot(test("testdata/linestyle.png"), t, "linestyle.png")
63 }
64
65 func TestFillStyle(t *testing.T) {
66 cmpimg.CheckPlot(func() {
67 p := plot.New()
68 p.Title.Text = "Fill style"
69 p.Legend.Top = true
70 p.Legend.Left = true
71
72 const n = 10
73 xys := make(plotter.XYs, n)
74 for i := range xys {
75 xys[i].X = float64(i)
76 xys[i].Y = float64(i)
77 }
78 h, err := plotter.NewHistogram(xys, n)
79 if err != nil {
80 t.Fatalf("could not create histogram: %+v", err)
81 }
82 h.FillColor = color.NRGBA{R: 255, A: 100}
83 h.LineStyle.Color = color.Transparent
84 p.Add(h)
85 p.Legend.Add("h", h)
86
87 const size = 5 * vg.Centimeter
88 err = p.Save(size, size, "testdata/fillstyle.tex")
89 if err != nil {
90 t.Fatalf("error: %+v", err)
91 }
92 }, t, "fillstyle.tex")
93 }
94
View as plain text