Debian Codegraph @

linear_series.go

raw symbols SCIP
1 package chart
2
3 import (
4 "fmt"
5 )
6
7 // Interface Assertions.
8 var (
9 _ Series = (*LinearSeries)(nil)
10 _ FirstValuesProvider = (*LinearSeries)(nil)
11 _ LastValuesProvider = (*LinearSeries)(nil)
12 )
13
14 // LinearSeries is a series that plots a line in a given domain.
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 // GetName returns the name of the time series.
30 func (ls LinearSeries) GetName() string {
31 return ls.Name
32 }
33
34 // GetStyle returns the line style.
35 func (ls LinearSeries) GetStyle() Style {
36 return ls.Style
37 }
38
39 // GetYAxis returns which YAxis the series draws on.
40 func (ls LinearSeries) GetYAxis() YAxisType {
41 return ls.YAxis
42 }
43
44 // Len returns the number of elements in the series.
45 func (ls LinearSeries) Len() int {
46 return len(ls.XValues)
47 }
48
49 // GetEndIndex returns the effective limit end.
50 func (ls LinearSeries) GetEndIndex() int {
51 return len(ls.XValues) - 1
52 }
53
54 // GetValues gets a value at a given index.
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 // GetFirstValues computes the first linear regression value.
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 // GetLastValues computes the last linear regression value.
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 // Render renders the series.
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 // Validate validates the series.
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 // IsZero returns if the linear series has computed coefficients or not.
105 func (ls LinearSeries) IsZero() bool {
106 return ls.m == 0 && ls.b == 0
107 }
108
109 // computeCoefficients computes the `m` and `b` terms in the linear formula given by `y = mx+b`.
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 }