2016-07-06 21:54:00 -04:00
|
|
|
package chart
|
|
|
|
|
2016-07-07 17:44:03 -04:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
2016-07-07 23:26:07 -04:00
|
|
|
|
|
|
|
"github.com/blendlabs/go-util"
|
2016-07-07 17:44:03 -04:00
|
|
|
)
|
2016-07-06 21:54:00 -04:00
|
|
|
|
|
|
|
// Series is a entity data set.
|
|
|
|
type Series interface {
|
|
|
|
GetName() string
|
|
|
|
GetStyle() Style
|
|
|
|
Len() int
|
2016-07-07 23:26:07 -04:00
|
|
|
|
2016-07-07 17:44:03 -04:00
|
|
|
GetValue(index int) (float64, float64)
|
2016-07-07 23:26:07 -04:00
|
|
|
|
|
|
|
GetXFormatter() Formatter
|
|
|
|
GetYFormatter() Formatter
|
2016-07-06 21:54:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TimeSeries is a line on a chart.
|
|
|
|
type TimeSeries struct {
|
|
|
|
Name string
|
|
|
|
Style Style
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-07-07 17:44:03 -04:00
|
|
|
// GetValue gets a value at a given index.
|
|
|
|
func (ts TimeSeries) GetValue(index int) (x float64, y float64) {
|
|
|
|
x = float64(ts.XValues[index].Unix())
|
|
|
|
y = ts.YValues[index]
|
|
|
|
return
|
2016-07-06 21:54:00 -04:00
|
|
|
}
|
|
|
|
|
2016-07-07 23:26:07 -04:00
|
|
|
// GetXFormatter returns the x value formatter.
|
|
|
|
func (ts TimeSeries) GetXFormatter() Formatter {
|
|
|
|
return func(v interface{}) string {
|
|
|
|
if typed, isTyped := v.(time.Time); isTyped {
|
|
|
|
return typed.Format(DefaultDateFormat)
|
|
|
|
}
|
|
|
|
if typed, isTyped := v.(int64); isTyped {
|
|
|
|
return time.Unix(typed, 0).Format(DefaultDateFormat)
|
|
|
|
}
|
|
|
|
if typed, isTyped := v.(float64); isTyped {
|
|
|
|
return time.Unix(int64(typed), 0).Format(DefaultDateFormat)
|
|
|
|
}
|
|
|
|
return util.StringEmpty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetYFormatter returns the y value formatter.
|
|
|
|
func (ts TimeSeries) GetYFormatter() Formatter {
|
|
|
|
return func(v interface{}) string {
|
|
|
|
if typed, isTyped := v.(float64); isTyped {
|
|
|
|
return fmt.Sprintf("%0.2f", typed)
|
|
|
|
}
|
|
|
|
return util.StringEmpty
|
|
|
|
}
|
2016-07-06 21:54:00 -04:00
|
|
|
}
|
|
|
|
|
2016-07-08 02:49:31 -04:00
|
|
|
// ContinuousSeries represents a line on a chart.
|
|
|
|
type ContinuousSeries struct {
|
2016-07-06 21:54:00 -04:00
|
|
|
Name string
|
|
|
|
Style Style
|
|
|
|
|
|
|
|
XValues []float64
|
|
|
|
YValues []float64
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetName returns the name of the time series.
|
2016-07-08 02:49:31 -04:00
|
|
|
func (cs ContinuousSeries) GetName() string {
|
2016-07-06 21:54:00 -04:00
|
|
|
return cs.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStyle returns the line style.
|
2016-07-08 02:49:31 -04:00
|
|
|
func (cs ContinuousSeries) GetStyle() Style {
|
2016-07-06 21:54:00 -04:00
|
|
|
return cs.Style
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the number of elements in the series.
|
2016-07-08 02:49:31 -04:00
|
|
|
func (cs ContinuousSeries) Len() int {
|
2016-07-06 21:54:00 -04:00
|
|
|
return len(cs.XValues)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetValue gets a value at a given index.
|
2016-07-08 02:49:31 -04:00
|
|
|
func (cs ContinuousSeries) GetValue(index int) (float64, float64) {
|
2016-07-07 00:12:19 -04:00
|
|
|
return cs.XValues[index], cs.YValues[index]
|
2016-07-06 21:54:00 -04:00
|
|
|
}
|
|
|
|
|
2016-07-07 23:26:07 -04:00
|
|
|
// GetXFormatter returns the xs value formatter.
|
2016-07-08 02:49:31 -04:00
|
|
|
func (cs ContinuousSeries) GetXFormatter() Formatter {
|
2016-07-07 23:26:07 -04:00
|
|
|
return func(v interface{}) string {
|
|
|
|
if typed, isTyped := v.(float64); isTyped {
|
|
|
|
return fmt.Sprintf("%0.2f", typed)
|
|
|
|
}
|
|
|
|
return util.StringEmpty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetYFormatter returns the y value formatter.
|
2016-07-08 02:49:31 -04:00
|
|
|
func (cs ContinuousSeries) GetYFormatter() Formatter {
|
2016-07-07 23:26:07 -04:00
|
|
|
return cs.GetXFormatter()
|
2016-07-06 21:54:00 -04:00
|
|
|
}
|