go-chart/chart.go

276 lines
7 KiB
Go
Raw Normal View History

2016-07-06 21:54:00 -04:00
package chart
import (
"io"
"math"
2016-07-06 21:54:00 -04:00
"github.com/golang/freetype/truetype"
)
// Chart is what we're drawing.
type Chart struct {
2016-07-07 17:44:03 -04:00
Title string
TitleStyle Style
2016-07-06 21:54:00 -04:00
Width int
Height int
2016-07-06 21:54:00 -04:00
2016-07-07 17:44:03 -04:00
Background Style
Canvas Style
Axes Style
FinalValueLabel Style
2016-07-06 21:54:00 -04:00
2016-07-07 17:44:03 -04:00
XRange Range
YRange Range
2016-07-06 21:54:00 -04:00
2016-07-07 17:44:03 -04:00
Font *truetype.Font
2016-07-06 21:54:00 -04:00
Series []Series
}
// GetCanvasTop gets the top corner pixel.
func (c Chart) GetCanvasTop() int {
return c.Canvas.Padding.GetTop(DefaultCanvasPadding.Top)
2016-07-06 21:54:00 -04:00
}
// GetCanvasLeft gets the left corner pixel.
func (c Chart) GetCanvasLeft() int {
return c.Canvas.Padding.GetLeft(DefaultCanvasPadding.Left)
2016-07-06 21:54:00 -04:00
}
// GetCanvasBottom gets the bottom corner pixel.
func (c Chart) GetCanvasBottom() int {
return c.Height - c.Canvas.Padding.GetBottom(DefaultCanvasPadding.Bottom)
2016-07-06 21:54:00 -04:00
}
// GetCanvasRight gets the right corner pixel.
func (c Chart) GetCanvasRight() int {
return c.Width - c.Canvas.Padding.GetRight(DefaultCanvasPadding.Right)
2016-07-06 21:54:00 -04:00
}
// GetCanvasWidth returns the width of the canvas.
func (c Chart) GetCanvasWidth() int {
return c.Width - (c.Canvas.Padding.GetLeft(DefaultCanvasPadding.Left) + c.Canvas.Padding.GetRight(DefaultCanvasPadding.Right))
2016-07-06 21:54:00 -04:00
}
// GetCanvasHeight returns the height of the canvas.
func (c Chart) GetCanvasHeight() int {
return c.Height - (c.Canvas.Padding.GetTop(DefaultCanvasPadding.Top) + c.Canvas.Padding.GetBottom(DefaultCanvasPadding.Bottom))
2016-07-06 21:54:00 -04:00
}
2016-07-07 17:44:03 -04:00
// GetFont returns the text font.
func (c Chart) GetFont() (*truetype.Font, error) {
2016-07-06 21:54:00 -04:00
if c.Font != nil {
return c.Font, nil
}
return GetDefaultFont()
}
// Render renders the chart with the given renderer to the given io.Writer.
2016-07-07 17:44:03 -04:00
func (c *Chart) Render(provider RendererProvider, w io.Writer) error {
xrange, yrange := c.initRanges()
font, err := c.GetFont()
if err != nil {
return err
}
2016-07-07 17:44:03 -04:00
2016-07-06 21:54:00 -04:00
r := provider(c.Width, c.Height)
r.SetFont(font)
2016-07-06 21:54:00 -04:00
c.drawBackground(r)
c.drawCanvas(r)
c.drawAxes(r, xrange, yrange)
2016-07-06 21:54:00 -04:00
for _, series := range c.Series {
2016-07-07 17:44:03 -04:00
c.drawSeries(r, series, xrange, yrange)
2016-07-06 21:54:00 -04:00
}
c.drawTitle(r)
2016-07-06 21:54:00 -04:00
return r.Save(w)
}
2016-07-07 17:44:03 -04:00
func (c Chart) initRanges() (xrange Range, yrange Range) {
//iterate over each series, pull out the min/max for x,y
var didSetFirstValues bool
var globalMinY, globalMinX float64
var globalMaxY, globalMaxX float64
for _, s := range c.Series {
seriesLength := s.Len()
for index := 0; index < seriesLength; index++ {
vx, vy := s.GetValue(index)
if didSetFirstValues {
if globalMinX > vx {
globalMinX = vx
}
if globalMinY > vy {
globalMinY = vy
}
if globalMaxX < vx {
globalMaxX = vx
}
if globalMaxY < vy {
globalMaxY = vy
}
} else {
globalMinX, globalMaxX = vx, vx
globalMinY, globalMaxY = vy, vy
didSetFirstValues = true
}
}
}
if c.XRange.IsZero() {
xrange.Min = globalMinX
xrange.Max = globalMaxX
} else {
xrange.Min = c.XRange.Min
xrange.Max = c.XRange.Max
}
xrange.Domain = c.GetCanvasWidth()
if c.YRange.IsZero() {
yrange.Min = globalMinY
yrange.Max = globalMaxY
} else {
yrange.Min = c.YRange.Min
yrange.Max = c.YRange.Max
}
yrange.Domain = c.GetCanvasHeight()
return
}
2016-07-06 21:54:00 -04:00
func (c Chart) drawBackground(r Renderer) {
2016-07-07 17:44:03 -04:00
r.SetFillColor(c.Background.GetFillColor(DefaultBackgroundColor))
2016-07-06 21:54:00 -04:00
r.MoveTo(0, 0)
r.LineTo(c.Width, 0)
r.LineTo(c.Width, c.Height)
r.LineTo(0, c.Height)
r.LineTo(0, 0)
r.Close()
2016-07-07 17:44:03 -04:00
r.Fill()
2016-07-06 21:54:00 -04:00
}
func (c Chart) drawCanvas(r Renderer) {
2016-07-07 17:44:03 -04:00
r.SetFillColor(c.Canvas.GetFillColor(DefaultCanvasColor))
2016-07-06 21:54:00 -04:00
r.MoveTo(c.GetCanvasLeft(), c.GetCanvasTop())
r.LineTo(c.GetCanvasRight(), c.GetCanvasTop())
r.LineTo(c.GetCanvasRight(), c.GetCanvasBottom())
r.LineTo(c.GetCanvasLeft(), c.GetCanvasBottom())
r.LineTo(c.GetCanvasLeft(), c.GetCanvasTop())
2016-07-07 17:44:03 -04:00
r.Fill()
2016-07-06 21:54:00 -04:00
r.Close()
}
func (c Chart) drawAxes(r Renderer, xrange, yrange Range) {
2016-07-07 17:44:03 -04:00
if c.Axes.Show {
r.SetStrokeColor(c.Axes.GetStrokeColor(DefaultAxisColor))
r.SetLineWidth(c.Axes.GetStrokeWidth(DefaultLineWidth))
2016-07-06 21:54:00 -04:00
r.MoveTo(c.GetCanvasLeft(), c.GetCanvasBottom())
r.LineTo(c.GetCanvasRight(), c.GetCanvasBottom())
r.LineTo(c.GetCanvasRight(), c.GetCanvasTop())
r.Stroke()
c.drawAxesLabels(r, xrange, yrange)
2016-07-06 21:54:00 -04:00
}
}
func (c Chart) drawAxesLabels(r Renderer, xrange, yrange Range) {
}
2016-07-07 17:44:03 -04:00
func (c Chart) drawSeries(r Renderer, s Series, xrange, yrange Range) {
r.SetStrokeColor(s.GetStyle().GetStrokeColor(DefaultLineColor))
r.SetLineWidth(s.GetStyle().GetStrokeWidth(DefaultLineWidth))
2016-07-06 22:04:21 -04:00
if s.Len() == 0 {
return
}
px := c.Canvas.Padding.GetLeft(DefaultCanvasPadding.Left)
py := c.Canvas.Padding.GetTop(DefaultCanvasPadding.Top)
2016-07-07 17:44:03 -04:00
cw := c.GetCanvasWidth()
2016-07-07 00:12:19 -04:00
v0x, v0y := s.GetValue(0)
2016-07-07 17:44:03 -04:00
x0 := cw - xrange.Translate(v0x)
2016-07-07 00:12:19 -04:00
y0 := yrange.Translate(v0y)
2016-07-07 17:44:03 -04:00
r.MoveTo(x0+px, y0+py)
2016-07-06 22:04:21 -04:00
2016-07-07 17:44:03 -04:00
var vx, vy float64
2016-07-06 22:04:21 -04:00
var x, y int
2016-07-07 17:44:03 -04:00
for index := 1; index < s.Len(); index++ {
2016-07-07 00:12:19 -04:00
vx, vy = s.GetValue(index)
2016-07-07 17:44:03 -04:00
x = cw - xrange.Translate(vx)
2016-07-07 00:12:19 -04:00
y = yrange.Translate(vy)
2016-07-07 17:44:03 -04:00
r.LineTo(x+px, y+py)
2016-07-06 22:04:21 -04:00
}
2016-07-07 00:12:19 -04:00
r.Stroke()
c.drawFinalValueLabel(r, s, yrange)
}
func (c Chart) drawFinalValueLabel(r Renderer, s Series, yrange Range) {
if c.FinalValueLabel.Show {
_, lv := s.GetValue(s.Len() - 1)
_, ll := s.GetLabel(s.Len() - 1)
py := c.Canvas.Padding.GetTop(DefaultCanvasPadding.Top)
ly := yrange.Translate(lv) + py
r.SetFontSize(c.FinalValueLabel.GetFontSize(DefaultFinalLabelFontSize))
textWidth := r.MeasureText(ll)
textHeight := int(math.Floor(DefaultFinalLabelFontSize))
halfTextHeight := textHeight >> 1
cx := c.GetCanvasRight() + int(c.Axes.GetStrokeWidth(DefaultAxisLineWidth))
pt := c.FinalValueLabel.Padding.GetTop(DefaultFinalLabelPadding.Top)
pl := c.FinalValueLabel.Padding.GetLeft(DefaultFinalLabelPadding.Left)
pr := c.FinalValueLabel.Padding.GetRight(DefaultFinalLabelPadding.Right)
pb := c.FinalValueLabel.Padding.GetBottom(DefaultFinalLabelPadding.Bottom)
textX := cx + pl + DefaultFinalLabelDeltaWidth
textY := ly + halfTextHeight
ltlx := cx + pl + DefaultFinalLabelDeltaWidth
ltly := ly - (pt + halfTextHeight)
ltrx := cx + pl + pr + textWidth
ltry := ly - (pt + halfTextHeight)
lbrx := cx + pl + pr + textWidth
lbry := ly + (pb + halfTextHeight)
lblx := cx + DefaultFinalLabelDeltaWidth
lbly := ly + (pb + halfTextHeight)
//draw the shape...
r.SetFillColor(c.FinalValueLabel.GetFillColor(DefaultFinalLabelBackgroundColor))
r.SetStrokeColor(c.FinalValueLabel.GetStrokeColor(s.GetStyle().GetStrokeColor(DefaultLineColor)))
r.SetLineWidth(c.FinalValueLabel.GetStrokeWidth(DefaultAxisLineWidth))
r.MoveTo(cx, ly)
r.LineTo(ltlx, ltly)
r.LineTo(ltrx, ltry)
r.LineTo(lbrx, lbry)
r.LineTo(lblx, lbly)
r.LineTo(cx, ly)
r.Close()
r.FillStroke()
r.SetFontColor(c.FinalValueLabel.GetFontColor(DefaultTextColor))
r.Text(ll, textX, textY)
}
2016-07-06 21:54:00 -04:00
}
func (c Chart) drawTitle(r Renderer) error {
2016-07-07 17:44:03 -04:00
if len(c.Title) > 0 && c.TitleStyle.Show {
r.SetFontColor(c.Canvas.GetFontColor(DefaultTextColor))
titleFontSize := c.Canvas.GetFontSize(DefaultTitleFontSize)
r.SetFontSize(titleFontSize)
2016-07-06 21:54:00 -04:00
textWidth := r.MeasureText(c.Title)
titleX := (c.Width >> 1) - (textWidth >> 1)
2016-07-07 17:44:03 -04:00
titleY := c.GetCanvasTop() + int(titleFontSize)
2016-07-06 21:54:00 -04:00
r.Text(c.Title, titleX, titleY)
}
return nil
}