-
Notifications
You must be signed in to change notification settings - Fork 0
/
charts.go
137 lines (117 loc) · 4.74 KB
/
charts.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
package envoycharts
import (
"io"
"time"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/opts"
"github.com/go-echarts/go-echarts/v2/types"
)
// Barchart takes a set of samples, and a title and writes the barchart to the specified writer
func Barchart(w io.Writer, samples []*Sample, title string, tz *time.Location) {
bar := charts.NewBar()
// use our custom template
useECTemplates()
// bar.Renderer = NewECRenderer(bar, bar.Validate)
bar.SetGlobalOptions(
charts.WithTitleOpts(opts.Title{Title: title}),
charts.WithLegendOpts(opts.Legend{Show: true, Data: []string{"Production", "Consumption", "Export/Import"}}),
charts.WithInitializationOpts(opts.Initialization{Theme: types.ThemeWesteros}),
// charts.WithToolboxOpts(opts.Toolbox{Show: true}),
// charts.WithXAxisOpts(opts.XAxis{Type: "time"}),
charts.WithYAxisOpts(opts.YAxis{Name: "kWh", Type: "value"}),
)
barProd := make([]opts.BarData, 0)
barCon := make([]opts.BarData, 0)
barNet := make([]opts.BarData, 0)
dates := make([]string, 0)
for _, v := range samples {
fu := v.Date.In(tz)
t := fu.Format("2006/01/02 15:04:05")
dates = append(dates, t)
barProd = append(barProd, opts.BarData{Value: v.ProductionW})
barCon = append(barCon, opts.BarData{Value: 0 - v.ConsumptionW})
barNet = append(barNet, opts.BarData{Value: 0 - v.NetW})
}
bar.SetXAxis(dates).
AddSeries("Production", barProd).
AddSeries("Consumption", barCon).
AddSeries("Export/Import", barNet)
bar.Render(w)
}
// Linechart takes a set of samples, and a title and writes the linechart to the specified writer
func Linechart(w io.Writer, samples []*Sample, title string, tz *time.Location) {
line := charts.NewLine()
// use our custom template
useECTemplates()
// line.Renderer = NewECRenderer(line, line.Validate)
line.SetGlobalOptions(
charts.WithTitleOpts(opts.Title{Title: title}),
charts.WithLegendOpts(opts.Legend{Show: true, Data: []string{"Production", "Consumption", "Export/Import"}}),
charts.WithInitializationOpts(opts.Initialization{Theme: types.ThemeWesteros}),
// charts.WithToolboxOpts(opts.Toolbox{Show: true}),
// charts.WithXAxisOpts(opts.XAxis{Type: "time"}),
charts.WithXAxisOpts(opts.XAxis{Name: "Time"}),
charts.WithYAxisOpts(opts.YAxis{Name: "W", Type: "value"}),
)
prod := make([]opts.LineData, 0)
con := make([]opts.LineData, 0)
net := make([]opts.LineData, 0)
dates := make([]string, 0)
for _, v := range samples {
fu := v.Date.In(tz)
t := fu.Format("2006/01/02 15:04:05")
dates = append(dates, t)
prod = append(prod, opts.LineData{Value: v.ProductionW})
con = append(con, opts.LineData{Value: 0 - v.ConsumptionW})
net = append(net, opts.LineData{Value: 0 - v.NetW})
}
line.SetXAxis(dates).
AddSeries("Production", prod).
AddSeries("Consumption", con).
AddSeries("Export/Import", net).
SetSeriesOptions(
charts.WithLineChartOpts(opts.LineChart{Smooth: true}),
charts.WithAreaStyleOpts(opts.AreaStyle{Opacity: 0.3}),
// charts.WithLabelOpts(opts.Label{Show: true}),
// charts.WithMarkLineNameTypeItemOpts(opts.MarkLineNameTypeItem{ Name: "kWh", Type: "value", }),
)
line.Render(w)
}
// LinechartDaily takes a set of daily values, and a title and writes the linechart to the specified writer
func LinechartDaily(w io.Writer, samples []*Daily, title string, tz *time.Location) {
line := charts.NewLine()
// use our custom template
useECTemplates()
// line.Renderer = NewECRenderer(line, line.Validate)
line.SetGlobalOptions(
charts.WithTitleOpts(opts.Title{Title: title}),
charts.WithLegendOpts(opts.Legend{Show: true, Data: []string{"Production", "Consumption", "Net"}}),
charts.WithInitializationOpts(opts.Initialization{Theme: types.ThemeWesteros}),
// charts.WithToolboxOpts(opts.Toolbox{Show: true}),
// charts.WithXAxisOpts(opts.XAxis{Type: "time"}),
charts.WithXAxisOpts(opts.XAxis{Name: "Date"}),
charts.WithYAxisOpts(opts.YAxis{Name: "kWh", Type: "value"}),
)
prod := make([]opts.LineData, 0)
con := make([]opts.LineData, 0)
net := make([]opts.LineData, 0)
dates := make([]string, 0)
for _, v := range samples {
t := v.Date.In(tz).Format("2006/01/02")
dates = append(dates, t)
prod = append(prod, opts.LineData{Value: v.ProductionkWh})
con = append(con, opts.LineData{Value: 0 - v.ConsumptionkWh})
net = append(net, opts.LineData{Value: v.ProductionkWh - v.ConsumptionkWh})
}
line.SetXAxis(dates).
AddSeries("Production", prod).
AddSeries("Consumption", con).
AddSeries("Net", net).
SetSeriesOptions(
charts.WithLineChartOpts(opts.LineChart{Smooth: true}),
charts.WithAreaStyleOpts(opts.AreaStyle{Opacity: 0.3}),
// charts.WithLabelOpts(opts.Label{Show: true}),
// charts.WithMarkLineNameTypeItemOpts(opts.MarkLineNameTypeItem{ Name: "kWh", Type: "value", }),
)
line.Render(w)
}