go-chart/annotation_series.go

75 lines
2.1 KiB
Go
Raw Normal View History

2016-07-10 04:11:47 -04:00
package chart
2016-07-11 02:06:14 -04:00
import "math"
2016-07-10 20:19:44 -04:00
2016-07-10 04:11:47 -04:00
// AnnotationSeries is a series of labels on the chart.
type AnnotationSeries struct {
Name string
Style Style
YAxis YAxisType
2016-07-28 17:30:00 -04:00
Annotations []Value2
2016-07-10 04:11:47 -04:00
}
2016-07-10 13:43:04 -04: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
}
2016-07-28 17:30:00 -04:00
func (as AnnotationSeries) annotationStyleDefaults(defaults Style) Style {
return Style{
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-10 20:19:44 -04:00
Top: math.MaxInt32,
Left: math.MaxInt32,
Right: 0,
Bottom: 0,
}
2016-07-16 23:53:46 -04:00
if as.Style.IsZero() || as.Style.Show {
2016-07-28 17:30:00 -04:00
seriesStyle := as.Style.InheritFrom(as.annotationStyleDefaults(defaults))
for _, a := range as.Annotations {
2016-07-28 17:30:00 -04:00
style := a.Style.InheritFrom(seriesStyle)
lx := canvasBox.Left + xrange.Translate(a.XValue)
ly := canvasBox.Bottom - yrange.Translate(a.YValue)
2016-07-11 21:48:51 -04:00
ab := MeasureAnnotation(r, canvasBox, style, lx, ly, a.Label)
box.Top = MinInt(box.Top, ab.Top)
box.Left = MinInt(box.Left, ab.Left)
box.Right = MaxInt(box.Right, ab.Right)
box.Bottom = MaxInt(box.Bottom, ab.Bottom)
}
}
return box
}
2016-07-10 04:11:47 -04:00
// Render draws the series.
2016-07-10 13:43:04 -04:00
func (as AnnotationSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) {
2016-07-16 23:53:46 -04:00
if as.Style.IsZero() || as.Style.Show {
2016-07-28 17:30:00 -04:00
seriesStyle := as.Style.InheritFrom(as.annotationStyleDefaults(defaults))
2016-07-10 04:11:47 -04:00
for _, a := range as.Annotations {
2016-07-28 17:30:00 -04:00
style := a.Style.InheritFrom(seriesStyle)
lx := canvasBox.Left + xrange.Translate(a.XValue)
ly := canvasBox.Bottom - yrange.Translate(a.YValue)
2016-07-11 21:48:51 -04:00
DrawAnnotation(r, canvasBox, style, lx, ly, a.Label)
2016-07-10 04:11:47 -04:00
}
}
}