go-chart/annotation_series.go

89 lines
2.5 KiB
Go
Raw Permalink Normal View History

2016-07-10 10:11:47 +02:00
package chart
2017-02-03 20:26:53 +01:00
import (
"fmt"
"math"
util "github.com/wcharczuk/go-chart/util"
2017-02-03 20:26:53 +01:00
)
2016-07-11 02:19:44 +02:00
2016-07-10 10:11:47 +02:00
// AnnotationSeries is a series of labels on the chart.
type AnnotationSeries struct {
Name string
Style Style
2016-08-01 01:54:09 +02:00
YAxis YAxisType
2016-07-28 23:30:00 +02:00
Annotations []Value2
2016-07-10 10:11:47 +02:00
}
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.
2016-08-01 01:54:09 +02:00
func (as AnnotationSeries) GetYAxis() YAxisType {
2016-07-10 19:43:04 +02:00
return as.YAxis
}
2016-07-28 23:30:00 +02:00
func (as AnnotationSeries) annotationStyleDefaults(defaults Style) Style {
return Style{
FontColor: DefaultTextColor,
2016-07-28 23:30:00 +02:00
Font: defaults.Font,
FillColor: DefaultAnnotationFillColor,
FontSize: DefaultAnnotationFontSize,
StrokeColor: defaults.StrokeColor,
StrokeWidth: defaults.StrokeWidth,
Padding: DefaultAnnotationPadding,
}
}
// 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,
}
2016-07-17 05:53:46 +02:00
if as.Style.IsZero() || as.Style.Show {
2016-07-28 23:30:00 +02:00
seriesStyle := as.Style.InheritFrom(as.annotationStyleDefaults(defaults))
for _, a := range as.Annotations {
2016-07-28 23:30:00 +02:00
style := a.Style.InheritFrom(seriesStyle)
lx := canvasBox.Left + xrange.Translate(a.XValue)
ly := canvasBox.Bottom - yrange.Translate(a.YValue)
2016-07-30 01:36:29 +02:00
ab := Draw.MeasureAnnotation(r, canvasBox, style, lx, ly, a.Label)
box.Top = util.Math.MinInt(box.Top, ab.Top)
box.Left = util.Math.MinInt(box.Left, ab.Left)
box.Right = util.Math.MaxInt(box.Right, ab.Right)
box.Bottom = util.Math.MaxInt(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-17 05:53:46 +02:00
if as.Style.IsZero() || as.Style.Show {
2016-07-28 23:30:00 +02:00
seriesStyle := as.Style.InheritFrom(as.annotationStyleDefaults(defaults))
2016-07-10 10:11:47 +02:00
for _, a := range as.Annotations {
2016-07-28 23:30:00 +02:00
style := a.Style.InheritFrom(seriesStyle)
lx := canvasBox.Left + xrange.Translate(a.XValue)
ly := canvasBox.Bottom - yrange.Translate(a.YValue)
2016-07-30 01:36:29 +02:00
Draw.Annotation(r, canvasBox, style, lx, ly, a.Label)
2016-07-10 10:11:47 +02:00
}
}
}
2017-02-03 20:26:53 +01:00
// Validate validates the series.
func (as AnnotationSeries) Validate() error {
if len(as.Annotations) == 0 {
return fmt.Errorf("annotation series requires annotations to be set and not empty")
}
return nil
}