wcharczuk / go-chart

go chart is a basic charting library in go.
MIT License
3.99k stars 325 forks source link

Single element donut charts renders as an empty canvas #203

Open sameeraaperera opened 1 year ago

sameeraaperera commented 1 year ago

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

AlejandroTorreblanca commented 1 year ago

205 Fixes the issue.