| 1 |
package chart |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math" |
| 6 |
) |
| 7 |
|
| 8 |
|
| 9 |
var ( |
| 10 |
_ Series = (*AnnotationSeries)(nil) |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
type AnnotationSeries struct { |
| 15 |
Name string |
| 16 |
Style Style |
| 17 |
YAxis YAxisType |
| 18 |
Annotations []Value2 |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
func (as AnnotationSeries) GetName() string { |
| 23 |
return as.Name |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
func (as AnnotationSeries) GetStyle() Style { |
| 28 |
return as.Style |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
func (as AnnotationSeries) GetYAxis() YAxisType { |
| 33 |
return as.YAxis |
| 34 |
} |
| 35 |
|
| 36 |
func (as AnnotationSeries) annotationStyleDefaults(defaults Style) Style { |
| 37 |
return Style{ |
| 38 |
FontColor: DefaultTextColor, |
| 39 |
Font: defaults.Font, |
| 40 |
FillColor: DefaultAnnotationFillColor, |
| 41 |
FontSize: DefaultAnnotationFontSize, |
| 42 |
StrokeColor: defaults.StrokeColor, |
| 43 |
StrokeWidth: defaults.StrokeWidth, |
| 44 |
Padding: DefaultAnnotationPadding, |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
func (as AnnotationSeries) Measure(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) Box { |
| 50 |
box := Box{ |
| 51 |
Top: math.MaxInt32, |
| 52 |
Left: math.MaxInt32, |
| 53 |
Right: 0, |
| 54 |
Bottom: 0, |
| 55 |
} |
| 56 |
if !as.Style.Hidden { |
| 57 |
seriesStyle := as.Style.InheritFrom(as.annotationStyleDefaults(defaults)) |
| 58 |
for _, a := range as.Annotations { |
| 59 |
style := a.Style.InheritFrom(seriesStyle) |
| 60 |
lx := canvasBox.Left + xrange.Translate(a.XValue) |
| 61 |
ly := canvasBox.Bottom - yrange.Translate(a.YValue) |
| 62 |
ab := Draw.MeasureAnnotation(r, canvasBox, style, lx, ly, a.Label) |
| 63 |
box.Top = MinInt(box.Top, ab.Top) |
| 64 |
box.Left = MinInt(box.Left, ab.Left) |
| 65 |
box.Right = MaxInt(box.Right, ab.Right) |
| 66 |
box.Bottom = MaxInt(box.Bottom, ab.Bottom) |
| 67 |
} |
| 68 |
} |
| 69 |
return box |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
func (as AnnotationSeries) Render(r Renderer, canvasBox Box, xrange, yrange Range, defaults Style) { |
| 74 |
if !as.Style.Hidden { |
| 75 |
seriesStyle := as.Style.InheritFrom(as.annotationStyleDefaults(defaults)) |
| 76 |
for _, a := range as.Annotations { |
| 77 |
style := a.Style.InheritFrom(seriesStyle) |
| 78 |
lx := canvasBox.Left + xrange.Translate(a.XValue) |
| 79 |
ly := canvasBox.Bottom - yrange.Translate(a.YValue) |
| 80 |
Draw.Annotation(r, canvasBox, style, lx, ly, a.Label) |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
func (as AnnotationSeries) Validate() error { |
| 87 |
if len(as.Annotations) == 0 { |
| 88 |
return fmt.Errorf("annotation series requires annotations to be set and not empty") |
| 89 |
} |
| 90 |
return nil |
| 91 |
} |