使用windows窗口展示go-echarts图表

在使用golang画一些柱状图,折线图,饼状图等图表的时候,go-echarts应该是个很不错的选择,它直接集成了 Apache ECharts,因此使用起来非常方便,但是它都是生成一个html文件,你还得在浏览器打开,略显麻烦,无写了个程序来直接展示页面,无需浏览器,代码很简单,直接粘贴,目前只支持windows系统。

go 复制代码
package main

import (
	"encoding/base64"
	"log"

	webview2 "github.com/jchv/go-webview2"

	"math/rand"

	"github.com/go-echarts/go-echarts/v2/charts"
	"github.com/go-echarts/go-echarts/v2/opts"
)

/*

go get -u github.com/go-echarts/go-echarts/v2/...

https://echarts.apache.org/examples/zh/index.html#chart-type-line

*/

func main() {
	echarts()
}

// generate random data for bar chart
func generateBarItems() []opts.BarData {
	items := make([]opts.BarData, 0)
	for i := 0; i < 7; i++ {
		items = append(items, opts.BarData{Value: rand.Intn(300)})
	}
	return items
}

func echarts() {
	// create a new bar instance
	bar := charts.NewBar()
	// set some global options like Title/Legend/ToolTip or anything else
	bar.SetGlobalOptions(charts.WithTitleOpts(opts.Title{
		Title:    "My first bar chart generated by go-echarts",
		Subtitle: "It's extremely easy to use, right?",
	}))

	// Put data into instance
	bar.SetXAxis([]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}).
		AddSeries("Category A", generateBarItems()).
		AddSeries("Category B", generateBarItems())

	// Where the magic happens
	// f, _ := os.Create("bar.html")
	// bar.Render(f)

	show(bar.RenderContent())
}

func show(content []byte) {
	w := webview2.NewWithOptions(webview2.WebViewOptions{
		Debug:     true,
		AutoFocus: true,
		WindowOptions: webview2.WindowOptions{
			Title:  "Minimal webview example",
			Width:  800,
			Height: 600,
			IconId: 2,
			Center: true,
		},
	})
	if w == nil {
		log.Fatalln("Failed to load webview.")
	}
	defer w.Destroy()

	w.Navigate(`data:text/html;base64,`+base64.StdEncoding.EncodeToString(content))
	
	w.Run()
}

运行后

相关推荐
尘中客1 小时前
放弃 Echarts?前端直接渲染后端高精度 SVG 矢量图流的踩坑记录
前端·javascript·echarts·前端开发·svg矢量图·echarts避坑
cch89183 小时前
汇编与Go:底层到高层的编程差异
java·汇编·golang
22信通小白5 小时前
USRP初学者使用手册(基础配置及bug记录)——Windows+MATLAB
windows·matlab·bug
胡斌附体5 小时前
Windows 打包方式与 exe图标说明
windows·electron·exe·package·build·nsis
小江的记录本6 小时前
【Linux】《Linux常用命令汇总表》
linux·运维·服务器·前端·windows·后端·macos
追烽少年x7 小时前
Windows API中线程异步API简介
windows
私人珍藏库8 小时前
[Windows] 绘画工具 Krita v5.3.1
人工智能·windows·媒体·工具·软件·多功能
白毛大侠10 小时前
Go Goroutine 与用户态是进程级
开发语言·后端·golang
tumeng071112 小时前
Node.JS 版本管理工具 Fnm 安装及配置(Windows)
windows·node.js
lolo大魔王12 小时前
Go语言的基础语法
开发语言·后端·golang