This repository has been archived by the owner on Aug 19, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chart.go
138 lines (123 loc) · 3.52 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
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
package main
import (
"fmt"
"os"
"time"
"github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/drawing"
)
func writeChart(dailyKeyCounts []DailyKeyCount, filename string, country string) {
dates := make([]time.Time, 0)
newKeys := make([]float64, 0)
newKeys14Days := make([]float64, 0)
nonExpiredKeys := make([]float64, 0)
for _, v := range dailyKeyCounts {
d, _ := time.Parse(isoDateFormat, v.Date)
dates = append(dates, d)
// dates = append(dates, float64(i))
newKeys = append(newKeys, float64(v.NewKeysCount))
newKeys14Days = append(newKeys14Days, float64(v.NewKeysInLast14Days))
nonExpiredKeys = append(nonExpiredKeys, float64(v.NonExpiredKeys))
}
activeKeysSeries := chart.TimeSeries{
Name: "Novi ključi (14 dni)",
XValues: dates,
YValues: newKeys14Days,
YAxis: chart.YAxisPrimary,
Style: chart.Style{
Show: true,
StrokeWidth: 2,
StrokeColor: drawing.Color{R: 248, G: 198, B: 45, A: 255},
FillColor: drawing.Color{R: 252, G: 244, B: 213, A: 255},
// FillColor: drawing.Color{R: 248, G: 198, B: 45, A: 50},
},
}
nonExpiredKeysSeries := chart.TimeSeries{
Name: "Nepretečeni ključi",
XValues: dates,
YValues: nonExpiredKeys,
YAxis: chart.YAxisPrimary,
Style: chart.Style{
Show: true,
StrokeWidth: 2,
// StrokeDashArray: []float64{5, 2},
StrokeColor: drawing.ColorRed, //.Color{R: 248, G: 150, B: 5, A: 255},
FillColor: drawing.Color{R: 252, G: 204, B: 183, A: 255},
// FillColor: drawing.Color{R: 248, G: 198, B: 45, A: 50},
},
}
newKeysSeries := chart.TimeSeries{
Name: "Novi ključi (na dan)",
XValues: dates,
YValues: newKeys,
YAxis: chart.YAxisSecondary,
Style: chart.Style{
Show: true,
StrokeWidth: 2,
StrokeColor: drawing.Color{R: 78, G: 126, B: 245, A: 255},
FillColor: drawing.Color{R: 78, G: 126, B: 245, A: 50},
},
}
newKeysMovingAverageSeries := chart.SMASeries{
Period: 7,
InnerSeries: newKeysSeries,
YAxis: chart.YAxisSecondary,
Name: "Novi ključi (povprečje 7 dni)",
Style: chart.Style{
Show: true,
StrokeWidth: 2,
StrokeDashArray: []float64{5, 2},
StrokeColor: drawing.Color{R: 78, G: 126, B: 245, A: 255},
},
}
graph := chart.Chart{
Width: 1000,
Height: 600,
Title: fmt.Sprintf("%s ključi na #OstaniZdrav strežniku", country),
TitleStyle: chart.StyleShow(),
XAxis: chart.XAxis{
Name: "Dan",
TickPosition: chart.TickPositionBetweenTicks,
Style: chart.Style{
Show: true,
TextRotationDegrees: 90,
},
},
YAxis: chart.YAxis{
Name: "Nepretečeni/Novi ključi (14 dni)",
NameStyle: chart.StyleShow(),
Style: chart.StyleShow(),
ValueFormatter: func(v interface{}) string {
if vf, isFloat := v.(float64); isFloat {
return fmt.Sprintf("%0.0f", vf)
}
return ""
},
},
YAxisSecondary: chart.YAxis{
Name: "Novi ključi (na dan)",
NameStyle: chart.StyleShow(),
Style: chart.StyleShow(),
ValueFormatter: func(v interface{}) string {
if vf, isFloat := v.(float64); isFloat {
return fmt.Sprintf("%0.0f", vf)
}
return ""
},
},
Series: []chart.Series{
activeKeysSeries,
chart.LastValueAnnotation(activeKeysSeries),
nonExpiredKeysSeries,
chart.LastValueAnnotation(nonExpiredKeysSeries),
newKeysSeries,
newKeysMovingAverageSeries,
},
}
graph.Elements = []chart.Renderable{
chart.Legend(&graph),
}
f, _ := os.Create(filename)
defer f.Close()
graph.Render(chart.PNG, f)
}