go-chart/drawing_helpers.go

134 lines
3.5 KiB
Go
Raw Normal View History

2016-07-10 10:11:47 +02:00
package chart
// 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
}
cb := canvasBox.Bottom
2016-07-12 03:48:51 +02:00
cl := canvasBox.Left
2016-07-10 10:11:47 +02:00
v0x, v0y := vs.GetValue(0)
2016-07-12 03:48:51 +02:00
x0 := cl + xrange.Translate(v0x)
y0 := cb - yrange.Translate(v0y)
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-12 03:48:51 +02:00
x = cl + xrange.Translate(vx)
y = cb - yrange.Translate(vy)
2016-07-10 19:43:04 +02:00
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-12 03:48:51 +02:00
x = cl + xrange.Translate(vx)
y = cb - yrange.Translate(vy)
2016-07-10 19:43:04 +02:00
r.LineTo(x, y)
2016-07-10 10:11:47 +02:00
}
r.Stroke()
}
// MeasureAnnotation measures how big an annotation would be.
2016-07-12 03:48:51 +02:00
func MeasureAnnotation(r Renderer, canvasBox Box, s Style, lx, ly int, label string) Box {
r.SetFont(s.GetFont())
r.SetFontSize(s.GetFontSize(DefaultAnnotationFontSize))
2016-07-12 03:48:51 +02:00
textBox := r.MeasureText(label)
halfTextHeight := textBox.Height >> 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)
2016-07-11 20:03:07 +02:00
strokeWidth := s.GetStrokeWidth()
2016-07-11 09:18:03 +02:00
top := ly - (pt + halfTextHeight)
2016-07-12 03:48:51 +02:00
right := lx + pl + pr + textBox.Width + DefaultAnnotationDeltaWidth + int(strokeWidth)
2016-07-11 09:18:03 +02:00
bottom := ly + (pb + halfTextHeight)
return Box{
2016-07-11 09:18:03 +02:00
Top: top,
Left: lx,
2016-07-11 09:18:03 +02:00
Right: right,
Bottom: bottom,
Width: right - lx,
Height: bottom - top,
}
}
2016-07-10 10:11:47 +02:00
// DrawAnnotation draws an anotation with a renderer.
2016-07-12 03:48:51 +02:00
func DrawAnnotation(r Renderer, canvasBox Box, s Style, lx, ly int, label string) {
r.SetFont(s.GetFont())
2016-07-10 10:11:47 +02:00
r.SetFontSize(s.GetFontSize(DefaultAnnotationFontSize))
2016-07-12 03:48:51 +02:00
textBox := r.MeasureText(label)
halfTextHeight := textBox.Height >> 1
2016-07-10 10:11:47 +02:00
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
2016-07-11 09:22:24 +02:00
ltx := lx + DefaultAnnotationDeltaWidth
2016-07-11 09:18:03 +02:00
lty := ly - (pt + halfTextHeight)
2016-07-10 10:11:47 +02:00
2016-07-12 03:48:51 +02:00
rtx := lx + pl + pr + textBox.Width + DefaultAnnotationDeltaWidth
2016-07-11 09:18:03 +02:00
rty := ly - (pt + halfTextHeight)
2016-07-10 10:11:47 +02:00
2016-07-12 03:48:51 +02:00
rbx := lx + pl + pr + textBox.Width + DefaultAnnotationDeltaWidth
2016-07-11 09:18:03 +02:00
rby := ly + (pb + halfTextHeight)
2016-07-10 10:11:47 +02:00
2016-07-11 09:22:24 +02:00
lbx := lx + DefaultAnnotationDeltaWidth
2016-07-11 09:18:03 +02:00
lby := ly + (pb + halfTextHeight)
2016-07-10 10:11:47 +02:00
//draw the shape...
r.SetFillColor(s.GetFillColor(DefaultAnnotationFillColor))
r.SetStrokeColor(s.GetStrokeColor())
2016-07-11 20:04:20 +02:00
r.SetStrokeWidth(s.GetStrokeWidth())
2016-07-10 19:43:04 +02:00
2016-07-10 10:11:47 +02:00
r.MoveTo(lx, ly)
2016-07-11 09:18:03 +02:00
r.LineTo(ltx, lty)
r.LineTo(rtx, rty)
r.LineTo(rbx, rby)
r.LineTo(lbx, lby)
2016-07-10 10:11:47 +02:00
r.LineTo(lx, ly)
r.Close()
r.FillStroke()
r.SetFontColor(s.GetFontColor(DefaultTextColor))
r.Text(label, textX, textY)
}
2016-07-12 03:48:51 +02:00
// DrawBox draws a box with a given style.
func DrawBox(r Renderer, b Box, s Style) {
r.SetFillColor(s.GetFillColor())
r.SetStrokeColor(s.GetStrokeColor(DefaultStrokeColor))
r.SetStrokeWidth(s.GetStrokeWidth(DefaultStrokeWidth))
r.MoveTo(b.Left, b.Top)
r.LineTo(b.Right, b.Top)
r.LineTo(b.Right, b.Bottom)
r.LineTo(b.Left, b.Bottom)
r.LineTo(b.Left, b.Top)
r.FillStroke()
}