-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.go
77 lines (61 loc) · 1.62 KB
/
plot.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
package main
import (
"github.com/diamondburned/gotk4/pkg/cairo"
"github.com/diamondburned/gotk4/pkg/gtk/v4"
)
type Plotter interface {
Plot(area *gtk.DrawingArea, cr *cairo.Context, width, height int)
}
type VRAMPlotter struct {
dataStorage *DataStorage
padding int
vertLines int
horizLines int
maxValue int
minValue int
}
func NewVRAMPlotter(dataStorage *DataStorage, minValue, maxValue int) *VRAMPlotter {
return &VRAMPlotter{
dataStorage: dataStorage,
padding: 100,
vertLines: 10,
horizLines: 20,
maxValue: maxValue,
minValue: minValue,
}
}
func (vp *VRAMPlotter) Plot(area *gtk.DrawingArea, cr *cairo.Context, width, height int) {
data := vp.dataStorage.GetData()
// TODO : refactor
fWidth := float64(width)
fHeight := float64(height)
fPadding := float64(vp.padding)
ContainerRectMin := (fPadding) / 2
ContainerXMax := fWidth - (fPadding)/2
ContainerYMax := fHeight - (fPadding)/2
usableContainerHeight := ContainerYMax - ContainerRectMin
usableContainerWidth := ContainerXMax - ContainerRectMin
dsSize := vp.dataStorage.maxPoints
if len(data) == 0 {
return
}
cr.SetSourceRGB(0, 0, 0)
cr.Paint()
vp.Grid(cr, width, height)
cr.SetSourceRGB(0, 1, 0)
cr.SetLineWidth(2)
cr.MoveTo(ContainerRectMin, ContainerYMax)
normalizedY := func(Y float64) float64 {
return (ContainerYMax - Y)
}
for i, dp := range data {
x := ContainerRectMin + ((float64(i) / float64(dsSize-1)) * usableContainerWidth)
y := normalizedY((float64(dp.UsedMB) / float64(dp.TotalMB)) * usableContainerHeight)
if i == 0 {
cr.MoveTo(x, y)
continue
}
cr.LineTo(x, y)
}
cr.Stroke()
}