From 1be8d43405cf646624d4f8e3dde1906e40f18489 Mon Sep 17 00:00:00 2001 From: vicanso Date: Mon, 7 Mar 2022 21:00:32 +0800 Subject: [PATCH] refactor: return error if sum value of pie is not gt 0 --- pie_chart.go | 4 ++++ pie_chart_test.go | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pie_chart.go b/pie_chart.go index 4833c34..099a91c 100644 --- a/pie_chart.go +++ b/pie_chart.go @@ -23,6 +23,7 @@ package charts import ( + "errors" "math" "github.com/golang/freetype/truetype" @@ -68,6 +69,9 @@ func pieChartRender(opt pieChartOption, result *basicRenderResult) error { values[index] = value total += value } + if total <= 0 { + return errors.New("The sum value of pie chart should gt 0") + } r := d.Render theme := NewTheme(opt.Theme) diff --git a/pie_chart_test.go b/pie_chart_test.go index 7cb9fde..84072be 100644 --- a/pie_chart_test.go +++ b/pie_chart_test.go @@ -46,10 +46,12 @@ func TestPieChartRender(t *testing.T) { SeriesList: NewPieSeriesList([]float64{ 5, 10, + 0, }, PieSeriesOption{ Names: []string{ "a", "b", + "c", }, Label: SeriesLabel{ Show: true, @@ -63,5 +65,5 @@ func TestPieChartRender(t *testing.T) { assert.Nil(err) data, err := d.Bytes() assert.Nil(err) - assert.Equal("\\na: 33.33%b: 66.66%", string(data)) + assert.Equal("\\na: 33.33%b: 66.66%c: 0%", string(data)) }