使用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()
}

运行后

相关推荐
Abigail_chow6 小时前
EXCEL如何快速批量给两字姓名中间加空格
windows·microsoft·excel·学习方法·政务
love530love7 小时前
【笔记】在 MSYS2(MINGW64)中正确安装 Rust
运维·开发语言·人工智能·windows·笔记·python·rust
roman_日积跬步-终至千里8 小时前
【Go语言基础【3】】变量、常量、值类型与引用类型
开发语言·算法·golang
roman_日积跬步-终至千里8 小时前
【Go语言基础】基本语法
开发语言·golang·xcode
代码搬运媛9 小时前
“packageManager“: “[email protected]“ 配置如何正确启动项目?
windows·webpack
小道士写程序9 小时前
Qt 5.12 上读取 .xlsx 文件(Windows 平台)
开发语言·windows·qt
八月林城12 小时前
echarts在uniapp中使用安卓真机运行时无法显示的问题
android·uni-app·echarts
异常君14 小时前
Windows 与 Linux 虚拟内存机制对比:设计理念与实现差异
java·linux·windows
搏博16 小时前
将图形可视化工具的 Python 脚本打包为 Windows 应用程序
开发语言·windows·python·matplotlib·数据可视化
电手17 小时前
Win10停更,Win11不好用?现在Mac电脑比Win11电脑更便宜
windows·macos·电脑·mac