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

mlange-42 / ark-pixel / 13725782068

07 Mar 2025 05:16PM CUT coverage: 28.089% (-19.3%) from 47.374%
13725782068

Pull #8

github

web-flow
Merge c22a6e346 into 2253cb584
Pull Request #8: Add first plot drawers

7 of 677 new or added lines in 11 files covered. (1.03%)

457 of 1627 relevant lines covered (28.09%)

55.34 hits per line

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

0.0
/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.
NEW
35
func (h *HeatMap) Initialize(w *ecs.World, win *opengl.Window) {
×
NEW
36
        h.Observer.Initialize(w)
×
NEW
37
        h.data = plotGrid{
×
NEW
38
                Grid: h.Observer,
×
NEW
39
        }
×
NEW
40

×
NEW
41
        h.scale = calcScaleCorrection()
×
NEW
42

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

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

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

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

×
NEW
61
        h.updateData(w)
×
NEW
62

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

×
NEW
65
        p := plot.New()
×
NEW
66
        setLabels(p, h.Labels)
×
NEW
67

×
NEW
68
        p.X.Tick.Marker = removeLastTicks{}
×
NEW
69

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

×
NEW
81
        p.Add(&heat)
×
NEW
82

×
NEW
83
        win.Clear(color.White)
×
NEW
84
        p.Draw(draw.New(c))
×
NEW
85

×
NEW
86
        img := c.Image()
×
NEW
87
        picture := pixel.PictureDataFromImage(img)
×
NEW
88

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

NEW
93
func (h *HeatMap) updateData(w *ecs.World) {
×
NEW
94
        h.data.Values = h.Observer.Values(w)
×
NEW
95
}
×
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