adding helpers for annotation series creation.

This commit is contained in:
Will Charczuk 2016-07-10 19:45:24 -07:00
parent c4a5de77ed
commit 28ce6623e1
2 changed files with 44 additions and 30 deletions

View file

@ -1,6 +1,47 @@
package chart 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. // Annotation is a label on the chart.
type Annotation struct { type Annotation struct {

View file

@ -68,36 +68,9 @@ func chartHandler(rc *web.RequestContext) web.ControllerResult {
XValues: s2x, XValues: s2x,
YValues: s2y, 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" { if format == "png" {