• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / ark-pixel / 13728789674

07 Mar 2025 08:31PM CUT coverage: 89.367% (+61.3%) from 28.089%
13728789674

push

github

web-flow
Re-activate unit tests (#9)

1454 of 1627 relevant lines covered (89.37%)

222405.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.23
/plot/heatmap.go
1
package plot
2

3
import (
4
        "image/color"
5

6
        pixel "github.com/gopxl/pixel/v2"
7
        "github.com/gopxl/pixel/v2/backends/opengl"
8
        "github.com/mlange-42/ark-tools/observer"
9
        "github.com/mlange-42/ark/ecs"
10
        "gonum.org/v1/plot"
11
        "gonum.org/v1/plot/palette"
12
        "gonum.org/v1/plot/plotter"
13
        "gonum.org/v1/plot/vg"
14
        "gonum.org/v1/plot/vg/draw"
15
        "gonum.org/v1/plot/vg/vgimg"
16
)
17

18
// HeatMap plot drawer.
19
//
20
// Plots a grid as a heatmap image.
21
// For large grids, this is relatively slow.
22
// Consider using [Image] instead.
23
type HeatMap struct {
24
        Observer observer.Grid   // Observers providing a Grid for contours.
25
        Palette  palette.Palette // Color palette. Optional.
26
        Min      float64         // Minimum value for color mapping. Optional.
27
        Max      float64         // Maximum value for color mapping. Optional. Is set to 1.0 if both Min and Max are zero.
28
        Labels   Labels          // Labels for plot and axes. Optional.
29

30
        data  plotGrid
31
        scale float64
32
}
33

34
// Initialize the drawer.
35
func (h *HeatMap) Initialize(w *ecs.World, win *opengl.Window) {
1✔
36
        h.Observer.Initialize(w)
1✔
37
        h.data = plotGrid{
1✔
38
                Grid: h.Observer,
1✔
39
        }
1✔
40

1✔
41
        h.scale = calcScaleCorrection()
1✔
42

1✔
43
        if h.Min == 0 && h.Max == 0 {
1✔
44
                h.Max = 1
×
45
        }
×
46
}
47

48
// Update the drawer.
49
func (h *HeatMap) Update(w *ecs.World) {
100✔
50
        h.Observer.Update(w)
100✔
51
}
100✔
52

53
// UpdateInputs handles input events of the previous frame update.
54
func (h *HeatMap) UpdateInputs(w *ecs.World, win *opengl.Window) {}
100✔
55

56
// Draw the drawer.
57
func (h *HeatMap) Draw(w *ecs.World, win *opengl.Window) {
100✔
58
        width := win.Canvas().Bounds().W()
100✔
59
        height := win.Canvas().Bounds().H()
100✔
60

100✔
61
        h.updateData(w)
100✔
62

100✔
63
        c := vgimg.New(vg.Points(width*h.scale)-10, vg.Points(height*h.scale)-10)
100✔
64

100✔
65
        p := plot.New()
100✔
66
        setLabels(p, h.Labels)
100✔
67

100✔
68
        p.X.Tick.Marker = removeLastTicks{}
100✔
69

100✔
70
        cols := h.Palette.Colors()
100✔
71
        heat := plotter.HeatMap{
100✔
72
                GridXYZ:    &h.data,
100✔
73
                Palette:    h.Palette,
100✔
74
                Rasterized: false,
100✔
75
                Underflow:  cols[0],
100✔
76
                Overflow:   cols[len(cols)-1],
100✔
77
                Min:        h.Min,
100✔
78
                Max:        h.Max,
100✔
79
        }
100✔
80

100✔
81
        p.Add(&heat)
100✔
82

100✔
83
        win.Clear(color.White)
100✔
84
        p.Draw(draw.New(c))
100✔
85

100✔
86
        img := c.Image()
100✔
87
        picture := pixel.PictureDataFromImage(img)
100✔
88

100✔
89
        sprite := pixel.NewSprite(picture, picture.Bounds())
100✔
90
        sprite.Draw(win, pixel.IM.Moved(pixel.V(picture.Rect.W()/2.0+5, picture.Rect.H()/2.0+5)))
100✔
91
}
100✔
92

93
func (h *HeatMap) updateData(w *ecs.World) {
100✔
94
        h.data.Values = h.Observer.Values(w)
100✔
95
}
100✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc