/
renderer.go
76 lines (61 loc) · 1.76 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
package graph
import (
"github.com/ul-gaul/go-basestation/data/packet"
"github.com/ul-gaul/go-basestation/ui/vars"
"github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
)
type Renderer func([]packet.RocketPacket) []chart.Series
type SimpleAdapter func(packet.RocketPacket) (float64, float64)
func NewSimpleRenderer(adapter SimpleAdapter) Renderer {
return func(packets []packet.RocketPacket) []chart.Series {
xValues := make([]float64, len(packets))
yValues := make([]float64, len(packets))
for i, pkt := range packets {
xValues[i], yValues[i] = adapter(pkt)
}
mainSeries := chart.ContinuousSeries{
Style: chart.Style{
StrokeColor: chart.GetDefaultColor(0),
},
XValues: xValues,
YValues: yValues,
}
return []chart.Series{
mainSeries,
}
}
}
type MultiSeriesAdapter func(packet.RocketPacket) (x float64, yValues []float64)
func NewMultiSeriesRenderer(adapter MultiSeriesAdapter, names ...string) Renderer {
return func(packets []packet.RocketPacket) []chart.Series {
var yValuesList [][]float64
xValues := make([]float64, len(packets))
for i, pkt := range packets {
var yValues []float64
xValues[i], yValues = adapter(pkt)
for j, yValue := range yValues {
if j >= len(yValuesList) {
yValuesList = append(yValuesList, make([]float64, len(packets)))
}
yValuesList[j][i] = yValue
}
}
series := make([]chart.Series, len(yValuesList))
for i, yValues := range yValuesList {
r, g, b, a := vars.Color(i).RGBA()
s := chart.ContinuousSeries{
Style: chart.Style{
StrokeColor: drawing.ColorFromAlphaMixedRGBA(r, g, b, a),
},
XValues: xValues,
YValues: yValues,
}
if i < len(names) {
s.Name = names[i]
}
series[i] = s
}
return series
}
}