-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.go
46 lines (38 loc) · 1001 Bytes
/
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
package slackactivity
import (
"fmt"
"time"
"github.com/slack-go/slack"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/plotutil"
"gonum.org/v1/plot/vg"
)
func GeneratePlot(
counts []MessageCount,
channel slack.Channel,
imageHeight int,
imageWidth int,
outputPath string,
) error {
values := make(plotter.Values, len(counts))
for i, r := range counts {
values[i] = float64(r.Count)
}
p := plot.New()
p.Title.Text = channel.Name + " [reported at " + time.Now().Format("2006/01/02") + " ]"
barwidth := float64(imageWidth / len(counts))
w := vg.Points(barwidth)
bars, err := plotter.NewBarChart(values, w)
if err != nil {
return fmt.Errorf("NewBarChart failed: %w", err)
}
bars.LineStyle.Width = vg.Length(0)
bars.Color = plotutil.Color(1)
bars.XMin = float64(-1 * len(counts))
p.Add(bars)
if err := p.Save(vg.Length(imageWidth), vg.Length(imageHeight), outputPath); err != nil {
return fmt.Errorf("plot.Save failed: %w", err)
}
return nil
}