diff --git a/annotation_series.go b/annotation_series.go index 22ed729..8090806 100644 --- a/annotation_series.go +++ b/annotation_series.go @@ -1,6 +1,47 @@ package chart -import "math" +import ( + "math" + "time" + + "github.com/wcharczuk/go-chart" +) + +// CreateContinuousSeriesLastValueLabel returns a (1) value annotation series. +func CreateContinuousSeriesLastValueLabel(name string, xvalues, yvalues []float64, valueFormatter ValueFormatter) AnnotationSeries { + return AnnotationSeries{ + Name: name, + Style: Style{ + Show: true, + StrokeColor: chart.GetDefaultSeriesStrokeColor(0), + }, + Annotations: []chart.Annotation{ + Annotation{ + X: xvalues[len(xvalues)-1], + Y: yvalues[len(yvalues)-1], + Label: valueFormatter(yvalues[len(yvalues)-1]), + }, + }, + } +} + +// CreateTimeSeriesLastValueLabel returns a (1) value annotation series. +func CreateTimeSeriesLastValueLabel(name string, xvalues []time.Time, yvalues []float64, valueFormatter ValueFormatter) AnnotationSeries { + return AnnotationSeries{ + Name: name, + Style: Style{ + Show: true, + StrokeColor: chart.GetDefaultSeriesStrokeColor(0), + }, + Annotations: []chart.Annotation{ + Annotation{ + X: xvalues[len(xvalues)-1], + Y: yvalues[len(yvalues)-1], + Label: valueFormatter(yvalues[len(yvalues)-1]), + }, + }, + } +} // Annotation is a label on the chart. type Annotation struct { diff --git a/testserver/main.go b/testserver/main.go index e65265b..0080627 100644 --- a/testserver/main.go +++ b/testserver/main.go @@ -68,36 +68,9 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult { XValues: s2x, YValues: s2y, }, - chart.AnnotationSeries{ - Name: "a - last value", - Style: chart.Style{ - Show: true, - StrokeColor: chart.GetDefaultSeriesStrokeColor(0), - }, - Annotations: []chart.Annotation{ - chart.Annotation{ - X: s1x[len(s1x)-1], - Y: s1y[len(s1y)-1], - Label: chart.FloatValueFormatter(s1y[len(s1y)-1]), - }, - }, - }, - chart.AnnotationSeries{ - Name: "b - last value", - YAxis: chart.YAxisSecondary, - Style: chart.Style{ - Show: true, - StrokeColor: chart.GetDefaultSeriesStrokeColor(1), - }, - Annotations: []chart.Annotation{ - chart.Annotation{ - X: s2x[len(s2x)-1], - Y: s2y[len(s2y)-1], - Label: chart.FloatValueFormatter(s2y[len(s2y)-1]), - }, - }, - }, }, + CreateContinuousSeriesLastValueLabel("a - last value", s1x, s1y, chart.FloatValueFormatter), + CreateContinuousSeriesLastValueLabel("b - last value", s2, s2y, chart.FloatValueFormatter), } if format == "png" {