Debian Codegraph @

continuous_series.go

raw symbols SCIP
1 package chart
2
3 import "fmt"
4
5 // Interface Assertions.
6 var (
7 _ Series = (*ContinuousSeries)(nil)
8 _ FirstValuesProvider = (*ContinuousSeries)(nil)
9 _ LastValuesProvider = (*ContinuousSeries)(nil)
10 )
11
12 // ContinuousSeries represents a line on a chart.
13 type ContinuousSeries struct {
14 Name string
15 Style Style
16
17 YAxis YAxisType
18
19 XValueFormatter ValueFormatter
20 YValueFormatter ValueFormatter
21
22 XValues []float64
23 YValues []float64
24 }
25
26 // GetName returns the name of the time series.
27 func (cs ContinuousSeries) GetName() string {
28 return cs.Name
29 }
30
31 // GetStyle returns the line style.
32 func (cs ContinuousSeries) GetStyle() Style {
33 return cs.Style
34 }
35
36 // Len returns the number of elements in the series.
37 func (cs ContinuousSeries) Len() int {
38 return len(cs.XValues)
39 }
40
41 // GetValues gets the x,y values at a given index.
42 func (cs ContinuousSeries) GetValues(index int) (float64, float64) {
43 return cs.XValues[index], cs.YValues[index]
44 }
45
46 // GetFirstValues gets the first x,y values.
47 func (cs ContinuousSeries) GetFirstValues() (float64, float64) {
48 return cs.XValues[0], cs.YValues[0]
49 }
50
51 // GetLastValues gets the last x,y values.
52 func (cs ContinuousSeries) GetLastValues() (float64, float64) {
53 return cs.XValues[len(cs.XValues)-1], cs.YValues[len(cs.YValues)-1]
54 }
55
56 // GetValueFormatters returns value formatter defaults for the series.
57 func (cs ContinuousSeries) GetValueFormatters() (x, y ValueFormatter) {
58 if cs.XValueFormatter != nil {
59 x = cs.XValueFormatter
60 } else {
61 x = FloatValueFormatter
62 }
63 if cs.YValueFormatter != nil {
64 y = cs.YValueFormatter
65 } else {
66 y = FloatValueFormatter
67 }
68 return
69 }
70
71 // GetYAxis returns which YAxis the series draws on.
72 func (cs ContinuousSeries) GetYAxis() YAxisType {
73 return cs.YAxis
74 }
75
76 // Render renders the series.
77 func (cs ContinuousSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
78 style := cs.Style.InheritFrom(defaults)
79 Draw.LineSeries(r, canvasBox, xrange, yrange, style, cs)
80 }
81
82 // Validate validates the series.
83 func (cs ContinuousSeries) Validate() error {
84 if len(cs.XValues) == 0 {
85 return fmt.Errorf("continuous series; must have xvalues set")
86 }
87
88 if len(cs.YValues) == 0 {
89 return fmt.Errorf("continuous series; must have yvalues set")
90 }
91
92 if len(cs.XValues) != len(cs.YValues) {
93 return fmt.Errorf("continuous series; must have same length xvalues as yvalues")
94 }
95 return nil
96 }