go-chart/draw.go

385 lines
9.4 KiB
Go
Raw Normal View History

2016-07-10 10:11:47 +02:00
package chart
import (
"math"
util "github.com/wcharczuk/go-chart/util"
)
2016-07-17 10:42:31 +02:00
2016-07-30 01:36:29 +02:00
var (
// Draw contains helpers for drawing common objects.
Draw = &draw{}
2016-07-17 10:42:31 +02:00
)
2016-07-30 01:36:29 +02:00
type draw struct{}
// LineSeries draws a line series with a renderer.
func (d draw) LineSeries(r Renderer, canvasBox Box, xrange, yrange Range, style Style, vs ValuesProvider) {
2016-07-10 10:11:47 +02:00
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.GetValues(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
yv0 := yrange.Translate(0)
2016-07-10 10:11:47 +02:00
var vx, vy float64
var x, y int
if style.ShouldDrawStroke() && style.ShouldDrawFill() {
style.GetFillOptions().WriteDrawingOptionsToRenderer(r)
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.GetValues(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.LineTo(x, util.Math.MinInt(cb, cb-yv0))
r.LineTo(x0, util.Math.MinInt(cb, cb-yv0))
r.LineTo(x0, y0)
2016-07-10 10:11:47 +02:00
r.Fill()
}
if style.ShouldDrawStroke() {
style.GetStrokeOptions().WriteDrawingOptionsToRenderer(r)
2016-07-10 10:11:47 +02:00
r.MoveTo(x0, y0)
for i := 1; i < vs.Len(); i++ {
vx, vy = vs.GetValues(i)
x = cl + xrange.Translate(vx)
y = cb - yrange.Translate(vy)
r.LineTo(x, y)
}
r.Stroke()
}
if style.ShouldDrawDot() {
2017-04-15 02:43:52 +02:00
defaultDotWidth := style.GetDotWidth()
style.GetDotOptions().WriteDrawingOptionsToRenderer(r)
for i := 0; i < vs.Len(); i++ {
vx, vy = vs.GetValues(i)
x = cl + xrange.Translate(vx)
y = cb - yrange.Translate(vy)
2017-04-15 02:43:52 +02:00
dotWidth := defaultDotWidth
if style.DotWidthProvider != nil {
2017-04-20 00:27:31 +02:00
dotWidth = style.DotWidthProvider(xrange, yrange, i, vx, vy)
2017-04-15 02:43:52 +02:00
}
if style.DotColorProvider != nil {
2017-04-20 00:27:31 +02:00
dotColor := style.DotColorProvider(xrange, yrange, i, vx, vy)
2017-04-15 02:43:52 +02:00
r.SetFillColor(dotColor)
r.SetStrokeColor(dotColor)
}
r.Circle(dotWidth, x, y)
r.FillStroke()
}
2016-07-10 10:11:47 +02:00
}
}
// BoundedSeries draws a series that implements BoundedValuesProvider.
func (d draw) BoundedSeries(r Renderer, canvasBox Box, xrange, yrange Range, style Style, bbs BoundedValuesProvider, drawOffsetIndexes ...int) {
drawOffsetIndex := 0
if len(drawOffsetIndexes) > 0 {
drawOffsetIndex = drawOffsetIndexes[0]
}
cb := canvasBox.Bottom
cl := canvasBox.Left
v0x, v0y1, v0y2 := bbs.GetBoundedValues(0)
x0 := cl + xrange.Translate(v0x)
y0 := cb - yrange.Translate(v0y1)
var vx, vy1, vy2 float64
var x, y int
xvalues := make([]float64, bbs.Len())
xvalues[0] = v0x
y2values := make([]float64, bbs.Len())
y2values[0] = v0y2
2016-07-30 18:12:03 +02:00
style.GetFillAndStrokeOptions().WriteToRenderer(r)
r.MoveTo(x0, y0)
for i := 1; i < bbs.Len(); i++ {
vx, vy1, vy2 = bbs.GetBoundedValues(i)
xvalues[i] = vx
y2values[i] = vy2
x = cl + xrange.Translate(vx)
y = cb - yrange.Translate(vy1)
if i > drawOffsetIndex {
r.LineTo(x, y)
} else {
r.MoveTo(x, y)
}
}
y = cb - yrange.Translate(vy2)
r.LineTo(x, y)
for i := bbs.Len() - 1; i >= drawOffsetIndex; i-- {
vx, vy2 = xvalues[i], y2values[i]
x = cl + xrange.Translate(vx)
y = cb - yrange.Translate(vy2)
r.LineTo(x, y)
}
r.Close()
r.FillStroke()
}
2016-07-30 01:36:29 +02:00
// HistogramSeries draws a value provider as boxes from 0.
func (d draw) HistogramSeries(r Renderer, canvasBox Box, xrange, yrange Range, style Style, vs ValuesProvider, barWidths ...int) {
2016-07-17 10:42:31 +02:00
if vs.Len() == 0 {
return
}
//calculate bar width?
seriesLength := vs.Len()
barWidth := int(math.Floor(float64(xrange.GetDomain()) / float64(seriesLength)))
2016-07-17 10:42:31 +02:00
if len(barWidths) > 0 {
barWidth = barWidths[0]
}
cb := canvasBox.Bottom
cl := canvasBox.Left
//foreach datapoint, draw a box.
for index := 0; index < seriesLength; index++ {
vx, vy := vs.GetValues(index)
2016-07-17 10:42:31 +02:00
y0 := yrange.Translate(0)
x := cl + xrange.Translate(vx)
y := yrange.Translate(vy)
2016-07-30 01:36:29 +02:00
d.Box(r, Box{
2016-07-17 10:42:31 +02:00
Top: cb - y0,
Left: x - (barWidth >> 1),
Right: x + (barWidth >> 1),
Bottom: cb - y,
2016-07-30 03:24:25 +02:00
}, style)
2016-07-17 10:42:31 +02:00
}
}
2017-05-15 01:33:48 +02:00
func (d draw) CandlestickSeries(r Renderer, canvasBox Box, xrange, yrange Range, style Style, cs CandlestickSeries) {
if cs.Len() == 0 {
return
}
candleValues := cs.CandleValues()
cb := canvasBox.Bottom
cl := canvasBox.Left
var cv CandleValue
for index := 0; index < len(candleValues); index++ {
2017-05-15 01:33:48 +02:00
cv = candleValues[index]
y0 := yrange.Translate(cv.Open)
y1 := yrange.Translate(cv.Close)
x0 := cl + xrange.Translate(util.Time.ToFloat64(util.Date.On(util.NYSEOpen(), cv.Timestamp)))
x1 := cl + xrange.Translate(util.Time.ToFloat64(util.Date.On(util.NYSEClose(), cv.Timestamp)))
x := x0 + ((x1 - x0) >> 1)
2017-05-15 01:33:48 +02:00
// draw open / close box.
if cv.Open < cv.Close {
d.Box(r, Box{
Top: cb - y0,
Left: x0,
Right: x1,
2017-05-15 01:33:48 +02:00
Bottom: cb - y1,
}, style.InheritFrom(Style{FillColor: ColorAlternateGreen}))
} else {
d.Box(r, Box{
Top: cb - y1,
Left: x0,
Right: x1,
2017-05-15 01:33:48 +02:00
Bottom: cb - y0,
}, style.InheritFrom(Style{FillColor: ColorRed}))
}
// draw high / low t bars
y0 = yrange.Translate(cv.High)
y1 = yrange.Translate(cv.Low)
style.InheritFrom(Style{StrokeColor: DefaultStrokeColor}).WriteToRenderer(r)
r.MoveTo(x0, cb-y0)
r.LineTo(x1, cb-y0)
r.Stroke()
2017-05-15 01:33:48 +02:00
r.MoveTo(x, cb-y0)
r.LineTo(x, cb-y1)
r.Stroke()
r.MoveTo(x0, cb-y1)
r.LineTo(x1, cb-y1)
r.Stroke()
2017-05-15 01:33:48 +02:00
}
}
// MeasureAnnotation measures how big an annotation would be.
2016-07-30 03:24:25 +02:00
func (d draw) MeasureAnnotation(r Renderer, canvasBox Box, style Style, lx, ly int, label string) Box {
style.WriteToRenderer(r)
2016-10-21 21:44:37 +02:00
defer r.ResetStyle()
2016-07-13 05:34:59 +02:00
2016-07-12 03:48:51 +02:00
textBox := r.MeasureText(label)
textWidth := textBox.Width()
textHeight := textBox.Height()
halfTextHeight := textHeight >> 1
2016-07-30 03:24:25 +02:00
pt := style.Padding.GetTop(DefaultAnnotationPadding.Top)
pl := style.Padding.GetLeft(DefaultAnnotationPadding.Left)
pr := style.Padding.GetRight(DefaultAnnotationPadding.Right)
pb := style.Padding.GetBottom(DefaultAnnotationPadding.Bottom)
2016-07-30 03:24:25 +02:00
strokeWidth := style.GetStrokeWidth()
2016-07-11 20:03:07 +02:00
2016-07-11 09:18:03 +02:00
top := ly - (pt + halfTextHeight)
right := lx + pl + pr + textWidth + 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,
}
}
2016-07-30 01:36:29 +02:00
// Annotation draws an anotation with a renderer.
func (d draw) Annotation(r Renderer, canvasBox Box, style Style, lx, ly int, label string) {
style.GetTextOptions().WriteToRenderer(r)
2016-10-21 21:44:37 +02:00
defer r.ResetStyle()
2016-07-12 03:48:51 +02:00
textBox := r.MeasureText(label)
textWidth := textBox.Width()
halfTextHeight := textBox.Height() >> 1
2016-07-10 10:11:47 +02:00
style.GetFillAndStrokeOptions().WriteToRenderer(r)
2016-07-18 20:43:41 +02:00
pt := style.Padding.GetTop(DefaultAnnotationPadding.Top)
pl := style.Padding.GetLeft(DefaultAnnotationPadding.Left)
pr := style.Padding.GetRight(DefaultAnnotationPadding.Right)
pb := style.Padding.GetBottom(DefaultAnnotationPadding.Bottom)
2016-07-10 10:11:47 +02:00
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
rtx := lx + pl + pr + textWidth + DefaultAnnotationDeltaWidth
2016-07-11 09:18:03 +02:00
rty := ly - (pt + halfTextHeight)
2016-07-10 10:11:47 +02:00
rbx := lx + pl + pr + textWidth + 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
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()
style.GetTextOptions().WriteToRenderer(r)
2016-07-10 10:11:47 +02:00
r.Text(label, textX, textY)
}
2016-07-12 03:48:51 +02:00
2016-07-30 01:36:29 +02:00
// Box draws a box with a given style.
func (d draw) Box(r Renderer, b Box, s Style) {
2016-10-21 21:44:37 +02:00
s.GetFillAndStrokeOptions().WriteToRenderer(r)
defer r.ResetStyle()
2016-07-13 05:34:59 +02:00
2016-07-12 03:48:51 +02: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-10-21 21:44:37 +02:00
func (d draw) BoxRotated(r Renderer, b Box, thetaDegrees float64, s Style) {
2017-05-16 22:31:36 +02:00
d.Box2d(r, b.Corners().Rotate(thetaDegrees), s)
2016-10-21 21:44:37 +02:00
}
2016-09-05 23:03:20 +02:00
2017-05-16 22:31:36 +02:00
func (d draw) Box2d(r Renderer, bc Box2d, s Style) {
2016-10-21 21:44:37 +02:00
s.GetFillAndStrokeOptions().WriteToRenderer(r)
defer r.ResetStyle()
2017-05-16 22:31:36 +02:00
r.MoveTo(int(bc.TopLeft.X), int(bc.TopLeft.Y))
r.LineTo(int(bc.TopRight.X), int(bc.TopRight.Y))
r.LineTo(int(bc.BottomRight.X), int(bc.BottomRight.Y))
r.LineTo(int(bc.BottomLeft.X), int(bc.BottomLeft.Y))
2016-09-05 23:03:20 +02:00
r.Close()
r.FillStroke()
}
// DrawText draws text with a given style.
2016-07-30 03:24:25 +02:00
func (d draw) Text(r Renderer, text string, x, y int, style Style) {
style.GetTextOptions().WriteToRenderer(r)
2016-10-21 21:44:37 +02:00
defer r.ResetStyle()
r.Text(text, x, y)
}
2016-10-21 21:44:37 +02:00
func (d draw) MeasureText(r Renderer, text string, style Style) Box {
style.GetTextOptions().WriteToRenderer(r)
defer r.ResetStyle()
return r.MeasureText(text)
}
2016-07-30 01:36:29 +02:00
// TextWithin draws the text within a given box.
2016-07-30 03:24:25 +02:00
func (d draw) TextWithin(r Renderer, text string, box Box, style Style) {
2016-10-21 21:44:37 +02:00
style.GetTextOptions().WriteToRenderer(r)
defer r.ResetStyle()
2016-07-30 03:24:25 +02:00
lines := Text.WrapFit(r, text, box.Width(), style)
linesBox := Text.MeasureLines(r, lines, style)
y := box.Top
switch style.GetTextVerticalAlign() {
case TextVerticalAlignBottom, TextVerticalAlignBaseline: // i have to build better baseline handling into measure text
y = y - linesBox.Height()
case TextVerticalAlignMiddle, TextVerticalAlignMiddleBaseline:
y = (y - linesBox.Height()) >> 1
}
var tx, ty int
for _, line := range lines {
lineBox := r.MeasureText(line)
switch style.GetTextHorizontalAlign() {
case TextHorizontalAlignCenter:
2016-07-30 03:40:43 +02:00
tx = box.Left + ((box.Width() - lineBox.Width()) >> 1)
2016-07-30 03:24:25 +02:00
case TextHorizontalAlignRight:
tx = box.Right - lineBox.Width()
default:
tx = box.Left
}
2016-10-21 21:44:37 +02:00
if style.TextRotationDegrees == 0 {
ty = y + lineBox.Height()
} else {
ty = y
}
2016-07-30 03:24:25 +02:00
2017-01-10 15:25:06 +01:00
r.Text(line, tx, ty)
2016-07-30 03:24:25 +02:00
y += lineBox.Height() + style.GetTextLineSpacing()
}
}