add custom value formatters for continuous series.

This commit is contained in:
Will Charczuk 2017-01-13 16:03:45 -08:00
parent e4cd38f602
commit 7776c351a8
2 changed files with 31 additions and 2 deletions

View file

@ -7,6 +7,9 @@ type ContinuousSeries struct {
YAxis YAxisType
XValueFormatter ValueFormatter
YValueFormatter ValueFormatter
XValues []float64
YValues []float64
}
@ -38,8 +41,16 @@ func (cs ContinuousSeries) GetLastValue() (float64, float64) {
// GetValueFormatters returns value formatter defaults for the series.
func (cs ContinuousSeries) GetValueFormatters() (x, y ValueFormatter) {
x = FloatValueFormatter
y = FloatValueFormatter
if cs.XValueFormatter != nil {
x = cs.XValueFormatter
} else {
x = FloatValueFormatter
}
if cs.YValueFormatter != nil {
y = cs.YValueFormatter
} else {
y = FloatValueFormatter
}
return
}

View file

@ -1,6 +1,7 @@
package chart
import (
"fmt"
"testing"
assert "github.com/blendlabs/go-assert"
@ -29,3 +30,20 @@ func TestContinuousSeries(t *testing.T) {
assert.Equal(10.0, xn)
assert.Equal(10.0, yn)
}
func TestContinuousSeriesValueFormatter(t *testing.T) {
assert := assert.New(t)
cs := ContinuousSeries{
XValueFormatter: func(v interface{}) string {
return fmt.Sprintf("%f foo", v)
},
YValueFormatter: func(v interface{}) string {
return fmt.Sprintf("%f bar", v)
},
}
xf, yf := cs.GetValueFormatters()
assert.Equal("0.100000 foo", xf(0.1))
assert.Equal("0.100000 bar", yf(0.1))
}