2016-07-10 10:11:47 +02:00
|
|
|
package chart
|
|
|
|
|
|
|
|
import "math"
|
|
|
|
|
|
|
|
// DrawLineSeries draws a line series with a renderer.
|
|
|
|
func DrawLineSeries(r Renderer, canvasBox Box, xrange, yrange Range, s Style, vs ValueProvider) {
|
|
|
|
if vs.Len() == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-10 19:43:04 +02:00
|
|
|
ct := canvasBox.Top
|
2016-07-10 10:11:47 +02:00
|
|
|
cb := canvasBox.Bottom
|
2016-07-10 19:43:04 +02:00
|
|
|
cr := canvasBox.Right
|
2016-07-10 10:11:47 +02:00
|
|
|
|
|
|
|
v0x, v0y := vs.GetValue(0)
|
2016-07-10 19:43:04 +02:00
|
|
|
x0 := cr - xrange.Translate(v0x)
|
|
|
|
y0 := yrange.Translate(v0y) + ct
|
2016-07-10 10:11:47 +02:00
|
|
|
|
|
|
|
var vx, vy float64
|
|
|
|
var x, y int
|
|
|
|
|
|
|
|
fill := s.GetFillColor()
|
|
|
|
if !fill.IsZero() {
|
|
|
|
r.SetFillColor(fill)
|
2016-07-10 19:43:04 +02:00
|
|
|
r.MoveTo(x0, y0)
|
2016-07-10 10:11:47 +02:00
|
|
|
for i := 1; i < vs.Len(); i++ {
|
|
|
|
vx, vy = vs.GetValue(i)
|
2016-07-10 19:43:04 +02:00
|
|
|
x = cr - xrange.Translate(vx)
|
|
|
|
y = yrange.Translate(vy) + ct
|
|
|
|
r.LineTo(x, y)
|
2016-07-10 10:11:47 +02:00
|
|
|
}
|
2016-07-10 19:43:04 +02:00
|
|
|
r.LineTo(x, cb)
|
|
|
|
r.LineTo(x0, cb)
|
2016-07-10 10:11:47 +02:00
|
|
|
r.Close()
|
|
|
|
r.Fill()
|
|
|
|
}
|
|
|
|
|
|
|
|
stroke := s.GetStrokeColor()
|
|
|
|
r.SetStrokeColor(stroke)
|
|
|
|
r.SetStrokeWidth(s.GetStrokeWidth(DefaultStrokeWidth))
|
|
|
|
|
2016-07-10 19:43:04 +02:00
|
|
|
r.MoveTo(x0, y0)
|
2016-07-10 10:11:47 +02:00
|
|
|
for i := 1; i < vs.Len(); i++ {
|
|
|
|
vx, vy = vs.GetValue(i)
|
2016-07-10 19:43:04 +02:00
|
|
|
x = cr - xrange.Translate(vx)
|
|
|
|
y = yrange.Translate(vy) + ct
|
|
|
|
r.LineTo(x, y)
|
2016-07-10 10:11:47 +02:00
|
|
|
}
|
|
|
|
r.Stroke()
|
|
|
|
}
|
|
|
|
|
|
|
|
// DrawAnnotation draws an anotation with a renderer.
|
|
|
|
func DrawAnnotation(r Renderer, canvasBox Box, xrange, yrange Range, s Style, lx, ly int, label string) {
|
|
|
|
r.SetFontSize(s.GetFontSize(DefaultAnnotationFontSize))
|
|
|
|
textWidth, _ := r.MeasureText(label)
|
|
|
|
textHeight := int(math.Floor(DefaultAnnotationFontSize))
|
|
|
|
halfTextHeight := textHeight >> 1
|
|
|
|
|
|
|
|
pt := s.Padding.GetTop(DefaultAnnotationPadding.Top)
|
|
|
|
pl := s.Padding.GetLeft(DefaultAnnotationPadding.Left)
|
|
|
|
pr := s.Padding.GetRight(DefaultAnnotationPadding.Right)
|
|
|
|
pb := s.Padding.GetBottom(DefaultAnnotationPadding.Bottom)
|
|
|
|
|
|
|
|
textX := lx + pl + DefaultAnnotationDeltaWidth
|
|
|
|
textY := ly + halfTextHeight
|
|
|
|
|
|
|
|
ltlx := lx + pl + DefaultAnnotationDeltaWidth
|
|
|
|
ltly := ly - (pt + halfTextHeight)
|
|
|
|
|
|
|
|
ltrx := lx + pl + pr + textWidth
|
|
|
|
ltry := ly - (pt + halfTextHeight)
|
|
|
|
|
|
|
|
lbrx := lx + pl + pr + textWidth
|
|
|
|
lbry := ly + (pb + halfTextHeight)
|
|
|
|
|
|
|
|
lblx := lx + DefaultAnnotationDeltaWidth
|
|
|
|
lbly := ly + (pb + halfTextHeight)
|
|
|
|
|
|
|
|
//draw the shape...
|
|
|
|
r.SetFillColor(s.GetFillColor(DefaultAnnotationFillColor))
|
|
|
|
r.SetStrokeColor(s.GetStrokeColor())
|
|
|
|
r.SetStrokeWidth(s.GetStrokeWidth())
|
2016-07-10 19:43:04 +02:00
|
|
|
|
2016-07-10 10:11:47 +02:00
|
|
|
r.MoveTo(lx, ly)
|
|
|
|
r.LineTo(ltlx, ltly)
|
|
|
|
r.LineTo(ltrx, ltry)
|
|
|
|
r.LineTo(lbrx, lbry)
|
|
|
|
r.LineTo(lblx, lbly)
|
|
|
|
r.LineTo(lx, ly)
|
|
|
|
r.Close()
|
|
|
|
r.FillStroke()
|
|
|
|
|
|
|
|
r.SetFontColor(s.GetFontColor(DefaultTextColor))
|
|
|
|
r.Text(label, textX, textY)
|
|
|
|
}
|