go-chart/chart.go

347 lines
8.7 KiB
Go
Raw Normal View History

2016-07-07 03:54:00 +02:00
package chart
import (
2016-07-08 05:26:07 +02:00
"errors"
2016-07-07 03:54:00 +02:00
"io"
"math"
2016-07-07 03:54:00 +02:00
"github.com/golang/freetype/truetype"
)
// Chart is what we're drawing.
type Chart struct {
2016-07-07 23:44:03 +02:00
Title string
TitleStyle Style
2016-07-07 03:54:00 +02:00
Width int
Height int
2016-07-09 02:57:14 +02:00
DPI float64
2016-07-07 03:54:00 +02:00
2016-07-10 05:14:11 +02:00
Background Style
Canvas Style
Axes Style
2016-07-07 03:54:00 +02:00
2016-07-07 23:44:03 +02:00
XRange Range
YRange Range
2016-07-07 03:54:00 +02:00
2016-07-07 23:44:03 +02:00
Font *truetype.Font
2016-07-07 03:54:00 +02:00
Series []Series
}
2016-07-09 02:57:14 +02:00
// GetDPI returns the dpi for the chart.
func (c Chart) GetDPI(defaults ...float64) float64 {
if c.DPI == 0 {
if len(defaults) > 0 {
return defaults[0]
}
return DefaultDPI
}
return c.DPI
}
2016-07-07 23:44:03 +02:00
// GetFont returns the text font.
func (c Chart) GetFont() (*truetype.Font, error) {
2016-07-08 06:15:35 +02:00
if c.Font == nil {
f, err := GetDefaultFont()
if err != nil {
return nil, err
}
2016-07-09 02:57:14 +02:00
return f, nil
2016-07-07 03:54:00 +02:00
}
2016-07-08 06:17:18 +02:00
return c.Font, nil
2016-07-07 03:54:00 +02:00
}
// Render renders the chart with the given renderer to the given io.Writer.
2016-07-10 05:14:11 +02:00
func (c *Chart) Render(rp RendererProvider, w io.Writer) error {
2016-07-08 05:26:07 +02:00
if len(c.Series) == 0 {
return errors.New("Please provide at least one series")
}
2016-07-10 05:14:11 +02:00
r, err := rp(c.Width, c.Height)
2016-07-09 02:57:14 +02:00
if err != nil {
return err
}
2016-07-10 05:14:11 +02:00
font, err := c.GetFont()
if err != nil {
return err
}
2016-07-10 05:14:11 +02:00
r.SetFont(font)
2016-07-09 02:57:14 +02:00
r.SetDPI(c.GetDPI(DefaultDPI))
2016-07-07 23:44:03 +02:00
canvasBox := c.calculateCanvasBox(r)
xrange, yrange := c.initRanges(canvasBox)
2016-07-07 03:54:00 +02:00
c.drawBackground(r)
c.drawCanvas(r, canvasBox)
c.drawAxes(r, canvasBox, xrange, yrange)
2016-07-08 02:50:16 +02:00
for index, series := range c.Series {
2016-07-10 05:14:11 +02:00
c.drawSeries(r, canvasBox, series, xrange, yrange)
2016-07-07 03:54:00 +02:00
}
c.drawTitle(r)
2016-07-07 03:54:00 +02:00
return r.Save(w)
}
2016-07-08 05:26:07 +02:00
func (c Chart) getAxisWidth() int {
asw := 0
if c.Axes.Show {
asw = int(c.Axes.GetStrokeWidth(DefaultAxisLineWidth))
}
return asw
}
func (c Chart) calculateCanvasBox(r Renderer) Box {
dpr := DefaultBackgroundPadding.Right
finalLabelWidth := c.calculateFinalLabelWidth(r)
if finalLabelWidth > dpr {
dpr = finalLabelWidth
}
2016-07-08 05:26:07 +02:00
axisBottomHeight := c.calculateBottomLabelHeight()
dpb := DefaultBackgroundPadding.Bottom
2016-07-08 05:26:07 +02:00
if dpb < axisBottomHeight {
dpb = axisBottomHeight
}
cb := Box{
Top: c.Background.Padding.GetTop(DefaultBackgroundPadding.Top),
Left: c.Background.Padding.GetLeft(DefaultBackgroundPadding.Left),
Right: c.Width - c.Background.Padding.GetRight(dpr),
Bottom: c.Height - c.Background.Padding.GetBottom(dpb),
}
cb.Height = cb.Bottom - cb.Top
cb.Width = cb.Right - cb.Left
return cb
}
func (c Chart) calculateFinalLabelWidth(r Renderer) int {
var finalLabelText string
for _, s := range c.Series {
2016-07-10 05:14:11 +02:00
if vs, isValueProvider := s.(ValueProvider); isValueProvider {
_, lv := vs.GetValue(vs.Len() - 1)
var ll string
if c.YRange.Formatter != nil {
ll = c.YRange.Formatter(lv)
} else if fp, isFormatterProvider := s.(FormatterProvider); isFormatterProvider {
ll = fp.GetYFormatter()(lv)
}
if len(finalLabelText) < len(ll) {
finalLabelText = ll
}
}
}
r.SetFontSize(c.FinalValueLabel.GetFontSize(DefaultFinalLabelFontSize))
2016-07-09 02:57:14 +02:00
textWidth, _ := r.MeasureText(finalLabelText)
2016-07-08 05:26:07 +02:00
asw := c.getAxisWidth()
pl := c.FinalValueLabel.Padding.GetLeft(DefaultFinalLabelPadding.Left)
pr := c.FinalValueLabel.Padding.GetRight(DefaultFinalLabelPadding.Right)
lsw := int(c.FinalValueLabel.GetStrokeWidth(DefaultAxisLineWidth))
2016-07-10 05:14:11 +02:00
return DefaultYAxisMargin +
pl + pr +
textWidth + asw + 2*lsw
}
2016-07-08 05:26:07 +02:00
func (c Chart) calculateBottomLabelHeight() int {
if c.Axes.Show {
return c.getAxisWidth() + int(math.Ceil(c.Axes.GetFontSize(DefaultAxisFontSize))) + DefaultXAxisMargin
}
return 0
}
func (c Chart) initRanges(canvasBox Box) (xrange Range, yrange Range) {
2016-07-07 23:44:03 +02:00
//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 {
2016-07-10 05:14:11 +02:00
if vp, isValueProvider := s.(ValueProvider); isValueProvider {
seriesLength := vp.Len()
for index := 0; index < seriesLength; index++ {
vx, vy := vp.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
2016-07-07 23:44:03 +02:00
}
}
}
2016-07-10 05:14:11 +02:00
if fp, isFormatterProvider := s.(FormatterProvider); isFormatterProvider {
if xrange.Formatter == nil {
xrange.Formatter = fp.GetXFormatter()
}
if yrange.Formatter == nil {
yrange.Formatter = fp.GetYFormatter()
}
2016-07-08 09:04:39 +02:00
}
2016-07-07 23:44:03 +02:00
}
if c.XRange.IsZero() {
xrange.Min = globalMinX
xrange.Max = globalMaxX
} else {
xrange.Min = c.XRange.Min
xrange.Max = c.XRange.Max
}
2016-07-08 09:16:02 +02:00
if c.XRange.Formatter != nil {
xrange.Formatter = c.XRange.Formatter
}
if c.XRange.Ticks != nil {
xrange.Ticks = c.XRange.Ticks
}
xrange.Domain = canvasBox.Width
2016-07-07 23:44:03 +02:00
if c.YRange.IsZero() {
yrange.Min = globalMinY
yrange.Max = globalMaxY
} else {
yrange.Min = c.YRange.Min
yrange.Max = c.YRange.Max
}
2016-07-08 09:16:02 +02:00
if c.YRange.Formatter != nil {
yrange.Formatter = c.YRange.Formatter
}
if c.YRange.Ticks != nil {
yrange.Ticks = c.YRange.Ticks
}
yrange.Domain = canvasBox.Height
2016-07-07 23:44:03 +02:00
return
}
2016-07-07 03:54:00 +02:00
func (c Chart) drawBackground(r Renderer) {
2016-07-07 23:44:03 +02:00
r.SetFillColor(c.Background.GetFillColor(DefaultBackgroundColor))
r.SetStrokeColor(c.Background.GetStrokeColor(DefaultBackgroundStrokeColor))
2016-07-08 07:18:53 +02:00
r.SetStrokeWidth(c.Background.GetStrokeWidth(DefaultStrokeWidth))
2016-07-07 03:54:00 +02: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()
r.FillStroke()
2016-07-07 03:54:00 +02:00
}
func (c Chart) drawCanvas(r Renderer, canvasBox Box) {
2016-07-07 23:44:03 +02:00
r.SetFillColor(c.Canvas.GetFillColor(DefaultCanvasColor))
r.SetStrokeColor(c.Canvas.GetStrokeColor(DefaultCanvasStrokColor))
2016-07-08 07:18:53 +02:00
r.SetStrokeWidth(c.Canvas.GetStrokeWidth(DefaultStrokeWidth))
r.MoveTo(canvasBox.Left, canvasBox.Top)
r.LineTo(canvasBox.Right, canvasBox.Top)
r.LineTo(canvasBox.Right, canvasBox.Bottom)
r.LineTo(canvasBox.Left, canvasBox.Bottom)
r.LineTo(canvasBox.Left, canvasBox.Top)
2016-07-07 03:54:00 +02:00
r.Close()
r.FillStroke()
2016-07-07 03:54:00 +02:00
}
func (c Chart) drawAxes(r Renderer, canvasBox Box, xrange, yrange Range) {
2016-07-07 23:44:03 +02:00
if c.Axes.Show {
r.SetStrokeColor(c.Axes.GetStrokeColor(DefaultAxisColor))
2016-07-08 07:18:53 +02:00
r.SetStrokeWidth(c.Axes.GetStrokeWidth(DefaultStrokeWidth))
r.MoveTo(canvasBox.Left, canvasBox.Bottom)
r.LineTo(canvasBox.Right, canvasBox.Bottom)
r.LineTo(canvasBox.Right, canvasBox.Top)
2016-07-07 03:54:00 +02:00
r.Stroke()
2016-07-08 05:26:07 +02:00
c.drawXAxisLabels(r, canvasBox, xrange)
c.drawYAxisLabels(r, canvasBox, yrange)
2016-07-07 03:54:00 +02:00
}
}
func (c Chart) generateRangeTicks(r Range, tickCount int, offset float64) []Tick {
var ticks []Tick
rangeTicks := Slices(tickCount, r.Max-r.Min)
for _, rv := range rangeTicks {
ticks = append(ticks, Tick{
RangeValue: rv + offset,
Label: r.Format(rv + offset),
})
}
return ticks
}
2016-07-08 05:26:07 +02:00
func (c Chart) drawYAxisLabels(r Renderer, canvasBox Box, yrange Range) {
tickFontSize := c.Axes.GetFontSize(DefaultAxisFontSize)
asw := c.getAxisWidth()
2016-07-10 05:14:11 +02:00
tx := canvasBox.Right + DefaultYAxisMargin + asw
2016-07-08 05:26:07 +02:00
r.SetFontColor(c.Axes.GetFontColor(DefaultAxisColor))
r.SetFontSize(tickFontSize)
ticks := yrange.Ticks
if ticks == nil {
minimumTickHeight := tickFontSize + DefaultMinimumTickVerticalSpacing
tickCount := int(math.Floor(float64(yrange.Domain) / float64(minimumTickHeight)))
2016-07-08 05:26:07 +02:00
if tickCount > DefaultMaxTickCount {
tickCount = DefaultMaxTickCount
}
ticks = c.generateRangeTicks(yrange, tickCount, yrange.Min)
2016-07-08 05:26:07 +02:00
}
for _, t := range ticks {
v := t.RangeValue
y := yrange.Translate(v)
ty := int(y)
r.Text(t.Label, tx, ty)
2016-07-08 05:26:07 +02:00
}
}
func (c Chart) drawXAxisLabels(r Renderer, canvasBox Box, xrange Range) {
tickFontSize := c.Axes.GetFontSize(DefaultAxisFontSize)
ty := canvasBox.Bottom + DefaultXAxisMargin + int(tickFontSize)
2016-07-08 05:26:07 +02:00
r.SetFontColor(c.Axes.GetFontColor(DefaultAxisColor))
r.SetFontSize(tickFontSize)
ticks := xrange.Ticks
if ticks == nil {
maxLabelWidth := 60
minimumTickWidth := maxLabelWidth + DefaultMinimumTickHorizontalSpacing
tickCount := int(math.Floor(float64(xrange.Domain) / float64(minimumTickWidth)))
2016-07-08 05:26:07 +02:00
if tickCount > DefaultMaxTickCount {
tickCount = DefaultMaxTickCount
}
ticks = c.generateRangeTicks(xrange, tickCount, xrange.Min)
2016-07-08 05:26:07 +02:00
}
for _, t := range ticks {
v := t.RangeValue
x := xrange.Translate(v)
2016-07-08 05:26:07 +02:00
tx := canvasBox.Left + int(x)
r.Text(t.Label, tx, ty)
2016-07-08 05:26:07 +02:00
}
}
2016-07-10 05:14:11 +02:00
func (c Chart) drawSeries(r Renderer, canvasBox Box, s Series, xrange, yrange Range) {
return s.Render(&c, r, canvasBox, xrange, yrange)
2016-07-07 03:54:00 +02:00
}
func (c Chart) drawTitle(r Renderer) error {
2016-07-07 23:44:03 +02: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-09 02:57:14 +02:00
textWidth, _ := r.MeasureText(c.Title)
2016-07-07 03:54:00 +02:00
titleX := (c.Width >> 1) - (textWidth >> 1)
titleY := c.TitleStyle.Padding.GetTop(DefaultTitleTop) + int(titleFontSize)
2016-07-07 03:54:00 +02:00
r.Text(c.Title, titleX, titleY)
}
return nil
}