A charts library for Golang

Overview

go-charts

license Build Status

go-charts基于go-chart生成数据图表,无其它模块的依赖纯golang的实现,支持svgpng的输出,Apache ECharts在前端开发中得到众多开发者的认可,go-charts兼容Apache ECharts的配置参数,简单快捷的生成相似的图表(svgpng),方便插入至Email或分享使用。下面为常用的几种图表截图(黑夜模式):

go-charts

支持图表类型

暂仅支持三种的图表类型:line, bar 以及 pie

示例

go-charts兼容了echarts的参数配置,可简单的使用json形式的配置字符串则可快速生成图表。

package main

import (
	"os"

	charts "github.com/vicanso/go-charts"
)

func main() {
	buf, err := charts.RenderEChartsToPNG(`{
		"title": {
			"text": "Line"
		},
		"xAxis": {
			"type": "category",
			"data": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
		},
		"series": [
			{
				"data": [150, 230, 224, 218, 135, 147, 260]
			}
		]
	}`)
	if err != nil {
		panic(err)
	}
	os.WriteFile("output.png", buf, 0600)
}

参数说明

  • theme 颜色主题,支持darklight模式,默认为light
  • padding 图表的内边距,单位px。支持以下几种模式的设置
    • padding: 5 设置内边距为5
    • padding: [5, 10] 设置上下的内边距为 5,左右的内边距为 10
    • padding:[5, 10, 5, 10] 分别设置上右下左边距
  • title 图表标题,包括标题内容、高度、颜色等
    • title.text 标题内容
    • title.textStyle.color 标题文字颜色
    • title.textStyle.fontSize 标题文字字体大小
    • title.textStyle.height 标题高度
  • xAxis 直角坐标系grid中的x轴,由于go-charts仅支持单一个x轴,因此若参数为数组多个x轴,只使用第一个配置
    • xAxis.boundaryGap 坐标轴两边留白策略,仅支持三种设置方式null, true或者falsenulltrue时则数据点展示在两个刻度中间
    • xAxis.splitNumber 坐标轴的分割段数,需要注意的是这个分割段数只是个预估值,最后实际显示的段数会在这个基础上根据分割后坐标轴刻度显示的易读程度作调整
    • xAxis.data x轴的展示文案,暂只支持字符串数组,如["Mon", "Tue"],其数量需要与展示点一致
  • yAxis 直角坐标系grid中的y轴,最多支持两个y轴
    • yAxis.min 坐标轴刻度最小值,若不设置则自动计算
    • yAxis.max 坐标轴刻度最大值,若不设置则自动计算
    • yAxis.axisLabel.formatter 刻度标签的内容格式器,如"formatter": "{value} kg"
  • legend 图表中不同系列的标记
    • legend.data 图例的数据数组,为字符串数组,如["Email", "Video Ads"]
    • legend.align 图例标记和文本的对齐,默认为标记靠左left
    • legend.padding legend的padding,配置方式与图表的padding一致
    • legend.left legend离容器左侧的距离,其值可以为具体的像素值(20)或百分比(20%)
    • legend.right legend离容器右侧的距离,其值可以为具体的像素值(20)或百分比(20%)
  • series 图表的数据项列表
    • series.type 图表的展示类型,暂支持line, bar以及pie,需要注意pie只能单独使用
    • series.yAxisIndex 该数据项使用的y轴,默认为0,对yAxis的配置对应
    • series.itemStyle.color 该数据项展示时使用的颜色
    • series.data 数据项对应的数据数组,支持以下形式的数据:
      • 数值 常用形式,数组数据为浮点数组,如[1.1, 2,3, 5.2]
      • 结构体 pie图表或bar图表中指定样式使用,如[{"value": 1048, "name": "Search Engine"},{"value": 735,"name": "Direct"}]

性能

简单的图表生成PNG在20ms左右,而SVG的性能则更快,性能上比起使用chrome headless加载echarts图表展示页面,截图生成的方式大幅度提升,基本能满足简单的图表生成需求。

goos: darwin
goarch: amd64
pkg: github.com/vicanso/go-charts
cpu: Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz
BenchmarkEChartsRenderPNG-8           60          17765045 ns/op         2492854 B/op       1007 allocs/op
BenchmarkEChartsRenderSVG-8          282           4303042 ns/op        32622688 B/op       2983 allocs/op
Comments
  • 画图的时候,会打印出很多<nil>

    画图的时候,会打印出很多

    	p, err := charts.LineRender(
    		y,
    		// charts.YAxisOptionFunc(charts.YAxisOption{Max: &max}),
    		charts.TitleTextOptionFunc("Line"),
    		charts.XAxisDataOptionFunc(x),
    		// charts.LegendLabelsOptionFunc(labels, "50"),
    		func(opt *charts.ChartOption) {
    			opt.Legend.Padding = charts.Box{
    				Top:    5,
    				Bottom: 10,
    			}
    			opt.SymbolShow = charts.FalseFlag()
    			opt.LineStrokeWidth = 1
    		},
    	)
    	if err != nil {
    		config.Log.Error(err)
    		return "", err
    	}
    

    image

    opened by leslieDD 4
  • Bar chart where label is x axis

    Bar chart where label is x axis

    Hi, First thank you for this amazing package. I want to know if there is a possibility to use labels as x axis. For example we have such data: ns/operation lib1: 120ms lib2: 45ms

    opened by eduardhasanaj 4
  • 中文对其有问题

    中文对其有问题

    image 代码:

    func writeFile(file string, buf []byte) error {
    	err := os.WriteFile(file, buf, 0600)
    	if err != nil {
    		return err
    	}
    	return nil
    }
    
    func BarChart() {
    	fontFile := "simsun.ttc"
    	fontBytes, err := ioutil.ReadFile(fontFile)
    	if err != nil {
    		log.Println(err)
    	}
    	InstallFont("simsun", fontBytes)
    	var MaxHeight = 3.0
    	chartOptions := ChartOption{
    		Type:       "png",
    		FontFamily: "simsun",
    		Title: TitleOption{
    			Text: "中文显示",
    		},
    		XAxis: NewXAxisOption([]string{
    			"品德方面",
    			"情绪方面",
    			"社会适应方面",
    			"特种方面",
    			"学习方面",
    			"行为方面",
    			"性格方面",
    			"习惯方面",
    		}),
    		YAxisList: []YAxisOption{{Max: &MaxHeight}},
    		SeriesList: []Series{
    			NewSeriesFromValues([]float64{
    				1.20,
    				1.00,
    				1.50,
    				1.0,
    				1.0,
    				1.10,
    				1.30,
    			}, ChartTypeBar),
    			{
    				Type: ChartTypeBar,
    				Data: []SeriesData{
    					{
    						Value: 1.2,
    					},
    					{
    						Value: 1.9,
    					},
    					{
    						Value: 2.30,
    					},
    					{
    						Value: 1.40,
    					},
    					{
    						Value: 1.00,
    					},
    					{
    						Value: 2.0,
    					},
    					{
    						Value: 1.80,
    					},
    				},
    				Label: SeriesLabel{
    					Show: true,
    				},
    			},
    		},
    	}
    	d, err := Render(chartOptions)
    	if err != nil {
    		panic(err)
    	}
    	buf, err := d.Bytes()
    	if err != nil {
    		panic(err)
    	}
    	writeFile("222.png", buf)
    } 
    

    将axis.go中axisLabel

    if unitCount != 0 && index%unitCount != modValue {
    				continue
    			}
    

    去掉,就正常了。

    opened by zyfjfj 3
  • 标题不支持设置中文字体

    标题不支持设置中文字体

    我安装了中文字体,但是标题部分没有生效,如图 image

    测试代码如下:

    x := "18:44:10 18:44:15 18:44:20 18:44:25 18:44:30 18:44:35 18:44:40 18:44:45 18:44:50 18:44:55 18:45:00 18:45:05 18:45:10 18:45:15 18:45:20 18:45:25 18:45:30 18:45:35 18:45:40 18:45:45 18:45:50 18:45:55 18:46:00 18:46:05 18:46:10 18:46:15 18:46:20 18:46:25 18:46:30 18:46:35 18:46:40 18:46:45 18:46:50 18:46:55 18:47:00 18:47:05 18:47:10 18:47:15 18:47:20 18:47:25 18:47:30 18:47:35 18:47:40 18:47:45 18:47:50 18:47:55 18:48:00 18:48:05 18:48:10 18:48:15 18:48:20 18:48:25 18:48:30 18:48:35 18:48:40 18:48:45 18:48:50 18:48:55 18:49:00 18:49:05 18:49:10 18:49:15 18:49:20"
    
    	valueX := strings.Split(x, " ")
    
    	valueY := [][]float64{}
    	valueY = append(valueY, [][]float64{
    		{
    			0,
    			0,
    			12046.8,
    			50940.4,
    			64277.6,
    			69571.2,
    			72629.4,
    			66802,
    			75399.8,
    			65165,
    			70525,
    			73513.4,
    			68959.8,
    			68037,
    			70576.6,
    			65701.2,
    			71732.2,
    			68568.4,
    			69133,
    			67981.8,
    			62586.8,
    			70843.8,
    			63619.4,
    			64814.2,
    			70443,
    			70087.4,
    			70710.4,
    			73406,
    			68357.2,
    			71322.4,
    			64685.8,
    			65596,
    			75884.6,
    			71428.4,
    			60309,
    			69036,
    			73502.4,
    			68335.2,
    			65319.2,
    			66490.2,
    			67470.4,
    			69440.6,
    			64983.2,
    			69458.2,
    			69676.8,
    			61542.4,
    			64884,
    			75708.6,
    			65860.2,
    			67289.6,
    			71214,
    			71063.2,
    			71760,
    			65326.8,
    			74354.2,
    			58777.4,
    			73552.6,
    			67964,
    			66412.6,
    			73250.6,
    			69759.2,
    			67226,
    			16999.4,
    		},
    		{
    			61542.4,
    			64884,
    			75708.6,
    			65860.2,
    			67289.6,
    			71214,
    			71063.2,
    			71760,
    			65326.8,
    			74354.2,
    			58777.4,
    			73552.6,
    			67964,
    			66412.6,
    			73250.6,
    			69759.2,
    			67226,
    			16999.4,
    			0,
    			0,
    			12046.8,
    			50940.4,
    			64277.6,
    			69571.2,
    			72629.4,
    			66802,
    			75399.8,
    			65165,
    			70525,
    			73513.4,
    			68959.8,
    			68037,
    			70576.6,
    			65701.2,
    			71732.2,
    			68568.4,
    			69133,
    			67981.8,
    			62586.8,
    			70843.8,
    			63619.4,
    			64814.2,
    			70443,
    			70087.4,
    			70710.4,
    			73406,
    			68357.2,
    			71322.4,
    			64685.8,
    			65596,
    			75884.6,
    			71428.4,
    			60309,
    			69036,
    			73502.4,
    			68335.2,
    			65319.2,
    			66490.2,
    			67470.4,
    			69440.6,
    			64983.2,
    			69458.2,
    			69676.8,
    		},
    	}...)
    
    	f := false
    	label := []string{
    		"成功",
    		"失败",
    	}
    
    	// 字体文件需要自行下载
    	buff, err := ioutil.ReadFile("./NotoSansSC-VF.ttf")
    	if err != nil {
    		panic(err)
    	}
    	err = charts.InstallFont("noto", buff)
    	if err != nil {
    		panic(err)
    	}
    
    	p, err := charts.LineRender(
    		valueY,
    		charts.FontFamilyOptionFunc("noto"),
    		charts.TitleTextOptionFunc("全部请求"),
    		charts.XAxisDataOptionFunc(valueX),
    		func(opt *charts.ChartOption) {
    			opt.XAxis.BoundaryGap = &f
    			opt.Padding = charts.Box{Left: 20, Right: 50, Top: 20, Bottom: 20}
    		},
    		charts.ThemeOptionFunc("grafana"),
    		charts.WidthOptionFunc(1000),
    		charts.LegendOptionFunc(charts.LegendOption{
    			Data: label,
    			Left: "100",
    		}),
    	)
    
    opened by nanakoww 2
  • 饼图当只有一个值时输出空白

    饼图当只有一个值时输出空白

    demo:

    package main
    
    import (
    	"io/ioutil"
    	"os"
    	"path/filepath"
    
    	"github.com/vicanso/go-charts/v2"
    )
    
    func writeFile(buf []byte) error {
    	tmpPath := "./tmp"
    	err := os.MkdirAll(tmpPath, 0700)
    	if err != nil {
    		return err
    	}
    
    	file := filepath.Join(tmpPath, "pie-chart.png")
    	err = ioutil.WriteFile(file, buf, 0600)
    	if err != nil {
    		return err
    	}
    	return nil
    }
    
    func main() {
    	values := []float64{
    		1048,
    	}
    	p, err := charts.PieRender(
    		values,
    		charts.TitleOptionFunc(charts.TitleOption{
    			Text:    "Rainfall vs Evaporation",
    			Subtext: "Fake Data",
    			Left:    charts.PositionCenter,
    		}),
    		charts.PaddingOptionFunc(charts.Box{
    			Top:    20,
    			Right:  20,
    			Bottom: 20,
    			Left:   20,
    		}),
    		charts.LegendOptionFunc(charts.LegendOption{
    			Orient: charts.OrientVertical,
    			Data: []string{
    				"Search Engine",
    			},
    			Left: charts.PositionLeft,
    		}),
    		charts.PieSeriesShowLabel(),
    	)
    	if err != nil {
    		panic(err)
    	}
    
    	buf, err := p.Bytes()
    	if err != nil {
    		panic(err)
    	}
    	err = writeFile(buf)
    	if err != nil {
    		panic(err)
    	}
    }
    

    结果: image

    opened by CodFrm 2
  • 饼图不支持中文且标签文字重叠

    饼图不支持中文且标签文字重叠

    我用的是V2.1.0 饼图不支持中文且标签文字重叠

    image
    func genCharts() []byte {
    	values := []float64{
    		1048,
    		735,
    		580,
    		484,
    		300,
    	}
    	p, err := charts.PieRender(
    		values,
    		charts.TitleOptionFunc(charts.TitleOption{
    			Text:    "测试中文",
    			Subtext: "Fake Data",
    			Left:    charts.PositionCenter,
    		}),
    		charts.PaddingOptionFunc(charts.Box{
    			Top:    20,
    			Right:  20,
    			Bottom: 20,
    			Left:   20,
    		}),
    		charts.LegendOptionFunc(charts.LegendOption{
    			Orient: charts.OrientVertical,
    			Data: []string{
    				"中文",
    				"Direct",
    				"Email",
    				"Union Ads",
    				"Video Ads",
    			},
    			Left: charts.PositionLeft,
    		}),
    		charts.PieSeriesShowLabel(),
    	)
    	if err != nil {
    		panic(err)
    	}
    
    	buf, err := p.Bytes()
    	if err != nil {
    		panic(err)
    	}
    
    	return buf
    }
    
    
    opened by xxf0512 0
  • x 轴的 SplitNumber 使用报错

    x 轴的 SplitNumber 使用报错

    生成的图片x轴太挤了,想调整一下显示的样式,如图 企业微信截图_d6a6d386-d3d1-488e-a7ff-004e67bac8a2

    但是在设置SplitNumber为任何值,都会报错,如图 image

    以下是我的代码:

    x := []string{
    	"18:49:20",
    	"18:49:25",
    	"18:49:30",
    	"18:49:35",
    	"18:49:40",
    	"18:49:45",
    	"18:49:50",
    	"18:49:55",
    	"18:50:00",
    	"18:50:05",
    	"18:50:10",
    	"18:50:15",
    	"18:50:20",
    	"18:50:25",
    	"18:50:30",
    }
    
    y := [][]float64{
    	{
    		0,
    		0,
    		78.47169811320752,
    		45.459999999999994,
    		44.04,
    		44.2,
    		44.42,
    		44.68,
    		44.12,
    		43.29,
    		43.23,
    		43.05,
    		42.88,
    		45.15,
    		0,
    	},
    }
    
    unit := "ms"
    p, err := charts.LineRender(
    	y,
    	charts.TitleTextOptionFunc("Error request"),
    	charts.XAxisDataOptionFunc(x),
    	charts.XAxisOptionFunc(charts.XAxisOption{SplitNumber: 3}),
    	charts.ThemeOptionFunc("light"),
    	charts.WidthOptionFunc(1200),
    	charts.HeightOptionFunc(400),
    	charts.YAxisOptionFunc(charts.YAxisOption{Formatter: "{value}" + unit}),
    	charts.LegendLabelsOptionFunc([]string{
    		"Average",
    		"90th Percentile",
    		"95th Percentile",
    		"99th Percentile",
    		"Max",
    	},
    	charts.PositionRight),
    )
    

    另外 y 轴可以提供填充颜色的参数吗,类似于下图 企业微信截图_1c65900b-190e-4b2e-a286-425b3f09718a

    opened by nanakoww 16
  • 平滑折线好像不支持

    平滑折线好像不支持

    { "xAxis": { "type": "category", "data": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] }, "yAxis": { "type": "value" }, "series": [ { "smooth":true, "data": [820, 932, 901, 934, 1290, 1330, 1320], "type": "line"

    }
    

    ] }

    smooth 没有生效,麻烦帮看下

    opened by lovepepsi 1
Releases(v2.3.0)
Owner
Tree Xie
Do something or nothing
Tree Xie
A charts library for Golang

go-charts go-charts基于go-chart生成数据图表,无其它模块的依赖纯golang的实现,支持svg与png的输出,Apache ECharts在前端开发中得到众多开发者的认可,go-charts兼容Apache ECharts的配置参数,简单快捷的生成相似的图表(svg或png

Tree Xie 85 Sep 30, 2022
SSR charts as a Component

kyoto charts SSR Charts as a Component. Prototyped for using in kyoto pages. Kyoto charts are using go-chart under the hood to generate charts on serv

Yurii Zinets 3 Jun 3, 2022
Helm : a tool for managing Charts

Helm is a tool for managing Charts. Charts are packages of pre-configured Kubernetes resources.

邓杰 0 Aug 25, 2022
Termbar - Generate terminal charts in Go

termbar Generate terminal bar charts in Go. a: ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 50 b: ▇

MergeStat 14 Mar 2, 2022
go chart is a basic charting library in native golang.

Package chart is a very simple golang native charting library that supports timeseries and continuous line charts.

Will Charczuk 3.6k Sep 23, 2022
Ltree Visualizer - A golang library to visualize postgres ltree type data using DOT language and Graphviz

Ltree Visualizer A golang library to visualize postgres ltree type data using DOT language and Graphviz What is Ltree? Ltree is a data type which is u

Vasubabu Jinagam 8 Jun 12, 2022
Gfx - Golang file system extension library

gfx Golang文件操作扩展库,包含工作和生活中关于文件操作的各种有用的使用方法,包括 更友好的API 文件创建 文件删除 文件复制 一切皆可配置 文件名

张川 0 Mar 10, 2022
3D Wireframe Drawing Library for Go

pinhole 3D Wireframe Drawing Library for Go Javascript Version Demo Why does this exist? I needed a CPU based 3D rendering library with a very simple

Josh Baker 543 Sep 19, 2022
HSM package provides a simple state chart library written in Go.

UML HSM HSM package provides a simple state chart library written in Go. Supported UML State Chart Features Feature Implemented Test case Simple state

Chris Castro 1 Apr 14, 2022
fswatch, this library could help watching filesystem changes, like macOS fswatch

fswatch go library for simple UNIX file system watching fswatch provides simple UNIX file system watching in Go. It is based around the Watcher struct

hitzhangjie 0 Jan 7, 2022
Globe wireframe visualizations in Golang

globe Globe wireframe visualizations in Golang backed by pinhole. Getting Started Install globe with $ go get -u github.com/mmcloughlin/globe Start wi

Michael McLoughlin 1.6k Sep 12, 2022
🚀Statsview is a real-time Golang runtime stats visualization profiler

Statsview is a real-time Golang runtime stats visualization profiler. It is built top on another open-source project, go-echarts, which helps statsview to show its graphs on the browser.

go-echarts 496 Sep 18, 2022
A golang implementation of endlessh exporting Prometheus metrics, visualized by a Grafana dashboard.

endlessh-go A golang implementation of endlessh exporting Prometheus metrics, visualized by a Grafana dashboard. Introduction Endlessh is a great idea

Shizun Ge 65 Sep 12, 2022
Interactive dependency graph visualization tool for golang

Interactive dependency graph visualization tool for golang using the awesome cytoscape graph visualizer.

Armin Becher 6 Sep 1, 2022
go-echarts 🎨 The adorable charts library for Golang

go-echarts ?? The adorable charts library for Golang. If a language can be used to build web scrapers, it definitely needs to provide a graceful data

go-echarts 4.9k Sep 24, 2022
A charts library for Golang

go-charts go-charts基于go-chart生成数据图表,无其它模块的依赖纯golang的实现,支持svg与png的输出,Apache ECharts在前端开发中得到众多开发者的认可,go-charts兼容Apache ECharts的配置参数,简单快捷的生成相似的图表(svg或png

Tree Xie 85 Sep 30, 2022
✨ #PTerm is a modern go module to beautify console output. Featuring charts, progressbars, tables, trees, and many more 🚀 It's completely configurable and 100% cross-platform compatible.

?? PTerm | Pretty Terminal Printer A golang module to print pretty text Show Demo Code PTerm.sh | Installation | Documentation | Quick Start | Example

null 3k Sep 20, 2022
Provide basic charts in go

Charts for Go Basic charts in go. This package focuses more on autoscaling, error bars, and logarithmic plots than on beautifull or marketing ready ch

Volker Dobler 737 Sep 21, 2022
Provide basic charts in go

Charts for Go Basic charts in go. This package focuses more on autoscaling, error bars, and logarithmic plots than on beautifull or marketing ready ch

Volker Dobler 738 Sep 27, 2022
Very simple charts with some debug data for Go programs

debugcharts Go memory debug charts. This package uses Plotly chart library. It is open source and free for use. Installation go get -v -u github.com/m

Marko Kevac 478 Sep 6, 2022