使用go-chart实现支持中文双坐标趋势图

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)
}

 执行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值