go-echarts 库使用

简介: go-echarts 库使用

go-echarts


chart 包是一个简单的本地图表库,支持时间序列和连续折线。是数据可视化第三方库。

安装


$ go get -u github.com/go-echarts/go-echarts/...
# 因为 gomod 的特殊的版本管理方式,使用 go get 方式并不能直接使用 v2 go-echarts 🐶
# 不过可以通过以下方法使用新版本...
$ cd $go-echarts-project
$ mkdir v2 && mv charts components datasets opts render templates types v2


go.mod


require github.com/go-echarts/go-echarts/v2


echart 特性

  • 简洁的 API 设计,使用如丝滑般流畅
  • 囊括了 25+ 种常见图表,应有尽有
  • 高度灵活的配置项,可轻松搭配出精美的图表
  • 详细的文档和示例,帮助开发者更快地上手项目
  • 多达 400+ 地图,为地理数据可视化提供强有力的支持

直方图:


package go_chart
import (
 "math/rand"
 "os"
 "testing"
 "github.com/go-echarts/go-echarts/v2/charts"
 "github.com/go-echarts/go-echarts/v2/opts"
)
//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 TestBar(t *testing.T) {
 // 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)
}

运行结果:

image.png

饼图

package go_chart
import (
 "math/rand"
 "os"
 "testing"
 "github.com/go-echarts/go-echarts/v2/charts"
 "github.com/go-echarts/go-echarts/v2/opts"
)
var (
 itemCntPie = 4
 seasons    = []string{"Spring", "Summer", "Autumn ", "Winter"}
)
func generatePieItems() []opts.PieData {
 items := make([]opts.PieData, 0)
 for i := 0; i < itemCntPie; i++ {
  items = append(items, opts.PieData{Name: seasons[i], Value: rand.Intn(100)})
 }
 return items
}
func pieBase() *charts.Pie {
 pie := charts.NewPie()
 pie.SetGlobalOptions(
  charts.WithTitleOpts(opts.Title{Title: "basic pie example"}),
 )
 pie.AddSeries("pie", generatePieItems())
 return pie
}
func pieShowLabel() *charts.Pie {
 pie := charts.NewPie()
 pie.SetGlobalOptions(
  charts.WithTitleOpts(opts.Title{Title: "label options"}),
 )
 pie.AddSeries("pie", generatePieItems()).
  SetSeriesOptions(charts.WithLabelOpts(
   opts.Label{
    Show:      true,
    Formatter: "{b}: {c}",
   }),
  )
 return pie
}
func pieRadius() *charts.Pie {
 pie := charts.NewPie()
 pie.SetGlobalOptions(
  charts.WithTitleOpts(opts.Title{Title: "Radius style"}),
 )
 pie.AddSeries("pie", generatePieItems()).
  SetSeriesOptions(
   charts.WithLabelOpts(opts.Label{
    Show:      true,
    Formatter: "{b}: {c}",
   }),
   charts.WithPieChartOpts(opts.PieChart{
    Radius: []string{"40%", "75%"},
   }),
  )
 return pie
}
func pieRoseArea() *charts.Pie {
 pie := charts.NewPie()
 pie.SetGlobalOptions(
  charts.WithTitleOpts(opts.Title{
   Title: "Rose(Area)",
  }),
 )
 pie.AddSeries("pie", generatePieItems()).
  SetSeriesOptions(
   charts.WithLabelOpts(opts.Label{
    Show:      true,
    Formatter: "{b}: {c}",
   }),
   charts.WithPieChartOpts(opts.PieChart{
    Radius:   []string{"40%", "75%"},
    RoseType: "area",
   }),
  )
 return pie
}
func pieRoseRadius() *charts.Pie {
 pie := charts.NewPie()
 pie.SetGlobalOptions(
  charts.WithTitleOpts(opts.Title{
   Title: "Rose(Radius)",
  }),
 )
 pie.AddSeries("pie", generatePieItems()).
  SetSeriesOptions(
   charts.WithLabelOpts(opts.Label{
    Show:      true,
    Formatter: "{b}: {c}",
   }),
   charts.WithPieChartOpts(opts.PieChart{
    Radius:   []string{"30%", "75%"},
    RoseType: "radius",
   }),
  )
 return pie
}
func pieRoseAreaRadius() *charts.Pie {
 pie := charts.NewPie()
 pie.SetGlobalOptions(
  charts.WithTitleOpts(opts.Title{
   Title: "Rose(Area/Radius)",
  }),
 )
 pie.AddSeries("area", generatePieItems()).
  SetSeriesOptions(
   charts.WithPieChartOpts(opts.PieChart{
    Radius:   []string{"30%", "75%"},
    RoseType: "area",
    Center:   []string{"25%", "50%"},
   }),
  )
 pie.AddSeries("pie", generatePieItems()).
  SetSeriesOptions(
   charts.WithLabelOpts(opts.Label{
    Show:      true,
    Formatter: "{b}: {c}",
   }),
   charts.WithPieChartOpts(opts.PieChart{
    Radius:   []string{"30%", "75%"},
    RoseType: "radius",
    Center:   []string{"75%", "50%"},
   }),
  )
 return pie
}
func pieInPie() *charts.Pie {
 pie := charts.NewPie()
 pie.SetGlobalOptions(
  charts.WithTitleOpts(opts.Title{
   Title: "pie in pie",
  }),
 )
 pie.AddSeries("area", generatePieItems(),
  charts.WithLabelOpts(opts.Label{
   Show:      true,
   Formatter: "{b}: {c}",
  }),
  charts.WithPieChartOpts(opts.PieChart{
   Radius:   []string{"50%", "55%"},
   RoseType: "area",
  }),
 )
 pie.AddSeries("radius", generatePieItems(),
  charts.WithPieChartOpts(opts.PieChart{
   Radius:   []string{"0%", "45%"},
   RoseType: "radius",
  }),
 )
 return pie
}
type PieExamples struct{}
//func TestPie(t *testing.T) {
// //(PieExamples)
// page := components.NewPage()
// page.AddCharts(
//  pieBase(),
//  pieShowLabel(),
//  pieRadius(),
//  pieRoseArea(),
//  pieRoseRadius(),
//  pieRoseAreaRadius(),
//  pieInPie(),
// )
// f, err := os.Create("examples/html/pie.html")
// if err != nil {
//  panic(err)
// }
// page.Render(io.MultiWriter(f))
//}
func TestPie(t *testing.T) {
 // create a new bar instance
 pie := pieBase()
 // Where the magic happens
 f, _ := os.Create("pie.html")
 pie.Render(f)
}

image.png

相关文章
|
4月前
|
数据采集 存储 Go
使用Go语言和chromedp库下载Instagram图片:简易指南
Go语言爬虫示例使用chromedp库下载Instagram图片,关键步骤包括设置代理IP、创建带代理的浏览器上下文及执行任务,如导航至用户页面、截图并存储图片。代码中新增`analyzeAndStoreImage`函数对图片进行分析和分类后存储。注意Instagram的反爬策略可能需要代码适时调整。
102 1
使用Go语言和chromedp库下载Instagram图片:简易指南
|
1月前
|
Rust 安全 算法
Go标准库的新 math/rand
Go标准库的新 math/rand
|
1月前
|
JSON 中间件 Go
go语言后端开发学习(四) —— 在go项目中使用Zap日志库
本文详细介绍了如何在Go项目中集成并配置Zap日志库。首先通过`go get -u go.uber.org/zap`命令安装Zap,接着展示了`Logger`与`Sugared Logger`两种日志记录器的基本用法。随后深入探讨了Zap的高级配置,包括如何将日志输出至文件、调整时间格式、记录调用者信息以及日志分割等。最后,文章演示了如何在gin框架中集成Zap,通过自定义中间件实现了日志记录和异常恢复功能。通过这些步骤,读者可以掌握Zap在实际项目中的应用与定制方法
go语言后端开发学习(四) —— 在go项目中使用Zap日志库
|
1天前
|
Shell Go API
Go语言grequests库并发请求的实战案例
Go语言grequests库并发请求的实战案例
|
1月前
|
存储 JSON 前端开发
一文搞懂 Go 1.21 的日志标准库 - slog
一文搞懂 Go 1.21 的日志标准库 - slog
40 2
|
1月前
|
Prometheus Cloud Native Go
Go 1.22 标准库 slices 新增函数和一些旧函数增加新特性
Go 1.22 标准库 slices 新增函数和一些旧函数增加新特性
|
19天前
|
XML Go 数据库
|
1月前
|
Go
【go笔记】标准库-strconv
【go笔记】标准库-strconv
|
1月前
|
Go 索引
【go笔记】标准库-strings
【go笔记】标准库-strings
|
1月前
|
Go
【go笔记】使用标准库flag解析命令行参数
【go笔记】使用标准库flag解析命令行参数

热门文章

最新文章