small ma fixes
This commit is contained in:
parent
6d4dfd9e48
commit
cedf89e17c
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user