tweaks to range validation.

This commit is contained in:
Will Charczuk 2017-03-02 14:26:21 -08:00
parent 114738a2de
commit b2de33058f

View File

@ -2,6 +2,7 @@ package chart
import ( import (
"errors" "errors"
"fmt"
"io" "io"
"math" "math"
@ -69,8 +70,12 @@ func (c Chart) GetHeight() 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 (c Chart) Render(rp RendererProvider, w io.Writer) error { func (c Chart) Render(rp RendererProvider, w io.Writer) error {
if len(c.Series) == 0 { if len(c.Series) == 0 {
return errors.New("Please provide at least one series") return errors.New("please provide at least one series")
} }
if visibleSeriesErr := c.checkHasVisibleSeries(); visibleSeriesErr != nil {
return visibleSeriesErr
}
c.YAxisSecondary.AxisType = YAxisSecondary c.YAxisSecondary.AxisType = YAxisSecondary
r, err := rp(c.GetWidth(), c.GetHeight()) r, err := rp(c.GetWidth(), c.GetHeight())
@ -134,6 +139,17 @@ func (c Chart) Render(rp RendererProvider, w io.Writer) error {
return r.Save(w) return r.Save(w)
} }
func (c Chart) checkHasVisibleSeries() error {
hasVisibleSeries := false
for _, s := range c.Series {
hasVisibleSeries = hasVisibleSeries || (s.GetStyle().IsZero() || s.GetStyle().Show)
}
if !hasVisibleSeries {
return fmt.Errorf("must have (1) visible series; make sure if you set a style, you set .Show = true")
}
return nil
}
func (c Chart) validateSeries() error { func (c Chart) validateSeries() error {
var err error var err error
for _, s := range c.Series { for _, s := range c.Series {
@ -272,15 +288,29 @@ func (c Chart) getRanges() (xrange, yrange, yrangeAlt Range) {
} }
func (c Chart) checkRanges(xr, yr, yra Range) error { func (c Chart) checkRanges(xr, yr, yra Range) error {
if math.IsInf(xr.GetDelta(), 0) || math.IsNaN(xr.GetDelta()) || xr.GetDelta() == 0 { xDelta := xr.GetDelta()
return errors.New("Invalid (infinite or NaN) x-range delta") if math.IsInf(xDelta, 0) {
return errors.New("infinite x-range delta")
} }
if math.IsInf(yr.GetDelta(), 0) || math.IsNaN(yr.GetDelta()) || yr.GetDelta() == 0 { if math.IsNaN(xDelta) {
return errors.New("Invalid (infinite or NaN) y-range delta") return errors.New("nan x-range delta")
} }
yDelta := yr.GetDelta()
if math.IsInf(yDelta, 0) {
return errors.New("infinite y-range delta")
}
if math.IsNaN(yDelta) {
return errors.New("nan y-range delta")
}
if c.hasSecondarySeries() { if c.hasSecondarySeries() {
if math.IsInf(yra.GetDelta(), 0) || math.IsNaN(yra.GetDelta()) || yra.GetDelta() == 0 { yraDelta := yra.GetDelta()
return errors.New("Invalid (infinite or NaN) y-secondary-range delta") if math.IsInf(yraDelta, 0) {
return errors.New("infinite secondary y-range delta")
}
if math.IsNaN(yraDelta) {
return errors.New("nan secondary y-range delta")
} }
} }