随笔 - 934, 文章 - 0, 评论 - 249, 阅读 - 345万

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

使用 gonum.org/v1/plot 画散点图

Posted on   蝈蝈俊  阅读(289)  评论(0编辑  收藏  举报

封装的基础函数

package pchart

import (
	"gonum.org/v1/plot"
	"gonum.org/v1/plot/font"
	"gonum.org/v1/plot/plotter"
	"gonum.org/v1/plot/vg"
	"io/ioutil"
	"log"
	"golang.org/x/image/font/opentype"
)

// 生成散列图
func BuildPlot(title, xlabel, ylabel, filename string, xy plotter.XYer) {

	plotter.DefaultGlyphStyle.Radius = vg.Points(0.5)

	font.DefaultCache.Add([]font.Face{
		{
			Font: font.Font{Typeface: "YaHei"},
			Face: GetZWFont2(),
		},
	})

	plotter.DefaultFont = font.Font{Typeface: "YaHei"}
	plot.DefaultFont = font.Font{Typeface: "YaHei"}

	p := plot.New()

	p.Title.Text = title
	p.X.Label.Text = xlabel
	p.Y.Label.Text = ylabel

	scatter2, _ := plotter.NewScatter(xy)
	p.Add(scatter2)

	// Save the plot to a PNG file.
	if err := p.Save(8*vg.Inch, 8*vg.Inch, filename); err != nil {
		panic(err)
	}
}



const (
	fontFile = "/Library/Fonts/Microsoft-YaHei.ttf" // 需要使用的字体文件
)

func GetZWFont2() *opentype.Font {
	// 读字体数据
	fontBytes, err := ioutil.ReadFile(fontFile)
	if err != nil {
		log.Println(err)
		return nil
	}
	font, err := opentype.Parse(fontBytes)
	if err != nil {
		log.Println(err)
		return nil
	}
	return font
}


绘图


package report

import (
	"fmt"
	"log"
	"sankuai/analysis"
	"sankuai/pchart"
	"sankuai/tools"
	"time"

	"gonum.org/v1/plot/plotter"
)

// 生成 吞吐量和 cpubusy的散点图
func BuildCapacityScatterPlot1(cad *analysis.CacheAppkeyData) string {
	centerTime, err := tools.GetCenterTime(cad.BeginTime, cad.EndTime)
	if err != nil {
		log.Println(err)
	}
	path := pathCheck(centerTime)

	fileName := fmt.Sprintf("%s/csp-%s-%s.png", path, cad.Appkey, centerTime.Format("20060102"))

	pts := plotter.XYs{}
	for curr := cad.BeginTime; !curr.After(cad.EndTime); curr = curr.Add(time.Minute) {

		// 计算吞吐量
		throughput := float64(0.0)
		avg1, ex11 := cad.TransactionAvgMap[curr]
		qpm1, ex12 := cad.TransactionQPMMap[curr]
		avg2, ex21 := cad.TransactionAvgpTestMap[curr]
		qpm2, ex22 := cad.TransactionQPMpTestMap[curr]
		if ex11 && ex12 {
			throughput += avg1 * qpm1
		}
		if ex21 && ex22 {
			throughput += avg2 * qpm2
		}
		// log.Println(throughput)

		// 计算合计 cpubusy
		cpubusy := float64(0.0)
		for _, cmap := range cad.HostsCPUBusyMap {
			cb, ex3 := cmap[curr]
			if ex3 {
				cpubusy += cb
			}
		}

		pts = append(pts, plotter.XY{X: throughput, Y: cpubusy})
	}

	pchart.BuildPlot("容量散点图", "吞吐量", "cpu.busy", fileName, pts)

	return fileName
}


相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
历史上的今天:
2014-11-07 Golang 的 TOML库
点击右上角即可分享
微信分享提示