From 7ae42ee2e13974870ad0b459cf5819d034bfc260 Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Fri, 13 Jan 2017 14:07:42 -0800 Subject: [PATCH] handle zero values with error. --- pie_chart.go | 14 +++++++++++--- pie_chart_test.go | 38 ++++++++++++++++++++++++++++++++++++++ value.go | 21 ++++++++++++++------- 3 files changed, 63 insertions(+), 10 deletions(-) diff --git a/pie_chart.go b/pie_chart.go index 720f61d..b2be1a6 100644 --- a/pie_chart.go +++ b/pie_chart.go @@ -2,6 +2,7 @@ package chart import ( "errors" + "fmt" "io" "github.com/golang/freetype/truetype" @@ -88,7 +89,10 @@ func (pc PieChart) Render(rp RendererProvider, w io.Writer) error { pc.drawBackground(r) pc.drawCanvas(r, canvasBox) - finalValues := pc.finalizeValues(pc.Values) + finalValues, err := pc.finalizeValues(pc.Values) + if err != nil { + return err + } pc.drawSlices(r, canvasBox, finalValues) pc.drawTitle(r) for _, a := range pc.Elements { @@ -158,8 +162,12 @@ func (pc PieChart) drawSlices(r Renderer, canvasBox Box, values []Value) { } } -func (pc PieChart) finalizeValues(values []Value) []Value { - return Values(values).Normalize() +func (pc PieChart) finalizeValues(values []Value) ([]Value, error) { + finalValues := Values(values).Normalize() + if len(finalValues) == 0 { + return nil, fmt.Errorf("pie chart must contain at least (1) non-zero value") + } + return finalValues, nil } func (pc PieChart) getDefaultCanvasBox() Box { diff --git a/pie_chart_test.go b/pie_chart_test.go index 2f536cd..788cbe2 100644 --- a/pie_chart_test.go +++ b/pie_chart_test.go @@ -29,3 +29,41 @@ func TestPieChart(t *testing.T) { pie.Render(PNG, b) assert.NotZero(b.Len()) } + +func TestPieChartDropsZeroValues(t *testing.T) { + assert := assert.New(t) + + pie := PieChart{ + Canvas: Style{ + FillColor: ColorLightGray, + }, + Values: []Value{ + {Value: 5, Label: "Blue"}, + {Value: 5, Label: "Green"}, + {Value: 0, Label: "Gray"}, + }, + } + + b := bytes.NewBuffer([]byte{}) + err := pie.Render(PNG, b) + assert.Nil(err) +} + +func TestPieChartAllZeroValues(t *testing.T) { + assert := assert.New(t) + + pie := PieChart{ + Canvas: Style{ + FillColor: ColorLightGray, + }, + Values: []Value{ + {Value: 0, Label: "Blue"}, + {Value: 0, Label: "Green"}, + {Value: 0, Label: "Gray"}, + }, + } + + b := bytes.NewBuffer([]byte{}) + err := pie.Render(PNG, b) + assert.NotNil(err) +} diff --git a/value.go b/value.go index 8a1da0d..0346c9a 100644 --- a/value.go +++ b/value.go @@ -26,13 +26,20 @@ func (vs Values) ValuesNormalized() []float64 { // Normalize returns the values normalized. func (vs Values) Normalize() []Value { - output := make([]Value, len(vs)) - total := Math.Sum(vs.Values()...) - for index, v := range vs { - output[index] = Value{ - Style: v.Style, - Label: v.Label, - Value: Math.RoundDown(v.Value/total, 0.0001), + var output []Value + var total float64 + + for _, v := range vs { + total += v.Value + } + + for _, v := range vs { + if v.Value > 0 { + output = append(output, Value{ + Style: v.Style, + Label: v.Label, + Value: Math.RoundDown(v.Value/total, 0.0001), + }) } } return output