go-chart icon indicating copy to clipboard operation
go-chart copied to clipboard

Single element donut charts renders as an empty canvas

Open sameeraaperera opened this issue 2 years ago • 1 comments

I had a requirement to generate charts that somtimes contain only 1 element(100%)/ But having only one element as input values ends up with just the canvas. Single item donut charts seem to have some custom handling here . https://github.com/wcharczuk/go-chart/blob/c1468e8ae4ed7c6f564f3bb54ae90b563dcf65ce/donut_chart.go#L134

func main() {
	donut := chart.DonutChart{
		Width:  512,
		Height: 512,
		Values: []chart.Value{
			{
				Value: 1,
			},
		},
	}
	f, _ := os.Create("donut.png")
	donut.Render(chart.PNG, f)
	
}

Output(just a white canvas): donut

Workaround As a workaround I ended up with adding an extra value with a very small value(1e-7) and very small stroke width which triggered the usual rendering. Its not ideal as you can still see a tiny white line but its something.

Values: []chart.Value{
			{
				Value: 1,
				Style: chart.Style{
					StrokeWidth: 1e-7,
				},
			},
			{
				Value: 1e-7,
				Style: chart.Style{
					StrokeWidth: 1e-7,
				},
			},
		},

Output donut

sameeraaperera avatar Oct 20 '22 22:10 sameeraaperera

#205 Fixes the issue.

AlejandroTorreblanca avatar Nov 30 '22 10:11 AlejandroTorreblanca