Debian Codegraph @

time_series.go

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