2016-07-14 12:27:23 -04:00
|
|
|
package chart
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DefaultMovingAverageWindowSize is the default number of values to average.
|
|
|
|
DefaultMovingAverageWindowSize = 5
|
|
|
|
)
|
|
|
|
|
|
|
|
// MovingAverageSeries is a computed series.
|
|
|
|
type MovingAverageSeries struct {
|
|
|
|
Name string
|
|
|
|
Style Style
|
|
|
|
YAxis YAxisType
|
|
|
|
|
|
|
|
WindowSize int
|
|
|
|
InnerSeries ValueProvider
|
2016-07-14 13:59:17 -04:00
|
|
|
|
2016-07-14 12:27:23 -04:00
|
|
|
valueBuffer *RingBuffer
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetName returns the name of the time series.
|
|
|
|
func (mas MovingAverageSeries) GetName() string {
|
|
|
|
return mas.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStyle returns the line style.
|
|
|
|
func (mas MovingAverageSeries) GetStyle() Style {
|
|
|
|
return mas.Style
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetYAxis returns which YAxis the series draws on.
|
|
|
|
func (mas MovingAverageSeries) GetYAxis() YAxisType {
|
|
|
|
return mas.YAxis
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the number of elements in the series.
|
|
|
|
func (mas *MovingAverageSeries) Len() int {
|
|
|
|
return mas.InnerSeries.Len()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetValue gets a value at a given index.
|
|
|
|
func (mas *MovingAverageSeries) GetValue(index int) (x float64, y float64) {
|
2016-07-14 15:07:51 -04:00
|
|
|
if mas.InnerSeries == nil {
|
|
|
|
return
|
|
|
|
}
|
2016-07-14 16:11:03 -04:00
|
|
|
if mas.valueBuffer == nil || index == 0 {
|
2016-07-14 12:27:23 -04:00
|
|
|
mas.valueBuffer = NewRingBufferWithCapacity(mas.GetWindowSize())
|
|
|
|
}
|
|
|
|
if mas.valueBuffer.Len() >= mas.GetWindowSize() {
|
|
|
|
mas.valueBuffer.Dequeue()
|
|
|
|
}
|
2016-07-14 13:59:17 -04:00
|
|
|
px, py := mas.InnerSeries.GetValue(index)
|
|
|
|
mas.valueBuffer.Enqueue(py)
|
|
|
|
x = px
|
2016-07-14 15:07:51 -04:00
|
|
|
y = mas.getAverage(mas.valueBuffer)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLastValue computes the last moving average value but walking back window size samples,
|
|
|
|
// and recomputing the last moving average chunk.
|
|
|
|
func (mas MovingAverageSeries) GetLastValue() (x float64, y float64) {
|
|
|
|
if mas.InnerSeries == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
windowSize := mas.GetWindowSize()
|
|
|
|
seriesLength := mas.InnerSeries.Len()
|
2016-07-14 16:11:03 -04:00
|
|
|
startAt := seriesLength - windowSize
|
2016-07-14 15:34:28 -04:00
|
|
|
if startAt < 0 {
|
2016-07-14 15:07:51 -04:00
|
|
|
startAt = 0
|
|
|
|
}
|
|
|
|
vb := NewRingBufferWithCapacity(windowSize)
|
|
|
|
for index := startAt; index < seriesLength; index++ {
|
|
|
|
xn, yn := mas.InnerSeries.GetValue(index)
|
|
|
|
vb.Enqueue(yn)
|
|
|
|
x = xn
|
|
|
|
}
|
|
|
|
y = mas.getAverage(vb)
|
2016-07-14 12:27:23 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWindowSize returns the window size.
|
|
|
|
func (mas MovingAverageSeries) GetWindowSize(defaults ...int) int {
|
|
|
|
if mas.WindowSize == 0 {
|
|
|
|
if len(defaults) > 0 {
|
|
|
|
return defaults[0]
|
|
|
|
}
|
|
|
|
return DefaultMovingAverageWindowSize
|
|
|
|
}
|
|
|
|
return mas.WindowSize
|
|
|
|
}
|
|
|
|
|
2016-07-14 15:07:51 -04:00
|
|
|
func (mas MovingAverageSeries) getAverage(valueBuffer *RingBuffer) float64 {
|
2016-07-14 12:27:23 -04:00
|
|
|
var accum float64
|
2016-07-14 15:07:51 -04:00
|
|
|
valueBuffer.Each(func(v interface{}) {
|
2016-07-14 12:27:23 -04:00
|
|
|
if typed, isTyped := v.(float64); isTyped {
|
|
|
|
accum += typed
|
|
|
|
}
|
|
|
|
})
|
2016-07-14 15:34:28 -04:00
|
|
|
return accum / float64(valueBuffer.Len())
|
2016-07-14 12:27:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Render renders the series.
|
|
|
|
func (mas *MovingAverageSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
|
|
|
|
style := mas.Style.WithDefaultsFrom(defaults)
|
|
|
|
DrawLineSeries(r, canvasBox, xrange, yrange, style, mas)
|
|
|
|
}
|