Git Product home page Git Product logo

go-charts's Introduction

go-charts

license Build Status

中文

go-charts base on go-chart,it is simpler way for generating charts, which supports svg and png format and themes: light, dark, grafana and ant. The default format is png and the default theme is light.

Apache ECharts is popular among Front-end developers, so go-charts supports the option of Apache ECharts. Developers can generate charts almost the same as Apache ECharts.

Screenshot of common charts, the left part is light theme, the right part is grafana theme.

go-charts

go-table

Chart Type

These chart types are supported: line, bar, horizontal bar, pie, radar or funnel and table.

Example

More examples can be found in the ./examples/ directory.

Line Chart

package main

import (
	charts "github.com/vicanso/go-charts/v2"
)

func main() {
	values := [][]float64{
		{
			120,
			132,
			101,
			134,
			90,
			230,
			210,
		},
		{
			// snip...
		},
		{
			// snip...
		},
		{
			// snip...
		},
		{
			// snip...
		},
	}
	p, err := charts.LineRender(
		values,
		charts.TitleTextOptionFunc("Line"),
		charts.XAxisDataOptionFunc([]string{
			"Mon",
			"Tue",
			"Wed",
			"Thu",
			"Fri",
			"Sat",
			"Sun",
		}),
		charts.LegendLabelsOptionFunc([]string{
			"Email",
			"Union Ads",
			"Video Ads",
			"Direct",
			"Search Engine",
		}, charts.PositionCenter),
	)

	if err != nil {
		panic(err)
	}

	buf, err := p.Bytes()
	if err != nil {
		panic(err)
	}
	// snip...
}

Bar Chart

package main

import (
	"github.com/vicanso/go-charts/v2"
)

func main() {
	values := [][]float64{
		{
			2.0,
			4.9,
			7.0,
			23.2,
			25.6,
			76.7,
			135.6,
			162.2,
			32.6,
			20.0,
			6.4,
			3.3,
		},
		{
			// snip...	
		},
	}
	p, err := charts.BarRender(
		values,
		charts.XAxisDataOptionFunc([]string{
			"Jan",
			"Feb",
			"Mar",
			"Apr",
			"May",
			"Jun",
			"Jul",
			"Aug",
			"Sep",
			"Oct",
			"Nov",
			"Dec",
		}),
		charts.LegendLabelsOptionFunc([]string{
			"Rainfall",
			"Evaporation",
		}, charts.PositionRight),
		charts.MarkLineOptionFunc(0, charts.SeriesMarkDataTypeAverage),
		charts.MarkPointOptionFunc(0, charts.SeriesMarkDataTypeMax,
			charts.SeriesMarkDataTypeMin),
		// custom option func
		func(opt *charts.ChartOption) {
			opt.SeriesList[1].MarkPoint = charts.NewMarkPoint(
				charts.SeriesMarkDataTypeMax,
				charts.SeriesMarkDataTypeMin,
			)
			opt.SeriesList[1].MarkLine = charts.NewMarkLine(
				charts.SeriesMarkDataTypeAverage,
			)
		},
	)
	if err != nil {
		panic(err)
	}

	buf, err := p.Bytes()
	if err != nil {
		panic(err)
	}
	// snip...
}

Horizontal Bar Chart

package main

import (
	"github.com/vicanso/go-charts/v2"
)

func main() {
	values := [][]float64{
		{
			18203,
			23489,
			29034,
			104970,
			131744,
			630230,
		},
		{
			// snip...	
		},
	}
	p, err := charts.HorizontalBarRender(
		values,
		charts.TitleTextOptionFunc("World Population"),
		charts.PaddingOptionFunc(charts.Box{
			Top:    20,
			Right:  40,
			Bottom: 20,
			Left:   20,
		}),
		charts.LegendLabelsOptionFunc([]string{
			"2011",
			"2012",
		}),
		charts.YAxisDataOptionFunc([]string{
			"Brazil",
			"Indonesia",
			"USA",
			"India",
			"China",
			"World",
		}),
	)
	if err != nil {
		panic(err)
	}

	buf, err := p.Bytes()
	if err != nil {
		panic(err)
	}
	// snip...
}

Pie Chart

package main

import (
	"github.com/vicanso/go-charts/v2"
)

func main() {
	values := []float64{
		1048,
		735,
		580,
		484,
		300,
	}
	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",
				"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)
	}
	// snip...	
}

Radar Chart

package main

import (
	"github.com/vicanso/go-charts/v2"
)

func main() {
	values := [][]float64{
		{
			4200,
			3000,
			20000,
			35000,
			50000,
			18000,
		},
		{
			// snip...
		},
	}
	p, err := charts.RadarRender(
		values,
		charts.TitleTextOptionFunc("Basic Radar Chart"),
		charts.LegendLabelsOptionFunc([]string{
			"Allocated Budget",
			"Actual Spending",
		}),
		charts.RadarIndicatorOptionFunc([]string{
			"Sales",
			"Administration",
			"Information Technology",
			"Customer Support",
			"Development",
			"Marketing",
		}, []float64{
			6500,
			16000,
			30000,
			38000,
			52000,
			25000,
		}),
	)
	if err != nil {
		panic(err)
	}

	buf, err := p.Bytes()
	if err != nil {
		panic(err)
	}
	// snip...
}

Funnel Chart

package main

import (
	"github.com/vicanso/go-charts/v2"
)

func main() {
	values := []float64{
		100,
		80,
		60,
		40,
		20,
	}
	p, err := charts.FunnelRender(
		values,
		charts.TitleTextOptionFunc("Funnel"),
		charts.LegendLabelsOptionFunc([]string{
			"Show",
			"Click",
			"Visit",
			"Inquiry",
			"Order",
		}),
	)
	if err != nil {
		panic(err)
	}

	buf, err := p.Bytes()
	if err != nil {
		panic(err)
	}
	// snip...
}

Table

package main

import (
	"github.com/vicanso/go-charts/v2"
)

func main() {
	header := []string{
		"Name",
		"Age",
		"Address",
		"Tag",
		"Action",
	}
	data := [][]string{
		{
			"John Brown",
			"32",
			"New York No. 1 Lake Park",
			"nice, developer",
			"Send Mail",
		},
		{
			"Jim Green	",
			"42",
			"London No. 1 Lake Park",
			"wow",
			"Send Mail",
		},
		{
			"Joe Black	",
			"32",
			"Sidney No. 1 Lake Park",
			"cool, teacher",
			"Send Mail",
		},
	}
	spans := map[int]int{
		0: 2,
		1: 1,
		// 设置第三列的span
		2: 3,
		3: 2,
		4: 2,
	}
	p, err := charts.TableRender(
		header,
		data,
		spans,
	)
	if err != nil {
		panic(err)
	}

	buf, err := p.Bytes()
	if err != nil {
		panic(err)
	}
	// snip...
}

ECharts Render

package main

import (
	"github.com/vicanso/go-charts/v2"
)

func main() {
	buf, err := charts.RenderEChartsToPNG(`{
		"title": {
			"text": "Line"
		},
		"xAxis": {
			"data": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
		},
		"series": [
			{
				"data": [150, 230, 224, 218, 135, 147, 260]
			}
		]
	}`)
	// snip...
}

ECharts Option

The name with [] is new parameter, others are the same as echarts.

  • [type] The canvas type, support svg and png, default is svg
  • [theme] The theme, support dark, light and grafana, default is light
  • [fontFamily] The font family for chart
  • [padding] The padding of chart
  • [box] The canvas box of chart
  • [width] The width of chart
  • [height] The height of chart
  • title Title component, including main title and subtitle
    • title.text The main title text, supporting for \n for newlines
    • title.subtextSubtitle text, supporting for \n for newlines
    • title.left Distance between title component and the left side of the container. Left value can be instant pixel value like 20; it can also be a percentage value relative to container width like '20%'; and it can also be 'left', 'center', or 'right'.
    • title.top Distance between title component and the top side of the container. Top value can be instant pixel value like 20
    • title.textStyle.color Text color for title
    • title.textStyle.fontSize Text font size for title
    • title.textStyle.fontFamily Text font family for title, it will change the font family for chart
  • xAxis The x axis in cartesian(rectangular) coordinate. go-charts only support one x axis.
    • xAxis.boundaryGap The boundary gap on both sides of a coordinate axis. The setting and behavior of category axes and non-category axes are different. If set null or true, the label appear in the center part of two axis ticks.
    • xAxis.splitNumber Number of segments that the axis is split into. Note that this number serves only as a recommendation, and the true segments may be adjusted based on readability
    • xAxis.data Category data, only support string array.
  • yAxis The y axis in cartesian(rectangular) coordinate, it support 2 y axis
    • yAxis.min The minimum value of axis. It will be automatically computed to make sure axis tick is equally distributed when not set
    • yAxis.max The maximum value of axis. It will be automatically computed to make sure axis tick is equally distributed when not se.
    • yAxis.axisLabel.formatter Formatter of axis label, which supports string template: "formatter": "{value} kg"
    • yAxis.axisLine.lineStyle.color The color for line
  • legend Legend component
    • legend.show Whether to show legend
    • legend.data Data array of legend, only support string array: ["Email", "Video Ads"]
    • legend.align Legend marker and text aligning. Support left and right, default is left
    • legend.padding legend space around content
    • legend.left Distance between legend component and the left side of the container. Left value can be instant pixel value like 20; it can also be a percentage value relative to container width like '20%'; and it can also be 'left', 'center', or 'right'.
    • legend.top Distance between legend component and the top side of the container. Top value can be instant pixel value like 20
  • radar Coordinate for radar charts
    • radar.indicator Indicator of radar chart, which is used to assign multiple variables(dimensions) in radar chart
      • radar.indicator.name Indicator's name
      • radar.indicator.max The maximum value of indicator
      • radar.indicator.min The minimum value of indicator, default value is 0.
  • series The series for chart
    • series.name Series name used for displaying in legend.
    • series.type Series type: line, bar, pie, radar or funnel
    • series.radius Radius of Pie chart:50%, default is 40%
    • series.yAxisIndex Index of y axis to combine with, which is useful for multiple y axes in one chart
    • series.label.show Whether to show label
    • series.label.distance Distance to the host graphic element
    • series.label.color Label color
    • series.itemStyle.color Color for the series's item
    • series.markPoint Mark point in a chart.
    • series.markPoint.symbolSize Symbol size, default is 30
    • series.markPoint.data Data array for mark points, each of which is an object and the type only support max and min: [{"type": "max"}, {"type": "min"}]
    • series.markLine Mark line in a chart
    • series.markPoint.data Data array for mark points, each of which is an object and the type only support max, min and average: `[{"type": "max"}, {"type": "min"}, {"type": "average"}]``
    • series.data Data array of series, which can be in the following forms:
      • value It's a float array: [1.1, 2,3, 5.2]
      • object It's a object value array: [{"value": 1048, "name": "Search Engine"},{"value": 735,"name": "Direct"}]
  • [children] The options of children chart

Performance

Generate a png chart will be less than 20ms. It's better than using chrome headless with echarts.

BenchmarkMultiChartPNGRender-8                78          15216336 ns/op         2298308 B/op       1148 allocs/op
BenchmarkMultiChartSVGRender-8               367           3356325 ns/op        20597282 B/op       3088 allocs/op

go-charts's People

Contributors

jentfoo avatar vicanso avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-charts's Issues

Funnel chart bug with 0 values

When 0 values are assigned to a funnel chart, it probably goes into an endless loop and does not complete.

To curcumvent this I currently assign 1 when the value is 0. Please fix.

Screenshot below using 1s instead of 0s. With 0s it does not complete generating.

image

FontFamily. Don't take effect!!!

func writeFile(buf []byte) error {
tmpPath := "./tmp"
err := os.MkdirAll(tmpPath, 0700)
if err != nil {
return err
}

file := filepath.Join(tmpPath, "chinese-line-chart.png")
err = ioutil.WriteFile(file, buf, 0600)
if err != nil {
	return err
}
return nil

}

func main() {
buf, err := ioutil.ReadFile("./fonts/xx.TTF")
if err != nil {
panic(err)
}
err = charts.InstallFont("noto", buf)
if err != nil {
panic(err)
}

//font, _ := truetype.Parse(buf)
    p, err := charts.TableOptionRender(charts.TableChartOption{
	//Font:   font,
	FontFamily: "noto",
	Header: []string{"名称", "年龄"},
	Data:   [][]string{{"张三", "18"}},
})
buf, err = p.Bytes()
if err != nil {
	panic(err)
}
err = writeFile(buf)
if err != nil {
	panic(err)
}

}

绘制饼图的时候,SeriesLabel会重叠在一起

代码:

package tests

import (
	"encoding/base64"
	"fmt"
	gcs "github.com/vicanso/go-charts/v2"
	"github.com/wcharczuk/go-chart/v2/drawing"
	"testing"
)

type factor struct {
	Name     string
	Score    float64
	ScoreMax float64
}

var factors = []factor{
	{Name: "A", Score: 0.5, ScoreMax: 10},
	{Name: "B", Score: 0.5, ScoreMax: 10},
	{Name: "C", Score: 0.5, ScoreMax: 10},
	{Name: "D", Score: 9, ScoreMax: 10},
	{Name: "E", Score: 8, ScoreMax: 10},
	{Name: "F", Score: 5, ScoreMax: 10},
	{Name: "G", Score: 10, ScoreMax: 10},
	{Name: "H", Score: 10, ScoreMax: 10},
}

// TestPie
// @description: 测试生成饼图
// @param t
func TestPie(t *testing.T) {
	var sources []float64
	var names []string

	for _, f := range factors {
		sources = append(sources, f.Score)
		names = append(names, f.Name)
	}

	var series = gcs.NewPieSeriesList(
		sources, gcs.PieSeriesOption{
			Names:  names,
			Label:  gcs.SeriesLabel{Show: true, Color: drawing.ColorBlue},
			Radius: "20%",
		},
	)

	render, err := gcs.Render(gcs.ChartOption{
		Type:       gcs.ChartOutputPNG,
		FontFamily: "SourceHanSansCN",
		SeriesList: series,
	})
	if err != nil {
		t.Errorf("生成饼图失败: %v", err)
		return
	}

	// 输出base64编码
	bys, err := render.Bytes()
	if err != nil {
		t.Errorf("生成饼图失败: %v", err)
		return
	}
	str := "data:image/png;base64," + base64.StdEncoding.EncodeToString(bys)
	t.Logf("生成饼图成功: %v", str)
}

效果:
image

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

标题不支持设置中文字体

我安装了中文字体,但是标题部分没有生效,如图
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",
		}),
	)

x轴能不能显示中文

用charts.InstallFont方式标题能够显示中文,但x轴不能显示中文,不知道怎么解决?

X轴能增加对时间的支持么

在X轴传的的数据是时间的时候,在画X刻度的时候,可以选择整点时间,比00:0001:0002:00,而不是像下面这样,看着没有什么 规则,在观看的时候,体验很不好
这个图还好:
image
下面这个图不太好友好了

表格:字体颜色设置不上

`package main

import (
"github.com/vicanso/go-charts/v2"
"io/ioutil"
"os"
"path/filepath"
)

func main() {
// 字体文件需要自行下载
// https://github.com/googlefonts/noto-cjk
buf, err := ioutil.ReadFile("xxx")
if err != nil {
panic(err)
}
err = charts.InstallFont("noto", buf)
if err != nil {
panic(err)
}

p, err := charts.TableOptionRender(charts.TableChartOption{
	Header: []string{
		"xxxxx",
		"xxxx",
		"xxxxx",
		"xxxxx",
	},
	HeaderFontColor: charts.Color{
		R: 16,
		G: 22,
		B: 30,
		A: 255,
	},
	FontFamily: "noto",
	Spans: []int{
		10, 3, 5, 5,
	},
	Data: [][]string{
		{
			"xxxx",
			"是",
			"933",
			"231",
		}
	},
	Width: 1200,
	TextAligns: []string{
		charts.AlignLeft,
		charts.AlignCenter,
		charts.AlignCenter,
		charts.AlignCenter,
	},
	CellStyle: func(tc charts.TableCell) *charts.Style {
		column := tc.Column
		if column != 1 {
			return nil
		}

		style := charts.Style{
			FontColor: charts.Color{
				R: 179,
				G: 53,
				B: 20,
				A: 255,
			},
		}
		//if value > 0 {
		//	style.FillColor = charts.Color{
		//		R: 179,
		//		G: 53,
		//		B: 20,
		//		A: 255,
		//	}
		//} else if value < 0 {
		//	style.FillColor = charts.Color{
		//		R: 33,
		//		G: 124,
		//		B: 50,
		//		A: 255,
		//	}
		//}
		return &style
	},
})
if err != nil {
	panic(err)
}

buf, err = p.Bytes()
if err != nil {
	panic(err)
}
err = writeFile(buf, "table-color.png")
if err != nil {
	panic(err)
}

}

func writeFile(buf []byte, filename string) error {
tmpPath := "./tmp"
err := os.MkdirAll(tmpPath, 0700)
if err != nil {
return err
}

file := filepath.Join(tmpPath, filename)
err = ioutil.WriteFile(file, buf, 0600)
if err != nil {
	return err
}
return nil

}
`
image

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

我用的是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
}

画图的时候,会打印出很多<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

bug: 当HorizontalBarRender的YAxisOptions Data超过6个时,数据展示不全

  • 当YAxisOptions的data超过6个时,数据展示会出问题

image

代码如下:

    values := [][]float64{
		{
			10570,
			18203,
			23489,
			29034,
			104970,
			131744,
			630230,
		},
		{
			12570,
			19325,
			23438,
			31000,
			121594,
			134141,
			681807,
		},
	}
	p, err := charts.HorizontalBarRender(
		values,
		charts.TitleTextOptionFunc("World Population"),
		//charts.PaddingOptionFunc(charts.Box{
		//	Top:    20,
		//	Right:  40,
		//	Bottom: 20,
		//	Left:   20,
		//}),
		charts.LegendLabelsOptionFunc([]string{
			"2011",
			"2012",
		}),
		charts.YAxisDataOptionFunc([]string{
			"Japan",
			"Brazil",
			"Indonesia",
			"USA",
			"India",
			"China",
			"World",
		}),
	)

y轴是否支持只显示整数?

image
看了下y轴好像是固定6个刻度,如果设定的最大值不是6的倍数,会出现小数

opt.YAxisOptions = []charts.YAxisOption{
{
	Min: charts.NewFloatPoint(0),
	Max: charts.NewFloatPoint(40),
},
}

横向柱状图,值和刻度不匹配。

image

版本:
github.com/vicanso/go-charts/v2 v2.5.5

data: 10 20 30 ---- 100

p, err := charts.HorizontalBarRender( [][]float64{ data, }, charts.TitleTextOptionFunc(titleText), charts.PaddingOptionFunc(charts.Box{ Top: 20, Right: 40, Bottom: 20, Left: 20, }), charts.LegendLabelsOptionFunc([]string{ "数量", }), charts.YAxisDataOptionFunc(xAxisData), charts.FontFamilyOptionFunc(fontName), charts.WidthOptionFunc(2000), charts.HeightOptionFunc(1000), func(opt *charts.ChartOption) { opt.SeriesList[0].Label.Show = true }, )

平滑折线好像不支持

{
"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 没有生效,麻烦帮看下

中文对其有问题

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
			}

去掉,就正常了。

LegendLabels颜色太少了

LegendLabels貌似只有几个颜色,循环使用。LegendLabels多了,就不够用了,分不清谁是谁了,
另外能展示负轴的图么,如下图,我获取的数据都是正数,但是可以设置成负载画图:
image

可以添加关闭(自动隐藏XAxis Label显示)配置的支持吗?

当前状态:x轴会自动根据Label文本总长度计算,显示和隐藏 label。
需求场景:在一些情况下,其实不在乎重叠,但是又没有配置选项可以设置全部显示。
需求:添加配置项,可以自定义显示全部(或者指定的label)。

bbbbbb

在添加了x轴label旋转角度支持之后,自动根据文本宽度计算较为符合的展示项,再使用文本总宽度来计算已然是不太合理了。
如果旋转了角度,可能使用文本高度来计算才更为合理。
aaaa

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

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

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

Y轴默认的最大值不能够进行设置(默认是12)

当Y轴的数据是:[1, 1, 0, 0, 1, 0, 0],此时Y轴的最大值就是1了,但是画出来的图,最大值是12,
如图1,这样展示的图就不直观了:
image
如图2:
image

其实,这样大部分的空间都没有用上,Y轴此时如果能设置成最大值是1就好了,

如何给 HorizontalBarRender 添加数值显示?

values := [][]float64{
{100,200,300,100,200,300 },
}
p, err := charts.HorizontalBarRender(
values,
charts.TitleTextOptionFunc("test"),
charts.PaddingOptionFunc(charts.Box{
Top: 20,
Right: 40,
Bottom: 20,
Left: 20,
}),
func(opt *charts.ChartOption) {
opt.SeriesList[0].Label.Show = true
},
charts.YAxisDataOptionFunc([]string{
"1",
"2",
"3",
"4",
"5",
"6",
}),
)
if err != nil {
panic(err)
}

buf, err := p.Bytes()
if err != nil {
	panic(err)
}
f, _ := os.Create("test.png")
f.Write(buf)
f.Close()

BarRender条形图bar 可以支持一下x轴Label旋转角度嘛

values := [][]float64{
{
2.0,
4.9,
7.0,
23.2,
25.6,
76.7,
135.6,
162.2,
32.6,
20.0,
6.4,
3.3,
},
}
p, err := charts.BarRender(
values,
charts.XAxisDataOptionFunc([]string{
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
}),
charts.LegendLabelsOptionFunc([]string{
"Rainfall",
}, charts.PositionRight),
charts.MarkLineOptionFunc(0, charts.SeriesMarkDataTypeAverage),
charts.MarkPointOptionFunc(0, charts.SeriesMarkDataTypeMax,
charts.SeriesMarkDataTypeMin),
// custom option func
func(opt *charts.ChartOption) {
opt.SeriesList[0].MarkPoint = charts.NewMarkPoint(
charts.SeriesMarkDataTypeMax,
charts.SeriesMarkDataTypeMin,
)
opt.SeriesList[0].MarkLine = charts.NewMarkLine(
charts.SeriesMarkDataTypeAverage,
)
},
charts.XAxisOptionFunc(charts.XAxisOption{
TextRotation: math.Pi / 4,
}),
)

这个目前好像不支持TextRotation: math.Pi / 4,

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.