go-charts: Go Library for Generating SVG/PNG Charts with Apache ECharts Compatibility
go-charts is a Go library that simplifies creating line, bar, and pie charts as SVG or PNG images, supports light, dark, and grafana themes, offers Apache ECharts‑compatible configuration, provides performance benchmarks, and includes guidance for handling Chinese characters.
go-charts is a Go library built on go-chart that simplifies the creation of data visualizations, supporting SVG and PNG output and three themes (light, dark, grafana).
It offers compatibility with Apache ECharts, allowing developers to generate charts using either go-charts parameters or ECharts JSON configuration, and provides examples for line, bar, and pie charts.
The documentation lists supported chart types (line, bar, pie) and details the extensive configuration options, including canvas type, theme, font, padding, box, dimensions, title, axes, legend, series, and nested children.
Example Go code demonstrates rendering a line chart to PNG and rendering the same chart via ECharts JSON to PNG, with a benchmark showing PNG rendering around 20 ms and SVG even faster.
package main
import (
"os"
"path/filepath"
charts "github.com/vicanso/go-charts"
)
func writeFile(file string, buf []byte) error {
tmpPath := "./tmp"
err := os.MkdirAll(tmpPath, 0700)
if err != nil {
return err
}
file = filepath.Join(tmpPath, file)
err = os.WriteFile(file, buf, 0600)
if err != nil {
return err
}
return nil
}
func chartsRender() ([]byte, error) {
d, err := charts.Render(charts.ChartOption{
Type: charts.ChartOutputPNG,
Title: charts.TitleOption{Text: "Line"},
XAxis: charts.NewXAxisOption([]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}),
SeriesList: charts.SeriesList{charts.NewSeriesFromValues([]float64{150, 230, 224, 218, 135, 147, 260})},
})
if err != nil {
return nil, err
}
return d.Bytes()
}
func echartsRender() ([]byte, error) {
return charts.RenderEChartsToPNG(`{
"title": {"text": "Line"},
"xAxis": {"data": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]},
"series": [{"data": [150, 230, 224, 218, 135, 147, 260]}]
}`)
}
type Render func() ([]byte, error)
func main() {
m := map[string]Render{"charts-line.png": chartsRender, "echarts-line.png": echartsRender}
for name, fn := range m {
buf, err := fn()
if err != nil {
panic(err)
}
err = writeFile(name, buf)
if err != nil {
panic(err)
}
}
}Performance benchmarks indicate that generating a simple PNG chart takes about 20 ms, while SVG rendering is faster, offering a significant speed advantage over headless‑Chrome screenshot approaches.
Notes on Chinese character support explain that the default font (Roboto) lacks Chinese glyphs, requiring additional font installation and setting the title text style font family to display Chinese correctly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.