From b2de33058f747b2a51bb84c22a128e60aa2f0946 Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Thu, 2 Mar 2017 14:26:21 -0800 Subject: [PATCH] tweaks to range validation. --- chart.go | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/chart.go b/chart.go index 1b2bd7f..49c64f1 100644 --- a/chart.go +++ b/chart.go @@ -2,6 +2,7 @@ package chart import ( "errors" + "fmt" "io" "math" @@ -69,8 +70,12 @@ func (c Chart) GetHeight() int { // Render renders the chart with the given renderer to the given io.Writer. func (c Chart) Render(rp RendererProvider, w io.Writer) error { 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 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) } +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 { var err error 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 { - if math.IsInf(xr.GetDelta(), 0) || math.IsNaN(xr.GetDelta()) || xr.GetDelta() == 0 { - return errors.New("Invalid (infinite or NaN) x-range delta") + xDelta := xr.GetDelta() + 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 { - return errors.New("Invalid (infinite or NaN) y-range delta") + if math.IsNaN(xDelta) { + 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 math.IsInf(yra.GetDelta(), 0) || math.IsNaN(yra.GetDelta()) || yra.GetDelta() == 0 { - return errors.New("Invalid (infinite or NaN) y-secondary-range delta") + yraDelta := yra.GetDelta() + 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") } }