From 4a5b82000a6115cd7a365ba549a6d6bcb887fdc4 Mon Sep 17 00:00:00 2001 From: shoopea Date: Sun, 5 Dec 2021 21:46:59 +0800 Subject: [PATCH] update value graph --- bot.go | 68 ++++++++++++++++++++++++++++++++++++++++++++---------- version.go | 8 +++---- 2 files changed, 60 insertions(+), 16 deletions(-) diff --git a/bot.go b/bot.go index 1ab1e6d..8835977 100644 --- a/bot.go +++ b/bot.go @@ -5,6 +5,7 @@ import ( "encoding/hex" "encoding/json" "fmt" + "math" "regexp" "strconv" "time" @@ -882,6 +883,32 @@ func botRegister(m *tb.Message) { } func botGraphValue(m *tb.Message) { + var ( + maxVal float64 + unitFactor float64 + unitName string + ) + for coID, dStats := range cfg.Stats { + if cfg.CompanyIsRegistered(coID) { + for _, stat := range dStats { + valueFloat := float64(stat.CompanyValueLastQuarter) + 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[uint8]plotter.XYs vals = make(map[uint8]plotter.XYs) for coID, dStats := range cfg.Stats { @@ -898,7 +925,7 @@ func botGraphValue(m *tb.Message) { valueFloat := float64(stat.CompanyValueLastQuarter) pt := plotter.XY{ X: dateFloat, - Y: valueFloat, + Y: valueFloat / unitFactor, } vals[coID] = append(vals[coID], pt) } @@ -908,19 +935,36 @@ func botGraphValue(m *tb.Message) { p := plot.New() p.Title.Text = "Company Values" p.X.Label.Text = "Year" - p.Y.Label.Text = "Value" - - for coID, xys := range vals { - cc := cfg.GetCompanyClient(coID) - err := plotutil.AddLinePoints(p, cc.Username, xys) - logErrorDebug(err, "Stats.ValueGraph : plotutil.AddLinePoints") - if err != nil { - bot.SendChat(m.Chat.ID, fmt.Sprintf("plotutil.AddLinePoints : %s", err)) - return - } + if unitName != "" { + p.Y.Label.Text = fmt.Sprintf("Value (%s)", unitName) + } else { + p.Y.Label.Text = "Value" } - err := p.Save(4*vg.Inch, 4*vg.Inch, "/app/data/points.png") + i := 0 + for coID, xys := range vals { + cc := cfg.GetCompanyClient(coID) + + l, s, err := plotter.NewLinePoints(xys) + logErrorDebug(err, "Stats.ValueGraph : plotter.NewLinePoints") + if err != nil { + bot.SendChat(m.Chat.ID, fmt.Sprintf("plotter.NewLinePoints : %s", err)) + return + } + + l.Color = plotutil.Color(i) + l.Dashes = plotutil.Dashes(2) + s.Color = plotutil.Color(i) + s.Shape = plotutil.Shape(0) + + p.Add(l) + p.Add(s) + p.Legend.Add(cc.Username, l, s) + + i++ + } + + err := p.Save(4*vg.Inch, 6*vg.Inch, "/app/data/points.png") logErrorDebug(err, "Stats.ValueGraph : plot.Save") if err != nil { bot.SendChat(m.Chat.ID, fmt.Sprintf("plot.Save : %s", err)) diff --git a/version.go b/version.go index aaf7b5b..4ad5eb6 100644 --- a/version.go +++ b/version.go @@ -1,6 +1,6 @@ // Code generated by version.sh (@generated) DO NOT EDIT. package main -var githash = "40c4261" -var buildstamp = "2021-12-05_10:22:54" -var commits = "229" -var version = "40c4261-b229 - 2021-12-05_10:22:54" +var githash = "76dacf3" +var buildstamp = "2021-12-05_13:46:48" +var commits = "230" +var version = "76dacf3-b230 - 2021-12-05_13:46:48"