| 1 |
package chart |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
) |
| 6 |
|
| 7 |
|
| 8 |
var ( |
| 9 |
_ Series = (*LinearSeries)(nil) |
| 10 |
_ FirstValuesProvider = (*LinearSeries)(nil) |
| 11 |
_ LastValuesProvider = (*LinearSeries)(nil) |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
type LinearSeries struct { |
| 16 |
Name string |
| 17 |
Style Style |
| 18 |
YAxis YAxisType |
| 19 |
|
| 20 |
XValues []float64 |
| 21 |
InnerSeries LinearCoefficientProvider |
| 22 |
|
| 23 |
m float64 |
| 24 |
b float64 |
| 25 |
stdev float64 |
| 26 |
avg float64 |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
func (ls LinearSeries) GetName() string { |
| 31 |
return ls.Name |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
func (ls LinearSeries) GetStyle() Style { |
| 36 |
return ls.Style |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
func (ls LinearSeries) GetYAxis() YAxisType { |
| 41 |
return ls.YAxis |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
func (ls LinearSeries) Len() int { |
| 46 |
return len(ls.XValues) |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
func (ls LinearSeries) GetEndIndex() int { |
| 51 |
return len(ls.XValues) - 1 |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
func (ls *LinearSeries) GetValues(index int) (x, y float64) { |
| 56 |
if ls.InnerSeries == nil || len(ls.XValues) == 0 { |
| 57 |
return |
| 58 |
} |
| 59 |
if ls.IsZero() { |
| 60 |
ls.computeCoefficients() |
| 61 |
} |
| 62 |
x = ls.XValues[index] |
| 63 |
y = (ls.m * ls.normalize(x)) + ls.b |
| 64 |
return |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
func (ls *LinearSeries) GetFirstValues() (x, y float64) { |
| 69 |
if ls.InnerSeries == nil || len(ls.XValues) == 0 { |
| 70 |
return |
| 71 |
} |
| 72 |
if ls.IsZero() { |
| 73 |
ls.computeCoefficients() |
| 74 |
} |
| 75 |
x, y = ls.GetValues(0) |
| 76 |
return |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
func (ls *LinearSeries) GetLastValues() (x, y float64) { |
| 81 |
if ls.InnerSeries == nil || len(ls.XValues) == 0 { |
| 82 |
return |
| 83 |
} |
| 84 |
if ls.IsZero() { |
| 85 |
ls.computeCoefficients() |
| 86 |
} |
| 87 |
x, y = ls.GetValues(ls.GetEndIndex()) |
| 88 |
return |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
func (ls *LinearSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) { |
| 93 |
Draw.LineSeries(r, canvasBox, xrange, yrange, ls.Style.InheritFrom(defaults), ls) |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
func (ls LinearSeries) Validate() error { |
| 98 |
if ls.InnerSeries == nil { |
| 99 |
return fmt.Errorf("linear regression series requires InnerSeries to be set") |
| 100 |
} |
| 101 |
return nil |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
func (ls LinearSeries) IsZero() bool { |
| 106 |
return ls.m == 0 && ls.b == 0 |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
func (ls *LinearSeries) computeCoefficients() { |
| 111 |
ls.m, ls.b, ls.stdev, ls.avg = ls.InnerSeries.Coefficients() |
| 112 |
} |
| 113 |
|
| 114 |
func (ls *LinearSeries) normalize(xvalue float64) float64 { |
| 115 |
if ls.avg > 0 && ls.stdev > 0 { |
| 116 |
return (xvalue - ls.avg) / ls.stdev |
| 117 |
} |
| 118 |
return xvalue |
| 119 |
} |