diff --git a/moving_average_series.go b/moving_average_series.go index ad5535c..2fe075a 100644 --- a/moving_average_series.go +++ b/moving_average_series.go @@ -42,7 +42,7 @@ func (mas *MovingAverageSeries) GetValue(index int) (x float64, y float64) { if mas.InnerSeries == nil { return } - if mas.valueBuffer == nil { + if mas.valueBuffer == nil || index == 0 { mas.valueBuffer = NewRingBufferWithCapacity(mas.GetWindowSize()) } if mas.valueBuffer.Len() >= mas.GetWindowSize() { @@ -63,7 +63,7 @@ func (mas MovingAverageSeries) GetLastValue() (x float64, y float64) { } windowSize := mas.GetWindowSize() seriesLength := mas.InnerSeries.Len() - startAt := seriesLength - (windowSize + 1) + startAt := seriesLength - windowSize if startAt < 0 { startAt = 0 } diff --git a/moving_average_series_test.go b/moving_average_series_test.go index 2127439..9996bcd 100644 --- a/moving_average_series_test.go +++ b/moving_average_series_test.go @@ -52,7 +52,7 @@ func TestMovingAverageSeriesGetValue(t *testing.T) { assert.Equal(6.0, yvalues[8]) } -func TestMovingAverageSeriesGetLastValue(t *testing.T) { +func TestMovingAverageSeriesGetLastValueWindowOverlap(t *testing.T) { assert := assert.New(t) mockSeries := mockValueProvider{ @@ -63,10 +63,29 @@ func TestMovingAverageSeriesGetLastValue(t *testing.T) { mas := &MovingAverageSeries{ InnerSeries: mockSeries, - WindowSize: 10, + WindowSize: 15, } lx, ly := mas.GetLastValue() assert.Equal(10.0, lx) assert.Equal(5.5, ly) } + +func TestMovingAverageSeriesGetLastValue(t *testing.T) { + assert := assert.New(t) + + mockSeries := mockValueProvider{ + Seq(1.0, 100.0), + Seq(100, 1.0), + } + assert.Equal(100, mockSeries.Len()) + + mas := &MovingAverageSeries{ + InnerSeries: mockSeries, + WindowSize: 10, + } + + lx, ly := mas.GetLastValue() + assert.Equal(100.0, lx) + assert.Equal(5.5, ly) +} diff --git a/testserver/main.go b/testserver/main.go index 68bcae2..43856d3 100644 --- a/testserver/main.go +++ b/testserver/main.go @@ -45,6 +45,49 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult { }, } + s1lv := chart.AnnotationSeries{ + Name: fmt.Sprintf("Last Value"), + Style: chart.Style{ + Show: true, + StrokeColor: chart.GetDefaultSeriesStrokeColor(0), + }, + Annotations: []chart.Annotation{ + chart.Annotation{ + X: float64(s1x[len(s1x)-1].Unix()), + Y: s1y[len(s1y)-1], + Label: fmt.Sprintf("%s - %s", "test", chart.FloatValueFormatter(s1y[len(s1y)-1])), + }, + }, + } + + s1ma := &chart.MovingAverageSeries{ + Name: "Average", + Style: chart.Style{ + Show: true, + StrokeColor: drawing.ColorRed, + StrokeDashArray: []float64{5, 1, 1}, + }, + WindowSize: 10, + InnerSeries: s1, + } + + s1malx, s1maly := s1ma.GetLastValue() + + s1malv := chart.AnnotationSeries{ + Name: fmt.Sprintf("Last Value"), + Style: chart.Style{ + Show: true, + StrokeColor: drawing.ColorRed, + }, + Annotations: []chart.Annotation{ + chart.Annotation{ + X: s1malx, + Y: s1maly, + Label: fmt.Sprintf("%s - %s", "test", chart.FloatValueFormatter(s1maly)), + }, + }, + } + c := chart.Chart{ Title: "A Test Chart", TitleStyle: chart.Style{ @@ -76,30 +119,9 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult { }, Series: []chart.Series{ s1, - &chart.MovingAverageSeries{ - Name: "Average", - Style: chart.Style{ - Show: true, - StrokeColor: drawing.ColorRed, - StrokeDashArray: []float64{5, 1, 1}, - }, - WindowSize: 10, - InnerSeries: s1, - }, - chart.AnnotationSeries{ - Name: fmt.Sprintf("Last Value"), - Style: chart.Style{ - Show: true, - StrokeColor: chart.GetDefaultSeriesStrokeColor(0), - }, - Annotations: []chart.Annotation{ - chart.Annotation{ - X: float64(s1x[len(s1x)-1].Unix()), - Y: s1y[len(s1y)-1], - Label: fmt.Sprintf("%s - %s", "test", chart.FloatValueFormatter(s1y[len(s1y)-1])), - }, - }, - }, + s1ma, + s1lv, + s1malv, }, }