-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtrend_line_test.go
102 lines (97 loc) · 3.34 KB
/
trend_line_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
package charts
import (
"strconv"
"testing"
"github.com/stretchr/testify/require"
)
func TestTrendLine(t *testing.T) {
t.Parallel()
tests := []struct {
name string
render func(*Painter) ([]byte, error)
result string
}{
{
name: "linear",
render: func(p *Painter) ([]byte, error) {
trendLine := newTrendLinePainter(p)
axisRange := newTestRange(p.Height(), 6, 0.0, 10.0, 0.0, 0.0)
xValues := []int{50, 150, 250, 350, 450, 550}
trend := SeriesTrendLine{
Type: SeriesTrendTypeLinear,
}
trendLine.add(trendLineRenderOption{
defaultStrokeColor: ColorBlack,
xValues: xValues,
seriesValues: []float64{1, 2, 3, 4, 5, 6},
axisRange: axisRange,
trends: []SeriesTrendLine{trend},
})
if _, err := trendLine.Render(); err != nil {
return nil, err
}
return p.Bytes()
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 600 400\"><path d=\"M 70 344\nL 170 308\nL 270 272\nL 370 236\nL 470 200\nL 570 164\" style=\"stroke-width:2;stroke:black;fill:none\"/></svg>",
},
{
name: "cubic",
render: func(p *Painter) ([]byte, error) {
trendLine := newTrendLinePainter(p)
axisRange := newTestRange(p.Height(), 6, 0.0, 40.0, 0.0, 0.0)
xValues := []int{50, 150, 250, 350, 450, 550}
trend := SeriesTrendLine{
Type: SeriesTrendTypeCubic,
}
trendLine.add(trendLineRenderOption{
defaultStrokeColor: ColorBlack,
xValues: xValues,
seriesValues: []float64{1, 4, 9, 16, 25, 36},
axisRange: axisRange,
trends: []SeriesTrendLine{trend},
})
if _, err := trendLine.Render(); err != nil {
return nil, err
}
return p.Bytes()
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 600 400\"><path d=\"M 70 371\nL 170 345\nL 270 300\nL 370 236\nL 470 155\nL 570 57\" style=\"stroke-width:2;stroke:black;fill:none\"/></svg>",
},
{
name: "average",
render: func(p *Painter) ([]byte, error) {
trendLine := newTrendLinePainter(p)
axisRange := newTestRange(p.Height(), 6, 0.0, 6.0, 0.0, 0.0)
xValues := []int{50, 150, 250, 350, 450, 550}
trend := SeriesTrendLine{
Type: SeriesTrendTypeAverage,
Window: 3,
}
trendLine.add(trendLineRenderOption{
defaultStrokeColor: ColorBlack,
xValues: xValues,
seriesValues: []float64{1, 2, 3, 4, 5, 6},
axisRange: axisRange,
trends: []SeriesTrendLine{trend},
})
if _, err := trendLine.Render(); err != nil {
return nil, err
}
return p.Bytes()
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 600 400\"><path d=\"M 70 320\nL 170 290\nL 270 260\nL 370 200\nL 470 140\nL 570 80\" style=\"stroke-width:2;stroke:black;fill:none\"/></svg>",
},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i)+"-"+tt.name, func(t *testing.T) {
p := NewPainter(PainterOptions{
OutputFormat: ChartOutputSVG,
Width: 600,
Height: 400,
}, PainterThemeOption(GetTheme(ThemeLight)))
data, err := tt.render(p.Child(PainterPaddingOption(NewBoxEqual(20))))
require.NoError(t, err)
assertEqualSVG(t, tt.result, data)
})
}
}