Helper API refactor (#40)
* api cleaup * updates * wtf * updates * snapshot. * tweaks * snapshot * api tweaks. * updates * updates * updates * changes. * updates * updates * sequence => seq * dont need to use curl, just using wget * fixing examples
This commit is contained in:
parent
43212f871f
commit
03708a90ef
100 changed files with 1687 additions and 1055 deletions
|
|
@ -1,6 +1,10 @@
|
|||
package chart
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
util "github.com/wcharczuk/go-chart/util"
|
||||
)
|
||||
|
||||
const (
|
||||
// DefaultSimpleMovingAveragePeriod is the default number of values to average.
|
||||
|
|
@ -14,7 +18,7 @@ type SMASeries struct {
|
|||
YAxis YAxisType
|
||||
|
||||
Period int
|
||||
InnerSeries ValueProvider
|
||||
InnerSeries ValuesProvider
|
||||
}
|
||||
|
||||
// GetName returns the name of the time series.
|
||||
|
|
@ -48,25 +52,25 @@ func (sma SMASeries) GetPeriod(defaults ...int) int {
|
|||
return sma.Period
|
||||
}
|
||||
|
||||
// GetValue gets a value at a given index.
|
||||
func (sma SMASeries) GetValue(index int) (x, y float64) {
|
||||
// GetValues gets a value at a given index.
|
||||
func (sma SMASeries) GetValues(index int) (x, y float64) {
|
||||
if sma.InnerSeries == nil || sma.InnerSeries.Len() == 0 {
|
||||
return
|
||||
}
|
||||
px, _ := sma.InnerSeries.GetValue(index)
|
||||
px, _ := sma.InnerSeries.GetValues(index)
|
||||
x = px
|
||||
y = sma.getAverage(index)
|
||||
return
|
||||
}
|
||||
|
||||
// GetLastValue computes the last moving average value but walking back window size samples,
|
||||
// GetLastValues computes the last moving average value but walking back window size samples,
|
||||
// and recomputing the last moving average chunk.
|
||||
func (sma SMASeries) GetLastValue() (x, y float64) {
|
||||
func (sma SMASeries) GetLastValues() (x, y float64) {
|
||||
if sma.InnerSeries == nil || sma.InnerSeries.Len() == 0 {
|
||||
return
|
||||
}
|
||||
seriesLen := sma.InnerSeries.Len()
|
||||
px, _ := sma.InnerSeries.GetValue(seriesLen - 1)
|
||||
px, _ := sma.InnerSeries.GetValues(seriesLen - 1)
|
||||
x = px
|
||||
y = sma.getAverage(seriesLen - 1)
|
||||
return
|
||||
|
|
@ -74,11 +78,11 @@ func (sma SMASeries) GetLastValue() (x, y float64) {
|
|||
|
||||
func (sma SMASeries) getAverage(index int) float64 {
|
||||
period := sma.GetPeriod()
|
||||
floor := Math.MaxInt(0, index-period)
|
||||
floor := util.Math.MaxInt(0, index-period)
|
||||
var accum float64
|
||||
var count float64
|
||||
for x := index; x >= floor; x-- {
|
||||
_, vy := sma.InnerSeries.GetValue(x)
|
||||
_, vy := sma.InnerSeries.GetValues(x)
|
||||
accum += vy
|
||||
count += 1.0
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue