2016-07-10 04:11:47 -04:00
|
|
|
package chart
|
|
|
|
|
2016-07-14 22:25:40 -04:00
|
|
|
import "github.com/wcharczuk/go-chart/drawing"
|
|
|
|
|
2016-07-10 04:11:47 -04:00
|
|
|
// 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-11 21:48:51 -04:00
|
|
|
cl := canvasBox.Left
|
2016-07-10 04:11:47 -04:00
|
|
|
|
|
|
|
v0x, v0y := vs.GetValue(0)
|
2016-07-11 21:48:51 -04:00
|
|
|
x0 := cl + xrange.Translate(v0x)
|
|
|
|
y0 := cb - yrange.Translate(v0y)
|
2016-07-10 04:11:47 -04:00
|
|
|
|
|
|
|
var vx, vy float64
|
|
|
|
var x, y int
|
|
|
|
|
|
|
|
fill := s.GetFillColor()
|
|
|
|
if !fill.IsZero() {
|
|
|
|
r.SetFillColor(fill)
|
2016-07-10 13:43:04 -04:00
|
|
|
r.MoveTo(x0, y0)
|
2016-07-10 04:11:47 -04:00
|
|
|
for i := 1; i < vs.Len(); i++ {
|
|
|
|
vx, vy = vs.GetValue(i)
|
2016-07-11 21:48:51 -04:00
|
|
|
x = cl + xrange.Translate(vx)
|
|
|
|
y = cb - yrange.Translate(vy)
|
2016-07-10 13:43:04 -04:00
|
|
|
r.LineTo(x, y)
|
2016-07-10 04:11:47 -04:00
|
|
|
}
|
2016-07-10 13:43:04 -04:00
|
|
|
r.LineTo(x, cb)
|
|
|
|
r.LineTo(x0, cb)
|
2016-07-10 04:11:47 -04:00
|
|
|
r.Close()
|
|
|
|
r.Fill()
|
|
|
|
}
|
|
|
|
|
2016-07-12 23:34:59 -04:00
|
|
|
r.SetStrokeColor(s.GetStrokeColor())
|
2016-07-14 13:59:17 -04:00
|
|
|
r.SetStrokeDashArray(s.GetStrokeDashArray())
|
2016-07-10 04:11:47 -04:00
|
|
|
r.SetStrokeWidth(s.GetStrokeWidth(DefaultStrokeWidth))
|
|
|
|
|
2016-07-10 13:43:04 -04:00
|
|
|
r.MoveTo(x0, y0)
|
2016-07-10 04:11:47 -04:00
|
|
|
for i := 1; i < vs.Len(); i++ {
|
|
|
|
vx, vy = vs.GetValue(i)
|
2016-07-11 21:48:51 -04:00
|
|
|
x = cl + xrange.Translate(vx)
|
|
|
|
y = cb - yrange.Translate(vy)
|
2016-07-10 13:43:04 -04:00
|
|
|
r.LineTo(x, y)
|
2016-07-10 04:11:47 -04:00
|
|
|
}
|
|
|
|
r.Stroke()
|
|
|
|
}
|
|
|
|
|
2016-07-10 19:26:18 -04:00
|
|
|
// MeasureAnnotation measures how big an annotation would be.
|
2016-07-11 21:48:51 -04:00
|
|
|
func MeasureAnnotation(r Renderer, canvasBox Box, s Style, lx, ly int, label string) Box {
|
2016-07-12 23:34:59 -04:00
|
|
|
r.SetFillColor(s.GetFillColor(DefaultAnnotationFillColor))
|
|
|
|
r.SetStrokeColor(s.GetStrokeColor())
|
|
|
|
r.SetStrokeWidth(s.GetStrokeWidth())
|
2016-07-10 19:26:18 -04:00
|
|
|
r.SetFont(s.GetFont())
|
2016-07-12 23:34:59 -04:00
|
|
|
r.SetFontColor(s.GetFontColor(DefaultTextColor))
|
2016-07-10 19:26:18 -04:00
|
|
|
r.SetFontSize(s.GetFontSize(DefaultAnnotationFontSize))
|
2016-07-12 23:34:59 -04:00
|
|
|
|
2016-07-11 21:48:51 -04:00
|
|
|
textBox := r.MeasureText(label)
|
2016-07-12 19:47:52 -04:00
|
|
|
textWidth := textBox.Width()
|
|
|
|
textHeight := textBox.Height()
|
|
|
|
halfTextHeight := textHeight >> 1
|
2016-07-10 19:26:18 -04: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)
|
|
|
|
|
2016-07-11 14:03:07 -04:00
|
|
|
strokeWidth := s.GetStrokeWidth()
|
|
|
|
|
2016-07-11 03:18:03 -04:00
|
|
|
top := ly - (pt + halfTextHeight)
|
2016-07-12 19:47:52 -04:00
|
|
|
right := lx + pl + pr + textWidth + DefaultAnnotationDeltaWidth + int(strokeWidth)
|
2016-07-11 03:18:03 -04:00
|
|
|
bottom := ly + (pb + halfTextHeight)
|
2016-07-10 19:26:18 -04:00
|
|
|
|
|
|
|
return Box{
|
2016-07-11 03:18:03 -04:00
|
|
|
Top: top,
|
2016-07-10 19:26:18 -04:00
|
|
|
Left: lx,
|
2016-07-11 03:18:03 -04:00
|
|
|
Right: right,
|
|
|
|
Bottom: bottom,
|
2016-07-10 19:26:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-10 04:11:47 -04:00
|
|
|
// DrawAnnotation draws an anotation with a renderer.
|
2016-07-11 21:48:51 -04:00
|
|
|
func DrawAnnotation(r Renderer, canvasBox Box, s Style, lx, ly int, label string) {
|
2016-07-12 23:34:59 -04:00
|
|
|
r.SetFillColor(s.GetFillColor(DefaultAnnotationFillColor))
|
|
|
|
r.SetStrokeColor(s.GetStrokeColor())
|
|
|
|
r.SetStrokeWidth(s.GetStrokeWidth())
|
2016-07-14 13:59:17 -04:00
|
|
|
r.SetStrokeDashArray(s.GetStrokeDashArray())
|
2016-07-12 23:34:59 -04:00
|
|
|
|
2016-07-11 21:48:51 -04:00
|
|
|
textBox := r.MeasureText(label)
|
2016-07-12 19:47:52 -04:00
|
|
|
textWidth := textBox.Width()
|
|
|
|
halfTextHeight := textBox.Height() >> 1
|
2016-07-10 04:11:47 -04: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 03:22:24 -04:00
|
|
|
ltx := lx + DefaultAnnotationDeltaWidth
|
2016-07-11 03:18:03 -04:00
|
|
|
lty := ly - (pt + halfTextHeight)
|
2016-07-10 04:11:47 -04:00
|
|
|
|
2016-07-12 19:47:52 -04:00
|
|
|
rtx := lx + pl + pr + textWidth + DefaultAnnotationDeltaWidth
|
2016-07-11 03:18:03 -04:00
|
|
|
rty := ly - (pt + halfTextHeight)
|
2016-07-10 04:11:47 -04:00
|
|
|
|
2016-07-12 19:47:52 -04:00
|
|
|
rbx := lx + pl + pr + textWidth + DefaultAnnotationDeltaWidth
|
2016-07-11 03:18:03 -04:00
|
|
|
rby := ly + (pb + halfTextHeight)
|
2016-07-10 04:11:47 -04:00
|
|
|
|
2016-07-11 03:22:24 -04:00
|
|
|
lbx := lx + DefaultAnnotationDeltaWidth
|
2016-07-11 03:18:03 -04:00
|
|
|
lby := ly + (pb + halfTextHeight)
|
2016-07-10 04:11:47 -04:00
|
|
|
|
|
|
|
r.MoveTo(lx, ly)
|
2016-07-11 03:18:03 -04:00
|
|
|
r.LineTo(ltx, lty)
|
|
|
|
r.LineTo(rtx, rty)
|
|
|
|
r.LineTo(rbx, rby)
|
|
|
|
r.LineTo(lbx, lby)
|
2016-07-10 04:11:47 -04:00
|
|
|
r.LineTo(lx, ly)
|
|
|
|
r.Close()
|
|
|
|
r.FillStroke()
|
|
|
|
|
2016-07-12 23:34:59 -04:00
|
|
|
r.SetFont(s.GetFont())
|
2016-07-10 04:11:47 -04:00
|
|
|
r.SetFontColor(s.GetFontColor(DefaultTextColor))
|
2016-07-12 23:34:59 -04:00
|
|
|
r.SetFontSize(s.GetFontSize(DefaultAnnotationFontSize))
|
|
|
|
|
2016-07-10 04:11:47 -04:00
|
|
|
r.Text(label, textX, textY)
|
|
|
|
}
|
2016-07-11 21:48:51 -04: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))
|
2016-07-14 13:59:17 -04:00
|
|
|
r.SetStrokeDashArray(s.GetStrokeDashArray())
|
2016-07-12 23:34:59 -04:00
|
|
|
|
2016-07-11 21:48:51 -04:00
|
|
|
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()
|
|
|
|
}
|
2016-07-12 19:47:52 -04:00
|
|
|
|
|
|
|
// DrawText draws text with a given style.
|
|
|
|
func DrawText(r Renderer, text string, x, y int, s Style) {
|
2016-07-14 21:51:42 -04:00
|
|
|
r.SetFontColor(s.GetFontColor(DefaultTextColor))
|
2016-07-12 19:47:52 -04:00
|
|
|
r.SetStrokeColor(s.GetStrokeColor())
|
|
|
|
r.SetStrokeWidth(s.GetStrokeWidth())
|
|
|
|
r.SetFont(s.GetFont())
|
|
|
|
r.SetFontSize(s.GetFontSize())
|
2016-07-12 23:34:59 -04:00
|
|
|
|
2016-07-12 19:47:52 -04:00
|
|
|
r.Text(text, x, y)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DrawTextCentered draws text with a given style centered.
|
|
|
|
func DrawTextCentered(r Renderer, text string, x, y int, s Style) {
|
2016-07-14 21:51:42 -04:00
|
|
|
r.SetFontColor(s.GetFontColor(DefaultTextColor))
|
2016-07-12 19:47:52 -04:00
|
|
|
r.SetStrokeColor(s.GetStrokeColor())
|
|
|
|
r.SetStrokeWidth(s.GetStrokeWidth())
|
|
|
|
r.SetFont(s.GetFont())
|
|
|
|
r.SetFontSize(s.GetFontSize())
|
|
|
|
|
|
|
|
tb := r.MeasureText(text)
|
|
|
|
tx := x - (tb.Width() >> 1)
|
|
|
|
ty := y - (tb.Height() >> 1)
|
|
|
|
r.Text(text, tx, ty)
|
|
|
|
}
|
2016-07-14 22:25:40 -04:00
|
|
|
|
2016-07-14 22:36:40 -04:00
|
|
|
// CreateLegend returns a legend renderable function.
|
|
|
|
func CreateLegend(c *Chart, style Style) Renderable {
|
2016-07-14 22:25:40 -04:00
|
|
|
return func(r Renderer, cb Box, defaults Style) {
|
2016-07-14 22:36:40 -04:00
|
|
|
workingStyle := style.WithDefaultsFrom(defaults.WithDefaultsFrom(Style{
|
|
|
|
FillColor: drawing.ColorWhite,
|
|
|
|
FontColor: DefaultTextColor,
|
|
|
|
FontSize: 8.0,
|
|
|
|
StrokeColor: DefaultAxisColor,
|
|
|
|
StrokeWidth: DefaultAxisLineWidth,
|
|
|
|
}))
|
|
|
|
|
2016-07-14 22:25:40 -04:00
|
|
|
// DEFAULTS
|
|
|
|
legendPadding := 5
|
|
|
|
lineTextGap := 5
|
|
|
|
lineLengthMinimum := 25
|
|
|
|
|
|
|
|
var labels []string
|
|
|
|
var lines []Style
|
|
|
|
for _, s := range c.Series {
|
|
|
|
if s.GetStyle().IsZero() || s.GetStyle().Show {
|
|
|
|
if _, isAnnotationSeries := s.(AnnotationSeries); !isAnnotationSeries {
|
|
|
|
labels = append(labels, s.GetName())
|
|
|
|
lines = append(lines, s.GetStyle())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
legend := Box{
|
|
|
|
Top: cb.Top, //padding
|
|
|
|
Left: cb.Left,
|
|
|
|
}
|
|
|
|
|
|
|
|
legendContent := Box{
|
|
|
|
Top: legend.Top + legendPadding,
|
|
|
|
Left: legend.Left + legendPadding,
|
|
|
|
}
|
|
|
|
|
2016-07-14 22:36:40 -04:00
|
|
|
r.SetFontColor(workingStyle.GetFontColor())
|
|
|
|
r.SetFontSize(workingStyle.GetFontSize())
|
2016-07-14 22:25:40 -04:00
|
|
|
|
|
|
|
// measure
|
|
|
|
for x := 0; x < len(labels); x++ {
|
|
|
|
if len(labels[x]) > 0 {
|
|
|
|
tb := r.MeasureText(labels[x])
|
|
|
|
legendContent.Bottom += (tb.Height() + DefaultMinimumTickVerticalSpacing)
|
|
|
|
rowRight := tb.Width() + legendContent.Left + lineLengthMinimum + lineTextGap
|
|
|
|
legendContent.Right = MaxInt(legendContent.Right, rowRight)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
legend = legend.Grow(legendContent)
|
2016-07-14 22:36:40 -04:00
|
|
|
DrawBox(r, legend, workingStyle)
|
2016-07-14 22:25:40 -04:00
|
|
|
|
|
|
|
legendContent.Right = legend.Right - legendPadding
|
|
|
|
legendContent.Bottom = legend.Bottom - legendPadding
|
|
|
|
|
|
|
|
ycursor := legendContent.Top
|
|
|
|
tx := legendContent.Left
|
|
|
|
for x := 0; x < len(labels); x++ {
|
|
|
|
if len(labels[x]) > 0 {
|
|
|
|
tb := r.MeasureText(labels[x])
|
|
|
|
ycursor += tb.Height()
|
|
|
|
|
|
|
|
r.Text(labels[x], tx, ycursor)
|
|
|
|
th2 := tb.Height() >> 1
|
|
|
|
|
|
|
|
lx := tx + tb.Width() + lineTextGap
|
|
|
|
ly := ycursor - th2
|
|
|
|
lx2 := legendContent.Right - legendPadding
|
|
|
|
|
|
|
|
r.SetStrokeColor(lines[x].GetStrokeColor())
|
|
|
|
r.SetStrokeWidth(lines[x].GetStrokeWidth())
|
|
|
|
r.SetStrokeDashArray(lines[x].GetStrokeDashArray())
|
|
|
|
|
|
|
|
r.MoveTo(lx, ly)
|
|
|
|
r.LineTo(lx2, ly)
|
|
|
|
r.Stroke()
|
|
|
|
|
|
|
|
ycursor += DefaultMinimumTickVerticalSpacing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|