Debian Codegraph @

min_max_series.go

raw symbols SCIP
1 package chart
2
3 import (
4 "fmt"
5 "math"
6 )
7
8 // MinSeries draws a horizontal line at the minimum value of the inner series.
9 type MinSeries struct {
10 Name string
11 Style Style
12 YAxis YAxisType
13 InnerSeries ValuesProvider
14
15 minValue *float64
16 }
17
18 // GetName returns the name of the time series.
19 func (ms MinSeries) GetName() string {
20 return ms.Name
21 }
22
23 // GetStyle returns the line style.
24 func (ms MinSeries) GetStyle() Style {
25 return ms.Style
26 }
27
28 // GetYAxis returns which YAxis the series draws on.
29 func (ms MinSeries) GetYAxis() YAxisType {
30 return ms.YAxis
31 }
32
33 // Len returns the number of elements in the series.
34 func (ms MinSeries) Len() int {
35 return ms.InnerSeries.Len()
36 }
37
38 // GetValues gets a value at a given index.
39 func (ms *MinSeries) GetValues(index int) (x, y float64) {
40 ms.ensureMinValue()
41 x, _ = ms.InnerSeries.GetValues(index)
42 y = *ms.minValue
43 return
44 }
45
46 // Render renders the series.
47 func (ms *MinSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
48 style := ms.Style.InheritFrom(defaults)
49 Draw.LineSeries(r, canvasBox, xrange, yrange, style, ms)
50 }
51
52 func (ms *MinSeries) ensureMinValue() {
53 if ms.minValue == nil {
54 minValue := math.MaxFloat64
55 var y float64
56 for x := 0; x < ms.InnerSeries.Len(); x++ {
57 _, y = ms.InnerSeries.GetValues(x)
58 if y < minValue {
59 minValue = y
60 }
61 }
62 ms.minValue = &minValue
63 }
64 }
65
66 // Validate validates the series.
67 func (ms *MinSeries) Validate() error {
68 if ms.InnerSeries == nil {
69 return fmt.Errorf("min series requires InnerSeries to be set")
70 }
71 return nil
72 }
73
74 // MaxSeries draws a horizontal line at the maximum value of the inner series.
75 type MaxSeries struct {
76 Name string
77 Style Style
78 YAxis YAxisType
79 InnerSeries ValuesProvider
80
81 maxValue *float64
82 }
83
84 // GetName returns the name of the time series.
85 func (ms MaxSeries) GetName() string {
86 return ms.Name
87 }
88
89 // GetStyle returns the line style.
90 func (ms MaxSeries) GetStyle() Style {
91 return ms.Style
92 }
93
94 // GetYAxis returns which YAxis the series draws on.
95 func (ms MaxSeries) GetYAxis() YAxisType {
96 return ms.YAxis
97 }
98
99 // Len returns the number of elements in the series.
100 func (ms MaxSeries) Len() int {
101 return ms.InnerSeries.Len()
102 }
103
104 // GetValues gets a value at a given index.
105 func (ms *MaxSeries) GetValues(index int) (x, y float64) {
106 ms.ensureMaxValue()
107 x, _ = ms.InnerSeries.GetValues(index)
108 y = *ms.maxValue
109 return
110 }
111
112 // Render renders the series.
113 func (ms *MaxSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
114 style := ms.Style.InheritFrom(defaults)
115 Draw.LineSeries(r, canvasBox, xrange, yrange, style, ms)
116 }
117
118 func (ms *MaxSeries) ensureMaxValue() {
119 if ms.maxValue == nil {
120 maxValue := -math.MaxFloat64
121 var y float64
122 for x := 0; x < ms.InnerSeries.Len(); x++ {
123 _, y = ms.InnerSeries.GetValues(x)
124 if y > maxValue {
125 maxValue = y
126 }
127 }
128 ms.maxValue = &maxValue
129 }
130 }
131
132 // Validate validates the series.
133 func (ms *MaxSeries) Validate() error {
134 if ms.InnerSeries == nil {
135 return fmt.Errorf("max series requires InnerSeries to be set")
136 }
137 return nil
138 }