api cleaup

This commit is contained in:
Will Charczuk 2017-04-28 16:07:36 -07:00
parent 43212f871f
commit 65a0895143
39 changed files with 747 additions and 699 deletions

View file

@ -10,7 +10,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
/* /*
In this example we add a new type of series, a `SimpleMovingAverageSeries` that takes another series as a required argument. In this example we add a new type of series, a `SimpleMovingAverageSeries` that takes another series as a required argument.
InnerSeries only needs to implement `ValueProvider`, so really you could chain `SimpleMovingAverageSeries` together if you wanted. InnerSeries only needs to implement `ValuesProvider`, so really you could chain `SimpleMovingAverageSeries` together if you wanted.
*/ */
mainSeries := chart.ContinuousSeries{ mainSeries := chart.ContinuousSeries{

View file

@ -10,7 +10,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
/* /*
In this example we add a new type of series, a `PolynomialRegressionSeries` that takes another series as a required argument. In this example we add a new type of series, a `PolynomialRegressionSeries` that takes another series as a required argument.
InnerSeries only needs to implement `ValueProvider`, so really you could chain `PolynomialRegressionSeries` together if you wanted. InnerSeries only needs to implement `ValuesProvider`, so really you could chain `PolynomialRegressionSeries` together if you wanted.
*/ */
mainSeries := chart.ContinuousSeries{ mainSeries := chart.ContinuousSeries{

View file

@ -10,7 +10,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
/* /*
In this example we add a new type of series, a `SimpleMovingAverageSeries` that takes another series as a required argument. In this example we add a new type of series, a `SimpleMovingAverageSeries` that takes another series as a required argument.
InnerSeries only needs to implement `ValueProvider`, so really you could chain `SimpleMovingAverageSeries` together if you wanted. InnerSeries only needs to implement `ValuesProvider`, so really you could chain `SimpleMovingAverageSeries` together if you wanted.
*/ */
mainSeries := chart.ContinuousSeries{ mainSeries := chart.ContinuousSeries{

View file

@ -1,9 +1,6 @@
package chart package chart
import ( import "fmt"
"fmt"
"math"
)
// BollingerBandsSeries draws bollinger bands for an inner series. // BollingerBandsSeries draws bollinger bands for an inner series.
// Bollinger bands are defined by two lines, one at SMA+k*stddev, one at SMA-k*stdev. // Bollinger bands are defined by two lines, one at SMA+k*stddev, one at SMA-k*stdev.
@ -14,9 +11,9 @@ type BollingerBandsSeries struct {
Period int Period int
K float64 K float64
InnerSeries ValueProvider InnerSeries ValuesProvider
valueBuffer *RingBuffer valueBuffer *ValueBuffer
} }
// GetName returns the name of the time series. // GetName returns the name of the time series.
@ -42,7 +39,9 @@ func (bbs BollingerBandsSeries) GetPeriod() int {
return bbs.Period return bbs.Period
} }
// GetK returns the K value. // GetK returns the K value, or the number of standard deviations above and below
// to band the simple moving average with.
// Typical K value is 2.0.
func (bbs BollingerBandsSeries) GetK(defaults ...float64) float64 { func (bbs BollingerBandsSeries) GetK(defaults ...float64) float64 {
if bbs.K == 0 { if bbs.K == 0 {
if len(defaults) > 0 { if len(defaults) > 0 {
@ -54,35 +53,35 @@ func (bbs BollingerBandsSeries) GetK(defaults ...float64) float64 {
} }
// Len returns the number of elements in the series. // Len returns the number of elements in the series.
func (bbs *BollingerBandsSeries) Len() int { func (bbs BollingerBandsSeries) Len() int {
return bbs.InnerSeries.Len() return bbs.InnerSeries.Len()
} }
// GetBoundedValue gets the bounded value for the series. // GetBoundedValues gets the bounded value for the series.
func (bbs *BollingerBandsSeries) GetBoundedValue(index int) (x, y1, y2 float64) { func (bbs *BollingerBandsSeries) GetBoundedValues(index int) (x, y1, y2 float64) {
if bbs.InnerSeries == nil { if bbs.InnerSeries == nil {
return return
} }
if bbs.valueBuffer == nil || index == 0 { if bbs.valueBuffer == nil || index == 0 {
bbs.valueBuffer = NewRingBufferWithCapacity(bbs.GetPeriod()) bbs.valueBuffer = NewValueBufferWithCapacity(bbs.GetPeriod())
} }
if bbs.valueBuffer.Len() >= bbs.GetPeriod() { if bbs.valueBuffer.Len() >= bbs.GetPeriod() {
bbs.valueBuffer.Dequeue() bbs.valueBuffer.Dequeue()
} }
px, py := bbs.InnerSeries.GetValue(index) px, py := bbs.InnerSeries.GetValues(index)
bbs.valueBuffer.Enqueue(py) bbs.valueBuffer.Enqueue(py)
x = px x = px
ay := bbs.getAverage(bbs.valueBuffer) ay := Sequence{bbs.valueBuffer}.Average()
std := bbs.getStdDev(bbs.valueBuffer) std := Sequence{bbs.valueBuffer}.StdDev()
y1 = ay + (bbs.GetK() * std) y1 = ay + (bbs.GetK() * std)
y2 = ay - (bbs.GetK() * std) y2 = ay - (bbs.GetK() * std)
return return
} }
// GetBoundedLastValue returns the last bounded value for the series. // GetBoundedLastValues returns the last bounded value for the series.
func (bbs *BollingerBandsSeries) GetBoundedLastValue() (x, y1, y2 float64) { func (bbs *BollingerBandsSeries) GetBoundedLastValues() (x, y1, y2 float64) {
if bbs.InnerSeries == nil { if bbs.InnerSeries == nil {
return return
} }
@ -93,15 +92,15 @@ func (bbs *BollingerBandsSeries) GetBoundedLastValue() (x, y1, y2 float64) {
startAt = 0 startAt = 0
} }
vb := NewRingBufferWithCapacity(period) vb := NewValueBufferWithCapacity(period)
for index := startAt; index < seriesLength; index++ { for index := startAt; index < seriesLength; index++ {
xn, yn := bbs.InnerSeries.GetValue(index) xn, yn := bbs.InnerSeries.GetValues(index)
vb.Enqueue(yn) vb.Enqueue(yn)
x = xn x = xn
} }
ay := bbs.getAverage(vb) ay := Sequence{vb}.Average()
std := bbs.getStdDev(vb) std := Sequence{vb}.StdDev()
y1 = ay + (bbs.GetK() * std) y1 = ay + (bbs.GetK() * std)
y2 = ay - (bbs.GetK() * std) y2 = ay - (bbs.GetK() * std)
@ -120,37 +119,6 @@ func (bbs *BollingerBandsSeries) Render(r Renderer, canvasBox Box, xrange, yrang
Draw.BoundedSeries(r, canvasBox, xrange, yrange, s, bbs, bbs.GetPeriod()) Draw.BoundedSeries(r, canvasBox, xrange, yrange, s, bbs, bbs.GetPeriod())
} }
func (bbs BollingerBandsSeries) getAverage(valueBuffer *RingBuffer) float64 {
var accum float64
valueBuffer.Each(func(v interface{}) {
if typed, isTyped := v.(float64); isTyped {
accum += typed
}
})
return accum / float64(valueBuffer.Len())
}
func (bbs BollingerBandsSeries) getVariance(valueBuffer *RingBuffer) float64 {
if valueBuffer.Len() == 0 {
return 0
}
var variance float64
m := bbs.getAverage(valueBuffer)
valueBuffer.Each(func(v interface{}) {
if n, isTyped := v.(float64); isTyped {
variance += (float64(n) - m) * (float64(n) - m)
}
})
return variance / float64(valueBuffer.Len())
}
func (bbs BollingerBandsSeries) getStdDev(valueBuffer *RingBuffer) float64 {
return math.Pow(bbs.getVariance(valueBuffer), 0.5)
}
// Validate validates the series. // Validate validates the series.
func (bbs BollingerBandsSeries) Validate() error { func (bbs BollingerBandsSeries) Validate() error {
if bbs.InnerSeries == nil { if bbs.InnerSeries == nil {

View file

@ -1,6 +1,7 @@
package chart package chart
import ( import (
"fmt"
"math" "math"
"testing" "testing"
@ -10,9 +11,9 @@ import (
func TestBollingerBandSeries(t *testing.T) { func TestBollingerBandSeries(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
s1 := mockValueProvider{ s1 := mockValuesProvider{
X: Sequence.Float64(1.0, 100.0), X: Generate.Float64(1.0, 100.0),
Y: Sequence.Random(100, 1024), Y: Generate.Random(100, 1024),
} }
bbs := &BollingerBandsSeries{ bbs := &BollingerBandsSeries{
@ -24,27 +25,27 @@ func TestBollingerBandSeries(t *testing.T) {
y2values := make([]float64, 100) y2values := make([]float64, 100)
for x := 0; x < 100; x++ { for x := 0; x < 100; x++ {
xvalues[x], y1values[x], y2values[x] = bbs.GetBoundedValue(x) xvalues[x], y1values[x], y2values[x] = bbs.GetBoundedValues(x)
} }
for x := bbs.GetPeriod(); x < 100; x++ { for x := bbs.GetPeriod(); x < 100; x++ {
assert.True(y1values[x] > y2values[x]) assert.True(y1values[x] > y2values[x], fmt.Sprintf("%v vs. %v", y1values[x], y2values[x]))
} }
} }
func TestBollingerBandLastValue(t *testing.T) { func TestBollingerBandLastValue(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
s1 := mockValueProvider{ s1 := mockValuesProvider{
X: Sequence.Float64(1.0, 100.0), X: Generate.Float64(1.0, 100.0),
Y: Sequence.Float64(1.0, 100.0), Y: Generate.Float64(1.0, 100.0),
} }
bbs := &BollingerBandsSeries{ bbs := &BollingerBandsSeries{
InnerSeries: s1, InnerSeries: s1,
} }
x, y1, y2 := bbs.GetBoundedLastValue() x, y1, y2 := bbs.GetBoundedLastValues()
assert.Equal(100.0, x) assert.Equal(100.0, x)
assert.Equal(101, math.Floor(y1)) assert.Equal(101, math.Floor(y1))
assert.Equal(83, math.Floor(y2)) assert.Equal(83, math.Floor(y2))

View file

@ -177,10 +177,10 @@ func (c Chart) getRanges() (xrange, yrange, yrangeAlt Range) {
for _, s := range c.Series { for _, s := range c.Series {
if s.GetStyle().IsZero() || s.GetStyle().Show { if s.GetStyle().IsZero() || s.GetStyle().Show {
seriesAxis := s.GetYAxis() seriesAxis := s.GetYAxis()
if bvp, isBoundedValueProvider := s.(BoundedValueProvider); isBoundedValueProvider { if bvp, isBoundedValuesProvider := s.(BoundedValuesProvider); isBoundedValuesProvider {
seriesLength := bvp.Len() seriesLength := bvp.Len()
for index := 0; index < seriesLength; index++ { for index := 0; index < seriesLength; index++ {
vx, vy1, vy2 := bvp.GetBoundedValue(index) vx, vy1, vy2 := bvp.GetBoundedValues(index)
minx = math.Min(minx, vx) minx = math.Min(minx, vx)
maxx = math.Max(maxx, vx) maxx = math.Max(maxx, vx)
@ -198,10 +198,10 @@ func (c Chart) getRanges() (xrange, yrange, yrangeAlt Range) {
seriesMappedToSecondaryAxis = true seriesMappedToSecondaryAxis = true
} }
} }
} else if vp, isValueProvider := s.(ValueProvider); isValueProvider { } else if vp, isValuesProvider := s.(ValuesProvider); isValuesProvider {
seriesLength := vp.Len() seriesLength := vp.Len()
for index := 0; index < seriesLength; index++ { for index := 0; index < seriesLength; index++ {
vx, vy := vp.GetValue(index) vx, vy := vp.GetValues(index)
minx = math.Min(minx, vx) minx = math.Min(minx, vx)
maxx = math.Max(maxx, vx) maxx = math.Max(maxx, vx)

View file

@ -391,8 +391,8 @@ func TestChartRegressionBadRangesByUser(t *testing.T) {
}, },
Series: []Series{ Series: []Series{
ContinuousSeries{ ContinuousSeries{
XValues: Sequence.Float64(1.0, 10.0), XValues: Generate.Float64(1.0, 10.0),
YValues: Sequence.Float64(1.0, 10.0), YValues: Generate.Float64(1.0, 10.0),
}, },
}, },
} }
@ -407,8 +407,8 @@ func TestChartValidatesSeries(t *testing.T) {
c := Chart{ c := Chart{
Series: []Series{ Series: []Series{
ContinuousSeries{ ContinuousSeries{
XValues: Sequence.Float64(1.0, 10.0), XValues: Generate.Float64(1.0, 10.0),
YValues: Sequence.Float64(1.0, 10.0), YValues: Generate.Float64(1.0, 10.0),
}, },
}, },
} }
@ -418,7 +418,7 @@ func TestChartValidatesSeries(t *testing.T) {
c = Chart{ c = Chart{
Series: []Series{ Series: []Series{
ContinuousSeries{ ContinuousSeries{
XValues: Sequence.Float64(1.0, 10.0), XValues: Generate.Float64(1.0, 10.0),
}, },
}, },
} }
@ -504,8 +504,8 @@ func TestChartE2ELine(t *testing.T) {
}, },
Series: []Series{ Series: []Series{
ContinuousSeries{ ContinuousSeries{
XValues: Sequence.Float64(0, 4, 1), XValues: Generate.Float64(0, 4, 1),
YValues: Sequence.Float64(0, 4, 1), YValues: Generate.Float64(0, 4, 1),
}, },
}, },
} }
@ -549,8 +549,8 @@ func TestChartE2ELineWithFill(t *testing.T) {
StrokeColor: drawing.ColorBlue, StrokeColor: drawing.ColorBlue,
FillColor: drawing.ColorRed, FillColor: drawing.ColorRed,
}, },
XValues: Sequence.Float64(0, 4, 1), XValues: Generate.Float64(0, 4, 1),
YValues: Sequence.Float64(0, 4, 1), YValues: Generate.Float64(0, 4, 1),
}, },
}, },
} }

View file

@ -7,7 +7,7 @@ type ConcatSeries []Series
func (cs ConcatSeries) Len() int { func (cs ConcatSeries) Len() int {
total := 0 total := 0
for _, s := range cs { for _, s := range cs {
if typed, isValueProvider := s.(ValueProvider); isValueProvider { if typed, isValuesProvider := s.(ValuesProvider); isValuesProvider {
total += typed.Len() total += typed.Len()
} }
} }
@ -19,10 +19,10 @@ func (cs ConcatSeries) Len() int {
func (cs ConcatSeries) GetValue(index int) (x, y float64) { func (cs ConcatSeries) GetValue(index int) (x, y float64) {
cursor := 0 cursor := 0
for _, s := range cs { for _, s := range cs {
if typed, isValueProvider := s.(ValueProvider); isValueProvider { if typed, isValuesProvider := s.(ValuesProvider); isValuesProvider {
len := typed.Len() len := typed.Len()
if index < cursor+len { if index < cursor+len {
x, y = typed.GetValue(index - cursor) //FENCEPOSTS. x, y = typed.GetValues(index - cursor) //FENCEPOSTS.
return return
} }
cursor += typed.Len() cursor += typed.Len()

View file

@ -10,18 +10,18 @@ func TestConcatSeries(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
s1 := ContinuousSeries{ s1 := ContinuousSeries{
XValues: Sequence.Float64(1.0, 10.0), XValues: Generate.Float64(1.0, 10.0),
YValues: Sequence.Float64(1.0, 10.0), YValues: Generate.Float64(1.0, 10.0),
} }
s2 := ContinuousSeries{ s2 := ContinuousSeries{
XValues: Sequence.Float64(11, 20.0), XValues: Generate.Float64(11, 20.0),
YValues: Sequence.Float64(10.0, 1.0), YValues: Generate.Float64(10.0, 1.0),
} }
s3 := ContinuousSeries{ s3 := ContinuousSeries{
XValues: Sequence.Float64(21, 30.0), XValues: Generate.Float64(21, 30.0),
YValues: Sequence.Float64(1.0, 10.0), YValues: Generate.Float64(1.0, 10.0),
} }
cs := ConcatSeries([]Series{s1, s2, s3}) cs := ConcatSeries([]Series{s1, s2, s3})

View file

@ -31,13 +31,13 @@ func (cs ContinuousSeries) Len() int {
return len(cs.XValues) return len(cs.XValues)
} }
// GetValue gets a value at a given index. // GetValues gets the x,y values at a given index.
func (cs ContinuousSeries) GetValue(index int) (float64, float64) { func (cs ContinuousSeries) GetValues(index int) (float64, float64) {
return cs.XValues[index], cs.YValues[index] return cs.XValues[index], cs.YValues[index]
} }
// GetLastValue gets the last value. // GetLastValues gets the last x,y values.
func (cs ContinuousSeries) GetLastValue() (float64, float64) { func (cs ContinuousSeries) GetLastValues() (float64, float64) {
return cs.XValues[len(cs.XValues)-1], cs.YValues[len(cs.YValues)-1] return cs.XValues[len(cs.XValues)-1], cs.YValues[len(cs.YValues)-1]
} }

View file

@ -12,21 +12,21 @@ func TestContinuousSeries(t *testing.T) {
cs := ContinuousSeries{ cs := ContinuousSeries{
Name: "Test Series", Name: "Test Series",
XValues: Sequence.Float64(1.0, 10.0), XValues: Generate.Float64(1.0, 10.0),
YValues: Sequence.Float64(1.0, 10.0), YValues: Generate.Float64(1.0, 10.0),
} }
assert.Equal("Test Series", cs.GetName()) assert.Equal("Test Series", cs.GetName())
assert.Equal(10, cs.Len()) assert.Equal(10, cs.Len())
x0, y0 := cs.GetValue(0) x0, y0 := cs.GetValues(0)
assert.Equal(1.0, x0) assert.Equal(1.0, x0)
assert.Equal(1.0, y0) assert.Equal(1.0, y0)
xn, yn := cs.GetValue(9) xn, yn := cs.GetValues(9)
assert.Equal(10.0, xn) assert.Equal(10.0, xn)
assert.Equal(10.0, yn) assert.Equal(10.0, yn)
xn, yn = cs.GetLastValue() xn, yn = cs.GetLastValues()
assert.Equal(10.0, xn) assert.Equal(10.0, xn)
assert.Equal(10.0, yn) assert.Equal(10.0, yn)
} }
@ -53,20 +53,20 @@ func TestContinuousSeriesValidate(t *testing.T) {
cs := ContinuousSeries{ cs := ContinuousSeries{
Name: "Test Series", Name: "Test Series",
XValues: Sequence.Float64(1.0, 10.0), XValues: Generate.Float64(1.0, 10.0),
YValues: Sequence.Float64(1.0, 10.0), YValues: Generate.Float64(1.0, 10.0),
} }
assert.Nil(cs.Validate()) assert.Nil(cs.Validate())
cs = ContinuousSeries{ cs = ContinuousSeries{
Name: "Test Series", Name: "Test Series",
XValues: Sequence.Float64(1.0, 10.0), XValues: Generate.Float64(1.0, 10.0),
} }
assert.NotNil(cs.Validate()) assert.NotNil(cs.Validate())
cs = ContinuousSeries{ cs = ContinuousSeries{
Name: "Test Series", Name: "Test Series",
YValues: Sequence.Float64(1.0, 10.0), YValues: Generate.Float64(1.0, 10.0),
} }
assert.NotNil(cs.Validate()) assert.NotNil(cs.Validate())
} }

22
draw.go
View file

@ -10,7 +10,7 @@ var (
type draw struct{} type draw struct{}
// LineSeries draws a line series with a renderer. // LineSeries draws a line series with a renderer.
func (d draw) LineSeries(r Renderer, canvasBox Box, xrange, yrange Range, style Style, vs ValueProvider) { func (d draw) LineSeries(r Renderer, canvasBox Box, xrange, yrange Range, style Style, vs ValuesProvider) {
if vs.Len() == 0 { if vs.Len() == 0 {
return return
} }
@ -18,7 +18,7 @@ func (d draw) LineSeries(r Renderer, canvasBox Box, xrange, yrange Range, style
cb := canvasBox.Bottom cb := canvasBox.Bottom
cl := canvasBox.Left cl := canvasBox.Left
v0x, v0y := vs.GetValue(0) v0x, v0y := vs.GetValues(0)
x0 := cl + xrange.Translate(v0x) x0 := cl + xrange.Translate(v0x)
y0 := cb - yrange.Translate(v0y) y0 := cb - yrange.Translate(v0y)
@ -31,7 +31,7 @@ func (d draw) LineSeries(r Renderer, canvasBox Box, xrange, yrange Range, style
style.GetFillOptions().WriteDrawingOptionsToRenderer(r) style.GetFillOptions().WriteDrawingOptionsToRenderer(r)
r.MoveTo(x0, y0) r.MoveTo(x0, y0)
for i := 1; i < vs.Len(); i++ { for i := 1; i < vs.Len(); i++ {
vx, vy = vs.GetValue(i) vx, vy = vs.GetValues(i)
x = cl + xrange.Translate(vx) x = cl + xrange.Translate(vx)
y = cb - yrange.Translate(vy) y = cb - yrange.Translate(vy)
r.LineTo(x, y) r.LineTo(x, y)
@ -47,7 +47,7 @@ func (d draw) LineSeries(r Renderer, canvasBox Box, xrange, yrange Range, style
r.MoveTo(x0, y0) r.MoveTo(x0, y0)
for i := 1; i < vs.Len(); i++ { for i := 1; i < vs.Len(); i++ {
vx, vy = vs.GetValue(i) vx, vy = vs.GetValues(i)
x = cl + xrange.Translate(vx) x = cl + xrange.Translate(vx)
y = cb - yrange.Translate(vy) y = cb - yrange.Translate(vy)
r.LineTo(x, y) r.LineTo(x, y)
@ -60,7 +60,7 @@ func (d draw) LineSeries(r Renderer, canvasBox Box, xrange, yrange Range, style
style.GetDotOptions().WriteDrawingOptionsToRenderer(r) style.GetDotOptions().WriteDrawingOptionsToRenderer(r)
for i := 0; i < vs.Len(); i++ { for i := 0; i < vs.Len(); i++ {
vx, vy = vs.GetValue(i) vx, vy = vs.GetValues(i)
x = cl + xrange.Translate(vx) x = cl + xrange.Translate(vx)
y = cb - yrange.Translate(vy) y = cb - yrange.Translate(vy)
@ -82,8 +82,8 @@ func (d draw) LineSeries(r Renderer, canvasBox Box, xrange, yrange Range, style
} }
} }
// BoundedSeries draws a series that implements BoundedValueProvider. // BoundedSeries draws a series that implements BoundedValuesProvider.
func (d draw) BoundedSeries(r Renderer, canvasBox Box, xrange, yrange Range, style Style, bbs BoundedValueProvider, drawOffsetIndexes ...int) { func (d draw) BoundedSeries(r Renderer, canvasBox Box, xrange, yrange Range, style Style, bbs BoundedValuesProvider, drawOffsetIndexes ...int) {
drawOffsetIndex := 0 drawOffsetIndex := 0
if len(drawOffsetIndexes) > 0 { if len(drawOffsetIndexes) > 0 {
drawOffsetIndex = drawOffsetIndexes[0] drawOffsetIndex = drawOffsetIndexes[0]
@ -92,7 +92,7 @@ func (d draw) BoundedSeries(r Renderer, canvasBox Box, xrange, yrange Range, sty
cb := canvasBox.Bottom cb := canvasBox.Bottom
cl := canvasBox.Left cl := canvasBox.Left
v0x, v0y1, v0y2 := bbs.GetBoundedValue(0) v0x, v0y1, v0y2 := bbs.GetBoundedValues(0)
x0 := cl + xrange.Translate(v0x) x0 := cl + xrange.Translate(v0x)
y0 := cb - yrange.Translate(v0y1) y0 := cb - yrange.Translate(v0y1)
@ -107,7 +107,7 @@ func (d draw) BoundedSeries(r Renderer, canvasBox Box, xrange, yrange Range, sty
style.GetFillAndStrokeOptions().WriteToRenderer(r) style.GetFillAndStrokeOptions().WriteToRenderer(r)
r.MoveTo(x0, y0) r.MoveTo(x0, y0)
for i := 1; i < bbs.Len(); i++ { for i := 1; i < bbs.Len(); i++ {
vx, vy1, vy2 = bbs.GetBoundedValue(i) vx, vy1, vy2 = bbs.GetBoundedValues(i)
xvalues[i] = vx xvalues[i] = vx
y2values[i] = vy2 y2values[i] = vy2
@ -133,7 +133,7 @@ func (d draw) BoundedSeries(r Renderer, canvasBox Box, xrange, yrange Range, sty
} }
// HistogramSeries draws a value provider as boxes from 0. // HistogramSeries draws a value provider as boxes from 0.
func (d draw) HistogramSeries(r Renderer, canvasBox Box, xrange, yrange Range, style Style, vs ValueProvider, barWidths ...int) { func (d draw) HistogramSeries(r Renderer, canvasBox Box, xrange, yrange Range, style Style, vs ValuesProvider, barWidths ...int) {
if vs.Len() == 0 { if vs.Len() == 0 {
return return
} }
@ -150,7 +150,7 @@ func (d draw) HistogramSeries(r Renderer, canvasBox Box, xrange, yrange Range, s
//foreach datapoint, draw a box. //foreach datapoint, draw a box.
for index := 0; index < seriesLength; index++ { for index := 0; index < seriesLength; index++ {
vx, vy := vs.GetValue(index) vx, vy := vs.GetValues(index)
y0 := yrange.Translate(0) y0 := yrange.Translate(0)
x := cl + xrange.Translate(vx) x := cl + xrange.Translate(vx)
y := yrange.Translate(vy) y := yrange.Translate(vy)

View file

@ -14,7 +14,7 @@ type EMASeries struct {
YAxis YAxisType YAxis YAxisType
Period int Period int
InnerSeries ValueProvider InnerSeries ValuesProvider
cache []float64 cache []float64
} }
@ -52,23 +52,23 @@ func (ema EMASeries) GetSigma() float64 {
return 2.0 / (float64(ema.GetPeriod()) + 1) return 2.0 / (float64(ema.GetPeriod()) + 1)
} }
// GetValue gets a value at a given index. // GetValues gets a value at a given index.
func (ema *EMASeries) GetValue(index int) (x, y float64) { func (ema *EMASeries) GetValues(index int) (x, y float64) {
if ema.InnerSeries == nil { if ema.InnerSeries == nil {
return return
} }
if len(ema.cache) == 0 { if len(ema.cache) == 0 {
ema.ensureCachedValues() ema.ensureCachedValues()
} }
vx, _ := ema.InnerSeries.GetValue(index) vx, _ := ema.InnerSeries.GetValues(index)
x = vx x = vx
y = ema.cache[index] y = ema.cache[index]
return return
} }
// GetLastValue computes the last moving average value but walking back window size samples, // GetLastValues computes the last moving average value but walking back window size samples,
// and recomputing the last moving average chunk. // and recomputing the last moving average chunk.
func (ema *EMASeries) GetLastValue() (x, y float64) { func (ema *EMASeries) GetLastValues() (x, y float64) {
if ema.InnerSeries == nil { if ema.InnerSeries == nil {
return return
} }
@ -76,7 +76,7 @@ func (ema *EMASeries) GetLastValue() (x, y float64) {
ema.ensureCachedValues() ema.ensureCachedValues()
} }
lastIndex := ema.InnerSeries.Len() - 1 lastIndex := ema.InnerSeries.Len() - 1
x, _ = ema.InnerSeries.GetValue(lastIndex) x, _ = ema.InnerSeries.GetValues(lastIndex)
y = ema.cache[lastIndex] y = ema.cache[lastIndex]
return return
} }
@ -86,7 +86,7 @@ func (ema *EMASeries) ensureCachedValues() {
ema.cache = make([]float64, seriesLength) ema.cache = make([]float64, seriesLength)
sigma := ema.GetSigma() sigma := ema.GetSigma()
for x := 0; x < seriesLength; x++ { for x := 0; x < seriesLength; x++ {
_, y := ema.InnerSeries.GetValue(x) _, y := ema.InnerSeries.GetValues(x)
if x == 0 { if x == 0 {
ema.cache[x] = y ema.cache[x] = y
continue continue

View file

@ -7,7 +7,7 @@ import (
) )
var ( var (
emaXValues = Sequence.Float64(1.0, 50.0) emaXValues = Generate.Float64(1.0, 50.0)
emaYValues = []float64{ emaYValues = []float64{
1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2,
1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2,
@ -75,7 +75,7 @@ var (
func TestEMASeries(t *testing.T) { func TestEMASeries(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
mockSeries := mockValueProvider{ mockSeries := mockValuesProvider{
emaXValues, emaXValues,
emaYValues, emaYValues,
} }
@ -91,7 +91,7 @@ func TestEMASeries(t *testing.T) {
var yvalues []float64 var yvalues []float64
for x := 0; x < ema.Len(); x++ { for x := 0; x < ema.Len(); x++ {
_, y := ema.GetValue(x) _, y := ema.GetValues(x)
yvalues = append(yvalues, y) yvalues = append(yvalues, y)
} }
@ -99,7 +99,7 @@ func TestEMASeries(t *testing.T) {
assert.InDelta(yv, emaExpected[index], emaDelta) assert.InDelta(yv, emaExpected[index], emaDelta)
} }
lvx, lvy := ema.GetLastValue() lvx, lvy := ema.GetLastValues()
assert.Equal(50.0, lvx) assert.Equal(50.0, lvx)
assert.InDelta(lvy, emaExpected[49], emaDelta) assert.InDelta(lvy, emaExpected[49], emaDelta)
} }

190
generate.go Normal file
View file

@ -0,0 +1,190 @@
package chart
import (
"math/rand"
"time"
)
var (
// Generate contains some sequence generation utilities.
// These utilities can be useful for generating test data.
Generate = &generate{
rnd: rand.New(rand.NewSource(time.Now().Unix())),
}
)
type generate struct {
rnd *rand.Rand
}
// Float64 produces an array of floats from [start,end] by optional steps.
func (g generate) Float64(start, end float64, steps ...float64) []float64 {
var values []float64
step := 1.0
if len(steps) > 0 {
step = steps[0]
}
if start < end {
for x := start; x <= end; x += step {
values = append(values, x)
}
} else {
for x := start; x >= end; x = x - step {
values = append(values, x)
}
}
return values
}
// Random generates a fixed length sequence of random values between (0, scale).
func (g generate) Random(samples int, scale float64) []float64 {
values := make([]float64, samples)
for x := 0; x < samples; x++ {
values[x] = g.rnd.Float64() * scale
}
return values
}
// Random generates a fixed length sequence of random values with a given average, above and below that average by (-scale, scale)
func (g generate) RandomWithAverage(samples int, average, scale float64) []float64 {
values := make([]float64, samples)
for x := 0; x < samples; x++ {
jitter := scale - (g.rnd.Float64() * (2 * scale))
values[x] = average + jitter
}
return values
}
// Days generates a sequence of timestamps by day, from -days to today.
func (g generate) Days(days int) []time.Time {
var values []time.Time
for day := days; day >= 0; day-- {
values = append(values, time.Now().AddDate(0, 0, -day))
}
return values
}
func (g generate) MarketHours(from, to time.Time, marketOpen, marketClose time.Time, isHoliday HolidayProvider) []time.Time {
var times []time.Time
cursor := Date.On(marketOpen, from)
toClose := Date.On(marketClose, to)
for cursor.Before(toClose) || cursor.Equal(toClose) {
todayOpen := Date.On(marketOpen, cursor)
todayClose := Date.On(marketClose, cursor)
isValidTradingDay := !isHoliday(cursor) && Date.IsWeekDay(cursor.Weekday())
if (cursor.Equal(todayOpen) || cursor.After(todayOpen)) && (cursor.Equal(todayClose) || cursor.Before(todayClose)) && isValidTradingDay {
times = append(times, cursor)
}
if cursor.After(todayClose) {
cursor = Date.NextMarketOpen(cursor, marketOpen, isHoliday)
} else {
cursor = Date.NextHour(cursor)
}
}
return times
}
func (g generate) MarketHourQuarters(from, to time.Time, marketOpen, marketClose time.Time, isHoliday HolidayProvider) []time.Time {
var times []time.Time
cursor := Date.On(marketOpen, from)
toClose := Date.On(marketClose, to)
for cursor.Before(toClose) || cursor.Equal(toClose) {
isValidTradingDay := !isHoliday(cursor) && Date.IsWeekDay(cursor.Weekday())
if isValidTradingDay {
todayOpen := Date.On(marketOpen, cursor)
todayNoon := Date.NoonOn(cursor)
today2pm := Date.On(Date.Time(14, 0, 0, 0, cursor.Location()), cursor)
todayClose := Date.On(marketClose, cursor)
times = append(times, todayOpen, todayNoon, today2pm, todayClose)
}
cursor = Date.NextDay(cursor)
}
return times
}
func (g generate) MarketDayCloses(from, to time.Time, marketOpen, marketClose time.Time, isHoliday HolidayProvider) []time.Time {
var times []time.Time
cursor := Date.On(marketOpen, from)
toClose := Date.On(marketClose, to)
for cursor.Before(toClose) || cursor.Equal(toClose) {
isValidTradingDay := !isHoliday(cursor) && Date.IsWeekDay(cursor.Weekday())
if isValidTradingDay {
todayClose := Date.On(marketClose, cursor)
times = append(times, todayClose)
}
cursor = Date.NextDay(cursor)
}
return times
}
func (g generate) MarketDayAlternateCloses(from, to time.Time, marketOpen, marketClose time.Time, isHoliday HolidayProvider) []time.Time {
var times []time.Time
cursor := Date.On(marketOpen, from)
toClose := Date.On(marketClose, to)
for cursor.Before(toClose) || cursor.Equal(toClose) {
isValidTradingDay := !isHoliday(cursor) && Date.IsWeekDay(cursor.Weekday())
if isValidTradingDay {
todayClose := Date.On(marketClose, cursor)
times = append(times, todayClose)
}
cursor = cursor.AddDate(0, 0, 2)
}
return times
}
func (g generate) MarketDayMondayCloses(from, to time.Time, marketOpen, marketClose time.Time, isHoliday HolidayProvider) []time.Time {
var times []time.Time
cursor := Date.On(marketClose, from)
toClose := Date.On(marketClose, to)
for cursor.Equal(toClose) || cursor.Before(toClose) {
isValidTradingDay := !isHoliday(cursor) && Date.IsWeekDay(cursor.Weekday())
if isValidTradingDay {
times = append(times, cursor)
}
cursor = Date.NextDayOfWeek(cursor, time.Monday)
}
return times
}
func (g generate) Hours(start time.Time, totalHours int) []time.Time {
times := make([]time.Time, totalHours)
last := start
for i := 0; i < totalHours; i++ {
times[i] = last
last = last.Add(time.Hour)
}
return times
}
// HoursFilled adds zero values for the data bounded by the start and end of the xdata array.
func (g generate) HoursFilled(xdata []time.Time, ydata []float64) ([]time.Time, []float64) {
start := Date.Start(xdata)
end := Date.End(xdata)
totalHours := Math.AbsInt(Date.DiffHours(start, end))
finalTimes := g.Hours(start, totalHours+1)
finalValues := make([]float64, totalHours+1)
var hoursFromStart int
for i, xd := range xdata {
hoursFromStart = Date.DiffHours(start, xd)
finalValues[hoursFromStart] = ydata[i]
}
return finalTimes, finalValues
}

View file

@ -10,10 +10,10 @@ import (
func TestSequenceFloat64(t *testing.T) { func TestSequenceFloat64(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
asc := Sequence.Float64(1.0, 10.0) asc := Generate.Float64(1.0, 10.0)
assert.Len(asc, 10) assert.Len(asc, 10)
desc := Sequence.Float64(10.0, 1.0) desc := Generate.Float64(10.0, 1.0)
assert.Len(desc, 10) assert.Len(desc, 10)
} }
@ -21,7 +21,7 @@ func TestSequenceMarketHours(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
today := time.Date(2016, 07, 01, 12, 0, 0, 0, Date.Eastern()) today := time.Date(2016, 07, 01, 12, 0, 0, 0, Date.Eastern())
mh := Sequence.MarketHours(today, today, NYSEOpen(), NYSEClose(), Date.IsNYSEHoliday) mh := Generate.MarketHours(today, today, NYSEOpen(), NYSEClose(), Date.IsNYSEHoliday)
assert.Len(mh, 8) assert.Len(mh, 8)
assert.Equal(Date.Eastern(), mh[0].Location()) assert.Equal(Date.Eastern(), mh[0].Location())
} }
@ -29,7 +29,7 @@ func TestSequenceMarketHours(t *testing.T) {
func TestSequenceMarketQuarters(t *testing.T) { func TestSequenceMarketQuarters(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
today := time.Date(2016, 07, 01, 12, 0, 0, 0, Date.Eastern()) today := time.Date(2016, 07, 01, 12, 0, 0, 0, Date.Eastern())
mh := Sequence.MarketHourQuarters(today, today, NYSEOpen(), NYSEClose(), Date.IsNYSEHoliday) mh := Generate.MarketHourQuarters(today, today, NYSEOpen(), NYSEClose(), Date.IsNYSEHoliday)
assert.Len(mh, 4) assert.Len(mh, 4)
assert.Equal(9, mh[0].Hour()) assert.Equal(9, mh[0].Hour())
assert.Equal(30, mh[0].Minute()) assert.Equal(30, mh[0].Minute())
@ -48,7 +48,7 @@ func TestSequenceHours(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
today := time.Date(2016, 07, 01, 12, 0, 0, 0, time.UTC) today := time.Date(2016, 07, 01, 12, 0, 0, 0, time.UTC)
seq := Sequence.Hours(today, 24) seq := Generate.Hours(today, 24)
end := Date.End(seq) end := Date.End(seq)
assert.Len(seq, 24) assert.Len(seq, 24)
@ -81,7 +81,7 @@ func TestSequenceHoursFill(t *testing.T) {
0.6, 0.6,
} }
filledTimes, filledValues := Sequence.HoursFill(xdata, ydata) filledTimes, filledValues := Generate.HoursFilled(xdata, ydata)
assert.Len(filledTimes, Date.DiffHours(Date.Start(xdata), Date.End(xdata))+1) assert.Len(filledTimes, Date.DiffHours(Date.Start(xdata), Date.End(xdata))+1)
assert.Equal(len(filledValues), len(filledTimes)) assert.Equal(len(filledValues), len(filledTimes))

View file

@ -9,7 +9,7 @@ type HistogramSeries struct {
Name string Name string
Style Style Style Style
YAxis YAxisType YAxis YAxisType
InnerSeries ValueProvider InnerSeries ValuesProvider
} }
// GetName implements Series.GetName. // GetName implements Series.GetName.
@ -27,19 +27,19 @@ func (hs HistogramSeries) GetYAxis() YAxisType {
return hs.YAxis return hs.YAxis
} }
// Len implements BoundedValueProvider.Len. // Len implements BoundedValuesProvider.Len.
func (hs HistogramSeries) Len() int { func (hs HistogramSeries) Len() int {
return hs.InnerSeries.Len() return hs.InnerSeries.Len()
} }
// GetValue implements ValueProvider.GetValue. // GetValues implements ValuesProvider.GetValues.
func (hs HistogramSeries) GetValue(index int) (x, y float64) { func (hs HistogramSeries) GetValues(index int) (x, y float64) {
return hs.InnerSeries.GetValue(index) return hs.InnerSeries.GetValues(index)
} }
// GetBoundedValue implements BoundedValueProvider.GetBoundedValue // GetBoundedValues implements BoundedValuesProvider.GetBoundedValue
func (hs HistogramSeries) GetBoundedValue(index int) (x, y1, y2 float64) { func (hs HistogramSeries) GetBoundedValues(index int) (x, y1, y2 float64) {
vx, vy := hs.InnerSeries.GetValue(index) vx, vy := hs.InnerSeries.GetValues(index)
x = vx x = vx

View file

@ -11,8 +11,8 @@ func TestHistogramSeries(t *testing.T) {
cs := ContinuousSeries{ cs := ContinuousSeries{
Name: "Test Series", Name: "Test Series",
XValues: Sequence.Float64(1.0, 20.0), XValues: Generate.Float64(1.0, 20.0),
YValues: Sequence.Float64(10.0, -10.0), YValues: Generate.Float64(10.0, -10.0),
} }
hs := HistogramSeries{ hs := HistogramSeries{
@ -20,8 +20,8 @@ func TestHistogramSeries(t *testing.T) {
} }
for x := 0; x < hs.Len(); x++ { for x := 0; x < hs.Len(); x++ {
csx, csy := cs.GetValue(0) csx, csy := cs.GetValues(0)
hsx, hsy1, hsy2 := hs.GetBoundedValue(0) hsx, hsy1, hsy2 := hs.GetBoundedValues(0)
assert.Equal(csx, hsx) assert.Equal(csx, hsx)
assert.True(hsy1 > 0) assert.True(hsy1 > 0)
assert.True(hsy2 <= 0) assert.True(hsy2 <= 0)

View file

@ -3,7 +3,7 @@ package chart
import "fmt" import "fmt"
// LastValueAnnotation returns an annotation series of just the last value of a value provider. // LastValueAnnotation returns an annotation series of just the last value of a value provider.
func LastValueAnnotation(innerSeries ValueProvider, vfs ...ValueFormatter) AnnotationSeries { func LastValueAnnotation(innerSeries ValuesProvider, vfs ...ValueFormatter) AnnotationSeries {
var vf ValueFormatter var vf ValueFormatter
if len(vfs) > 0 { if len(vfs) > 0 {
vf = vfs[0] vf = vfs[0]
@ -14,11 +14,11 @@ func LastValueAnnotation(innerSeries ValueProvider, vfs ...ValueFormatter) Annot
} }
var lastValue Value2 var lastValue Value2
if typed, isTyped := innerSeries.(LastValueProvider); isTyped { if typed, isTyped := innerSeries.(LastValuesProvider); isTyped {
lastValue.XValue, lastValue.YValue = typed.GetLastValue() lastValue.XValue, lastValue.YValue = typed.GetLastValues()
lastValue.Label = vf(lastValue.YValue) lastValue.Label = vf(lastValue.YValue)
} else { } else {
lastValue.XValue, lastValue.YValue = innerSeries.GetValue(innerSeries.Len() - 1) lastValue.XValue, lastValue.YValue = innerSeries.GetValues(innerSeries.Len() - 1)
lastValue.Label = vf(lastValue.YValue) lastValue.Label = vf(lastValue.YValue)
} }

View file

@ -11,7 +11,7 @@ type LinearRegressionSeries struct {
Limit int Limit int
Offset int Offset int
InnerSeries ValueProvider InnerSeries ValuesProvider
m float64 m float64
b float64 b float64
@ -62,8 +62,8 @@ func (lrs LinearRegressionSeries) GetOffset() int {
return lrs.Offset return lrs.Offset
} }
// GetValue gets a value at a given index. // GetValues gets a value at a given index.
func (lrs *LinearRegressionSeries) GetValue(index int) (x, y float64) { func (lrs *LinearRegressionSeries) GetValues(index int) (x, y float64) {
if lrs.InnerSeries == nil || lrs.InnerSeries.Len() == 0 { if lrs.InnerSeries == nil || lrs.InnerSeries.Len() == 0 {
return return
} }
@ -72,13 +72,13 @@ func (lrs *LinearRegressionSeries) GetValue(index int) (x, y float64) {
} }
offset := lrs.GetOffset() offset := lrs.GetOffset()
effectiveIndex := Math.MinInt(index+offset, lrs.InnerSeries.Len()) effectiveIndex := Math.MinInt(index+offset, lrs.InnerSeries.Len())
x, y = lrs.InnerSeries.GetValue(effectiveIndex) x, y = lrs.InnerSeries.GetValues(effectiveIndex)
y = (lrs.m * lrs.normalize(x)) + lrs.b y = (lrs.m * lrs.normalize(x)) + lrs.b
return return
} }
// GetLastValue computes the last linear regression value. // GetLastValues computes the last linear regression value.
func (lrs *LinearRegressionSeries) GetLastValue() (x, y float64) { func (lrs *LinearRegressionSeries) GetLastValues() (x, y float64) {
if lrs.InnerSeries == nil || lrs.InnerSeries.Len() == 0 { if lrs.InnerSeries == nil || lrs.InnerSeries.Len() == 0 {
return return
} }
@ -86,7 +86,7 @@ func (lrs *LinearRegressionSeries) GetLastValue() (x, y float64) {
lrs.computeCoefficients() lrs.computeCoefficients()
} }
endIndex := lrs.GetEndIndex() endIndex := lrs.GetEndIndex()
x, y = lrs.InnerSeries.GetValue(endIndex) x, y = lrs.InnerSeries.GetValues(endIndex)
y = (lrs.m * lrs.normalize(x)) + lrs.b y = (lrs.m * lrs.normalize(x)) + lrs.b
return return
} }
@ -102,18 +102,18 @@ func (lrs *LinearRegressionSeries) computeCoefficients() {
p := float64(endIndex - startIndex) p := float64(endIndex - startIndex)
xvalues := NewRingBufferWithCapacity(lrs.Len()) xvalues := NewValueBufferWithCapacity(lrs.Len())
for index := startIndex; index < endIndex; index++ { for index := startIndex; index < endIndex; index++ {
x, _ := lrs.InnerSeries.GetValue(index) x, _ := lrs.InnerSeries.GetValues(index)
xvalues.Enqueue(x) xvalues.Enqueue(x)
} }
lrs.avgx = xvalues.Average() lrs.avgx = Sequence{xvalues}.Average()
lrs.stddevx = xvalues.StdDev() lrs.stddevx = Sequence{xvalues}.StdDev()
var sumx, sumy, sumxx, sumxy float64 var sumx, sumy, sumxx, sumxy float64
for index := startIndex; index < endIndex; index++ { for index := startIndex; index < endIndex; index++ {
x, y := lrs.InnerSeries.GetValue(index) x, y := lrs.InnerSeries.GetValues(index)
x = lrs.normalize(x) x = lrs.normalize(x)

View file

@ -11,19 +11,19 @@ func TestLinearRegressionSeries(t *testing.T) {
mainSeries := ContinuousSeries{ mainSeries := ContinuousSeries{
Name: "A test series", Name: "A test series",
XValues: Sequence.Float64(1.0, 100.0), XValues: Generate.Float64(1.0, 100.0),
YValues: Sequence.Float64(1.0, 100.0), YValues: Generate.Float64(1.0, 100.0),
} }
linRegSeries := &LinearRegressionSeries{ linRegSeries := &LinearRegressionSeries{
InnerSeries: mainSeries, InnerSeries: mainSeries,
} }
lrx0, lry0 := linRegSeries.GetValue(0) lrx0, lry0 := linRegSeries.GetValues(0)
assert.InDelta(1.0, lrx0, 0.0000001) assert.InDelta(1.0, lrx0, 0.0000001)
assert.InDelta(1.0, lry0, 0.0000001) assert.InDelta(1.0, lry0, 0.0000001)
lrxn, lryn := linRegSeries.GetLastValue() lrxn, lryn := linRegSeries.GetLastValues()
assert.InDelta(100.0, lrxn, 0.0000001) assert.InDelta(100.0, lrxn, 0.0000001)
assert.InDelta(100.0, lryn, 0.0000001) assert.InDelta(100.0, lryn, 0.0000001)
} }
@ -33,19 +33,19 @@ func TestLinearRegressionSeriesDesc(t *testing.T) {
mainSeries := ContinuousSeries{ mainSeries := ContinuousSeries{
Name: "A test series", Name: "A test series",
XValues: Sequence.Float64(100.0, 1.0), XValues: Generate.Float64(100.0, 1.0),
YValues: Sequence.Float64(100.0, 1.0), YValues: Generate.Float64(100.0, 1.0),
} }
linRegSeries := &LinearRegressionSeries{ linRegSeries := &LinearRegressionSeries{
InnerSeries: mainSeries, InnerSeries: mainSeries,
} }
lrx0, lry0 := linRegSeries.GetValue(0) lrx0, lry0 := linRegSeries.GetValues(0)
assert.InDelta(100.0, lrx0, 0.0000001) assert.InDelta(100.0, lrx0, 0.0000001)
assert.InDelta(100.0, lry0, 0.0000001) assert.InDelta(100.0, lry0, 0.0000001)
lrxn, lryn := linRegSeries.GetLastValue() lrxn, lryn := linRegSeries.GetLastValues()
assert.InDelta(1.0, lrxn, 0.0000001) assert.InDelta(1.0, lrxn, 0.0000001)
assert.InDelta(1.0, lryn, 0.0000001) assert.InDelta(1.0, lryn, 0.0000001)
} }
@ -55,8 +55,8 @@ func TestLinearRegressionSeriesWindowAndOffset(t *testing.T) {
mainSeries := ContinuousSeries{ mainSeries := ContinuousSeries{
Name: "A test series", Name: "A test series",
XValues: Sequence.Float64(100.0, 1.0), XValues: Generate.Float64(100.0, 1.0),
YValues: Sequence.Float64(100.0, 1.0), YValues: Generate.Float64(100.0, 1.0),
} }
linRegSeries := &LinearRegressionSeries{ linRegSeries := &LinearRegressionSeries{
@ -67,11 +67,11 @@ func TestLinearRegressionSeriesWindowAndOffset(t *testing.T) {
assert.Equal(10, linRegSeries.Len()) assert.Equal(10, linRegSeries.Len())
lrx0, lry0 := linRegSeries.GetValue(0) lrx0, lry0 := linRegSeries.GetValues(0)
assert.InDelta(90.0, lrx0, 0.0000001) assert.InDelta(90.0, lrx0, 0.0000001)
assert.InDelta(90.0, lry0, 0.0000001) assert.InDelta(90.0, lry0, 0.0000001)
lrxn, lryn := linRegSeries.GetLastValue() lrxn, lryn := linRegSeries.GetLastValues()
assert.InDelta(80.0, lrxn, 0.0000001) assert.InDelta(80.0, lrxn, 0.0000001)
assert.InDelta(80.0, lryn, 0.0000001) assert.InDelta(80.0, lryn, 0.0000001)
} }

View file

@ -17,7 +17,7 @@ type MACDSeries struct {
Name string Name string
Style Style Style Style
YAxis YAxisType YAxis YAxisType
InnerSeries ValueProvider InnerSeries ValuesProvider
PrimaryPeriod int PrimaryPeriod int
SecondaryPeriod int SecondaryPeriod int
@ -89,8 +89,8 @@ func (macd MACDSeries) Len() int {
return macd.InnerSeries.Len() return macd.InnerSeries.Len()
} }
// GetValue gets a value at a given index. For MACD it is the signal value. // GetValues gets a value at a given index. For MACD it is the signal value.
func (macd *MACDSeries) GetValue(index int) (x float64, y float64) { func (macd *MACDSeries) GetValues(index int) (x float64, y float64) {
if macd.InnerSeries == nil { if macd.InnerSeries == nil {
return return
} }
@ -99,10 +99,10 @@ func (macd *MACDSeries) GetValue(index int) (x float64, y float64) {
macd.ensureChildSeries() macd.ensureChildSeries()
} }
_, lv := macd.macdl.GetValue(index) _, lv := macd.macdl.GetValues(index)
_, sv := macd.signal.GetValue(index) _, sv := macd.signal.GetValues(index)
x, _ = macd.InnerSeries.GetValue(index) x, _ = macd.InnerSeries.GetValues(index)
y = lv - sv y = lv - sv
return return
@ -130,7 +130,7 @@ type MACDSignalSeries struct {
Name string Name string
Style Style Style Style
YAxis YAxisType YAxis YAxisType
InnerSeries ValueProvider InnerSeries ValuesProvider
PrimaryPeriod int PrimaryPeriod int
SecondaryPeriod int SecondaryPeriod int
@ -191,8 +191,8 @@ func (macds *MACDSignalSeries) Len() int {
return macds.InnerSeries.Len() return macds.InnerSeries.Len()
} }
// GetValue gets a value at a given index. For MACD it is the signal value. // GetValues gets a value at a given index. For MACD it is the signal value.
func (macds *MACDSignalSeries) GetValue(index int) (x float64, y float64) { func (macds *MACDSignalSeries) GetValues(index int) (x float64, y float64) {
if macds.InnerSeries == nil { if macds.InnerSeries == nil {
return return
} }
@ -200,8 +200,8 @@ func (macds *MACDSignalSeries) GetValue(index int) (x float64, y float64) {
if macds.signal == nil { if macds.signal == nil {
macds.ensureSignal() macds.ensureSignal()
} }
x, _ = macds.InnerSeries.GetValue(index) x, _ = macds.InnerSeries.GetValues(index)
_, y = macds.signal.GetValue(index) _, y = macds.signal.GetValues(index)
return return
} }
@ -229,7 +229,7 @@ type MACDLineSeries struct {
Name string Name string
Style Style Style Style
YAxis YAxisType YAxis YAxisType
InnerSeries ValueProvider InnerSeries ValuesProvider
PrimaryPeriod int PrimaryPeriod int
SecondaryPeriod int SecondaryPeriod int
@ -300,8 +300,8 @@ func (macdl *MACDLineSeries) Len() int {
return macdl.InnerSeries.Len() return macdl.InnerSeries.Len()
} }
// GetValue gets a value at a given index. For MACD it is the signal value. // GetValues gets a value at a given index. For MACD it is the signal value.
func (macdl *MACDLineSeries) GetValue(index int) (x float64, y float64) { func (macdl *MACDLineSeries) GetValues(index int) (x float64, y float64) {
if macdl.InnerSeries == nil { if macdl.InnerSeries == nil {
return return
} }
@ -309,10 +309,10 @@ func (macdl *MACDLineSeries) GetValue(index int) (x float64, y float64) {
macdl.ensureEMASeries() macdl.ensureEMASeries()
} }
x, _ = macdl.InnerSeries.GetValue(index) x, _ = macdl.InnerSeries.GetValues(index)
_, emav1 := macdl.ema1.GetValue(index) _, emav1 := macdl.ema1.GetValues(index)
_, emav2 := macdl.ema2.GetValue(index) _, emav2 := macdl.ema2.GetValues(index)
y = emav2 - emav1 y = emav2 - emav1
return return

View file

@ -65,7 +65,7 @@ var (
func TestMACDSeries(t *testing.T) { func TestMACDSeries(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
mockSeries := mockValueProvider{ mockSeries := mockValuesProvider{
emaXValues, emaXValues,
emaYValues, emaYValues,
} }
@ -77,7 +77,7 @@ func TestMACDSeries(t *testing.T) {
var yvalues []float64 var yvalues []float64
for x := 0; x < mas.Len(); x++ { for x := 0; x < mas.Len(); x++ {
_, y := mas.GetValue(x) _, y := mas.GetValues(x)
yvalues = append(yvalues, y) yvalues = append(yvalues, y)
} }

View file

@ -112,31 +112,31 @@ func (mhr MarketHoursRange) GetMarketClose() time.Time {
// GetTicks returns the ticks for the range. // GetTicks returns the ticks for the range.
// This is to override the default continous ticks that would be generated for the range. // This is to override the default continous ticks that would be generated for the range.
func (mhr *MarketHoursRange) GetTicks(r Renderer, defaults Style, vf ValueFormatter) []Tick { func (mhr *MarketHoursRange) GetTicks(r Renderer, defaults Style, vf ValueFormatter) []Tick {
times := Sequence.MarketHours(mhr.Min, mhr.Max, mhr.GetMarketOpen(), mhr.GetMarketClose(), mhr.GetHolidayProvider()) times := Generate.MarketHours(mhr.Min, mhr.Max, mhr.GetMarketOpen(), mhr.GetMarketClose(), mhr.GetHolidayProvider())
timesWidth := mhr.measureTimes(r, defaults, vf, times) timesWidth := mhr.measureTimes(r, defaults, vf, times)
if timesWidth <= mhr.Domain { if timesWidth <= mhr.Domain {
return mhr.makeTicks(vf, times) return mhr.makeTicks(vf, times)
} }
times = Sequence.MarketHourQuarters(mhr.Min, mhr.Max, mhr.GetMarketOpen(), mhr.GetMarketClose(), mhr.GetHolidayProvider()) times = Generate.MarketHourQuarters(mhr.Min, mhr.Max, mhr.GetMarketOpen(), mhr.GetMarketClose(), mhr.GetHolidayProvider())
timesWidth = mhr.measureTimes(r, defaults, vf, times) timesWidth = mhr.measureTimes(r, defaults, vf, times)
if timesWidth <= mhr.Domain { if timesWidth <= mhr.Domain {
return mhr.makeTicks(vf, times) return mhr.makeTicks(vf, times)
} }
times = Sequence.MarketDayCloses(mhr.Min, mhr.Max, mhr.GetMarketOpen(), mhr.GetMarketClose(), mhr.GetHolidayProvider()) times = Generate.MarketDayCloses(mhr.Min, mhr.Max, mhr.GetMarketOpen(), mhr.GetMarketClose(), mhr.GetHolidayProvider())
timesWidth = mhr.measureTimes(r, defaults, vf, times) timesWidth = mhr.measureTimes(r, defaults, vf, times)
if timesWidth <= mhr.Domain { if timesWidth <= mhr.Domain {
return mhr.makeTicks(vf, times) return mhr.makeTicks(vf, times)
} }
times = Sequence.MarketDayAlternateCloses(mhr.Min, mhr.Max, mhr.GetMarketOpen(), mhr.GetMarketClose(), mhr.GetHolidayProvider()) times = Generate.MarketDayAlternateCloses(mhr.Min, mhr.Max, mhr.GetMarketOpen(), mhr.GetMarketClose(), mhr.GetHolidayProvider())
timesWidth = mhr.measureTimes(r, defaults, vf, times) timesWidth = mhr.measureTimes(r, defaults, vf, times)
if timesWidth <= mhr.Domain { if timesWidth <= mhr.Domain {
return mhr.makeTicks(vf, times) return mhr.makeTicks(vf, times)
} }
times = Sequence.MarketDayMondayCloses(mhr.Min, mhr.Max, mhr.GetMarketOpen(), mhr.GetMarketClose(), mhr.GetHolidayProvider()) times = Generate.MarketDayMondayCloses(mhr.Min, mhr.Max, mhr.GetMarketOpen(), mhr.GetMarketClose(), mhr.GetHolidayProvider())
timesWidth = mhr.measureTimes(r, defaults, vf, times) timesWidth = mhr.measureTimes(r, defaults, vf, times)
if timesWidth <= mhr.Domain { if timesWidth <= mhr.Domain {
return mhr.makeTicks(vf, times) return mhr.makeTicks(vf, times)

View file

@ -10,7 +10,7 @@ type MinSeries struct {
Name string Name string
Style Style Style Style
YAxis YAxisType YAxis YAxisType
InnerSeries ValueProvider InnerSeries ValuesProvider
minValue *float64 minValue *float64
} }
@ -35,10 +35,10 @@ func (ms MinSeries) Len() int {
return ms.InnerSeries.Len() return ms.InnerSeries.Len()
} }
// GetValue gets a value at a given index. // GetValues gets a value at a given index.
func (ms *MinSeries) GetValue(index int) (x, y float64) { func (ms *MinSeries) GetValues(index int) (x, y float64) {
ms.ensureMinValue() ms.ensureMinValue()
x, _ = ms.InnerSeries.GetValue(index) x, _ = ms.InnerSeries.GetValues(index)
y = *ms.minValue y = *ms.minValue
return return
} }
@ -54,7 +54,7 @@ func (ms *MinSeries) ensureMinValue() {
minValue := math.MaxFloat64 minValue := math.MaxFloat64
var y float64 var y float64
for x := 0; x < ms.InnerSeries.Len(); x++ { for x := 0; x < ms.InnerSeries.Len(); x++ {
_, y = ms.InnerSeries.GetValue(x) _, y = ms.InnerSeries.GetValues(x)
if y < minValue { if y < minValue {
minValue = y minValue = y
} }
@ -76,7 +76,7 @@ type MaxSeries struct {
Name string Name string
Style Style Style Style
YAxis YAxisType YAxis YAxisType
InnerSeries ValueProvider InnerSeries ValuesProvider
maxValue *float64 maxValue *float64
} }
@ -101,10 +101,10 @@ func (ms MaxSeries) Len() int {
return ms.InnerSeries.Len() return ms.InnerSeries.Len()
} }
// GetValue gets a value at a given index. // GetValues gets a value at a given index.
func (ms *MaxSeries) GetValue(index int) (x, y float64) { func (ms *MaxSeries) GetValues(index int) (x, y float64) {
ms.ensureMaxValue() ms.ensureMaxValue()
x, _ = ms.InnerSeries.GetValue(index) x, _ = ms.InnerSeries.GetValues(index)
y = *ms.maxValue y = *ms.maxValue
return return
} }
@ -120,7 +120,7 @@ func (ms *MaxSeries) ensureMaxValue() {
maxValue := -math.MaxFloat64 maxValue := -math.MaxFloat64
var y float64 var y float64
for x := 0; x < ms.InnerSeries.Len(); x++ { for x := 0; x < ms.InnerSeries.Len(); x++ {
_, y = ms.InnerSeries.GetValue(x) _, y = ms.InnerSeries.GetValues(x)
if y > maxValue { if y > maxValue {
maxValue = y maxValue = y
} }

View file

@ -17,7 +17,7 @@ type PolynomialRegressionSeries struct {
Limit int Limit int
Offset int Offset int
Degree int Degree int
InnerSeries ValueProvider InnerSeries ValuesProvider
coeffs []float64 coeffs []float64
} }
@ -79,8 +79,8 @@ func (prs *PolynomialRegressionSeries) Validate() error {
return nil return nil
} }
// GetValue returns the series value for a given index. // GetValues returns the series value for a given index.
func (prs *PolynomialRegressionSeries) GetValue(index int) (x, y float64) { func (prs *PolynomialRegressionSeries) GetValues(index int) (x, y float64) {
if prs.InnerSeries == nil || prs.InnerSeries.Len() == 0 { if prs.InnerSeries == nil || prs.InnerSeries.Len() == 0 {
return return
} }
@ -95,13 +95,13 @@ func (prs *PolynomialRegressionSeries) GetValue(index int) (x, y float64) {
offset := prs.GetOffset() offset := prs.GetOffset()
effectiveIndex := Math.MinInt(index+offset, prs.InnerSeries.Len()) effectiveIndex := Math.MinInt(index+offset, prs.InnerSeries.Len())
x, y = prs.InnerSeries.GetValue(effectiveIndex) x, y = prs.InnerSeries.GetValues(effectiveIndex)
y = prs.apply(x) y = prs.apply(x)
return return
} }
// GetLastValue computes the last poly regression value. // GetLastValues computes the last poly regression value.
func (prs *PolynomialRegressionSeries) GetLastValue() (x, y float64) { func (prs *PolynomialRegressionSeries) GetLastValues() (x, y float64) {
if prs.InnerSeries == nil || prs.InnerSeries.Len() == 0 { if prs.InnerSeries == nil || prs.InnerSeries.Len() == 0 {
return return
} }
@ -113,7 +113,7 @@ func (prs *PolynomialRegressionSeries) GetLastValue() (x, y float64) {
prs.coeffs = coeffs prs.coeffs = coeffs
} }
endIndex := prs.GetEndIndex() endIndex := prs.GetEndIndex()
x, y = prs.InnerSeries.GetValue(endIndex) x, y = prs.InnerSeries.GetValues(endIndex)
y = prs.apply(x) y = prs.apply(x)
return return
} }
@ -138,7 +138,7 @@ func (prs *PolynomialRegressionSeries) values() (xvalues, yvalues []float64) {
yvalues = make([]float64, endIndex-startIndex) yvalues = make([]float64, endIndex-startIndex)
for index := startIndex; index < endIndex; index++ { for index := startIndex; index < endIndex; index++ {
x, y := prs.InnerSeries.GetValue(index) x, y := prs.InnerSeries.GetValues(index)
xvalues[index-startIndex] = x xvalues[index-startIndex] = x
yvalues[index-startIndex] = y yvalues[index-startIndex] = y
} }

View file

@ -29,7 +29,7 @@ func TestPolynomialRegression(t *testing.T) {
} }
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
_, y := poly.GetValue(i) _, y := poly.GetValues(i)
assert.InDelta(float64(i*i), y, matrix.DefaultEpsilon) assert.InDelta(float64(i*i), y, matrix.DefaultEpsilon)
} }
} }

View file

@ -1,252 +0,0 @@
package chart
import (
"fmt"
"math"
"strings"
)
const (
ringBufferMinimumGrow = 4
ringBufferShrinkThreshold = 32
ringBufferGrowFactor = 200
ringBufferDefaultCapacity = 4
)
var (
emptyArray = make([]interface{}, 0)
)
// NewRingBuffer creates a new, empty, RingBuffer.
func NewRingBuffer() *RingBuffer {
return &RingBuffer{
array: make([]interface{}, ringBufferDefaultCapacity),
head: 0,
tail: 0,
size: 0,
}
}
// NewRingBufferWithCapacity creates a new RingBuffer pre-allocated with the given capacity.
func NewRingBufferWithCapacity(capacity int) *RingBuffer {
return &RingBuffer{
array: make([]interface{}, capacity),
head: 0,
tail: 0,
size: 0,
}
}
// NewRingBufferFromSlice createsa ring buffer out of a slice.
func NewRingBufferFromSlice(values []interface{}) *RingBuffer {
return &RingBuffer{
array: values,
head: 0,
tail: len(values) - 1,
size: len(values),
}
}
// RingBuffer is a fifo buffer that is backed by a pre-allocated array, instead of allocating
// a whole new node object for each element (which saves GC churn).
// Enqueue can be O(n), Dequeue can be O(1).
type RingBuffer struct {
array []interface{}
head int
tail int
size int
}
// Len returns the length of the ring buffer (as it is currently populated).
// Actual memory footprint may be different.
func (rb *RingBuffer) Len() int {
return rb.size
}
// TotalLen returns the total size of the ring bufffer, including empty elements.
func (rb *RingBuffer) TotalLen() int {
return len(rb.array)
}
// Clear removes all objects from the RingBuffer.
func (rb *RingBuffer) Clear() {
if rb.head < rb.tail {
arrayClear(rb.array, rb.head, rb.size)
} else {
arrayClear(rb.array, rb.head, len(rb.array)-rb.head)
arrayClear(rb.array, 0, rb.tail)
}
rb.head = 0
rb.tail = 0
rb.size = 0
}
// Enqueue adds an element to the "back" of the RingBuffer.
func (rb *RingBuffer) Enqueue(object interface{}) {
if rb.size == len(rb.array) {
newCapacity := int(len(rb.array) * int(ringBufferGrowFactor/100))
if newCapacity < (len(rb.array) + ringBufferMinimumGrow) {
newCapacity = len(rb.array) + ringBufferMinimumGrow
}
rb.setCapacity(newCapacity)
}
rb.array[rb.tail] = object
rb.tail = (rb.tail + 1) % len(rb.array)
rb.size++
}
// Dequeue removes the first element from the RingBuffer.
func (rb *RingBuffer) Dequeue() interface{} {
if rb.size == 0 {
return nil
}
removed := rb.array[rb.head]
rb.head = (rb.head + 1) % len(rb.array)
rb.size--
return removed
}
// Peek returns but does not remove the first element.
func (rb *RingBuffer) Peek() interface{} {
if rb.size == 0 {
return nil
}
return rb.array[rb.head]
}
// PeekBack returns but does not remove the last element.
func (rb *RingBuffer) PeekBack() interface{} {
if rb.size == 0 {
return nil
}
if rb.tail == 0 {
return rb.array[len(rb.array)-1]
}
return rb.array[rb.tail-1]
}
func (rb *RingBuffer) setCapacity(capacity int) {
newArray := make([]interface{}, capacity)
if rb.size > 0 {
if rb.head < rb.tail {
arrayCopy(rb.array, rb.head, newArray, 0, rb.size)
} else {
arrayCopy(rb.array, rb.head, newArray, 0, len(rb.array)-rb.head)
arrayCopy(rb.array, 0, newArray, len(rb.array)-rb.head, rb.tail)
}
}
rb.array = newArray
rb.head = 0
if rb.size == capacity {
rb.tail = 0
} else {
rb.tail = rb.size
}
}
// TrimExcess resizes the buffer to better fit the contents.
func (rb *RingBuffer) TrimExcess() {
threshold := float64(len(rb.array)) * 0.9
if rb.size < int(threshold) {
rb.setCapacity(rb.size)
}
}
// AsSlice returns the ring buffer, in order, as a slice.
func (rb *RingBuffer) AsSlice() []interface{} {
newArray := make([]interface{}, rb.size)
if rb.size == 0 {
return newArray
}
if rb.head < rb.tail {
arrayCopy(rb.array, rb.head, newArray, 0, rb.size)
} else {
arrayCopy(rb.array, rb.head, newArray, 0, len(rb.array)-rb.head)
arrayCopy(rb.array, 0, newArray, len(rb.array)-rb.head, rb.tail)
}
return newArray
}
// Each calls the consumer for each element in the buffer.
func (rb *RingBuffer) Each(consumer func(value interface{})) {
if rb.size == 0 {
return
}
if rb.head < rb.tail {
for cursor := rb.head; cursor < rb.tail; cursor++ {
consumer(rb.array[cursor])
}
} else {
for cursor := rb.head; cursor < len(rb.array); cursor++ {
consumer(rb.array[cursor])
}
for cursor := 0; cursor < rb.tail; cursor++ {
consumer(rb.array[cursor])
}
}
}
func (rb *RingBuffer) String() string {
var values []string
for _, elem := range rb.AsSlice() {
values = append(values, fmt.Sprintf("%v", elem))
}
return strings.Join(values, " <= ")
}
// Average returns the float average of the values in the buffer.
func (rb *RingBuffer) Average() float64 {
var accum float64
rb.Each(func(v interface{}) {
if typed, isTyped := v.(float64); isTyped {
accum += typed
}
})
return accum / float64(rb.Len())
}
// Variance computes the variance of the buffer.
func (rb *RingBuffer) Variance() float64 {
if rb.Len() == 0 {
return 0
}
var variance float64
m := rb.Average()
rb.Each(func(v interface{}) {
if n, isTyped := v.(float64); isTyped {
variance += (float64(n) - m) * (float64(n) - m)
}
})
return variance / float64(rb.Len())
}
// StdDev returns the standard deviation.
func (rb *RingBuffer) StdDev() float64 {
return math.Pow(rb.Variance(), 0.5)
}
func arrayClear(source []interface{}, index, length int) {
for x := 0; x < length; x++ {
absoluteIndex := x + index
source[absoluteIndex] = nil
}
}
func arrayCopy(source []interface{}, sourceIndex int, destination []interface{}, destinationIndex, length int) {
for x := 0; x < length; x++ {
from := sourceIndex + x
to := destinationIndex + x
destination[to] = source[from]
}
}

View file

@ -1,190 +1,82 @@
package chart package chart
import ( import "math"
"math/rand"
"time"
)
var ( // SequenceProvider is a provider for values for a sequence.
// Sequence contains some sequence utilities. type SequenceProvider interface {
// These utilities can be useful for generating test data. Len() int
Sequence = &sequence{ GetValue(int) float64
rnd: rand.New(rand.NewSource(time.Now().Unix())),
}
)
type sequence struct {
rnd *rand.Rand
} }
// Float64 produces an array of floats from [start,end] by optional steps. // Sequence is a utility wrapper for sequence providers.
func (s sequence) Float64(start, end float64, steps ...float64) []float64 { type Sequence struct {
var values []float64 SequenceProvider
step := 1.0
if len(steps) > 0 {
step = steps[0]
}
if start < end {
for x := start; x <= end; x += step {
values = append(values, x)
}
} else {
for x := start; x >= end; x = x - step {
values = append(values, x)
}
}
return values
} }
// Random generates a fixed length sequence of random values between (0, scale). // Each applies the `mapfn` to all values in the value provider.
func (s sequence) Random(samples int, scale float64) []float64 { func (s Sequence) Each(mapfn func(int, float64)) {
values := make([]float64, samples) for i := 0; i < s.Len(); i++ {
mapfn(i, s.GetValue(i))
for x := 0; x < samples; x++ {
values[x] = s.rnd.Float64() * scale
} }
return values
} }
// Random generates a fixed length sequence of random values with a given average, above and below that average by (-scale, scale) // Map applies the `mapfn` to all values in the value provider,
func (s sequence) RandomWithAverage(samples int, average, scale float64) []float64 { // returning a new sequence.
values := make([]float64, samples) func (s Sequence) Map(mapfn func(i int, v float64) float64) Sequence {
output := make([]float64, s.Len())
for x := 0; x < samples; x++ { for i := 0; i < s.Len(); i++ {
jitter := scale - (s.rnd.Float64() * (2 * scale)) mapfn(i, s.GetValue(i))
values[x] = average + jitter
} }
return Sequence{Array(output)}
return values
} }
// Days generates a sequence of timestamps by day, from -days to today. // Average returns the float average of the values in the buffer.
func (s sequence) Days(days int) []time.Time { func (s Sequence) Average() float64 {
var values []time.Time if s.Len() == 0 {
for day := days; day >= 0; day-- { return 0
values = append(values, time.Now().AddDate(0, 0, -day))
}
return values
}
func (s sequence) MarketHours(from, to time.Time, marketOpen, marketClose time.Time, isHoliday HolidayProvider) []time.Time {
var times []time.Time
cursor := Date.On(marketOpen, from)
toClose := Date.On(marketClose, to)
for cursor.Before(toClose) || cursor.Equal(toClose) {
todayOpen := Date.On(marketOpen, cursor)
todayClose := Date.On(marketClose, cursor)
isValidTradingDay := !isHoliday(cursor) && Date.IsWeekDay(cursor.Weekday())
if (cursor.Equal(todayOpen) || cursor.After(todayOpen)) && (cursor.Equal(todayClose) || cursor.Before(todayClose)) && isValidTradingDay {
times = append(times, cursor)
}
if cursor.After(todayClose) {
cursor = Date.NextMarketOpen(cursor, marketOpen, isHoliday)
} else {
cursor = Date.NextHour(cursor)
}
}
return times
}
func (s sequence) MarketHourQuarters(from, to time.Time, marketOpen, marketClose time.Time, isHoliday HolidayProvider) []time.Time {
var times []time.Time
cursor := Date.On(marketOpen, from)
toClose := Date.On(marketClose, to)
for cursor.Before(toClose) || cursor.Equal(toClose) {
isValidTradingDay := !isHoliday(cursor) && Date.IsWeekDay(cursor.Weekday())
if isValidTradingDay {
todayOpen := Date.On(marketOpen, cursor)
todayNoon := Date.NoonOn(cursor)
today2pm := Date.On(Date.Time(14, 0, 0, 0, cursor.Location()), cursor)
todayClose := Date.On(marketClose, cursor)
times = append(times, todayOpen, todayNoon, today2pm, todayClose)
}
cursor = Date.NextDay(cursor)
}
return times
}
func (s sequence) MarketDayCloses(from, to time.Time, marketOpen, marketClose time.Time, isHoliday HolidayProvider) []time.Time {
var times []time.Time
cursor := Date.On(marketOpen, from)
toClose := Date.On(marketClose, to)
for cursor.Before(toClose) || cursor.Equal(toClose) {
isValidTradingDay := !isHoliday(cursor) && Date.IsWeekDay(cursor.Weekday())
if isValidTradingDay {
todayClose := Date.On(marketClose, cursor)
times = append(times, todayClose)
}
cursor = Date.NextDay(cursor)
}
return times
}
func (s sequence) MarketDayAlternateCloses(from, to time.Time, marketOpen, marketClose time.Time, isHoliday HolidayProvider) []time.Time {
var times []time.Time
cursor := Date.On(marketOpen, from)
toClose := Date.On(marketClose, to)
for cursor.Before(toClose) || cursor.Equal(toClose) {
isValidTradingDay := !isHoliday(cursor) && Date.IsWeekDay(cursor.Weekday())
if isValidTradingDay {
todayClose := Date.On(marketClose, cursor)
times = append(times, todayClose)
}
cursor = cursor.AddDate(0, 0, 2)
}
return times
}
func (s sequence) MarketDayMondayCloses(from, to time.Time, marketOpen, marketClose time.Time, isHoliday HolidayProvider) []time.Time {
var times []time.Time
cursor := Date.On(marketClose, from)
toClose := Date.On(marketClose, to)
for cursor.Equal(toClose) || cursor.Before(toClose) {
isValidTradingDay := !isHoliday(cursor) && Date.IsWeekDay(cursor.Weekday())
if isValidTradingDay {
times = append(times, cursor)
}
cursor = Date.NextDayOfWeek(cursor, time.Monday)
}
return times
}
func (s sequence) Hours(start time.Time, totalHours int) []time.Time {
times := make([]time.Time, totalHours)
last := start
for i := 0; i < totalHours; i++ {
times[i] = last
last = last.Add(time.Hour)
} }
return times var accum float64
for i := 0; i < s.Len(); i++ {
accum += s.GetValue(i)
}
return accum / float64(s.Len())
} }
// HoursFill adds zero values for the data bounded by the start and end of the xdata array. // Variance computes the variance of the buffer.
func (s sequence) HoursFill(xdata []time.Time, ydata []float64) ([]time.Time, []float64) { func (s Sequence) Variance() float64 {
start := Date.Start(xdata) if s.Len() == 0 {
end := Date.End(xdata) return 0
totalHours := Math.AbsInt(Date.DiffHours(start, end))
finalTimes := s.Hours(start, totalHours+1)
finalValues := make([]float64, totalHours+1)
var hoursFromStart int
for i, xd := range xdata {
hoursFromStart = Date.DiffHours(start, xd)
finalValues[hoursFromStart] = ydata[i]
} }
return finalTimes, finalValues m := s.Average()
var variance, v float64
for i := 0; i < s.Len(); i++ {
v = s.GetValue(i)
variance += (v - m) * (v - m)
}
return variance / float64(s.Len())
}
// StdDev returns the standard deviation.
func (s Sequence) StdDev() float64 {
if s.Len() == 0 {
return 0
}
return math.Pow(s.Variance(), 0.5)
}
// Array is a wrapper for an array of floats that implements `ValuesProvider`.
type Array []float64
// Len returns the value provider length.
func (a Array) Len() int {
return len(a)
}
// GetValue returns the value at a given index.
func (a Array) GetValue(index int) float64 {
return a[index]
} }

View file

@ -14,7 +14,7 @@ type SMASeries struct {
YAxis YAxisType YAxis YAxisType
Period int Period int
InnerSeries ValueProvider InnerSeries ValuesProvider
} }
// GetName returns the name of the time series. // GetName returns the name of the time series.
@ -48,25 +48,25 @@ func (sma SMASeries) GetPeriod(defaults ...int) int {
return sma.Period return sma.Period
} }
// GetValue gets a value at a given index. // GetValues gets a value at a given index.
func (sma SMASeries) GetValue(index int) (x, y float64) { func (sma SMASeries) GetValues(index int) (x, y float64) {
if sma.InnerSeries == nil || sma.InnerSeries.Len() == 0 { if sma.InnerSeries == nil || sma.InnerSeries.Len() == 0 {
return return
} }
px, _ := sma.InnerSeries.GetValue(index) px, _ := sma.InnerSeries.GetValues(index)
x = px x = px
y = sma.getAverage(index) y = sma.getAverage(index)
return return
} }
// GetLastValue computes the last moving average value but walking back window size samples, // GetLastValues computes the last moving average value but walking back window size samples,
// and recomputing the last moving average chunk. // and recomputing the last moving average chunk.
func (sma SMASeries) GetLastValue() (x, y float64) { func (sma SMASeries) GetLastValues() (x, y float64) {
if sma.InnerSeries == nil || sma.InnerSeries.Len() == 0 { if sma.InnerSeries == nil || sma.InnerSeries.Len() == 0 {
return return
} }
seriesLen := sma.InnerSeries.Len() seriesLen := sma.InnerSeries.Len()
px, _ := sma.InnerSeries.GetValue(seriesLen - 1) px, _ := sma.InnerSeries.GetValues(seriesLen - 1)
x = px x = px
y = sma.getAverage(seriesLen - 1) y = sma.getAverage(seriesLen - 1)
return return
@ -78,7 +78,7 @@ func (sma SMASeries) getAverage(index int) float64 {
var accum float64 var accum float64
var count float64 var count float64
for x := index; x >= floor; x-- { for x := index; x >= floor; x-- {
_, vy := sma.InnerSeries.GetValue(x) _, vy := sma.InnerSeries.GetValues(x)
accum += vy accum += vy
count += 1.0 count += 1.0
} }

View file

@ -6,16 +6,16 @@ import (
"github.com/blendlabs/go-assert" "github.com/blendlabs/go-assert"
) )
type mockValueProvider struct { type mockValuesProvider struct {
X []float64 X []float64
Y []float64 Y []float64
} }
func (m mockValueProvider) Len() int { func (m mockValuesProvider) Len() int {
return Math.MinInt(len(m.X), len(m.Y)) return Math.MinInt(len(m.X), len(m.Y))
} }
func (m mockValueProvider) GetValue(index int) (x, y float64) { func (m mockValuesProvider) GetValues(index int) (x, y float64) {
if index < 0 { if index < 0 {
panic("negative index at GetValue()") panic("negative index at GetValue()")
} }
@ -30,9 +30,9 @@ func (m mockValueProvider) GetValue(index int) (x, y float64) {
func TestSMASeriesGetValue(t *testing.T) { func TestSMASeriesGetValue(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
mockSeries := mockValueProvider{ mockSeries := mockValuesProvider{
Sequence.Float64(1.0, 10.0), Generate.Float64(1.0, 10.0),
Sequence.Float64(10, 1.0), Generate.Float64(10, 1.0),
} }
assert.Equal(10, mockSeries.Len()) assert.Equal(10, mockSeries.Len())
@ -43,7 +43,7 @@ func TestSMASeriesGetValue(t *testing.T) {
var yvalues []float64 var yvalues []float64
for x := 0; x < mas.Len(); x++ { for x := 0; x < mas.Len(); x++ {
_, y := mas.GetValue(x) _, y := mas.GetValues(x)
yvalues = append(yvalues, y) yvalues = append(yvalues, y)
} }
@ -61,9 +61,9 @@ func TestSMASeriesGetValue(t *testing.T) {
func TestSMASeriesGetLastValueWindowOverlap(t *testing.T) { func TestSMASeriesGetLastValueWindowOverlap(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
mockSeries := mockValueProvider{ mockSeries := mockValuesProvider{
Sequence.Float64(1.0, 10.0), Generate.Float64(1.0, 10.0),
Sequence.Float64(10, 1.0), Generate.Float64(10, 1.0),
} }
assert.Equal(10, mockSeries.Len()) assert.Equal(10, mockSeries.Len())
@ -74,11 +74,11 @@ func TestSMASeriesGetLastValueWindowOverlap(t *testing.T) {
var yvalues []float64 var yvalues []float64
for x := 0; x < mas.Len(); x++ { for x := 0; x < mas.Len(); x++ {
_, y := mas.GetValue(x) _, y := mas.GetValues(x)
yvalues = append(yvalues, y) yvalues = append(yvalues, y)
} }
lx, ly := mas.GetLastValue() lx, ly := mas.GetLastValues()
assert.Equal(10.0, lx) assert.Equal(10.0, lx)
assert.Equal(5.5, ly) assert.Equal(5.5, ly)
assert.Equal(yvalues[len(yvalues)-1], ly) assert.Equal(yvalues[len(yvalues)-1], ly)
@ -87,9 +87,9 @@ func TestSMASeriesGetLastValueWindowOverlap(t *testing.T) {
func TestSMASeriesGetLastValue(t *testing.T) { func TestSMASeriesGetLastValue(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
mockSeries := mockValueProvider{ mockSeries := mockValuesProvider{
Sequence.Float64(1.0, 100.0), Generate.Float64(1.0, 100.0),
Sequence.Float64(100, 1.0), Generate.Float64(100, 1.0),
} }
assert.Equal(100, mockSeries.Len()) assert.Equal(100, mockSeries.Len())
@ -100,11 +100,11 @@ func TestSMASeriesGetLastValue(t *testing.T) {
var yvalues []float64 var yvalues []float64
for x := 0; x < mas.Len(); x++ { for x := 0; x < mas.Len(); x++ {
_, y := mas.GetValue(x) _, y := mas.GetValues(x)
yvalues = append(yvalues, y) yvalues = append(yvalues, y)
} }
lx, ly := mas.GetLastValue() lx, ly := mas.GetLastValues()
assert.Equal(100.0, lx) assert.Equal(100.0, lx)
assert.Equal(6, ly) assert.Equal(6, ly)
assert.Equal(yvalues[len(yvalues)-1], ly) assert.Equal(yvalues[len(yvalues)-1], ly)

View file

@ -200,7 +200,7 @@ func (sbc StackedBarChart) drawYAxis(r Renderer, canvasBox Box) {
r.LineTo(canvasBox.Right+DefaultHorizontalTickWidth, canvasBox.Bottom) r.LineTo(canvasBox.Right+DefaultHorizontalTickWidth, canvasBox.Bottom)
r.Stroke() r.Stroke()
ticks := Sequence.Float64(1.0, 0.0, 0.2) ticks := Generate.Float64(1.0, 0.0, 0.2)
for _, t := range ticks { for _, t := range ticks {
axisStyle.GetStrokeOptions().WriteToRenderer(r) axisStyle.GetStrokeOptions().WriteToRenderer(r)
ty := canvasBox.Bottom - int(t*float64(canvasBox.Height())) ty := canvasBox.Bottom - int(t*float64(canvasBox.Height()))

View file

@ -31,15 +31,15 @@ func (ts TimeSeries) Len() int {
return len(ts.XValues) return len(ts.XValues)
} }
// GetValue gets a value at a given index. // GetValues gets a value at a given index.
func (ts TimeSeries) GetValue(index int) (x, y float64) { func (ts TimeSeries) GetValues(index int) (x, y float64) {
x = Time.ToFloat64(ts.XValues[index]) x = Time.ToFloat64(ts.XValues[index])
y = ts.YValues[index] y = ts.YValues[index]
return return
} }
// GetLastValue gets the last value. // GetLastValues gets the last value.
func (ts TimeSeries) GetLastValue() (x, y float64) { func (ts TimeSeries) GetLastValues() (x, y float64) {
x = Time.ToFloat64(ts.XValues[len(ts.XValues)-1]) x = Time.ToFloat64(ts.XValues[len(ts.XValues)-1])
y = ts.YValues[len(ts.YValues)-1] y = ts.YValues[len(ts.YValues)-1]
return return

View file

@ -24,7 +24,7 @@ func TestTimeSeriesGetValue(t *testing.T) {
}, },
} }
x0, y0 := ts.GetValue(0) x0, y0 := ts.GetValues(0)
assert.NotZero(x0) assert.NotZero(x0)
assert.Equal(1.0, y0) assert.Equal(1.0, y0)
} }

229
value_buffer.go Normal file
View file

@ -0,0 +1,229 @@
package chart
import (
"fmt"
"strings"
)
const (
valueBufferMinimumGrow = 4
valueBufferShrinkThreshold = 32
valueBufferGrowFactor = 200
valueBufferDefaultCapacity = 4
)
var (
emptyArray = make([]float64, 0)
)
// NewValueBuffer creates a new value buffer with an optional set of values.
func NewValueBuffer(values ...float64) *ValueBuffer {
var tail int
array := make([]float64, Math.MaxInt(len(values), valueBufferDefaultCapacity))
if len(values) > 0 {
copy(array, values)
tail = len(values)
}
return &ValueBuffer{
array: array,
head: 0,
tail: tail,
size: len(values),
}
}
// NewValueBufferWithCapacity creates a new ValueBuffer pre-allocated with the given capacity.
func NewValueBufferWithCapacity(capacity int) *ValueBuffer {
return &ValueBuffer{
array: make([]float64, capacity),
head: 0,
tail: 0,
size: 0,
}
}
// ValueBuffer is a fifo buffer that is backed by a pre-allocated array, instead of allocating
// a whole new node object for each element (which saves GC churn).
// Enqueue can be O(n), Dequeue can be O(1).
type ValueBuffer struct {
array []float64
head int
tail int
size int
}
// Len returns the length of the ValueBuffer (as it is currently populated).
// Actual memory footprint may be different.
func (vb *ValueBuffer) Len() int {
return vb.size
}
// GetValue implements sequence provider.
func (vb *ValueBuffer) GetValue(index int) float64 {
effectiveIndex := (vb.head + index) % len(vb.array)
return vb.array[effectiveIndex]
}
// Capacity returns the total size of the ValueBuffer, including empty elements.
func (vb *ValueBuffer) Capacity() int {
return len(vb.array)
}
// SetCapacity sets the capacity of the ValueBuffer.
func (vb *ValueBuffer) SetCapacity(capacity int) {
newArray := make([]float64, capacity)
if vb.size > 0 {
if vb.head < vb.tail {
arrayCopy(vb.array, vb.head, newArray, 0, vb.size)
} else {
arrayCopy(vb.array, vb.head, newArray, 0, len(vb.array)-vb.head)
arrayCopy(vb.array, 0, newArray, len(vb.array)-vb.head, vb.tail)
}
}
vb.array = newArray
vb.head = 0
if vb.size == capacity {
vb.tail = 0
} else {
vb.tail = vb.size
}
}
// Clear removes all objects from the ValueBuffer.
func (vb *ValueBuffer) Clear() {
if vb.head < vb.tail {
arrayClear(vb.array, vb.head, vb.size)
} else {
arrayClear(vb.array, vb.head, len(vb.array)-vb.head)
arrayClear(vb.array, 0, vb.tail)
}
vb.head = 0
vb.tail = 0
vb.size = 0
}
// Enqueue adds an element to the "back" of the ValueBuffer.
func (vb *ValueBuffer) Enqueue(value float64) {
if vb.size == len(vb.array) {
newCapacity := int(len(vb.array) * int(valueBufferGrowFactor/100))
if newCapacity < (len(vb.array) + valueBufferMinimumGrow) {
newCapacity = len(vb.array) + valueBufferMinimumGrow
}
vb.SetCapacity(newCapacity)
}
vb.array[vb.tail] = value
vb.tail = (vb.tail + 1) % len(vb.array)
vb.size++
}
// Dequeue removes the first element from the RingBuffer.
func (vb *ValueBuffer) Dequeue() float64 {
if vb.size == 0 {
return 0
}
removed := vb.array[vb.head]
vb.head = (vb.head + 1) % len(vb.array)
vb.size--
return removed
}
// Peek returns but does not remove the first element.
func (vb *ValueBuffer) Peek() float64 {
if vb.size == 0 {
return 0
}
return vb.array[vb.head]
}
// PeekBack returns but does not remove the last element.
func (vb *ValueBuffer) PeekBack() float64 {
if vb.size == 0 {
return 0
}
if vb.tail == 0 {
return vb.array[len(vb.array)-1]
}
return vb.array[vb.tail-1]
}
// TrimExcess resizes the buffer to better fit the contents.
func (vb *ValueBuffer) TrimExcess() {
threshold := float64(len(vb.array)) * 0.9
if vb.size < int(threshold) {
vb.SetCapacity(vb.size)
}
}
// Array returns the ring buffer, in order, as an array.
func (vb *ValueBuffer) Array() Array {
newArray := make([]float64, vb.size)
if vb.size == 0 {
return newArray
}
if vb.head < vb.tail {
arrayCopy(vb.array, vb.head, newArray, 0, vb.size)
} else {
arrayCopy(vb.array, vb.head, newArray, 0, len(vb.array)-vb.head)
arrayCopy(vb.array, 0, newArray, len(vb.array)-vb.head, vb.tail)
}
return Array(newArray)
}
// Each calls the consumer for each element in the buffer.
func (vb *ValueBuffer) Each(mapfn func(int, float64)) {
if vb.size == 0 {
return
}
var index int
if vb.head < vb.tail {
for cursor := vb.head; cursor < vb.tail; cursor++ {
mapfn(index, vb.array[cursor])
index++
}
} else {
for cursor := vb.head; cursor < len(vb.array); cursor++ {
mapfn(index, vb.array[cursor])
index++
}
for cursor := 0; cursor < vb.tail; cursor++ {
mapfn(index, vb.array[cursor])
index++
}
}
}
// String returns a string representation for value buffers.
func (vb *ValueBuffer) String() string {
var values []string
for _, elem := range vb.Array() {
values = append(values, fmt.Sprintf("%v", elem))
}
return strings.Join(values, " <= ")
}
// --------------------------------------------------------------------------------
// Util methods
// --------------------------------------------------------------------------------
func arrayClear(source []float64, index, length int) {
for x := 0; x < length; x++ {
absoluteIndex := x + index
source[absoluteIndex] = 0
}
}
func arrayCopy(source []float64, sourceIndex int, destination []float64, destinationIndex, length int) {
for x := 0; x < length; x++ {
from := sourceIndex + x
to := destinationIndex + x
destination[to] = source[from]
}
}

View file

@ -6,10 +6,10 @@ import (
"github.com/blendlabs/go-assert" "github.com/blendlabs/go-assert"
) )
func TestRingBuffer(t *testing.T) { func TestValueBuffer(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
buffer := NewRingBuffer() buffer := NewValueBuffer()
buffer.Enqueue(1) buffer.Enqueue(1)
assert.Equal(1, buffer.Len()) assert.Equal(1, buffer.Len())
@ -96,14 +96,14 @@ func TestRingBuffer(t *testing.T) {
value = buffer.Dequeue() value = buffer.Dequeue()
assert.Equal(8, value) assert.Equal(8, value)
assert.Equal(0, buffer.Len()) assert.Equal(0, buffer.Len())
assert.Nil(buffer.Peek()) assert.Zero(buffer.Peek())
assert.Nil(buffer.PeekBack()) assert.Zero(buffer.PeekBack())
} }
func TestRingBufferClear(t *testing.T) { func TestRingBufferClear(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
buffer := NewRingBuffer() buffer := NewValueBuffer()
buffer.Enqueue(1) buffer.Enqueue(1)
buffer.Enqueue(1) buffer.Enqueue(1)
buffer.Enqueue(1) buffer.Enqueue(1)
@ -117,21 +117,21 @@ func TestRingBufferClear(t *testing.T) {
buffer.Clear() buffer.Clear()
assert.Equal(0, buffer.Len()) assert.Equal(0, buffer.Len())
assert.Nil(buffer.Peek()) assert.Zero(buffer.Peek())
assert.Nil(buffer.PeekBack()) assert.Zero(buffer.PeekBack())
} }
func TestRingBufferAsSlice(t *testing.T) { func TestRingBufferAsSlice(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
buffer := NewRingBuffer() buffer := NewValueBuffer()
buffer.Enqueue(1) buffer.Enqueue(1)
buffer.Enqueue(2) buffer.Enqueue(2)
buffer.Enqueue(3) buffer.Enqueue(3)
buffer.Enqueue(4) buffer.Enqueue(4)
buffer.Enqueue(5) buffer.Enqueue(5)
contents := buffer.AsSlice() contents := buffer.Array()
assert.Len(contents, 5) assert.Len(contents, 5)
assert.Equal(1, contents[0]) assert.Equal(1, contents[0])
assert.Equal(2, contents[1]) assert.Equal(2, contents[1])
@ -143,20 +143,40 @@ func TestRingBufferAsSlice(t *testing.T) {
func TestRingBufferEach(t *testing.T) { func TestRingBufferEach(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
buffer := NewRingBuffer() buffer := NewValueBuffer()
for x := 1; x < 17; x++ { for x := 1; x < 17; x++ {
buffer.Enqueue(x) buffer.Enqueue(float64(x))
} }
called := 0 called := 0
buffer.Each(func(v interface{}) { buffer.Each(func(_ int, v float64) {
if typed, isTyped := v.(int); isTyped { if v == float64(called+1) {
if typed == (called + 1) { called++
called++
}
} }
}) })
assert.Equal(16, called) assert.Equal(16, called)
} }
func TestNewValueBuffer(t *testing.T) {
assert := assert.New(t)
empty := NewValueBuffer()
assert.NotNil(empty)
assert.Zero(empty.Len())
assert.Equal(valueBufferDefaultCapacity, empty.Capacity())
assert.Zero(empty.Peek())
assert.Zero(empty.PeekBack())
}
func TestNewValueBufferWithValues(t *testing.T) {
assert := assert.New(t)
values := NewValueBuffer(1, 2, 3, 4)
assert.NotNil(values)
assert.Equal(4, values.Len())
assert.Equal(valueBufferDefaultCapacity, values.Capacity())
assert.Equal(1, values.Peek())
assert.Equal(4, values.PeekBack())
}

View file

@ -2,38 +2,38 @@ package chart
import "github.com/wcharczuk/go-chart/drawing" import "github.com/wcharczuk/go-chart/drawing"
// ValueProvider is a type that produces values. // ValuesProvider is a type that produces values.
type ValueProvider interface { type ValuesProvider interface {
Len() int Len() int
GetValue(index int) (float64, float64) GetValues(index int) (float64, float64)
} }
// BoundedValueProvider allows series to return a range. // BoundedValuesProvider allows series to return a range.
type BoundedValueProvider interface { type BoundedValuesProvider interface {
Len() int Len() int
GetBoundedValue(index int) (x, y1, y2 float64) GetBoundedValues(index int) (x, y1, y2 float64)
} }
// LastValueProvider is a special type of value provider that can return it's (potentially computed) last value. // LastValuesProvider is a special type of value provider that can return it's (potentially computed) last value.
type LastValueProvider interface { type LastValuesProvider interface {
GetLastValue() (x, y float64) GetLastValues() (x, y float64)
} }
// BoundedLastValueProvider is a special type of value provider that can return it's (potentially computed) bounded last value. // BoundedLastValuesProvider is a special type of value provider that can return it's (potentially computed) bounded last value.
type BoundedLastValueProvider interface { type BoundedLastValuesProvider interface {
GetBoundedLastValue() (x, y1, y2 float64) GetBoundedLastValues() (x, y1, y2 float64)
} }
// FullValueProvider is an interface that combines `ValueProvider` and `LastValueProvider` // FullValuesProvider is an interface that combines `ValuesProvider` and `LastValuesProvider`
type FullValueProvider interface { type FullValuesProvider interface {
ValueProvider ValuesProvider
LastValueProvider LastValuesProvider
} }
// FullBoundedValueProvider is an interface that combines `BoundedValueProvider` and `BoundedLastValueProvider` // FullBoundedValuesProvider is an interface that combines `BoundedValuesProvider` and `BoundedLastValuesProvider`
type FullBoundedValueProvider interface { type FullBoundedValuesProvider interface {
BoundedValueProvider BoundedValuesProvider
BoundedLastValueProvider BoundedLastValuesProvider
} }
// SizeProvider is a provider for integer size. // SizeProvider is a provider for integer size.