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

运行后

相关推荐
Ticnix3 小时前
ECharts初始化、销毁、resize 适配组件封装(含完整封装代码)
前端·echarts
雨中风华4 小时前
Linux, macOS系统实现远程目录访问(等同于windows平台xFsRedir软件的目录重定向)
linux·windows·macos
yuuki2332336 小时前
【C++】继承
开发语言·c++·windows
参.商.6 小时前
【Day 27】121.买卖股票的最佳时机 122.买卖股票的最佳时机II
leetcode·golang
牛奔6 小时前
如何理解 Go 的调度模型,以及 G / M / P 各自的职责
开发语言·后端·golang
非凡ghost6 小时前
PowerDirector安卓版(威力导演安卓版)
android·windows·学习·软件需求
牛奔8 小时前
Go 是如何做抢占式调度的?
开发语言·后端·golang
疯狂敲代码的老刘8 小时前
JDK 1.6到25 全版本网盘合集 (Windows + Mac + Linux)
java·linux·windows·macos·jdk
love530love10 小时前
Windows 下 GCC 编译器安装与排错实录
人工智能·windows·python·gcc·msys2·gtk·msys2 mingw 64
清云随笔10 小时前
Golang基础
golang