forked from wcharczuk/go-chart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
percent_change_series.go
89 lines (76 loc) · 2.38 KB
/
percent_change_series.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package chart
// Interface Assertions.
var (
_ Series = (*PercentChangeSeries)(nil)
_ FirstValuesProvider = (*PercentChangeSeries)(nil)
_ LastValuesProvider = (*PercentChangeSeries)(nil)
_ ValueFormatterProvider = (*PercentChangeSeries)(nil)
)
// PercentChangeSeriesSource is a series that
// can be used with a PercentChangeSeries
type PercentChangeSeriesSource interface {
Series
FirstValuesProvider
LastValuesProvider
ValuesProvider
ValueFormatterProvider
}
// PercentChangeSeries applies a
// percentage difference function to a given continuous series.
type PercentChangeSeries struct {
Name string
Style Style
YAxis YAxisType
InnerSeries PercentChangeSeriesSource
}
// GetName returns the name of the time series.
func (pcs PercentChangeSeries) GetName() string {
return pcs.Name
}
// GetStyle returns the line style.
func (pcs PercentChangeSeries) GetStyle() Style {
return pcs.Style
}
// Len implements part of Series.
func (pcs PercentChangeSeries) Len() int {
return pcs.InnerSeries.Len()
}
// GetFirstValues implements FirstValuesProvider.
func (pcs PercentChangeSeries) GetFirstValues() (x, y float64) {
return pcs.InnerSeries.GetFirstValues()
}
// GetValues gets x, y values at a given index.
func (pcs PercentChangeSeries) GetValues(index int) (x, y float64) {
_, fy := pcs.InnerSeries.GetFirstValues()
x0, y0 := pcs.InnerSeries.GetValues(index)
x = x0
y = PercentDifference(fy, y0)
return
}
// GetValueFormatters returns value formatter defaults for the series.
func (pcs PercentChangeSeries) GetValueFormatters() (x, y ValueFormatter) {
x, _ = pcs.InnerSeries.GetValueFormatters()
y = PercentValueFormatter
return
}
// GetYAxis returns which YAxis the series draws on.
func (pcs PercentChangeSeries) GetYAxis() YAxisType {
return pcs.YAxis
}
// GetLastValues gets the last values.
func (pcs PercentChangeSeries) GetLastValues() (x, y float64) {
_, fy := pcs.InnerSeries.GetFirstValues()
x0, y0 := pcs.InnerSeries.GetLastValues()
x = x0
y = PercentDifference(fy, y0)
return
}
// Render renders the series.
func (pcs PercentChangeSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
style := pcs.Style.InheritFrom(defaults)
Draw.LineSeries(r, canvasBox, xrange, yrange, style, pcs)
}
// Validate validates the series.
func (pcs PercentChangeSeries) Validate() error {
return pcs.InnerSeries.Validate()
}