-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtable_test.go
167 lines (144 loc) · 3.83 KB
/
table_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright ©2018 Peter Paolucci. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// This file is based on gonum.org/v1/plot/align_test.go which is
// Copyright ©2017 The gonum Authors. All rights reserved.
package plotext
import (
"math"
"os"
"testing"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/draw"
"gonum.org/v1/plot/vg/vgimg"
"github.com/pplcc/plotext/internal"
)
func TestAt(t *testing.T) {
// 2 3 1
// +----+------+--+
// | | | |
// | | | | 3
// | | | |
// +----+------+--+
// | | | | 1
// +----+------+--+
// | | | |
// | | | | 2
// +----+------+--+
// +----+------+--+ 0.5
table := Table{
ColWidths: []float64{2, 3, 1},
RowHeights: []float64{3, 1, 2, 0.5},
PadTop: 1,
PadBottom: 2,
PadRight: 3,
PadLeft: 4,
PadX: 5,
PadY: 6,
}
img := vgimg.New(vg.Points(150), vg.Points(175))
dc := draw.New(img)
for j := 0; j < len(table.RowHeights); j++ {
for i := 0; i < len(table.ColWidths); i++ {
c := table.At(dc, i, j)
// frame
c.StrokeLines(plotter.DefaultLineStyle, []vg.Point{c.Rectangle.Min, {X: c.Rectangle.Min.X, Y: c.Rectangle.Max.Y}, c.Rectangle.Max, {X: c.Rectangle.Max.X, Y: c.Rectangle.Min.Y}, c.Rectangle.Min})
// cross
c.StrokeLine2(plotter.DefaultLineStyle, c.Rectangle.Min.X, c.Rectangle.Min.Y, c.Rectangle.Max.X, c.Rectangle.Max.Y)
c.StrokeLine2(plotter.DefaultLineStyle, c.Rectangle.Min.X, c.Rectangle.Max.Y, c.Rectangle.Max.X, c.Rectangle.Min.Y)
}
}
testFile := "testdata/tableat.png"
w, err := os.Create(testFile)
if err != nil {
panic(err)
}
png := vgimg.PngCanvas{Canvas: img}
if _, err := png.WriteTo(w); err != nil {
panic(err)
}
internal.TestImage(t, testFile)
}
func TestAlign(t *testing.T) {
// 2 3 1
// +----+------+--+
// | | | |
// | | | | 3
// | | | |
// +----+------+--+
// | | | | 1
// +----+------+--+
// | | | |
// | | | | 2
// +----+------+--+
// +----+------+--+ 0.5
table := Table{
ColWidths: []float64{2, 3, 1},
RowHeights: []float64{3, 1, 2, 0.5},
PadTop: 1,
PadBottom: 2,
PadRight: 3,
PadLeft: 4,
PadX: 5,
PadY: 6,
}
plots := make([][]*plot.Plot, len(table.RowHeights))
for j := 0; j < len(table.RowHeights); j++ {
plots[j] = make([]*plot.Plot, len(table.ColWidths))
for i := 0; i < len(table.ColWidths); i++ {
if i == 0 && j == 2 {
// This shows what happens when there are nil plots.
continue
}
p, err := plot.New()
if err != nil {
panic(err)
}
if j == 0 && i == 2 {
// This shows what happens when the axis padding
// is different among plots.
p.X.Padding, p.Y.Padding = 0, 0
}
if true && j == 1 && i == 1 {
// To test the Align function, we make the axis labels
// on one of the plots stick out.
p.Y.Max = 1e9
p.X.Max = 1e9
p.X.Tick.Label.Rotation = math.Pi / 2
p.X.Tick.Label.XAlign = draw.XRight
p.X.Tick.Label.YAlign = draw.YCenter
p.X.Tick.Label.Font.Size = 8
p.Y.Tick.Label.Font.Size = 8
} else {
p.Y.Max = 1e9
p.X.Max = 1e9
p.X.Tick.Label.Font.Size = 1
p.Y.Tick.Label.Font.Size = 1
}
plots[j][i] = p
}
}
img := vgimg.New(vg.Points(300), vg.Points(350))
dc := draw.New(img)
canvases := table.Align(plots, dc)
for j := 0; j < len(table.RowHeights); j++ {
for i := 0; i < len(table.ColWidths); i++ {
if plots[j][i] != nil {
plots[j][i].Draw(canvases[j][i])
}
}
}
testFile := "testdata/tablealign.png"
w, err := os.Create(testFile)
if err != nil {
panic(err)
}
png := vgimg.PngCanvas{Canvas: img}
if _, err := png.WriteTo(w); err != nil {
panic(err)
}
internal.TestImage(t, testFile)
}