go-chart/annotation_series.go

129 lines
3.3 KiB
Go
Raw Normal View History

2016-07-10 10:11:47 +02:00
package chart
import (
"math"
"time"
)
// 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,
2016-07-11 06:00:03 +02:00
StrokeColor: GetDefaultSeriesStrokeColor(0),
},
2016-07-11 06:00:03 +02:00
Annotations: []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,
2016-07-11 06:00:03 +02:00
StrokeColor: GetDefaultSeriesStrokeColor(0),
},
2016-07-11 06:00:03 +02:00
Annotations: []Annotation{
Annotation{
2016-07-11 06:00:03 +02:00
X: float64(xvalues[len(xvalues)-1].Unix()),
Y: yvalues[len(yvalues)-1],
Label: valueFormatter(yvalues[len(yvalues)-1]),
},
},
}
}
2016-07-11 02:19:44 +02:00
2016-07-10 10:11:47 +02:00
// Annotation is a label on the chart.
type Annotation struct {
X, Y float64
Label string
}
// AnnotationSeries is a series of labels on the chart.
type AnnotationSeries struct {
Name string
Style Style
YAxis YAxisType
Annotations []Annotation
}
2016-07-10 19:43:04 +02:00
// GetName returns the name of the time series.
func (as AnnotationSeries) GetName() string {
return as.Name
}
// GetStyle returns the line style.
func (as AnnotationSeries) GetStyle() Style {
return as.Style
}
// GetYAxis returns which YAxis the series draws on.
func (as AnnotationSeries) GetYAxis() YAxisType {
return as.YAxis
}
// Measure returns a bounds box of the series.
func (as AnnotationSeries) Measure(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) Box {
box := Box{
2016-07-11 02:19:44 +02:00
Top: math.MaxInt32,
Left: math.MaxInt32,
Right: 0,
Bottom: 0,
}
if as.Style.Show {
style := as.Style.WithDefaultsFrom(Style{
Font: defaults.Font,
FillColor: DefaultAnnotationFillColor,
FontSize: DefaultAnnotationFontSize,
StrokeColor: defaults.StrokeColor,
StrokeWidth: defaults.StrokeWidth,
Padding: DefaultAnnotationPadding,
})
for _, a := range as.Annotations {
lx := canvasBox.Right - xrange.Translate(a.X)
ly := yrange.Translate(a.Y) + canvasBox.Top
2016-07-11 02:19:44 +02:00
ab := MeasureAnnotation(r, canvasBox, xrange, yrange, style, lx, ly, a.Label)
if ab.Top < box.Top {
box.Top = ab.Top
}
2016-07-11 02:19:44 +02:00
if ab.Left < box.Left {
box.Left = ab.Left
}
2016-07-11 02:19:44 +02:00
if ab.Right > box.Right {
box.Right = ab.Right
}
2016-07-11 02:19:44 +02:00
if ab.Bottom > box.Bottom {
box.Bottom = ab.Bottom
}
}
}
return box
}
2016-07-10 10:11:47 +02:00
// Render draws the series.
2016-07-10 19:43:04 +02:00
func (as AnnotationSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
2016-07-10 10:11:47 +02:00
if as.Style.Show {
2016-07-10 19:43:04 +02:00
style := as.Style.WithDefaultsFrom(Style{
Font: defaults.Font,
2016-07-10 19:43:04 +02:00
FillColor: DefaultAnnotationFillColor,
FontSize: DefaultAnnotationFontSize,
StrokeColor: defaults.StrokeColor,
StrokeWidth: defaults.StrokeWidth,
Padding: DefaultAnnotationPadding,
})
2016-07-10 10:11:47 +02:00
for _, a := range as.Annotations {
2016-07-10 19:43:04 +02:00
lx := canvasBox.Right - xrange.Translate(a.X)
2016-07-10 10:11:47 +02:00
ly := yrange.Translate(a.Y) + canvasBox.Top
2016-07-10 19:43:04 +02:00
DrawAnnotation(r, canvasBox, xrange, yrange, style, lx, ly, a.Label)
2016-07-10 10:11:47 +02:00
}
}
}