-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchart.go
109 lines (96 loc) · 2.22 KB
/
chart.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
//
// chart.go
// Copyright (C) 2016 Karol Będkowski
//
package main
import (
"github.com/wcharczuk/go-chart"
"os"
"time"
)
func MyTimeMinuteValueFormatter(v interface{}) string {
return chart.TimeValueFormatterWithFormat(v, "02.01 15:04")
}
type Plot struct {
Rows Rows
Cols []string
Width int
Height int
UseSecoundAxis bool
}
func (p *Plot) plotChart(filename string) {
series := make([]chart.TimeSeries, 0, len(p.Cols))
numRows := len(p.Rows)
ts := chart.TimeSeries{
XValues: make([]time.Time, 0, numRows),
YValues: make([]float64, 0, numRows),
Name: p.Cols[0],
Style: chart.Style{
Show: true,
FontColor: chart.ColorBlue,
StrokeColor: chart.ColorBlue,
},
}
series = append(series, ts)
if len(p.Cols) == 2 {
ts := chart.TimeSeries{
XValues: make([]time.Time, 0, numRows),
YValues: make([]float64, 0, numRows),
Name: p.Cols[1],
Style: chart.Style{
Show: true,
FontColor: chart.ColorGreen,
StrokeColor: chart.ColorGreen,
},
}
if p.UseSecoundAxis {
ts.YAxis = chart.YAxisSecondary
}
series = append(series, ts)
}
for _, row := range p.Rows {
ts := time.Unix(row.TS, 0)
for serieNo, col := range row.Values {
if col.Valid {
series[serieNo].XValues = append(series[serieNo].XValues, ts)
series[serieNo].YValues = append(series[serieNo].YValues, float64(col.Value))
}
}
}
cSeries := make([]chart.Series, 0, len(series))
for _, s := range series {
cSeries = append(cSeries, s)
}
graph := chart.Chart{
Width: p.Width,
Height: p.Height,
XAxis: chart.XAxis{
Style: chart.Style{
Show: true,
},
},
YAxis: chart.YAxis{
Style: chart.Style{
Show: true, //enables / displays the y-axis
},
},
Series: cSeries,
}
tsRange := p.Rows[numRows-1].TS - p.Rows[0].TS
if tsRange < 60*60*24*2 { // 2 days
graph.XAxis.ValueFormatter = MyTimeMinuteValueFormatter
}
if len(p.Cols) > 1 && p.UseSecoundAxis {
graph.YAxisSecondary = chart.YAxis{
Style: chart.Style{
Show: true, //enables / displays the secondary y-axis
},
}
}
graph.Elements = []chart.Renderable{
chart.Legend(&graph),
}
f, _ := os.Create(filename)
defer f.Close()
graph.Render(chart.PNG, f)
}