forked from tomarus/chart
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchart_test.go
More file actions
191 lines (167 loc) · 4.35 KB
/
chart_test.go
File metadata and controls
191 lines (167 loc) · 4.35 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package chart
import (
"bufio"
"bytes"
"fmt"
"os"
"testing"
"time"
"github.com/tomarus/chart/axis"
"github.com/tomarus/chart/data"
"github.com/tomarus/chart/image"
"github.com/tomarus/chart/png"
"github.com/tomarus/chart/svg"
)
func TestChart(t *testing.T) {
var out bytes.Buffer
w := bufio.NewWriter(&out)
opts := &Options{
Title: "Test Title",
Image: svg.New(),
Size: "small",
Scheme: "random",
Theme: "light",
Start: time.Now().AddDate(0, 0, -1).Unix(),
End: time.Now().Unix(),
Axes: []*axis.Axis{
axis.NewTime(axis.Bottom, "01-02 15:04").Ticks(12),
axis.NewSI(axis.Left, 1000).Ticks(5),
},
W: w,
}
// test sizes
c, err := NewChart(opts)
if err != nil {
t.Fatal(err)
}
if c.width != 720 {
t.Fatal("width should be 720")
}
opts.Size = "big"
c, _ = NewChart(opts)
if c.width != 1440 {
t.Fatal("width should be 1440")
}
opts.Width = 320
opts.Height = 240
c, _ = NewChart(opts)
if c.width != 320 || c.height != 240 {
t.Fatal("expected width/ehgith 320x240")
}
// test palette
opts.Width = 0
opts.Height = 0
opts.Size = "auto"
opts.Scheme = ""
c, _ = NewChart(opts)
if c.palette.GetHexColor("background") != "#ffffff" {
t.Fatal("default scheme should be white")
}
// test data
c.AddData(&data.Options{Title: "testing"}, []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12})
if c.width != 12 {
// A width of 12px is actually unviewable
t.Errorf("Expected width of 10, got %d", c.width)
}
c, _ = NewChart(opts)
err = c.Render()
if err == nil {
t.Fatal("expected error no data available")
}
c, _ = NewChart(opts)
c.AddData(&data.Options{Title: "testing"}, []float64{1, 2, 3, 4, 5, 6})
err = c.Render()
if err == nil {
t.Fatal("expected error xdiv <= datalen")
}
opts.Width = 720
opts.Height = 540
c, _ = NewChart(opts)
c.AddData(&data.Options{}, []float64{1, 2, 3, 4, 5, 6})
err = c.Render()
if err != nil {
t.Fatalf("unexpected error %v", err)
}
// TODO: actually test svg output somehow
opts.Image = png.New()
c, _ = NewChart(opts)
c.AddData(&data.Options{}, []float64{1, 2, 3, 4, 5, 6})
err = c.Render()
if err != nil {
t.Fatalf("unexpected error %v", err)
}
// TODO: actually test png output somehow
}
func testimg(img image.Image) {
var out bytes.Buffer
w := bufio.NewWriter(&out)
opts := &Options{
Title: "Test Title",
Image: img,
Size: "small",
Scheme: "white",
Theme: "light",
Start: time.Now().AddDate(0, 0, -1).Unix(),
End: time.Now().Unix(),
Axes: []*axis.Axis{
axis.NewTime(axis.Bottom, "01-02 15:04").Duration(4 * time.Hour).Grid(4),
axis.NewSI(axis.Left, 1000).Ticks(4).Grid(2),
},
W: w,
}
c, _ := NewChart(opts)
c.AddData(&data.Options{}, []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12})
c.Render()
}
func BenchmarkSVG(b *testing.B) {
for i := 0; i < b.N; i++ {
testimg(svg.New())
}
}
func BenchmarkPNG(b *testing.B) {
for i := 0; i < b.N; i++ {
testimg(png.New())
}
}
func ExampleChart() {
opts := &Options{
Title: "Title on top of the chart",
Image: svg.New(), // or png.New()
Size: "big", // big is 1440px, small is 720px, auto is size of dataset
Height: 300, // Defaults to -1, when size=auto height is set to width/4, otherwise set fixed height
Width: 900, // If a width is supplied, height is implied and both are used in stead of size setting
Scheme: "white", // or black/random/pink/solarized or hsl:180,0.5,0.25
Theme: "light", // default is dark.
Start: time.Now().AddDate(0, 0, -1).Unix(),
End: time.Now().Unix(),
W: os.Stdout,
Axes: []*axis.Axis{
axis.NewTime(axis.Bottom, "01-02 15:04").Duration(4 * time.Hour).Grid(4),
axis.NewSI(axis.Left, 1000).Ticks(4).Grid(2),
//
// - Example custom time format
// axis.New(axis.Bottom, func(in float64) string {
// return time.Unix(int64(in), 0).Format("01-02 15:04")
// return in.(time.Time).Format("2006-01-02")
// }).Duration(4 * time.Hour).Grid(),
//
// - Example other custom format
// axis.New(axis.Left, func(in float64) string {
// return fmt.Sprintf("%.1f", in/3.14)
// }).Ticks(5).Grid(),
},
}
c, err := NewChart(opts)
if err != nil {
panic(err)
}
exdata := make([]float64, 256)
for i := 0; i < 255; i++ {
exdata[i] = float64(i)
}
warn := c.AddData(&data.Options{Type: "area", Title: "My Data Description"}, exdata)
if err != nil {
fmt.Println(warn)
}
c.Render()
}