go-chart/time_series.go

94 lines
2.1 KiB
Go
Raw Permalink Normal View History

2016-07-10 05:14:11 +02:00
package chart
2017-02-03 20:26:53 +01:00
import (
"fmt"
"time"
2018-12-06 16:38:58 +01:00
util "git.gutmet.org/go-chart.git/util"
2017-02-03 20:26:53 +01:00
)
2016-07-10 05:14:11 +02:00
2018-09-07 21:52:30 +02:00
// Interface Assertions.
var (
_ Series = (*TimeSeries)(nil)
_ FirstValuesProvider = (*TimeSeries)(nil)
_ LastValuesProvider = (*TimeSeries)(nil)
_ ValueFormatterProvider = (*TimeSeries)(nil)
)
2016-07-10 05:14:11 +02:00
// TimeSeries is a line on a chart.
type TimeSeries struct {
2016-07-10 10:11:47 +02:00
Name string
Style Style
2016-08-01 01:54:09 +02:00
YAxis YAxisType
2016-07-10 05:14:11 +02:00
XValues []time.Time
YValues []float64
}
// GetName returns the name of the time series.
func (ts TimeSeries) GetName() string {
return ts.Name
}
// GetStyle returns the line style.
func (ts TimeSeries) GetStyle() Style {
return ts.Style
}
// Len returns the number of elements in the series.
func (ts TimeSeries) Len() int {
return len(ts.XValues)
}
2018-09-07 21:52:30 +02:00
// GetValues gets x, y values at a given index.
func (ts TimeSeries) GetValues(index int) (x, y float64) {
x = util.Time.ToFloat64(ts.XValues[index])
2016-07-10 05:14:11 +02:00
y = ts.YValues[index]
return
}
2018-09-07 21:52:30 +02:00
// GetFirstValues gets the first values.
func (ts TimeSeries) GetFirstValues() (x, y float64) {
x = util.Time.ToFloat64(ts.XValues[0])
y = ts.YValues[0]
return
}
// GetLastValues gets the last values.
func (ts TimeSeries) GetLastValues() (x, y float64) {
x = util.Time.ToFloat64(ts.XValues[len(ts.XValues)-1])
y = ts.YValues[len(ts.YValues)-1]
return
}
2016-07-10 19:43:04 +02:00
// GetValueFormatters returns value formatter defaults for the series.
func (ts TimeSeries) GetValueFormatters() (x, y ValueFormatter) {
x = TimeValueFormatter
y = FloatValueFormatter
return
}
// GetYAxis returns which YAxis the series draws on.
2016-08-01 01:54:09 +02:00
func (ts TimeSeries) GetYAxis() YAxisType {
2016-07-10 19:43:04 +02:00
return ts.YAxis
}
2016-07-10 10:11:47 +02:00
// Render renders the series.
func (ts TimeSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
style := ts.Style.InheritFrom(defaults)
2016-07-30 01:36:29 +02:00
Draw.LineSeries(r, canvasBox, xrange, yrange, style, ts)
2016-07-10 05:14:11 +02:00
}
2017-02-03 20:26:53 +01:00
// Validate validates the series.
func (ts TimeSeries) Validate() error {
if len(ts.XValues) == 0 {
return fmt.Errorf("time series must have xvalues set")
}
if len(ts.YValues) == 0 {
return fmt.Errorf("time series must have yvalues set")
}
return nil
}