small ma fixes
This commit is contained in:
parent
6d4dfd9e48
commit
cedf89e17c
3 changed files with 69 additions and 28 deletions
|
@ -42,7 +42,7 @@ func (mas *MovingAverageSeries) GetValue(index int) (x float64, y float64) {
|
||||||
if mas.InnerSeries == nil {
|
if mas.InnerSeries == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if mas.valueBuffer == nil {
|
if mas.valueBuffer == nil || index == 0 {
|
||||||
mas.valueBuffer = NewRingBufferWithCapacity(mas.GetWindowSize())
|
mas.valueBuffer = NewRingBufferWithCapacity(mas.GetWindowSize())
|
||||||
}
|
}
|
||||||
if mas.valueBuffer.Len() >= mas.GetWindowSize() {
|
if mas.valueBuffer.Len() >= mas.GetWindowSize() {
|
||||||
|
@ -63,7 +63,7 @@ func (mas MovingAverageSeries) GetLastValue() (x float64, y float64) {
|
||||||
}
|
}
|
||||||
windowSize := mas.GetWindowSize()
|
windowSize := mas.GetWindowSize()
|
||||||
seriesLength := mas.InnerSeries.Len()
|
seriesLength := mas.InnerSeries.Len()
|
||||||
startAt := seriesLength - (windowSize + 1)
|
startAt := seriesLength - windowSize
|
||||||
if startAt < 0 {
|
if startAt < 0 {
|
||||||
startAt = 0
|
startAt = 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,7 +52,7 @@ func TestMovingAverageSeriesGetValue(t *testing.T) {
|
||||||
assert.Equal(6.0, yvalues[8])
|
assert.Equal(6.0, yvalues[8])
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMovingAverageSeriesGetLastValue(t *testing.T) {
|
func TestMovingAverageSeriesGetLastValueWindowOverlap(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
mockSeries := mockValueProvider{
|
mockSeries := mockValueProvider{
|
||||||
|
@ -63,10 +63,29 @@ func TestMovingAverageSeriesGetLastValue(t *testing.T) {
|
||||||
|
|
||||||
mas := &MovingAverageSeries{
|
mas := &MovingAverageSeries{
|
||||||
InnerSeries: mockSeries,
|
InnerSeries: mockSeries,
|
||||||
WindowSize: 10,
|
WindowSize: 15,
|
||||||
}
|
}
|
||||||
|
|
||||||
lx, ly := mas.GetLastValue()
|
lx, ly := mas.GetLastValue()
|
||||||
assert.Equal(10.0, lx)
|
assert.Equal(10.0, lx)
|
||||||
assert.Equal(5.5, ly)
|
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{
|
c := chart.Chart{
|
||||||
Title: "A Test Chart",
|
Title: "A Test Chart",
|
||||||
TitleStyle: chart.Style{
|
TitleStyle: chart.Style{
|
||||||
|
@ -76,30 +119,9 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult {
|
||||||
},
|
},
|
||||||
Series: []chart.Series{
|
Series: []chart.Series{
|
||||||
s1,
|
s1,
|
||||||
&chart.MovingAverageSeries{
|
s1ma,
|
||||||
Name: "Average",
|
s1lv,
|
||||||
Style: chart.Style{
|
s1malv,
|
||||||
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])),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue