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

mlange-42 / arche-pixel / 13722336367

07 Mar 2025 02:05PM CUT coverage: 88.192% (-0.06%) from 88.253%
13722336367

Pull #57

github

web-flow
Merge 30e2708bf into a3b6ea536
Pull Request #57: Test CI

1434 of 1626 relevant lines covered (88.19%)

222511.39 hits per line

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

96.88
/plot/bars.go
1
package plot
2

3
import (
4
        "fmt"
5
        "image/color"
6

7
        pixel "github.com/gopxl/pixel/v2"
8
        "github.com/gopxl/pixel/v2/backends/opengl"
9
        "github.com/mlange-42/arche-model/observer"
10
        "github.com/mlange-42/arche/ecs"
11
        "gonum.org/v1/plot"
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
// Bars plot drawer.
19
//
20
// Creates a bar per column of the observer.
21
type Bars struct {
22
        Observer observer.Row // Observer providing a data series for bars.
23
        Columns  []string     // Columns to show, by name. Optional, default all.
24
        YLim     [2]float64   // Y axis limits. Optional, default auto.
25
        Labels   Labels       // Labels for plot and axes. Optional.
26

27
        indices []int
28
        headers []string
29
        series  plotter.Values
30
        scale   float64
31
}
32

33
// Initialize the drawer.
34
func (b *Bars) Initialize(w *ecs.World, win *opengl.Window) {
2✔
35
        b.Observer.Initialize(w)
2✔
36

2✔
37
        headers := b.Observer.Header()
2✔
38

2✔
39
        if len(b.Columns) == 0 {
3✔
40
                b.indices = make([]int, len(headers))
1✔
41
                for i := 0; i < len(b.indices); i++ {
4✔
42
                        b.indices[i] = i
3✔
43
                }
3✔
44
        } else {
1✔
45
                b.indices = make([]int, len(b.Columns))
1✔
46
                var ok bool
1✔
47
                for i := 0; i < len(b.indices); i++ {
3✔
48
                        b.indices[i], ok = find(headers, b.Columns[i])
2✔
49
                        if !ok {
2✔
50
                                panic(fmt.Sprintf("column '%s' not found", b.Columns[i]))
×
51
                        }
52
                }
53
        }
54

55
        b.series = make([]float64, len(b.indices))
2✔
56
        b.headers = make([]string, len(b.indices))
2✔
57
        for i, idx := range b.indices {
7✔
58
                b.headers[i] = headers[idx]
5✔
59
        }
5✔
60

61
        b.scale = calcScaleCorrection()
2✔
62

63
}
64

65
// Update the drawer.
66
func (b *Bars) Update(w *ecs.World) {
200✔
67
        b.Observer.Update(w)
200✔
68
}
200✔
69

70
// UpdateInputs handles input events of the previous frame update.
71
func (b *Bars) UpdateInputs(w *ecs.World, win *opengl.Window) {}
117✔
72

73
// Draw the drawer.
74
func (b *Bars) Draw(w *ecs.World, win *opengl.Window) {
117✔
75
        width := win.Canvas().Bounds().W()
117✔
76
        height := win.Canvas().Bounds().H()
117✔
77

117✔
78
        b.updateData(w)
117✔
79

117✔
80
        c := vgimg.New(vg.Points(width*b.scale)-10, vg.Points(height*b.scale)-10)
117✔
81

117✔
82
        p := plot.New()
117✔
83
        setLabels(p, b.Labels)
117✔
84

117✔
85
        if b.YLim[0] != 0 || b.YLim[1] != 0 {
234✔
86
                p.Y.Min = b.YLim[0]
117✔
87
                p.Y.Max = b.YLim[1]
117✔
88
        }
117✔
89

90
        bw := 0.5 * (width - 50) / float64(len(b.series))
117✔
91
        bars, err := plotter.NewBarChart(b.series, vg.Points(bw))
117✔
92
        if err != nil {
117✔
93
                panic(err)
×
94
        }
95
        bars.Color = defaultColors[0]
117✔
96
        p.Add(bars)
117✔
97
        p.NominalX(b.headers...)
117✔
98

117✔
99
        win.Clear(color.White)
117✔
100
        p.Draw(draw.New(c))
117✔
101

117✔
102
        img := c.Image()
117✔
103
        picture := pixel.PictureDataFromImage(img)
117✔
104

117✔
105
        sprite := pixel.NewSprite(picture, picture.Bounds())
117✔
106
        sprite.Draw(win, pixel.IM.Moved(pixel.V(picture.Rect.W()/2.0+5, picture.Rect.H()/2.0+5)))
117✔
107
}
108

109
func (b *Bars) updateData(w *ecs.World) {
117✔
110
        values := b.Observer.Values(w)
117✔
111

117✔
112
        for i, idx := range b.indices {
451✔
113
                b.series[i] = values[idx]
334✔
114
        }
334✔
115
}
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