2016-07-17 14:10:04 -04:00
|
|
|
package chart
|
|
|
|
|
2017-05-12 20:12:23 -04:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
util "github.com/wcharczuk/go-chart/util"
|
|
|
|
)
|
2017-02-03 14:26:53 -05:00
|
|
|
|
2016-07-17 14:10:04 -04:00
|
|
|
const (
|
|
|
|
// DefaultSimpleMovingAveragePeriod is the default number of values to average.
|
|
|
|
DefaultSimpleMovingAveragePeriod = 16
|
|
|
|
)
|
|
|
|
|
|
|
|
// SMASeries is a computed series.
|
|
|
|
type SMASeries struct {
|
|
|
|
Name string
|
|
|
|
Style Style
|
2016-07-31 19:54:09 -04:00
|
|
|
YAxis YAxisType
|
2016-07-17 14:10:04 -04:00
|
|
|
|
|
|
|
Period int
|
2017-05-12 20:12:23 -04:00
|
|
|
InnerSeries ValuesProvider
|
2016-07-17 14:10:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetName returns the name of the time series.
|
|
|
|
func (sma SMASeries) GetName() string {
|
|
|
|
return sma.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStyle returns the line style.
|
|
|
|
func (sma SMASeries) GetStyle() Style {
|
|
|
|
return sma.Style
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetYAxis returns which YAxis the series draws on.
|
2016-07-31 19:54:09 -04:00
|
|
|
func (sma SMASeries) GetYAxis() YAxisType {
|
2016-07-17 14:10:04 -04:00
|
|
|
return sma.YAxis
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the number of elements in the series.
|
|
|
|
func (sma SMASeries) Len() int {
|
|
|
|
return sma.InnerSeries.Len()
|
|
|
|
}
|
|
|
|
|
2016-07-27 15:34:15 -04:00
|
|
|
// GetPeriod returns the window size.
|
|
|
|
func (sma SMASeries) GetPeriod(defaults ...int) int {
|
|
|
|
if sma.Period == 0 {
|
|
|
|
if len(defaults) > 0 {
|
|
|
|
return defaults[0]
|
|
|
|
}
|
|
|
|
return DefaultSimpleMovingAveragePeriod
|
|
|
|
}
|
|
|
|
return sma.Period
|
|
|
|
}
|
|
|
|
|
2017-05-12 20:12:23 -04:00
|
|
|
// GetValues gets a value at a given index.
|
|
|
|
func (sma SMASeries) GetValues(index int) (x, y float64) {
|
2017-03-05 19:54:40 -05:00
|
|
|
if sma.InnerSeries == nil || sma.InnerSeries.Len() == 0 {
|
2016-07-17 14:10:04 -04:00
|
|
|
return
|
|
|
|
}
|
2017-05-12 20:12:23 -04:00
|
|
|
px, _ := sma.InnerSeries.GetValues(index)
|
2016-07-17 14:10:04 -04:00
|
|
|
x = px
|
|
|
|
y = sma.getAverage(index)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-12 20:12:23 -04:00
|
|
|
// GetLastValues computes the last moving average value but walking back window size samples,
|
2016-07-17 14:10:04 -04:00
|
|
|
// and recomputing the last moving average chunk.
|
2017-05-12 20:12:23 -04:00
|
|
|
func (sma SMASeries) GetLastValues() (x, y float64) {
|
2017-03-05 19:54:40 -05:00
|
|
|
if sma.InnerSeries == nil || sma.InnerSeries.Len() == 0 {
|
2016-07-17 14:10:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
seriesLen := sma.InnerSeries.Len()
|
2017-05-12 20:12:23 -04:00
|
|
|
px, _ := sma.InnerSeries.GetValues(seriesLen - 1)
|
2016-07-17 14:10:04 -04:00
|
|
|
x = px
|
|
|
|
y = sma.getAverage(seriesLen - 1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sma SMASeries) getAverage(index int) float64 {
|
|
|
|
period := sma.GetPeriod()
|
2017-05-12 20:12:23 -04:00
|
|
|
floor := util.Math.MaxInt(0, index-period)
|
2016-07-17 14:10:04 -04:00
|
|
|
var accum float64
|
|
|
|
var count float64
|
|
|
|
for x := index; x >= floor; x-- {
|
2017-05-12 20:12:23 -04:00
|
|
|
_, vy := sma.InnerSeries.GetValues(x)
|
2016-07-17 14:10:04 -04:00
|
|
|
accum += vy
|
|
|
|
count += 1.0
|
|
|
|
}
|
|
|
|
return accum / count
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render renders the series.
|
2016-07-17 16:29:01 -04:00
|
|
|
func (sma SMASeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
|
2016-07-21 17:11:27 -04:00
|
|
|
style := sma.Style.InheritFrom(defaults)
|
2016-07-29 19:36:29 -04:00
|
|
|
Draw.LineSeries(r, canvasBox, xrange, yrange, style, sma)
|
2016-07-17 14:10:04 -04:00
|
|
|
}
|
2017-02-03 14:26:53 -05:00
|
|
|
|
|
|
|
// Validate validates the series.
|
|
|
|
func (sma SMASeries) Validate() error {
|
|
|
|
if sma.InnerSeries == nil {
|
|
|
|
return fmt.Errorf("sma series requires InnerSeries to be set")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|