handle zero values
This commit is contained in:
parent
fc9b6a6d55
commit
e4cd38f602
2 changed files with 27 additions and 1 deletions
|
@ -2,6 +2,7 @@ package chart
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
|
@ -85,7 +86,7 @@ func (bc BarChart) GetBarWidth() int {
|
||||||
// Render renders the chart with the given renderer to the given io.Writer.
|
// Render renders the chart with the given renderer to the given io.Writer.
|
||||||
func (bc BarChart) Render(rp RendererProvider, w io.Writer) error {
|
func (bc BarChart) Render(rp RendererProvider, w io.Writer) error {
|
||||||
if len(bc.Bars) == 0 {
|
if len(bc.Bars) == 0 {
|
||||||
return errors.New("Please provide at least one bar.")
|
return errors.New("please provide at least one bar")
|
||||||
}
|
}
|
||||||
|
|
||||||
r, err := rp(bc.GetWidth(), bc.GetHeight())
|
r, err := rp(bc.GetWidth(), bc.GetHeight())
|
||||||
|
@ -111,6 +112,9 @@ func (bc BarChart) Render(rp RendererProvider, w io.Writer) error {
|
||||||
|
|
||||||
canvasBox = bc.getDefaultCanvasBox()
|
canvasBox = bc.getDefaultCanvasBox()
|
||||||
yr = bc.getRanges()
|
yr = bc.getRanges()
|
||||||
|
if yr.GetMax()-yr.GetMin() == 0 {
|
||||||
|
return fmt.Errorf("invalid data range; cannot be zero")
|
||||||
|
}
|
||||||
yr = bc.setRangeDomains(canvasBox, yr)
|
yr = bc.setRangeDomains(canvasBox, yr)
|
||||||
yf = bc.getValueFormatters()
|
yf = bc.getValueFormatters()
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,28 @@ func TestBarChartRender(t *testing.T) {
|
||||||
assert.NotZero(buf.Len())
|
assert.NotZero(buf.Len())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBarChartRenderZero(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
bc := BarChart{
|
||||||
|
Width: 1024,
|
||||||
|
Title: "Test Title",
|
||||||
|
TitleStyle: StyleShow(),
|
||||||
|
XAxis: StyleShow(),
|
||||||
|
YAxis: YAxis{
|
||||||
|
Style: StyleShow(),
|
||||||
|
},
|
||||||
|
Bars: []Value{
|
||||||
|
{Value: 0.0, Label: "One"},
|
||||||
|
{Value: 0.0, Label: "Two"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
err := bc.Render(PNG, buf)
|
||||||
|
assert.NotNil(err)
|
||||||
|
}
|
||||||
|
|
||||||
func TestBarChartProps(t *testing.T) {
|
func TestBarChartProps(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue