go-chart/chart.go

350 lines
8.9 KiB
Go
Raw Normal View History

2016-07-06 21:54:00 -04:00
package chart
import (
2016-07-07 23:26:07 -04:00
"errors"
2016-07-06 21:54:00 -04:00
"io"
2016-07-10 20:19:44 -04:00
"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-08 20:57:14 -04:00
DPI float64
2016-07-06 21:54:00 -04:00
2016-07-09 23:14:11 -04:00
Background Style
Canvas Style
2016-07-06 21:54:00 -04:00
2016-07-10 04:11:47 -04:00
XAxis XAxis
YAxis YAxis
YAxisSecondary YAxis
2016-07-06 21:54:00 -04:00
2016-07-12 22:14:14 -04:00
Font *truetype.Font
Series []Series
Elements []Renderable
2016-07-06 21:54:00 -04:00
}
2016-07-08 20:57:14 -04: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 17:44:03 -04:00
// GetFont returns the text font.
func (c Chart) GetFont() (*truetype.Font, error) {
2016-07-08 00:15:35 -04:00
if c.Font == nil {
f, err := GetDefaultFont()
if err != nil {
return nil, err
}
2016-07-08 20:57:14 -04:00
return f, nil
2016-07-06 21:54:00 -04:00
}
2016-07-08 00:17:18 -04:00
return c.Font, nil
2016-07-06 21:54:00 -04:00
}
// Render renders the chart with the given renderer to the given io.Writer.
2016-07-10 13:43:04 -04:00
func (c Chart) Render(rp RendererProvider, w io.Writer) error {
2016-07-07 23:26:07 -04:00
if len(c.Series) == 0 {
return errors.New("Please provide at least one series")
}
2016-07-11 21:48:51 -04:00
c.YAxisSecondary.AxisType = YAxisSecondary
2016-07-09 23:14:11 -04:00
r, err := rp(c.Width, c.Height)
2016-07-08 20:57:14 -04:00
if err != nil {
return err
}
2016-07-09 23:14:11 -04:00
font, err := c.GetFont()
if err != nil {
return err
}
2016-07-10 13:43:04 -04:00
c.Font = font
2016-07-09 23:14:11 -04:00
r.SetFont(font)
2016-07-08 20:57:14 -04:00
r.SetDPI(c.GetDPI(DefaultDPI))
2016-07-07 17:44:03 -04:00
c.drawBackground(r)
var xt, yt, yta []Tick
xr, yr, yra := c.getRanges()
2016-07-10 13:43:04 -04:00
canvasBox := c.getDefaultCanvasBox()
xf, yf, yfa := c.getValueFormatters()
xr, yr, yra = c.setRangeDomains(canvasBox, xr, yr, yra)
2016-07-10 13:43:04 -04:00
if c.hasAxes() {
xt, yt, yta = c.getAxesTicks(r, xr, yr, yra, xf, yf, yfa)
2016-07-11 21:48:51 -04:00
canvasBox = c.getAxisAdjustedCanvasBox(r, canvasBox, xr, yr, yra, xt, yt, yta)
xr, yr, yra = c.setRangeDomains(canvasBox, xr, yr, yra)
xt, yt, yta = c.getAxesTicks(r, xr, yr, yra, xf, yf, yfa)
}
2016-07-10 13:43:04 -04:00
if c.hasAnnotationSeries() {
canvasBox = c.getAnnotationAdjustedCanvasBox(r, canvasBox, xr, yr, yra, xf, yf, yfa)
xr, yr, yra = c.setRangeDomains(canvasBox, xr, yr, yra)
xt, yt, yta = c.getAxesTicks(r, xr, yr, yra, xf, yf, yfa)
}
c.drawCanvas(r, canvasBox)
c.drawAxes(r, canvasBox, xr, yr, yra, xt, yt, yta)
2016-07-10 20:19:44 -04:00
2016-07-07 20:50:16 -04:00
for index, series := range c.Series {
c.drawSeries(r, canvasBox, xr, yr, yra, series, index)
2016-07-06 21:54:00 -04:00
}
c.drawTitle(r)
2016-07-12 22:14:14 -04:00
for _, a := range c.Elements {
a(r, canvasBox)
}
2016-07-06 21:54:00 -04:00
return r.Save(w)
}
2016-07-10 04:11:47 -04:00
func (c Chart) getRanges() (xrange, yrange, yrangeAlt Range) {
var minx, maxx float64 = math.MaxFloat64, 0
var miny, maxy float64 = math.MaxFloat64, 0
var minya, maxya float64 = math.MaxFloat64, 0
2016-07-10 04:11:47 -04:00
2016-07-07 17:44:03 -04:00
for _, s := range c.Series {
2016-07-10 20:19:44 -04:00
seriesAxis := s.GetYAxis()
2016-07-09 23:14:11 -04:00
if vp, isValueProvider := s.(ValueProvider); isValueProvider {
seriesLength := vp.Len()
for index := 0; index < seriesLength; index++ {
vx, vy := vp.GetValue(index)
minx = math.Min(minx, vx)
maxx = math.Max(maxx, vx)
2016-07-10 20:19:44 -04:00
if seriesAxis == YAxisPrimary {
miny = math.Min(miny, vy)
maxy = math.Max(maxy, vy)
2016-07-10 20:19:44 -04:00
} else if seriesAxis == YAxisSecondary {
minya = math.Min(minya, vy)
maxya = math.Max(maxya, vy)
2016-07-07 17:44:03 -04:00
}
}
}
}
2016-07-10 04:11:47 -04:00
if !c.XAxis.Range.IsZero() {
xrange.Min = c.XAxis.Range.Min
xrange.Max = c.XAxis.Range.Max
} else {
xrange.Min = minx
xrange.Max = maxx
2016-07-10 04:11:47 -04:00
}
if !c.YAxis.Range.IsZero() {
yrange.Min = c.YAxis.Range.Min
yrange.Max = c.YAxis.Range.Max
2016-07-07 17:44:03 -04:00
} else {
yrange.Min = miny
yrange.Max = maxy
2016-07-11 21:48:51 -04:00
yrange.Min, yrange.Max = yrange.GetRoundedRangeBounds()
2016-07-07 17:44:03 -04:00
}
2016-07-10 14:19:56 -04:00
if !c.YAxisSecondary.Range.IsZero() {
yrangeAlt.Min = c.YAxisSecondary.Range.Min
yrangeAlt.Max = c.YAxisSecondary.Range.Max
} else {
yrangeAlt.Min = minya
yrangeAlt.Max = maxya
2016-07-11 21:48:51 -04:00
yrangeAlt.Min, yrangeAlt.Max = yrangeAlt.GetRoundedRangeBounds()
2016-07-10 14:19:56 -04:00
}
2016-07-10 20:19:44 -04:00
2016-07-10 04:11:47 -04:00
return
}
2016-07-10 13:43:04 -04:00
func (c Chart) getDefaultCanvasBox() Box {
dpt := c.Background.Padding.GetTop(DefaultBackgroundPadding.Top)
2016-07-10 04:11:47 -04:00
dpl := c.Background.Padding.GetLeft(DefaultBackgroundPadding.Left)
dpr := c.Background.Padding.GetRight(DefaultBackgroundPadding.Right)
dpb := c.Background.Padding.GetBottom(DefaultBackgroundPadding.Bottom)
return Box{
Top: dpt,
2016-07-10 04:11:47 -04:00
Left: dpl,
Right: c.Width - dpr,
Bottom: c.Height - dpb,
2016-07-07 17:44:03 -04:00
}
2016-07-10 04:11:47 -04:00
}
2016-07-10 13:43:04 -04:00
func (c Chart) getValueFormatters() (x, y, ya ValueFormatter) {
for _, s := range c.Series {
if vfp, isVfp := s.(ValueFormatterProvider); isVfp {
sx, sy := vfp.GetValueFormatters()
if s.GetYAxis() == YAxisPrimary {
x = sx
y = sy
} else if s.GetYAxis() == YAxisSecondary {
x = sx
ya = sy
}
}
}
if c.XAxis.ValueFormatter != nil {
x = c.XAxis.ValueFormatter
}
if c.YAxis.ValueFormatter != nil {
y = c.YAxis.ValueFormatter
}
if c.YAxisSecondary.ValueFormatter != nil {
ya = c.YAxisSecondary.ValueFormatter
}
return
}
func (c Chart) hasAxes() bool {
return c.XAxis.Style.Show || c.YAxis.Style.Show || c.YAxisSecondary.Style.Show
}
2016-07-10 13:43:04 -04:00
func (c Chart) getAxesTicks(r Renderer, xr, yr, yar Range, xf, yf, yfa ValueFormatter) (xticks, yticks, yticksAlt []Tick) {
if c.XAxis.Style.Show {
xticks = c.XAxis.GetTicks(r, xr, xf)
}
if c.YAxis.Style.Show {
yticks = c.YAxis.GetTicks(r, yr, yf)
}
if c.YAxisSecondary.Style.Show {
2016-07-10 14:19:56 -04:00
yticksAlt = c.YAxisSecondary.GetTicks(r, yar, yf)
2016-07-10 13:43:04 -04:00
}
return
}
2016-07-11 21:48:51 -04:00
func (c Chart) getAxisAdjustedCanvasBox(r Renderer, canvasBox Box, xr, yr, yra Range, xticks, yticks, yticksAlt []Tick) Box {
axesOuterBox := canvasBox.Clone()
2016-07-10 13:43:04 -04:00
if c.XAxis.Style.Show {
2016-07-11 21:48:51 -04:00
axesBounds := c.XAxis.Measure(r, canvasBox, xr, xticks)
axesOuterBox = axesOuterBox.Grow(axesBounds)
2016-07-10 13:43:04 -04:00
}
if c.YAxis.Style.Show {
2016-07-11 21:48:51 -04:00
axesBounds := c.YAxis.Measure(r, canvasBox, yr, yticks)
axesOuterBox = axesOuterBox.Grow(axesBounds)
2016-07-10 13:43:04 -04:00
}
if c.YAxisSecondary.Style.Show {
2016-07-11 21:48:51 -04:00
axesBounds := c.YAxisSecondary.Measure(r, canvasBox, yra, yticksAlt)
axesOuterBox = axesOuterBox.Grow(axesBounds)
2016-07-11 21:48:51 -04:00
}
2016-07-11 03:12:14 -04:00
return canvasBox.OuterConstrain(c.asBox(), axesOuterBox)
2016-07-10 04:11:47 -04:00
}
2016-07-12 02:32:31 -04:00
func (c Chart) setRangeDomains(canvasBox Box, xr, yr, yra Range) (xr2, yr2, yra2 Range) {
xr2.Min, xr2.Max = xr.Min, xr.Max
xr2.Domain = canvasBox.Width()
2016-07-12 02:32:31 -04:00
yr2.Min, yr2.Max = yr.Min, yr.Max
yr2.Domain = canvasBox.Height()
2016-07-12 02:32:31 -04:00
yra2.Min, yra2.Max = yra.Min, yra.Max
yra2.Domain = canvasBox.Height()
2016-07-12 02:32:31 -04:00
return
2016-07-07 17:44:03 -04:00
}
func (c Chart) hasAnnotationSeries() bool {
for _, s := range c.Series {
2016-07-10 20:19:44 -04:00
if as, isAnnotationSeries := s.(AnnotationSeries); isAnnotationSeries {
if as.Style.Show {
return true
}
}
}
2016-07-10 20:19:44 -04:00
return false
}
func (c Chart) getAnnotationAdjustedCanvasBox(r Renderer, canvasBox Box, xr, yr, yra Range, xf, yf, yfa ValueFormatter) Box {
annotationSeriesBox := canvasBox.Clone()
for seriesIndex, s := range c.Series {
2016-07-10 20:19:44 -04:00
if as, isAnnotationSeries := s.(AnnotationSeries); isAnnotationSeries {
if as.Style.Show {
style := c.seriesStyleDefaults(seriesIndex)
var annotationBounds Box
if as.YAxis == YAxisPrimary {
annotationBounds = as.Measure(r, canvasBox, xr, yr, style)
} else if as.YAxis == YAxisSecondary {
2016-07-10 20:19:44 -04:00
annotationBounds = as.Measure(r, canvasBox, xr, yra, style)
}
2016-07-10 20:19:44 -04:00
annotationSeriesBox = annotationSeriesBox.Grow(annotationBounds)
}
}
}
return canvasBox.OuterConstrain(c.asBox(), annotationSeriesBox)
}
2016-07-06 21:54:00 -04:00
func (c Chart) drawBackground(r Renderer) {
DrawBox(r, Box{Right: c.Width, Bottom: c.Height}, c.Canvas.WithDefaultsFrom(Style{
FillColor: DefaultBackgroundColor,
StrokeColor: DefaultBackgroundStrokeColor,
StrokeWidth: DefaultStrokeWidth,
}))
2016-07-06 21:54:00 -04:00
}
func (c Chart) drawCanvas(r Renderer, canvasBox Box) {
DrawBox(r, canvasBox, c.Canvas.WithDefaultsFrom(Style{
FillColor: DefaultCanvasColor,
StrokeColor: DefaultCanvasStrokeColor,
StrokeWidth: DefaultStrokeWidth,
}))
2016-07-06 21:54:00 -04:00
}
2016-07-10 13:43:04 -04:00
func (c Chart) drawAxes(r Renderer, canvasBox Box, xrange, yrange, yrangeAlt Range, xticks, yticks, yticksAlt []Tick) {
2016-07-10 04:11:47 -04:00
if c.XAxis.Style.Show {
2016-07-10 13:43:04 -04:00
c.XAxis.Render(r, canvasBox, xrange, xticks)
}
2016-07-10 04:11:47 -04:00
if c.YAxis.Style.Show {
2016-07-11 21:48:51 -04:00
c.YAxis.Render(r, canvasBox, yrange, yticks)
2016-07-07 23:26:07 -04:00
}
2016-07-10 04:11:47 -04:00
if c.YAxisSecondary.Style.Show {
2016-07-11 21:48:51 -04:00
c.YAxisSecondary.Render(r, canvasBox, yrangeAlt, yticksAlt)
2016-07-07 23:26:07 -04:00
}
}
2016-07-10 04:11:47 -04:00
func (c Chart) drawSeries(r Renderer, canvasBox Box, xrange, yrange, yrangeAlt Range, s Series, seriesIndex int) {
if s.GetYAxis() == YAxisPrimary {
s.Render(r, canvasBox, xrange, yrange, c.seriesStyleDefaults(seriesIndex))
2016-07-10 04:11:47 -04:00
} else if s.GetYAxis() == YAxisSecondary {
s.Render(r, canvasBox, xrange, yrangeAlt, c.seriesStyleDefaults(seriesIndex))
2016-07-10 04:11:47 -04:00
}
2016-07-06 21:54:00 -04:00
}
2016-07-10 13:43:04 -04:00
func (c Chart) drawTitle(r Renderer) {
2016-07-07 17:44:03 -04:00
if len(c.Title) > 0 && c.TitleStyle.Show {
2016-07-10 13:43:04 -04:00
r.SetFont(c.TitleStyle.GetFont(c.Font))
r.SetFontColor(c.TitleStyle.GetFontColor(DefaultTextColor))
titleFontSize := c.TitleStyle.GetFontSize(DefaultTitleFontSize)
2016-07-07 17:44:03 -04:00
r.SetFontSize(titleFontSize)
2016-07-10 13:43:04 -04:00
2016-07-11 21:48:51 -04:00
textBox := r.MeasureText(c.Title)
2016-07-10 13:43:04 -04:00
textWidth := textBox.Width()
textHeight := textBox.Height()
2016-07-10 13:43:04 -04:00
2016-07-06 21:54:00 -04:00
titleX := (c.Width >> 1) - (textWidth >> 1)
2016-07-10 13:43:04 -04:00
titleY := c.TitleStyle.Padding.GetTop(DefaultTitleTop) + textHeight
2016-07-06 21:54:00 -04:00
r.Text(c.Title, titleX, titleY)
}
}
func (c Chart) seriesStyleDefaults(seriesIndex int) Style {
strokeColor := GetDefaultSeriesStrokeColor(seriesIndex)
return Style{
StrokeColor: strokeColor,
StrokeWidth: DefaultStrokeWidth,
Font: c.Font,
FontSize: DefaultFontSize,
}
}
func (c Chart) asBox() Box {
return Box{Right: c.Width, Bottom: c.Height}
}