This commit is contained in:
Will Charczuk 2016-07-09 20:14:11 -07:00
parent 59a5b2cbac
commit 7bb82ae691
6 changed files with 324 additions and 267 deletions

215
chart.go
View file

@ -17,10 +17,9 @@ type Chart struct {
Height int Height int
DPI float64 DPI float64
Background Style Background Style
Canvas Style Canvas Style
Axes Style Axes Style
FinalValueLabel Style
XRange Range XRange Range
YRange Range YRange Range
@ -53,21 +52,20 @@ func (c Chart) GetFont() (*truetype.Font, error) {
} }
// Render renders the chart with the given renderer to the given io.Writer. // Render renders the chart with the given renderer to the given io.Writer.
func (c *Chart) Render(provider RendererProvider, w io.Writer) error { func (c *Chart) Render(rp RendererProvider, w io.Writer) error {
if len(c.Series) == 0 { if len(c.Series) == 0 {
return errors.New("Please provide at least one series") return errors.New("Please provide at least one series")
} }
r, err := provider(c.Width, c.Height) r, err := rp(c.Width, c.Height)
if err != nil { if err != nil {
return err return err
} }
if c.hasText() {
font, err := c.GetFont() font, err := c.GetFont()
if err != nil { if err != nil {
return err return err
}
r.SetFont(font)
} }
r.SetFont(font)
r.SetDPI(c.GetDPI(DefaultDPI)) r.SetDPI(c.GetDPI(DefaultDPI))
canvasBox := c.calculateCanvasBox(r) canvasBox := c.calculateCanvasBox(r)
@ -77,16 +75,12 @@ func (c *Chart) Render(provider RendererProvider, w io.Writer) error {
c.drawCanvas(r, canvasBox) c.drawCanvas(r, canvasBox)
c.drawAxes(r, canvasBox, xrange, yrange) c.drawAxes(r, canvasBox, xrange, yrange)
for index, series := range c.Series { for index, series := range c.Series {
c.drawSeries(r, canvasBox, index, series, xrange, yrange) c.drawSeries(r, canvasBox, series, xrange, yrange)
} }
c.drawTitle(r) c.drawTitle(r)
return r.Save(w) return r.Save(w)
} }
func (c Chart) hasText() bool {
return c.TitleStyle.Show || c.Axes.Show || c.FinalValueLabel.Show
}
func (c Chart) getAxisWidth() int { func (c Chart) getAxisWidth() int {
asw := 0 asw := 0
if c.Axes.Show { if c.Axes.Show {
@ -119,21 +113,19 @@ func (c Chart) calculateCanvasBox(r Renderer) Box {
} }
func (c Chart) calculateFinalLabelWidth(r Renderer) int { func (c Chart) calculateFinalLabelWidth(r Renderer) int {
if !c.FinalValueLabel.Show {
return 0
}
var finalLabelText string var finalLabelText string
for _, s := range c.Series { for _, s := range c.Series {
_, lv := s.GetValue(s.Len() - 1) if vs, isValueProvider := s.(ValueProvider); isValueProvider {
var ll string _, lv := vs.GetValue(vs.Len() - 1)
if c.YRange.Formatter != nil { var ll string
ll = c.YRange.Formatter(lv) if c.YRange.Formatter != nil {
} else { ll = c.YRange.Formatter(lv)
ll = s.GetYFormatter()(lv) } else if fp, isFormatterProvider := s.(FormatterProvider); isFormatterProvider {
} ll = fp.GetYFormatter()(lv)
if len(finalLabelText) < len(ll) { }
finalLabelText = ll if len(finalLabelText) < len(ll) {
finalLabelText = ll
}
} }
} }
@ -145,7 +137,7 @@ func (c Chart) calculateFinalLabelWidth(r Renderer) int {
pr := c.FinalValueLabel.Padding.GetRight(DefaultFinalLabelPadding.Right) pr := c.FinalValueLabel.Padding.GetRight(DefaultFinalLabelPadding.Right)
lsw := int(c.FinalValueLabel.GetStrokeWidth(DefaultAxisLineWidth)) lsw := int(c.FinalValueLabel.GetStrokeWidth(DefaultAxisLineWidth))
return DefaultFinalLabelDeltaWidth + return DefaultYAxisMargin +
pl + pr + pl + pr +
textWidth + asw + 2*lsw textWidth + asw + 2*lsw
} }
@ -163,33 +155,37 @@ func (c Chart) initRanges(canvasBox Box) (xrange Range, yrange Range) {
var globalMinY, globalMinX float64 var globalMinY, globalMinX float64
var globalMaxY, globalMaxX float64 var globalMaxY, globalMaxX float64
for _, s := range c.Series { for _, s := range c.Series {
seriesLength := s.Len() if vp, isValueProvider := s.(ValueProvider); isValueProvider {
for index := 0; index < seriesLength; index++ { seriesLength := vp.Len()
vx, vy := s.GetValue(index) for index := 0; index < seriesLength; index++ {
if didSetFirstValues { vx, vy := vp.GetValue(index)
if globalMinX > vx { if didSetFirstValues {
globalMinX = vx if globalMinX > vx {
globalMinX = vx
}
if globalMinY > vy {
globalMinY = vy
}
if globalMaxX < vx {
globalMaxX = vx
}
if globalMaxY < vy {
globalMaxY = vy
}
} else {
globalMinX, globalMaxX = vx, vx
globalMinY, globalMaxY = vy, vy
didSetFirstValues = true
} }
if globalMinY > vy {
globalMinY = vy
}
if globalMaxX < vx {
globalMaxX = vx
}
if globalMaxY < vy {
globalMaxY = vy
}
} else {
globalMinX, globalMaxX = vx, vx
globalMinY, globalMaxY = vy, vy
didSetFirstValues = true
} }
} }
if xrange.Formatter == nil { if fp, isFormatterProvider := s.(FormatterProvider); isFormatterProvider {
xrange.Formatter = s.GetXFormatter() if xrange.Formatter == nil {
} xrange.Formatter = fp.GetXFormatter()
if yrange.Formatter == nil { }
yrange.Formatter = s.GetYFormatter() if yrange.Formatter == nil {
yrange.Formatter = fp.GetYFormatter()
}
} }
} }
@ -281,7 +277,7 @@ func (c Chart) generateRangeTicks(r Range, tickCount int, offset float64) []Tick
func (c Chart) drawYAxisLabels(r Renderer, canvasBox Box, yrange Range) { func (c Chart) drawYAxisLabels(r Renderer, canvasBox Box, yrange Range) {
tickFontSize := c.Axes.GetFontSize(DefaultAxisFontSize) tickFontSize := c.Axes.GetFontSize(DefaultAxisFontSize)
asw := c.getAxisWidth() asw := c.getAxisWidth()
tx := canvasBox.Right + DefaultFinalLabelDeltaWidth + asw tx := canvasBox.Right + DefaultYAxisMargin + asw
r.SetFontColor(c.Axes.GetFontColor(DefaultAxisColor)) r.SetFontColor(c.Axes.GetFontColor(DefaultAxisColor))
r.SetFontSize(tickFontSize) r.SetFontSize(tickFontSize)
@ -332,111 +328,8 @@ func (c Chart) drawXAxisLabels(r Renderer, canvasBox Box, xrange Range) {
} }
} }
func (c Chart) drawSeries(r Renderer, canvasBox Box, index int, s Series, xrange, yrange Range) { func (c Chart) drawSeries(r Renderer, canvasBox Box, s Series, xrange, yrange Range) {
if s.Len() == 0 { return s.Render(&c, r, canvasBox, xrange, yrange)
return
}
cx := canvasBox.Left
cy := canvasBox.Top
cb := canvasBox.Bottom
cw := canvasBox.Width
v0x, v0y := s.GetValue(0)
x0 := cw - xrange.Translate(v0x)
y0 := yrange.Translate(v0y)
var vx, vy float64
var x, y int
fill := s.GetStyle().GetFillColor()
if !fill.IsZero() {
r.SetFillColor(fill)
r.MoveTo(x0+cx, y0+cy)
for i := 1; i < s.Len(); i++ {
vx, vy = s.GetValue(i)
x = cw - xrange.Translate(vx)
y = yrange.Translate(vy)
r.LineTo(x+cx, y+cy)
}
r.LineTo(x+cx, cb)
r.LineTo(x0+cx, cb)
r.Close()
r.Fill()
}
stroke := s.GetStyle().GetStrokeColor(GetDefaultSeriesStrokeColor(index))
r.SetStrokeColor(stroke)
r.SetStrokeWidth(s.GetStyle().GetStrokeWidth(DefaultStrokeWidth))
r.MoveTo(x0+cx, y0+cy)
for i := 1; i < s.Len(); i++ {
vx, vy = s.GetValue(i)
x = cw - xrange.Translate(vx)
y = yrange.Translate(vy)
r.LineTo(x+cx, y+cy)
}
r.Stroke()
c.drawFinalValueLabel(r, canvasBox, index, s, yrange)
}
func (c Chart) drawFinalValueLabel(r Renderer, canvasBox Box, index int, s Series, yrange Range) {
if c.FinalValueLabel.Show {
_, lv := s.GetValue(s.Len() - 1)
ll := yrange.Format(lv)
py := canvasBox.Top
ly := yrange.Translate(lv) + py
r.SetFontSize(c.FinalValueLabel.GetFontSize(DefaultFinalLabelFontSize))
textWidth, _ := r.MeasureText(ll)
textHeight := int(math.Floor(DefaultFinalLabelFontSize))
halfTextHeight := textHeight >> 1
asw := 0
if c.Axes.Show {
asw = int(c.Axes.GetStrokeWidth(DefaultAxisLineWidth))
}
cx := canvasBox.Right + asw
pt := c.FinalValueLabel.Padding.GetTop(DefaultFinalLabelPadding.Top)
pl := c.FinalValueLabel.Padding.GetLeft(DefaultFinalLabelPadding.Left)
pr := c.FinalValueLabel.Padding.GetRight(DefaultFinalLabelPadding.Right)
pb := c.FinalValueLabel.Padding.GetBottom(DefaultFinalLabelPadding.Bottom)
textX := cx + pl + DefaultFinalLabelDeltaWidth
textY := ly + halfTextHeight
ltlx := cx + pl + DefaultFinalLabelDeltaWidth
ltly := ly - (pt + halfTextHeight)
ltrx := cx + pl + pr + textWidth
ltry := ly - (pt + halfTextHeight)
lbrx := cx + pl + pr + textWidth
lbry := ly + (pb + halfTextHeight)
lblx := cx + DefaultFinalLabelDeltaWidth
lbly := ly + (pb + halfTextHeight)
//draw the shape...
r.SetFillColor(c.FinalValueLabel.GetFillColor(DefaultFinalLabelBackgroundColor))
r.SetStrokeColor(c.FinalValueLabel.GetStrokeColor(s.GetStyle().GetStrokeColor(GetDefaultSeriesStrokeColor(index))))
r.SetStrokeWidth(c.FinalValueLabel.GetStrokeWidth(DefaultAxisLineWidth))
r.MoveTo(cx, ly)
r.LineTo(ltlx, ltly)
r.LineTo(ltrx, ltry)
r.LineTo(lbrx, lbry)
r.LineTo(lblx, lbly)
r.LineTo(cx, ly)
r.Close()
r.FillStroke()
r.SetFontColor(c.FinalValueLabel.GetFontColor(DefaultTextColor))
r.Text(ll, textX, textY)
}
} }
func (c Chart) drawTitle(r Renderer) error { func (c Chart) drawTitle(r Renderer) error {

70
continuous_series.go Normal file
View file

@ -0,0 +1,70 @@
package chart
import (
"fmt"
"github.com/blendlabs/go-util"
)
// ContinuousSeries represents a line on a chart.
type ContinuousSeries struct {
Name string
Style Style
FinalValueLabel Style
XValues []float64
YValues []float64
}
// GetName returns the name of the time series.
func (cs ContinuousSeries) GetName() string {
return cs.Name
}
// GetStyle returns the line style.
func (cs ContinuousSeries) GetStyle() Style {
return cs.Style
}
// Len returns the number of elements in the series.
func (cs ContinuousSeries) Len() int {
return len(cs.XValues)
}
// GetValue gets a value at a given index.
func (cs ContinuousSeries) GetValue(index int) (float64, float64) {
return cs.XValues[index], cs.YValues[index]
}
// GetXFormatter returns the xs value formatter.
func (cs ContinuousSeries) GetXFormatter() Formatter {
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.
func (cs ContinuousSeries) GetYFormatter() Formatter {
return cs.GetXFormatter()
}
// Render renders the series.
func (cs ContinuousSeries) Render(c *Chart, r Renderer, canvasBox Box, xrange, yrange Range) error {
DrawLineSeries(c, r, canvasBox, xrange, yrange, cs)
if cs.FinalValueLabel.Show {
asw := 0
if c.Axes.Show {
asw = int(c.Axes.GetStrokeWidth(DefaultAxisLineWidth))
}
_, lv := cs.GetValue(cs.Len() - 1)
ll := yrange.Format(lv)
lx := canvasBox.Right + asw
ly := yrange.Translate(lv) + canvasBox.Top
DrawAnnotation(c, r, canvasBox, xrange, yrange, cs.FinalValueLabel, lx, ly, ll)
}
}

View file

@ -24,14 +24,16 @@ const (
DefaultFontSize = 10.0 DefaultFontSize = 10.0
// DefaultTitleFontSize is the default title font size. // DefaultTitleFontSize is the default title font size.
DefaultTitleFontSize = 18.0 DefaultTitleFontSize = 18.0
// DefaultFinalLabelDeltaWidth is the width of the left triangle out of the final label. // DefaultAnnotationDeltaWidth is the width of the left triangle out of annotations.
DefaultFinalLabelDeltaWidth = 10 DefaultAnnotationDeltaWidth = 10
// DefaultFinalLabelFontSize is the font size of the final label. // DefaultAnnotationFontSize is the font size of annotations.
DefaultFinalLabelFontSize = 10.0 DefaultAnnotationFontSize = 10.0
// DefaultAxisFontSize is the font size of the axis labels. // DefaultAxisFontSize is the font size of the axis labels.
DefaultAxisFontSize = 10.0 DefaultAxisFontSize = 10.0
// DefaultTitleTop is the default distance from the top of the chart to put the title. // DefaultTitleTop is the default distance from the top of the chart to put the title.
DefaultTitleTop = 10 DefaultTitleTop = 10
// DefaultYAxisMargin is the default distance from the right of the canvas to the y axis labels.
DefaultYAxisMargin = 5
// DefaultXAxisMargin is the default distance from bottom of the canvas to the x axis labels. // DefaultXAxisMargin is the default distance from bottom of the canvas to the x axis labels.
DefaultXAxisMargin = 10 DefaultXAxisMargin = 10
// DefaultMinimumTickHorizontalSpacing is the minimum distance between horizontal ticks. // DefaultMinimumTickHorizontalSpacing is the minimum distance between horizontal ticks.
@ -69,8 +71,8 @@ var (
// DefaultFillColor is the default fill color. // DefaultFillColor is the default fill color.
// It is equivalent to #0074d9. // It is equivalent to #0074d9.
DefaultFillColor = drawing.Color{R: 0, G: 217, B: 116, A: 255} DefaultFillColor = drawing.Color{R: 0, G: 217, B: 116, A: 255}
// DefaultFinalLabelBackgroundColor is the default final label background color. // DefaultAnnotationFillColor is the default annotation background color.
DefaultFinalLabelBackgroundColor = drawing.Color{R: 255, G: 255, B: 255, A: 255} DefaultAnnotationFillColor = drawing.Color{R: 255, G: 255, B: 255, A: 255}
) )
var ( var (
@ -90,8 +92,8 @@ func GetDefaultSeriesStrokeColor(index int) drawing.Color {
} }
var ( var (
// DefaultFinalLabelPadding is the padding around the final label. // DefaultAnnotationPadding is the padding around an annotation.
DefaultFinalLabelPadding = Box{Top: 5, Left: 0, Right: 7, Bottom: 5} DefaultAnnotationPadding = Box{Top: 5, Left: 0, Right: 7, Bottom: 5}
// DefaultBackgroundPadding is the default canvas padding config. // DefaultBackgroundPadding is the default canvas padding config.
DefaultBackgroundPadding = Box{Top: 5, Left: 5, Right: 5, Bottom: 5} DefaultBackgroundPadding = Box{Top: 5, Left: 5, Right: 5, Bottom: 5}
) )

192
series.go
View file

@ -1,121 +1,117 @@
package chart package chart
import ( import "math"
"fmt"
"time"
"github.com/blendlabs/go-util" // Series is a entity data set. It constitutes an item to draw on the chart.
) // The series interface is the bare minimum you need to implement to draw something on a chart.
// Series is a entity data set.
type Series interface { type Series interface {
GetName() string GetName() string
GetStyle() Style GetStyle() Style
Render(c *Chart, r Renderer, canvasBox Box, xrange, yrange Range) error
}
// ValueProvider is a series that is a set of values.
type ValueProvider interface {
Len() int Len() int
GetValue(index int) (float64, float64) GetValue(index int) (float64, float64)
}
// FormatterProvider is a series that has custom formatters.
type FormatterProvider interface {
GetXFormatter() Formatter GetXFormatter() Formatter
GetYFormatter() Formatter GetYFormatter() Formatter
} }
// TimeSeries is a line on a chart. // DrawLineSeries draws a line series with a renderer.
type TimeSeries struct { func DrawLineSeries(c *Chart, r Renderer, canvasBox Box, xrange, yrange Range, vs ValueProvider) error {
Name string if vs.Len() == 0 {
Style Style return
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)
}
// 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
}
// 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. cx := canvasBox.Left
func (ts TimeSeries) GetYFormatter() Formatter { cy := canvasBox.Top
return func(v interface{}) string { cb := canvasBox.Bottom
if typed, isTyped := v.(float64); isTyped { cw := canvasBox.Width
return fmt.Sprintf("%0.2f", typed)
v0x, v0y := vs.GetValue(0)
x0 := cw - xrange.Translate(v0x)
y0 := yrange.Translate(v0y)
var vx, vy float64
var x, y int
fill := s.GetStyle().GetFillColor()
if !fill.IsZero() {
r.SetFillColor(fill)
r.MoveTo(x0+cx, y0+cy)
for i := 1; i < vs.Len(); i++ {
vx, vy = vs.GetValue(i)
x = cw - xrange.Translate(vx)
y = yrange.Translate(vy)
r.LineTo(x+cx, y+cy)
} }
return util.StringEmpty r.LineTo(x+cx, cb)
r.LineTo(x0+cx, cb)
r.Close()
r.Fill()
} }
}
// ContinuousSeries represents a line on a chart. stroke := s.GetStyle().GetStrokeColor(GetDefaultSeriesStrokeColor(index))
type ContinuousSeries struct { r.SetStrokeColor(stroke)
Name string r.SetStrokeWidth(s.GetStyle().GetStrokeWidth(DefaultStrokeWidth))
Style Style
XValues []float64 r.MoveTo(x0+cx, y0+cy)
YValues []float64 for i := 1; i < vs.Len(); i++ {
} vx, vy = vs.GetValue(i)
x = cw - xrange.Translate(vx)
// GetName returns the name of the time series. y = yrange.Translate(vy)
func (cs ContinuousSeries) GetName() string { r.LineTo(x+cx, y+cy)
return cs.Name
}
// GetStyle returns the line style.
func (cs ContinuousSeries) GetStyle() Style {
return cs.Style
}
// Len returns the number of elements in the series.
func (cs ContinuousSeries) Len() int {
return len(cs.XValues)
}
// GetValue gets a value at a given index.
func (cs ContinuousSeries) GetValue(index int) (float64, float64) {
return cs.XValues[index], cs.YValues[index]
}
// GetXFormatter returns the xs value formatter.
func (cs ContinuousSeries) GetXFormatter() Formatter {
return func(v interface{}) string {
if typed, isTyped := v.(float64); isTyped {
return fmt.Sprintf("%0.2f", typed)
}
return util.StringEmpty
} }
r.Stroke()
} }
// GetYFormatter returns the y value formatter. // DrawAnnotation draws an anotation with a renderer.
func (cs ContinuousSeries) GetYFormatter() Formatter { func DrawAnnotation(c *Chart, r Renderer, canvasBox Box, xrange, yrange, s Style, lx, ly int, lv string) {
return cs.GetXFormatter() py := canvasBox.Top
r.SetFontSize(s.GetFontSize(DefaultFinalLabelFontSize))
textWidth, _ := r.MeasureText(ll)
textHeight := int(math.Floor(DefaultFinalLabelFontSize))
halfTextHeight := textHeight >> 1
pt := s.Padding.GetTop(DefaultFinalLabelPadding.Top)
pl := s.Padding.GetLeft(DefaultFinalLabelPadding.Left)
pr := s.Padding.GetRight(DefaultFinalLabelPadding.Right)
pb := s.Padding.GetBottom(DefaultFinalLabelPadding.Bottom)
textX := lx + pl + DefaultFinalLabelDeltaWidth
textY := ly + halfTextHeight
ltlx := lx + pl + DefaultFinalLabelDeltaWidth
ltly := ly - (pt + halfTextHeight)
ltrx := lx + pl + pr + textWidth
ltry := ly - (pt + halfTextHeight)
lbrx := lx + pl + pr + textWidth
lbry := ly + (pb + halfTextHeight)
lblx := lx + DefaultFinalLabelDeltaWidth
lbly := ly + (pb + halfTextHeight)
//draw the shape...
r.SetFillColor(s.GetFillColor(DefaultAnnotationFillColor))
r.SetStrokeColor(s.GetStrokeColor())
r.SetStrokeWidth(s.GetStrokeWidth())
r.MoveTo(lx, ly)
r.LineTo(ltlx, ltly)
r.LineTo(ltrx, ltry)
r.LineTo(lbrx, lbry)
r.LineTo(lblx, lbly)
r.LineTo(cx, ly)
r.Close()
r.FillStroke()
r.SetFontColor(s.GetFontColor(DefaultTextColor))
r.Text(ll, textX, textY)
} }

66
time_series.go Normal file
View file

@ -0,0 +1,66 @@
package chart
import (
"fmt"
"time"
"github.com/blendlabs/go-util"
)
// TimeSeries is a line on a chart.
type TimeSeries struct {
Name string
Style Style
FinalValueLabel 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)
}
// 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
}
// 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
}
}

30
time_series_test.go Normal file
View file

@ -0,0 +1,30 @@
package chart
import (
"testing"
"time"
"github.com/blendlabs/go-assert"
)
func TestTimeSeriesGetValue(t *testing.T) {
assert := assert.New(t)
ts := TimeSeries{
Name: "Test",
XValues: []time.Time{
time.Now().AddDate(0, 0, -5),
time.Now().AddDate(0, 0, -4),
time.Now().AddDate(0, 0, -3),
time.Now().AddDate(0, 0, -2),
time.Now().AddDate(0, 0, -1),
},
YValues: []float64{
1.0, 2.0, 3.0, 4.0, 5.0,
},
}
x0, y0 := ts.GetValue(0)
assert.NotZero(x0)
assert.Equal(1.0, y0)
}