From 149bc3ad5d41090faee1d6faee27def7d6678b08 Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Sun, 10 Jul 2016 21:00:03 -0700 Subject: [PATCH] adding tests. --- annotation_series.go | 12 ++- annotation_series_test.go | 156 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+), 7 deletions(-) create mode 100644 annotation_series_test.go diff --git a/annotation_series.go b/annotation_series.go index 8090806..aeed26d 100644 --- a/annotation_series.go +++ b/annotation_series.go @@ -3,8 +3,6 @@ package chart import ( "math" "time" - - "github.com/wcharczuk/go-chart" ) // CreateContinuousSeriesLastValueLabel returns a (1) value annotation series. @@ -13,9 +11,9 @@ func CreateContinuousSeriesLastValueLabel(name string, xvalues, yvalues []float6 Name: name, Style: Style{ Show: true, - StrokeColor: chart.GetDefaultSeriesStrokeColor(0), + StrokeColor: GetDefaultSeriesStrokeColor(0), }, - Annotations: []chart.Annotation{ + Annotations: []Annotation{ Annotation{ X: xvalues[len(xvalues)-1], Y: yvalues[len(yvalues)-1], @@ -31,11 +29,11 @@ func CreateTimeSeriesLastValueLabel(name string, xvalues []time.Time, yvalues [] Name: name, Style: Style{ Show: true, - StrokeColor: chart.GetDefaultSeriesStrokeColor(0), + StrokeColor: GetDefaultSeriesStrokeColor(0), }, - Annotations: []chart.Annotation{ + Annotations: []Annotation{ Annotation{ - X: xvalues[len(xvalues)-1], + X: float64(xvalues[len(xvalues)-1].Unix()), Y: yvalues[len(yvalues)-1], Label: valueFormatter(yvalues[len(yvalues)-1]), }, diff --git a/annotation_series_test.go b/annotation_series_test.go new file mode 100644 index 0000000..b96af48 --- /dev/null +++ b/annotation_series_test.go @@ -0,0 +1,156 @@ +package chart + +import ( + "image/color" + "testing" + "time" + + "github.com/blendlabs/go-assert" + "github.com/wcharczuk/go-chart/drawing" +) + +func TestCreateContinuousSeriesLastValueLabel(t *testing.T) { + assert := assert.New(t) + + as := CreateContinuousSeriesLastValueLabel("test", []float64{1, 2, 3}, []float64{4, 5, 6}, FloatValueFormatter) + assert.Equal("test", as.GetName()) + assert.False(as.GetStyle().IsZero()) + assert.True(as.GetStyle().Show) + assert.NotEmpty(as.Annotations) + assert.Equal(3.0, as.Annotations[0].X) + assert.Equal(6.0, as.Annotations[0].Y) + assert.Equal("6.00", as.Annotations[0].Label) +} + +func TestCreateTimeSeriesLastValueLabel(t *testing.T) { + assert := assert.New(t) + + d := time.Now().AddDate(0, 0, -1) + + as := CreateTimeSeriesLastValueLabel("test", []time.Time{ + time.Now().AddDate(0, 0, -3), + time.Now().AddDate(0, 0, -2), + d, + }, []float64{4, 5, 6}, FloatValueFormatter) + assert.Equal("test", as.GetName()) + assert.False(as.GetStyle().IsZero()) + assert.True(as.GetStyle().Show) + assert.NotEmpty(as.Annotations) + assert.Equal(float64(d.Unix()), as.Annotations[0].X) + assert.Equal(6.0, as.Annotations[0].Y) + assert.Equal("6.00", as.Annotations[0].Label) +} + +func TestAnnotationSeriesMeasure(t *testing.T) { + assert := assert.New(t) + + as := AnnotationSeries{ + Style: Style{ + Show: true, + }, + Annotations: []Annotation{ + Annotation{X: 1.0, Y: 1.0, Label: "1.0"}, + Annotation{X: 2.0, Y: 2.0, Label: "2.0"}, + Annotation{X: 3.0, Y: 3.0, Label: "3.0"}, + Annotation{X: 4.0, Y: 4.0, Label: "4.0"}, + }, + } + + r, err := PNG(110, 110) + assert.Nil(err) + + f, err := GetDefaultFont() + assert.Nil(err) + + xrange := Range{ + Min: 1.0, + Max: 4.0, + Domain: 100, + } + yrange := Range{ + Min: 1.0, + Max: 4.0, + Domain: 100, + } + + cb := Box{ + Top: 5, + Left: 5, + Right: 105, + Bottom: 105, + Height: 100, + Width: 100, + } + sd := Style{ + FontSize: 10.0, + Font: f, + } + + box := as.Measure(r, cb, xrange, yrange, sd) + assert.False(box.IsZero()) + assert.Equal(-5.0, box.Top) + assert.Equal(5.0, box.Left) + assert.Equal(139.0, box.Right) //the top,left annotation sticks up 5px and out ~44px. + assert.Equal(115.0, box.Bottom) +} + +func TestAnnotationSeriesRender(t *testing.T) { + assert := assert.New(t) + + as := AnnotationSeries{ + Style: Style{ + Show: true, + FillColor: drawing.ColorWhite, + StrokeColor: drawing.ColorBlack, + }, + Annotations: []Annotation{ + Annotation{X: 1.0, Y: 1.0, Label: "1.0"}, + Annotation{X: 2.0, Y: 2.0, Label: "2.0"}, + Annotation{X: 3.0, Y: 3.0, Label: "3.0"}, + Annotation{X: 4.0, Y: 4.0, Label: "4.0"}, + }, + } + + r, err := PNG(110, 110) + assert.Nil(err) + + f, err := GetDefaultFont() + assert.Nil(err) + + xrange := Range{ + Min: 1.0, + Max: 4.0, + Domain: 100, + } + yrange := Range{ + Min: 1.0, + Max: 4.0, + Domain: 100, + } + + cb := Box{ + Top: 5, + Left: 5, + Right: 105, + Bottom: 105, + Height: 100, + Width: 100, + } + sd := Style{ + FontSize: 10.0, + Font: f, + } + + as.Render(r, cb, xrange, yrange, sd) + + rr, isRaster := r.(*rasterRenderer) + assert.True(isRaster) + assert.NotNil(rr) + + c := rr.i.At(39, 70) + converted, isRGBA := color.RGBAModel.Convert(c).(color.RGBA) + assert.True(isRGBA) + assert.Equal(3, converted.R) + assert.Equal(3, converted.G) + assert.Equal(3, converted.B) +}