Debian Codegraph @

sma_series.go

raw symbols SCIP
1 package chart
2
3 import (
4 "fmt"
5 )
6
7 const (
8 // DefaultSimpleMovingAveragePeriod is the default number of values to average.
9 DefaultSimpleMovingAveragePeriod = 16
10 )
11
12 // Interface Assertions.
13 var (
14 _ Series = (*SMASeries)(nil)
15 _ FirstValuesProvider = (*SMASeries)(nil)
16 _ LastValuesProvider = (*SMASeries)(nil)
17 )
18
19 // SMASeries is a computed series.
20 type SMASeries struct {
21 Name string
22 Style Style
23 YAxis YAxisType
24
25 Period int
26 InnerSeries ValuesProvider
27 }
28
29 // GetName returns the name of the time series.
30 func (sma SMASeries) GetName() string {
31 return sma.Name
32 }
33
34 // GetStyle returns the line style.
35 func (sma SMASeries) GetStyle() Style {
36 return sma.Style
37 }
38
39 // GetYAxis returns which YAxis the series draws on.
40 func (sma SMASeries) GetYAxis() YAxisType {
41 return sma.YAxis
42 }
43
44 // Len returns the number of elements in the series.
45 func (sma SMASeries) Len() int {
46 return sma.InnerSeries.Len()
47 }
48
49 // GetPeriod returns the window size.
50 func (sma SMASeries) GetPeriod(defaults ...int) int {
51 if sma.Period == 0 {
52 if len(defaults) > 0 {
53 return defaults[0]
54 }
55 return DefaultSimpleMovingAveragePeriod
56 }
57 return sma.Period
58 }
59
60 // GetValues gets a value at a given index.
61 func (sma SMASeries) GetValues(index int) (x, y float64) {
62 if sma.InnerSeries == nil || sma.InnerSeries.Len() == 0 {
63 return
64 }
65 px, _ := sma.InnerSeries.GetValues(index)
66 x = px
67 y = sma.getAverage(index)
68 return
69 }
70
71 // GetFirstValues computes the first moving average value.
72 func (sma SMASeries) GetFirstValues() (x, y float64) {
73 if sma.InnerSeries == nil || sma.InnerSeries.Len() == 0 {
74 return
75 }
76 px, _ := sma.InnerSeries.GetValues(0)
77 x = px
78 y = sma.getAverage(0)
79 return
80 }
81
82 // GetLastValues computes the last moving average value but walking back window size samples,
83 // and recomputing the last moving average chunk.
84 func (sma SMASeries) GetLastValues() (x, y float64) {
85 if sma.InnerSeries == nil || sma.InnerSeries.Len() == 0 {
86 return
87 }
88 seriesLen := sma.InnerSeries.Len()
89 px, _ := sma.InnerSeries.GetValues(seriesLen - 1)
90 x = px
91 y = sma.getAverage(seriesLen - 1)
92 return
93 }
94
95 func (sma SMASeries) getAverage(index int) float64 {
96 period := sma.GetPeriod()
97 floor := MaxInt(0, index-period)
98 var accum float64
99 var count float64
100 for x := index; x >= floor; x-- {
101 _, vy := sma.InnerSeries.GetValues(x)
102 accum += vy
103 count += 1.0
104 }
105 return accum / count
106 }
107
108 // Render renders the series.
109 func (sma SMASeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
110 style := sma.Style.InheritFrom(defaults)
111 Draw.LineSeries(r, canvasBox, xrange, yrange, style, sma)
112 }
113
114 // Validate validates the series.
115 func (sma SMASeries) Validate() error {
116 if sma.InnerSeries == nil {
117 return fmt.Errorf("sma series requires InnerSeries to be set")
118 }
119 return nil
120 }