package main
import (
"bytes"
"encoding/base64"
"fmt"
"github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/v2"
"io"
"math"
"math/rand"
"os"
"time"
"gonum.org/v1/plot/plotutil"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
)
func testDoubleY(title string, yList [][]float64, xList []float64, legend []string, lineColors []int, addStep float64, Secondary int) (string, error) {
var series []chart.Series
if lineColors == nil {
lineColors = []int{5, 6, 4}
}
for i, y := range yList {
s := chart.ContinuousSeries{
XValues: xList,
YValues: y,
Style: chart.Style{
StrokeWidth: 5,
StrokeColor: chart.GetAlternateColor(lineColors[i]),
},
Name: legend[i],
}
if i == Secondary {
s.YAxis = chart.YAxisSecondary
}
series = append(series, s)
}
maxY := 0.0
for i := 0; i < len(yList); i++ {
for j := 0; j < len(yList[i]); j++ {
if yList[i][j] > maxY {
maxY = yList[i][j]
}
}
}
var yTicks []chart.Tick
for i := 0.0; i <= maxY+addStep+addStep; i += addStep {
yTicks = append(yTicks, chart.Tick{
Value: i,
Label: fmt.Sprintf("%.0f", math.Round(i/addStep)*addStep),
})
}
f, err := os.OpenFile("./AlibabaPuHuiTi-3-85-Bold.ttf", os.O_RDONLY, 0644) // 0644表示文件权限
if err != nil {
return "", err
}
defer f.Close()
byteFile, _ := io.ReadAll(f)
ff, _ := truetype.Parse(byteFile)
graph := chart.Chart{
Font: ff,
Title: title,
XAxis: chart.XAxis{
TickPosition: chart.TickPositionBetweenTicks,
ValueFormatter: func(v interface{}) string {
return time.Unix(int64(v.(float64)), 0).Format("01/02")
},
},
YAxis: chart.YAxis{
Range: &chart.ContinuousRange{
Min: 0.0,
Max: maxY + addStep,
},
Ticks: yTicks,
},
YAxisSecondary: chart.YAxis{},
Series: series,
}
fmt.Println(graph.XAxis.GetGridLines(graph.YAxis.Ticks))
graph.Elements = []chart.Renderable{
chart.Legend(&graph),
}
f, err = os.OpenFile("./output.png", os.O_WRONLY, 0777) // 064
graph.Render(chart.PNG, f)
var buf bytes.Buffer
err = graph.Render(chart.PNG, &buf)
if err != nil {
return "", err
}
// 将图片转换为Base64字符串
encoded := base64.StdEncoding.EncodeToString(buf.Bytes())
return encoded, nil
}
func main() {
yList := [][]float64{}
xList := make([]float64, 90)
y := make([]float64, 90)
y1 := make([]float64, 90)
y2 := make([]float64, 90)
y3 := make([]float64, 90)
y4 := make([]float64, 90)
y5 := make([]float64, 90)
y6 := make([]float64, 90)
y7 := make([]float64, 90)
y8 := make([]float64, 90)
y9 := make([]float64, 90)
for i := 0; i < 90; i++ {
xList[i] = float64(time.Now().AddDate(0, 0, -i).Unix())
y[i] = rand.Float64() + 10
y1[i] = rand.Float64() + 15
y2[i] = rand.Float64() + 20
y3[i] = rand.Float64() + 30
y4[i] = rand.Float64() + 40
y5[i] = rand.Float64() + 50
y6[i] = rand.Float64() + 60
y7[i] = rand.Float64() + 70
y8[i] = rand.Float64() + 80
y9[i] = rand.Float64() + 90
}
//{5,6,4}
//{4,5,3}
yList = append(yList, y, y1, y2)
testDoubleY("新增趋势图", yList, xList, []string{"xxx新增趋势", "xxx", "xx新增", "3", "4", "5", "6", "7", "8", "9"}, []int{5, 6, 4, 3, 4, 5, 6, 7, 8, 9}, 10, -1)
//GenerateChart2Base64("123", yList, xList, []string{"123"}, nil, 0.05)
}
执行结果: