-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.go
132 lines (118 loc) · 2.87 KB
/
renderer.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
package main
import (
"html/template"
"io"
"log"
"os"
"time"
chart "github.com/wcharczuk/go-chart"
)
type RenderSpec struct {
Title string
TimeFormat string
Data []*DatedEntry
Titles []string
DataFormatter func(*DatedEntry) []float64
NumToDisplay int
}
func (rs RenderSpec) FormatData(de *DatedEntry) []float64 {
return rs.DataFormatter(de)
}
const (
templateText = `
<center>
{{- $spec := .}}
<h3>{{.Title}}</h3>
<table border=1 style="border-collapse:collapse">
<tr>
<td>Date</td>
{{- range $spec.Titles}}
<td>{{.}}</td>
{{- end}}
</tr>
{{- range $_, $d := $spec.Data}}
<tr>
<td><b>{{$d.Date.Format $spec.TimeFormat}}</b></td>
{{- $filteredData := ($spec.FormatData $d)}}
{{- range $filteredData}}
<td>{{.}}</td>
{{- end}}
</tr>
{{- end}}
</table>
</center>
<br>`
)
func RenderTemplate(w io.Writer, spec RenderSpec) {
num := spec.NumToDisplay
length := len(spec.Data)
if length < num {
num = length
}
newSpec := spec
newSpec.Data = make([]*DatedEntry, num)
for i := 0; i < num; i++ {
newSpec.Data[i] = spec.Data[length-1-i]
}
t, err := template.New("view").Parse(templateText)
if err != nil {
log.Fatalf("error parsing template: %v", err)
}
if err := t.Execute(w, newSpec); err != nil {
log.Fatalf("could not execute template: %v", err)
}
}
func RenderChart(w io.Writer, spec RenderSpec) {
num := spec.NumToDisplay
length := len(spec.Data)
if length < num {
num = length
}
newSpec := spec
newSpec.Data = spec.Data[length-1-num:]
titles := newSpec.Titles
numTypes := len(titles)
numPoints := len(newSpec.Data)
dates := make([]time.Time, numPoints)
dataSet := make([][]float64, numTypes)
for i := 0; i < numTypes; i++ {
dataSet[i] = make([]float64, numPoints)
}
for i, s := range newSpec.Data {
dates[i] = s.Date
formattedData := newSpec.FormatData(s)
for j := 0; j < numTypes; j++ {
dataSet[j][i] = formattedData[j]
}
}
graph := chart.Chart{
XAxis: chart.XAxis{Style: chart.Style{Show: true}},
YAxis: chart.YAxis{Style: chart.Style{Show: true}},
Background: chart.Style{Padding: chart.Box{Top: 20, Left: 20}},
Series: make([]chart.Series, numTypes),
}
for i := 0; i < numTypes; i++ {
graph.Series[i] = chart.TimeSeries{
Name: titles[i],
XValues: dates,
YValues: dataSet[i],
}
}
// note we have to do this as a separate step because we need a reference to graph
graph.Elements = []chart.Renderable{
chart.Legend(&graph),
}
if err := graph.Render(chart.PNG, w); err != nil {
log.Fatalf("failed to render: %v", err)
}
}
func RenderChartToFile(filename string, spec RenderSpec) {
f, err := os.Create(filename)
if err != nil {
log.Fatalf("could not create summary files: %v", err)
}
RenderChart(f, spec)
if err := f.Close(); err != nil {
log.Fatalf("failed to close %s: %v", filename, err)
}
}