From 6d48c49c0760cf08189bf88a4804187235dbe1fe Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Sat, 16 Jul 2016 22:56:12 -0700 Subject: [PATCH] tests for some things that came up as regressions when building out examples. --- chart.go | 12 +++++-- chart_test.go | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++ range.go | 4 ++- 3 files changed, 104 insertions(+), 3 deletions(-) diff --git a/chart.go b/chart.go index 5f7a3d6..bf82a57 100644 --- a/chart.go +++ b/chart.go @@ -373,12 +373,20 @@ func (c Chart) getAnnotationAdjustedCanvasBox(r Renderer, canvasBox Box, xr, yr, return canvasBox.OuterConstrain(c.Box(), annotationSeriesBox) } +func (c Chart) getBackgroundStyle() Style { + return c.Background.WithDefaultsFrom(c.styleDefaultsBackground()) +} + func (c Chart) drawBackground(r Renderer) { - DrawBox(r, c.Box(), c.Background.WithDefaultsFrom(c.styleDefaultsBackground())) + DrawBox(r, c.Box(), c.getBackgroundStyle()) +} + +func (c Chart) getCanvasStyle() Style { + return c.Canvas.WithDefaultsFrom(c.styleDefaultsCanvas()) } func (c Chart) drawCanvas(r Renderer, canvasBox Box) { - DrawBox(r, canvasBox, c.Canvas.WithDefaultsFrom(c.styleDefaultsCanvas())) + DrawBox(r, canvasBox, c.getCanvasStyle()) } func (c Chart) drawAxes(r Renderer, canvasBox Box, xrange, yrange, yrangeAlt Range, xticks, yticks, yticksAlt []Tick) { diff --git a/chart_test.go b/chart_test.go index 2dceba6..16c7902 100644 --- a/chart_test.go +++ b/chart_test.go @@ -7,6 +7,7 @@ import ( "time" "github.com/blendlabs/go-assert" + "github.com/wcharczuk/go-chart/drawing" ) func TestChartGetDPI(t *testing.T) { @@ -123,6 +124,96 @@ func TestChartGetRanges(t *testing.T) { assert.Equal(19.7, yra2.Max) } +func TestChartGetRangesUseTicks(t *testing.T) { + assert := assert.New(t) + + // this test asserts that ticks should supercede manual ranges when generating the overall ranges. + + c := Chart{ + YAxis: YAxis{ + Ticks: []Tick{ + {0.0, "Zero"}, + {1.0, "1.0"}, + {2.0, "2.0"}, + {3.0, "3.0"}, + {4.0, "4.0"}, + {5.0, "Five"}, + }, + Range: Range{ + Min: -5.0, + Max: 5.0, + }, + }, + Series: []Series{ + ContinuousSeries{ + XValues: []float64{-2.0, -1.0, 0, 1.0, 2.0}, + YValues: []float64{1.0, 2.0, 3.0, 4.0, 4.5}, + }, + }, + } + + xr, yr, yar := c.getRanges() + assert.Equal(-2.0, xr.Min) + assert.Equal(2.0, xr.Max) + assert.Equal(0.0, yr.Min) + assert.Equal(5.0, yr.Max) + assert.True(yar.IsZero(), yar.String()) +} + +func TestChartGetRangesUseUserRanges(t *testing.T) { + assert := assert.New(t) + + // this test asserts that ticks should supercede manual ranges when generating the overall ranges. + + c := Chart{ + YAxis: YAxis{ + Range: Range{ + Min: -5.0, + Max: 5.0, + }, + }, + Series: []Series{ + ContinuousSeries{ + XValues: []float64{-2.0, -1.0, 0, 1.0, 2.0}, + YValues: []float64{1.0, 2.0, 3.0, 4.0, 4.5}, + }, + }, + } + + xr, yr, yar := c.getRanges() + assert.Equal(-2.0, xr.Min) + assert.Equal(2.0, xr.Max) + assert.Equal(-5.0, yr.Min) + assert.Equal(5.0, yr.Max) + assert.True(yar.IsZero(), yar.String()) +} + +func TestChartGetBackgroundStyle(t *testing.T) { + assert := assert.New(t) + + c := Chart{ + Background: Style{ + FillColor: drawing.ColorBlack, + }, + } + + bs := c.getBackgroundStyle() + assert.Equal(bs.FillColor.String(), drawing.ColorBlack.String()) +} + +func TestChartGetCanvasStyle(t *testing.T) { + assert := assert.New(t) + + c := Chart{ + Canvas: Style{ + FillColor: drawing.ColorBlack, + }, + } + + bs := c.getCanvasStyle() + assert.Equal(bs.FillColor.String(), drawing.ColorBlack.String()) +} + func TestChartGetDefaultCanvasBox(t *testing.T) { assert := assert.New(t) diff --git a/range.go b/range.go index b8ba59d..ddea1ee 100644 --- a/range.go +++ b/range.go @@ -14,7 +14,9 @@ type Range struct { // IsZero returns if the range has been set or not. func (r Range) IsZero() bool { - return r.Min == 0 && r.Max == 0 && r.Domain == 0 + return (r.Min == 0 || math.IsNaN(r.Min)) && + (r.Max == 0 || math.IsNaN(r.Max)) && + r.Domain == 0 } // Delta returns the difference between the min and max value.