add deliveries
This commit is contained in:
91
bot_graph.go
91
bot_graph.go
@@ -215,6 +215,97 @@ func botGraphValueDelta(m *tb.Message) {
|
||||
return
|
||||
}
|
||||
|
||||
func botGraphDeliveries(m *tb.Message) {
|
||||
var (
|
||||
maxVal float64
|
||||
unitFactor float64
|
||||
unitName string
|
||||
)
|
||||
for _, dStats := range cfg.StatsMonthly {
|
||||
for _, stat := range dStats {
|
||||
valueFloat := float64(stat.DeliveredCargoLastQuarter)
|
||||
if math.Abs(valueFloat) > maxVal {
|
||||
maxVal = math.Abs(valueFloat)
|
||||
}
|
||||
}
|
||||
}
|
||||
if maxVal > 1000000000 {
|
||||
unitFactor = 1000000000
|
||||
unitName = "billion"
|
||||
} else if maxVal > 1000000 {
|
||||
unitFactor = 1000000
|
||||
unitName = "million"
|
||||
} else {
|
||||
unitFactor = 1
|
||||
unitName = ""
|
||||
}
|
||||
|
||||
var vals map[int]plotter.XYs
|
||||
vals = make(map[int]plotter.XYs)
|
||||
for ccID, dStats := range cfg.StatsMonthly {
|
||||
vals[ccID] = make(plotter.XYs, 0)
|
||||
for dStr, stat := range dStats {
|
||||
d, err := time.Parse("20060102", dStr)
|
||||
logErrorDebug(err, "botGraphDeliveries : time.Parse")
|
||||
if err != nil {
|
||||
bot.SendChat(m.Chat.ID, fmt.Sprintf("time.Parse : %s", err))
|
||||
return
|
||||
}
|
||||
dateFloat := float64(d.Year()) + float64(d.Month()-1)/12
|
||||
valueFloat := float64(stat.DeliveredCargoLastQuarter)
|
||||
pt := plotter.XY{
|
||||
X: dateFloat,
|
||||
Y: valueFloat / unitFactor,
|
||||
}
|
||||
vals[ccID] = append(vals[ccID], pt)
|
||||
}
|
||||
sort.Slice(vals[ccID], func(i, j int) bool { return vals[ccID][i].X < vals[ccID][j].X })
|
||||
}
|
||||
|
||||
p := plot.New()
|
||||
p.Title.Text = "Company Deliveries"
|
||||
p.X.Label.Text = "Year"
|
||||
if unitName != "" {
|
||||
p.Y.Label.Text = fmt.Sprintf("Deliveries (%s)", unitName)
|
||||
} else {
|
||||
p.Y.Label.Text = "Deliveries"
|
||||
}
|
||||
|
||||
i := 0
|
||||
for ccID, xys := range vals {
|
||||
cc := cfg.Clients[ccID]
|
||||
|
||||
l, s, err := plotter.NewLinePoints(xys)
|
||||
logErrorDebug(err, "botGraphDeliveries : plotter.NewLinePoints")
|
||||
if err != nil {
|
||||
bot.SendChat(m.Chat.ID, fmt.Sprintf("plotter.NewLinePoints : %s", err))
|
||||
return
|
||||
}
|
||||
|
||||
l.Color = cfg.GetClientColor(ccID)
|
||||
l.Dashes = plotutil.Dashes(2)
|
||||
s.Color = cfg.GetClientColor(ccID)
|
||||
s.Shape = plotutil.Shape(0)
|
||||
|
||||
p.Add(l)
|
||||
p.Add(s)
|
||||
p.Legend.Add(cc.Username, l, s)
|
||||
|
||||
i++
|
||||
}
|
||||
|
||||
err := p.Save(12*vg.Inch, 8*vg.Inch, "/app/data/points.png")
|
||||
logErrorDebug(err, "botGraphDeliveries : plot.Save")
|
||||
if err != nil {
|
||||
bot.SendChat(m.Chat.ID, fmt.Sprintf("plot.Save : %s", err))
|
||||
return
|
||||
}
|
||||
|
||||
bot.SendChatImage(m.Chat.ID, "/app/data/points.png")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func botGraphMoney(m *tb.Message) {
|
||||
var (
|
||||
maxVal float64
|
||||
|
||||
Reference in New Issue
Block a user