From c84b9d0c44e1230ee155bca2f7791f241c5f2e4a Mon Sep 17 00:00:00 2001 From: dustoair <107600816+dustoair@users.noreply.github.com> Date: Sat, 17 Sep 2022 17:10:52 +0800 Subject: [PATCH] add go-charts --- go.mod | 7 ++- goCharts/draw.go | 110 +++++++++++++++++++++++++++++++++++++++++++ goCharts/util.go | 28 +++++++++++ handle/replyImage.go | 9 ++++ 4 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 goCharts/draw.go create mode 100644 goCharts/util.go diff --git a/go.mod b/go.mod index d33e56f..25f3cb2 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,10 @@ go 1.19 require ( github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/dustin/go-humanize v1.0.0 // indirect github.com/fsnotify/fsnotify v1.5.4 // indirect github.com/go-redis/redis/v8 v8.11.5 // indirect + github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/jinzhu/gorm v1.9.16 // indirect github.com/jinzhu/inflection v1.0.0 // indirect @@ -21,9 +23,12 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/viper v1.12.0 // indirect github.com/subosito/gotenv v1.3.0 // indirect + github.com/vicanso/go-charts/v2 v2.2.0 // indirect + github.com/wcharczuk/go-chart/v2 v2.1.0 // indirect + golang.org/x/image v0.0.0-20220902085622-e7cb96979f69 // indirect golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect golang.org/x/text v0.3.7 // indirect gopkg.in/ini.v1 v1.66.4 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect - gopkg.in/yaml.v3 v3.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/goCharts/draw.go b/goCharts/draw.go new file mode 100644 index 0000000..c923215 --- /dev/null +++ b/goCharts/draw.go @@ -0,0 +1,110 @@ +package goCharts + +/* +* +@author: sre +@date: 2022/9/17 0017 +@desc: todo +* +*/ + +import ( + "github.com/vicanso/go-charts/v2" +) + +func Draw() { + values := [][]float64{ + { + 120, + 132, + 101, + // 134, + //charts.GetNullValue(), + 134, + 90, + 230, + 210, + }, + { + 220, + 182, + 191, + 234, + 290, + 330, + 310, + }, + { + 150, + 232, + 201, + 154, + 190, + 330, + 410, + }, + { + 320, + 332, + 301, + 334, + 390, + 330, + 320, + }, + { + 820, + 932, + 901, + 934, + 1290, + 1330, + 1320, + }, + } + p, err := charts.LineRender( + values, + //charts.SVGTypeOption(), + charts.ThemeOptionFunc(charts.ThemeGrafana), + charts.WidthOptionFunc(1080), + charts.HeightOptionFunc(1920), + charts.TitleTextOptionFunc("Line"), + charts.XAxisDataOptionFunc([]string{ + "Mon", + "Tue", + "Wed", + "Thu", + "Fri", + "Sat", + "Sun", + }), + charts.LegendLabelsOptionFunc([]string{ + "Email", + "Union Ads", + "Video Ads", + "Direct", + "Search Engine", + }, "50"), + func(opt *charts.ChartOption) { + opt.Legend.Padding = charts.Box{ + Top: 5, + Bottom: 10, + } + opt.SymbolShow = charts.FalseFlag() + opt.LineStrokeWidth = 1 + }, + ) + + if err != nil { + panic(err) + } + + buf, err := p.Bytes() + if err != nil { + panic(err) + } + err = writeFile(buf) + if err != nil { + panic(err) + } +} diff --git a/goCharts/util.go b/goCharts/util.go new file mode 100644 index 0000000..cdd72f8 --- /dev/null +++ b/goCharts/util.go @@ -0,0 +1,28 @@ +package goCharts + +import ( + "os" + "path/filepath" +) + +/* +* +@author: sre +@date: 2022/9/17 0017 +@desc: todo +* +*/ +func writeFile(buf []byte) error { + tmpPath := "./tmp" + err := os.MkdirAll(tmpPath, 0700) + if err != nil { + return err + } + + file := filepath.Join(tmpPath, "line-chart.png") + err = os.WriteFile(file, buf, 0600) + if err != nil { + return err + } + return nil +} diff --git a/handle/replyImage.go b/handle/replyImage.go index 61e572e..b47f49b 100644 --- a/handle/replyImage.go +++ b/handle/replyImage.go @@ -2,12 +2,21 @@ package handle import ( "WechatGateWay/global" + "WechatGateWay/goCharts" "WechatGateWay/third_part" "WechatGateWay/utils" "net/http" ) func replyImage(msgContent MsgContent, timestamp, nonce string, w http.ResponseWriter) { + //third_part.SendPicMid(msgContent.FromUsername, utils.RandMapValue(global.ImageMap)) + goCharts.Draw() + filename := "tmp/line-chart.png" + go RecordWechatLog(msgContent.FromUsername, msgContent.ToUsername, msgContent.CreateTime, msgContent.MsgType, filename) + third_part.SendPicFile(msgContent.FromUsername, filename) +} + +func replyImageEmoj(msgContent MsgContent, timestamp, nonce string, w http.ResponseWriter) { //third_part.SendPicMid(msgContent.FromUsername, utils.RandMapValue(global.ImageMap)) filename := global.WechatImagePath + "/" + utils.RandomFile(global.WechatImagePath) go RecordWechatLog(msgContent.FromUsername, msgContent.ToUsername, msgContent.CreateTime, msgContent.MsgType, filename)