| 1 |
package chart |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"time" |
| 6 |
) |
| 7 |
|
| 8 |
|
| 9 |
var ( |
| 10 |
_ Series = (*TimeSeries)(nil) |
| 11 |
_ FirstValuesProvider = (*TimeSeries)(nil) |
| 12 |
_ LastValuesProvider = (*TimeSeries)(nil) |
| 13 |
_ ValueFormatterProvider = (*TimeSeries)(nil) |
| 14 |
) |
| 15 |
|
| 16 |
|
| 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 |
|
| 28 |
func (ts TimeSeries) GetName() string { |
| 29 |
return ts.Name |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
func (ts TimeSeries) GetStyle() Style { |
| 34 |
return ts.Style |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
func (ts TimeSeries) Len() int { |
| 39 |
return len(ts.XValues) |
| 40 |
} |
| 41 |
|
| 42 |
|
| 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 |
|
| 50 |
func (ts TimeSeries) GetFirstValues() (x, y float64) { |
| 51 |
x = TimeToFloat64(ts.XValues[0]) |
| 52 |
y = ts.YValues[0] |
| 53 |
return |
| 54 |
} |
| 55 |
|
| 56 |
|
| 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 |
|
| 64 |
func (ts TimeSeries) GetValueFormatters() (x, y ValueFormatter) { |
| 65 |
x = TimeValueFormatter |
| 66 |
y = FloatValueFormatter |
| 67 |
return |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
func (ts TimeSeries) GetYAxis() YAxisType { |
| 72 |
return ts.YAxis |
| 73 |
} |
| 74 |
|
| 75 |
|
| 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 |
|
| 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 |
} |