From 7776c351a8d8126c48ef18b382ee62ba4d3eccb4 Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Fri, 13 Jan 2017 16:03:45 -0800 Subject: [PATCH] add custom value formatters for continuous series. --- continuous_series.go | 15 +++++++++++++-- continuous_series_test.go | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/continuous_series.go b/continuous_series.go index fe8d068..f182eb1 100644 --- a/continuous_series.go +++ b/continuous_series.go @@ -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 } diff --git a/continuous_series_test.go b/continuous_series_test.go index 171db37..e8df2d6 100644 --- a/continuous_series_test.go +++ b/continuous_series_test.go @@ -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)) +}