handle zero values with error.
This commit is contained in:
parent
7163f89845
commit
7ae42ee2e1
14
pie_chart.go
14
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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
15
value.go
15
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{
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user