2016-07-06 21:54:00 -04:00
|
|
|
package chart
|
|
|
|
|
|
|
|
import (
|
2016-07-07 23:26:07 -04:00
|
|
|
"errors"
|
2017-03-02 17:26:21 -05:00
|
|
|
"fmt"
|
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
|
|
|
|
2017-04-14 20:43:52 -04:00
|
|
|
ColorPalette ColorPalette
|
|
|
|
|
2016-07-07 20:14:25 -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 23:34:59 -04:00
|
|
|
Font *truetype.Font
|
|
|
|
defaultFont *truetype.Font
|
|
|
|
|
2016-07-12 22:14:14 -04:00
|
|
|
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.
|
2016-07-12 23:34:59 -04:00
|
|
|
func (c Chart) GetFont() *truetype.Font {
|
2016-07-08 00:15:35 -04:00
|
|
|
if c.Font == nil {
|
2016-07-12 23:34:59 -04:00
|
|
|
return c.defaultFont
|
|
|
|
}
|
|
|
|
return c.Font
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWidth returns the chart width or the default value.
|
|
|
|
func (c Chart) GetWidth() int {
|
|
|
|
if c.Width == 0 {
|
|
|
|
return DefaultChartWidth
|
2016-07-06 21:54:00 -04:00
|
|
|
}
|
2016-07-12 23:34:59 -04:00
|
|
|
return c.Width
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHeight returns the chart height or the default value.
|
|
|
|
func (c Chart) GetHeight() int {
|
|
|
|
if c.Height == 0 {
|
|
|
|
return DefaultChartHeight
|
|
|
|
}
|
|
|
|
return c.Height
|
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 {
|
2017-03-02 17:26:21 -05:00
|
|
|
return errors.New("please provide at least one series")
|
2016-07-07 23:26:07 -04:00
|
|
|
}
|
2017-03-02 17:26:21 -05:00
|
|
|
if visibleSeriesErr := c.checkHasVisibleSeries(); visibleSeriesErr != nil {
|
|
|
|
return visibleSeriesErr
|
|
|
|
}
|
|
|
|
|
2016-07-11 21:48:51 -04:00
|
|
|
c.YAxisSecondary.AxisType = YAxisSecondary
|
|
|
|
|
2016-07-12 23:34:59 -04:00
|
|
|
r, err := rp(c.GetWidth(), c.GetHeight())
|
2016-07-08 20:57:14 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-09 23:14:11 -04:00
|
|
|
|
2016-07-12 23:34:59 -04:00
|
|
|
if c.Font == nil {
|
|
|
|
defaultFont, err := GetDefaultFont()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.defaultFont = defaultFont
|
2016-07-07 20:14:25 -04:00
|
|
|
}
|
2016-07-08 20:57:14 -04:00
|
|
|
r.SetDPI(c.GetDPI(DefaultDPI))
|
2016-07-07 17:44:03 -04:00
|
|
|
|
2016-07-10 19:26:18 -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()
|
2017-03-06 02:20:58 -05:00
|
|
|
|
2016-07-10 19:26:18 -04:00
|
|
|
xr, yr, yra = c.setRangeDomains(canvasBox, xr, yr, yra)
|
2016-07-10 13:43:04 -04:00
|
|
|
|
2016-07-13 01:55:46 -04:00
|
|
|
err = c.checkRanges(xr, yr, yra)
|
|
|
|
if err != nil {
|
2017-02-22 20:15:13 -05:00
|
|
|
r.Save(w)
|
2016-07-13 01:55:46 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-10 19:26:18 -04:00
|
|
|
if c.hasAxes() {
|
|
|
|
xt, yt, yta = c.getAxesTicks(r, xr, yr, yra, xf, yf, yfa)
|
2016-09-11 12:13:57 -04:00
|
|
|
canvasBox = c.getAxesAdjustedCanvasBox(r, canvasBox, xr, yr, yra, xt, yt, yta)
|
2016-07-10 19:26:18 -04:00
|
|
|
xr, yr, yra = c.setRangeDomains(canvasBox, xr, yr, yra)
|
2016-07-17 02:45:28 -04:00
|
|
|
|
|
|
|
// do a second pass in case things haven't settled yet.
|
2016-07-10 19:26:18 -04:00
|
|
|
xt, yt, yta = c.getAxesTicks(r, xr, yr, yra, xf, yf, yfa)
|
2016-09-11 12:13:57 -04:00
|
|
|
canvasBox = c.getAxesAdjustedCanvasBox(r, canvasBox, xr, yr, yra, xt, yt, yta)
|
2016-07-17 02:45:28 -04:00
|
|
|
xr, yr, yra = c.setRangeDomains(canvasBox, xr, yr, yra)
|
2016-07-10 19:26:18 -04:00
|
|
|
}
|
2016-07-10 13:43:04 -04:00
|
|
|
|
2016-07-10 19:26:18 -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)
|
|
|
|
}
|
2016-07-07 22:11:30 -04:00
|
|
|
|
|
|
|
c.drawCanvas(r, canvasBox)
|
2016-07-18 14:53:29 -04:00
|
|
|
c.drawAxes(r, canvasBox, xr, yr, yra, xt, yt, yta)
|
2016-07-07 20:50:16 -04:00
|
|
|
for index, series := range c.Series {
|
2016-07-10 19:26:18 -04:00
|
|
|
c.drawSeries(r, canvasBox, xr, yr, yra, series, index)
|
2016-07-06 21:54:00 -04:00
|
|
|
}
|
2016-07-18 14:53:29 -04:00
|
|
|
|
2016-07-07 20:14:25 -04:00
|
|
|
c.drawTitle(r)
|
2016-07-12 19:47:52 -04:00
|
|
|
|
2016-07-12 22:14:14 -04:00
|
|
|
for _, a := range c.Elements {
|
2016-07-14 21:29:06 -04:00
|
|
|
a(r, canvasBox, c.styleDefaultsElements())
|
2016-07-12 22:14:14 -04:00
|
|
|
}
|
|
|
|
|
2016-07-06 21:54:00 -04:00
|
|
|
return r.Save(w)
|
|
|
|
}
|
|
|
|
|
2017-03-02 17:26:21 -05:00
|
|
|
func (c Chart) checkHasVisibleSeries() error {
|
|
|
|
hasVisibleSeries := false
|
2017-03-05 19:54:40 -05:00
|
|
|
var style Style
|
2017-03-02 17:26:21 -05:00
|
|
|
for _, s := range c.Series {
|
2017-03-05 19:54:40 -05:00
|
|
|
style = s.GetStyle()
|
|
|
|
hasVisibleSeries = hasVisibleSeries || (style.IsZero() || style.Show)
|
2017-03-02 17:26:21 -05:00
|
|
|
}
|
|
|
|
if !hasVisibleSeries {
|
|
|
|
return fmt.Errorf("must have (1) visible series; make sure if you set a style, you set .Show = true")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-03 14:26:53 -05:00
|
|
|
func (c Chart) validateSeries() error {
|
|
|
|
var err error
|
|
|
|
for _, s := range c.Series {
|
|
|
|
err = s.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-10 04:11:47 -04:00
|
|
|
func (c Chart) getRanges() (xrange, yrange, yrangeAlt Range) {
|
2016-08-05 19:55:55 -04:00
|
|
|
var minx, maxx float64 = math.MaxFloat64, -math.MaxFloat64
|
|
|
|
var miny, maxy float64 = math.MaxFloat64, -math.MaxFloat64
|
|
|
|
var minya, maxya float64 = math.MaxFloat64, -math.MaxFloat64
|
2016-07-10 04:11:47 -04:00
|
|
|
|
2016-07-21 17:11:27 -04:00
|
|
|
seriesMappedToSecondaryAxis := false
|
2016-07-17 16:06:07 -04:00
|
|
|
|
2016-07-16 23:53:46 -04:00
|
|
|
// note: a possible future optimization is to not scan the series values if
|
|
|
|
// all axis are represented by either custom ticks or custom ranges.
|
2016-07-07 17:44:03 -04:00
|
|
|
for _, s := range c.Series {
|
2016-07-12 23:34:59 -04:00
|
|
|
if s.GetStyle().IsZero() || s.GetStyle().Show {
|
|
|
|
seriesAxis := s.GetYAxis()
|
2016-07-15 12:02:50 -04:00
|
|
|
if bvp, isBoundedValueProvider := s.(BoundedValueProvider); isBoundedValueProvider {
|
|
|
|
seriesLength := bvp.Len()
|
|
|
|
for index := 0; index < seriesLength; index++ {
|
|
|
|
vx, vy1, vy2 := bvp.GetBoundedValue(index)
|
|
|
|
|
|
|
|
minx = math.Min(minx, vx)
|
|
|
|
maxx = math.Max(maxx, vx)
|
|
|
|
|
|
|
|
if seriesAxis == YAxisPrimary {
|
|
|
|
miny = math.Min(miny, vy1)
|
|
|
|
miny = math.Min(miny, vy2)
|
|
|
|
maxy = math.Max(maxy, vy1)
|
|
|
|
maxy = math.Max(maxy, vy2)
|
|
|
|
} else if seriesAxis == YAxisSecondary {
|
|
|
|
minya = math.Min(minya, vy1)
|
|
|
|
minya = math.Min(minya, vy2)
|
|
|
|
maxya = math.Max(maxya, vy1)
|
|
|
|
maxya = math.Max(maxya, vy2)
|
2016-07-21 17:11:27 -04:00
|
|
|
seriesMappedToSecondaryAxis = true
|
2016-07-15 12:02:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if vp, isValueProvider := s.(ValueProvider); isValueProvider {
|
2016-07-12 23:34:59 -04:00
|
|
|
seriesLength := vp.Len()
|
|
|
|
for index := 0; index < seriesLength; index++ {
|
|
|
|
vx, vy := vp.GetValue(index)
|
|
|
|
|
|
|
|
minx = math.Min(minx, vx)
|
|
|
|
maxx = math.Max(maxx, vx)
|
|
|
|
|
|
|
|
if seriesAxis == YAxisPrimary {
|
|
|
|
miny = math.Min(miny, vy)
|
|
|
|
maxy = math.Max(maxy, vy)
|
|
|
|
} else if seriesAxis == YAxisSecondary {
|
|
|
|
minya = math.Min(minya, vy)
|
|
|
|
maxya = math.Max(maxya, vy)
|
2016-07-21 17:11:27 -04:00
|
|
|
seriesMappedToSecondaryAxis = true
|
2016-07-12 23:34:59 -04:00
|
|
|
}
|
2016-07-07 17:44:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-23 01:43:27 -04:00
|
|
|
if c.XAxis.Range == nil {
|
2016-07-21 17:11:27 -04:00
|
|
|
xrange = &ContinuousRange{}
|
2016-07-23 01:43:27 -04:00
|
|
|
} else {
|
|
|
|
xrange = c.XAxis.Range
|
2016-07-21 17:11:27 -04:00
|
|
|
}
|
|
|
|
|
2016-07-23 01:43:27 -04:00
|
|
|
if c.YAxis.Range == nil {
|
2016-07-21 17:11:27 -04:00
|
|
|
yrange = &ContinuousRange{}
|
2016-07-23 01:43:27 -04:00
|
|
|
} else {
|
|
|
|
yrange = c.YAxis.Range
|
2016-07-21 17:11:27 -04:00
|
|
|
}
|
|
|
|
|
2016-07-23 01:43:27 -04:00
|
|
|
if c.YAxisSecondary.Range == nil {
|
2016-07-21 17:11:27 -04:00
|
|
|
yrangeAlt = &ContinuousRange{}
|
2016-07-23 01:43:27 -04:00
|
|
|
} else {
|
|
|
|
yrangeAlt = c.YAxisSecondary.Range
|
2016-07-21 17:11:27 -04:00
|
|
|
}
|
|
|
|
|
2016-07-16 23:53:46 -04:00
|
|
|
if len(c.XAxis.Ticks) > 0 {
|
2016-08-05 19:55:55 -04:00
|
|
|
tickMin, tickMax := math.MaxFloat64, -math.MaxFloat64
|
2016-07-16 23:53:46 -04:00
|
|
|
for _, t := range c.XAxis.Ticks {
|
|
|
|
tickMin = math.Min(tickMin, t.Value)
|
|
|
|
tickMax = math.Max(tickMax, t.Value)
|
|
|
|
}
|
2016-07-21 17:11:27 -04:00
|
|
|
xrange.SetMin(tickMin)
|
|
|
|
xrange.SetMax(tickMax)
|
2016-07-23 01:43:27 -04:00
|
|
|
} else if xrange.IsZero() {
|
2016-07-21 17:11:27 -04:00
|
|
|
xrange.SetMin(minx)
|
|
|
|
xrange.SetMax(maxx)
|
2016-07-10 04:11:47 -04:00
|
|
|
}
|
|
|
|
|
2016-07-16 23:53:46 -04:00
|
|
|
if len(c.YAxis.Ticks) > 0 {
|
2016-08-05 19:55:55 -04:00
|
|
|
tickMin, tickMax := math.MaxFloat64, -math.MaxFloat64
|
2016-07-16 23:53:46 -04:00
|
|
|
for _, t := range c.YAxis.Ticks {
|
|
|
|
tickMin = math.Min(tickMin, t.Value)
|
|
|
|
tickMax = math.Max(tickMax, t.Value)
|
|
|
|
}
|
2016-07-21 17:11:27 -04:00
|
|
|
yrange.SetMin(tickMin)
|
|
|
|
yrange.SetMax(tickMax)
|
2016-07-23 01:43:27 -04:00
|
|
|
} else if yrange.IsZero() {
|
2016-07-21 17:11:27 -04:00
|
|
|
yrange.SetMin(miny)
|
|
|
|
yrange.SetMax(maxy)
|
|
|
|
|
2017-03-06 02:20:58 -05:00
|
|
|
// only round if we're showing the axis
|
|
|
|
if c.YAxis.Style.Show {
|
|
|
|
delta := yrange.GetDelta()
|
|
|
|
roundTo := Math.GetRoundToForDelta(delta)
|
|
|
|
rmin, rmax := Math.RoundDown(yrange.GetMin(), roundTo), Math.RoundUp(yrange.GetMax(), roundTo)
|
|
|
|
|
|
|
|
yrange.SetMin(rmin)
|
|
|
|
yrange.SetMax(rmax)
|
|
|
|
}
|
2016-07-07 17:44:03 -04:00
|
|
|
}
|
2016-07-10 14:19:56 -04:00
|
|
|
|
2016-07-16 23:53:46 -04:00
|
|
|
if len(c.YAxisSecondary.Ticks) > 0 {
|
2016-08-05 19:55:55 -04:00
|
|
|
tickMin, tickMax := math.MaxFloat64, -math.MaxFloat64
|
2016-07-16 23:53:46 -04:00
|
|
|
for _, t := range c.YAxis.Ticks {
|
|
|
|
tickMin = math.Min(tickMin, t.Value)
|
|
|
|
tickMax = math.Max(tickMax, t.Value)
|
|
|
|
}
|
2016-07-21 17:11:27 -04:00
|
|
|
yrangeAlt.SetMin(tickMin)
|
|
|
|
yrangeAlt.SetMax(tickMax)
|
2016-07-23 01:43:27 -04:00
|
|
|
} else if seriesMappedToSecondaryAxis && yrangeAlt.IsZero() {
|
2016-07-21 17:11:27 -04:00
|
|
|
yrangeAlt.SetMin(minya)
|
|
|
|
yrangeAlt.SetMax(maxya)
|
|
|
|
|
2017-03-06 02:21:39 -05:00
|
|
|
if c.YAxisSecondary.Style.Show {
|
|
|
|
delta := yrangeAlt.GetDelta()
|
|
|
|
roundTo := Math.GetRoundToForDelta(delta)
|
|
|
|
rmin, rmax := Math.RoundDown(yrangeAlt.GetMin(), roundTo), Math.RoundUp(yrangeAlt.GetMax(), roundTo)
|
|
|
|
yrangeAlt.SetMin(rmin)
|
|
|
|
yrangeAlt.SetMax(rmax)
|
|
|
|
}
|
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-13 01:55:46 -04:00
|
|
|
func (c Chart) checkRanges(xr, yr, yra Range) error {
|
2017-03-02 17:26:21 -05:00
|
|
|
xDelta := xr.GetDelta()
|
|
|
|
if math.IsInf(xDelta, 0) {
|
|
|
|
return errors.New("infinite x-range delta")
|
|
|
|
}
|
|
|
|
if math.IsNaN(xDelta) {
|
|
|
|
return errors.New("nan x-range delta")
|
|
|
|
}
|
2017-03-02 17:39:32 -05:00
|
|
|
if xDelta == 0 {
|
|
|
|
return errors.New("zero x-range delta; there needs to be at least (2) values")
|
|
|
|
}
|
2017-03-02 17:26:21 -05:00
|
|
|
|
|
|
|
yDelta := yr.GetDelta()
|
|
|
|
if math.IsInf(yDelta, 0) {
|
|
|
|
return errors.New("infinite y-range delta")
|
2016-07-13 01:55:46 -04:00
|
|
|
}
|
2017-03-02 17:26:21 -05:00
|
|
|
if math.IsNaN(yDelta) {
|
|
|
|
return errors.New("nan y-range delta")
|
2016-07-13 01:55:46 -04:00
|
|
|
}
|
2017-03-06 02:28:35 -05:00
|
|
|
if yDelta == 0 {
|
|
|
|
return errors.New("zero y-range delta")
|
|
|
|
}
|
|
|
|
|
2016-07-13 01:55:46 -04:00
|
|
|
if c.hasSecondarySeries() {
|
2017-03-02 17:26:21 -05:00
|
|
|
yraDelta := yra.GetDelta()
|
|
|
|
if math.IsInf(yraDelta, 0) {
|
|
|
|
return errors.New("infinite secondary y-range delta")
|
|
|
|
}
|
|
|
|
if math.IsNaN(yraDelta) {
|
|
|
|
return errors.New("nan secondary y-range delta")
|
2016-07-13 01:55:46 -04:00
|
|
|
}
|
2017-03-06 02:28:35 -05:00
|
|
|
if yraDelta == 0 {
|
|
|
|
return errors.New("zero secondary y-range delta")
|
|
|
|
}
|
2016-07-13 01:55:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-10 13:43:04 -04:00
|
|
|
func (c Chart) getDefaultCanvasBox() Box {
|
2016-07-17 15:52:26 -04:00
|
|
|
return c.Box()
|
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
|
|
|
|
}
|
|
|
|
|
2016-07-10 19:26:18 -04:00
|
|
|
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 {
|
2016-08-05 19:55:55 -04:00
|
|
|
xticks = c.XAxis.GetTicks(r, xr, c.styleDefaultsAxes(), xf)
|
2016-07-10 13:43:04 -04:00
|
|
|
}
|
|
|
|
if c.YAxis.Style.Show {
|
2016-08-05 19:55:55 -04:00
|
|
|
yticks = c.YAxis.GetTicks(r, yr, c.styleDefaultsAxes(), yf)
|
2016-07-10 13:43:04 -04:00
|
|
|
}
|
|
|
|
if c.YAxisSecondary.Style.Show {
|
2016-08-05 19:55:55 -04:00
|
|
|
yticksAlt = c.YAxisSecondary.GetTicks(r, yar, c.styleDefaultsAxes(), yfa)
|
2016-07-10 13:43:04 -04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-11 12:13:57 -04:00
|
|
|
func (c Chart) getAxesAdjustedCanvasBox(r Renderer, canvasBox Box, xr, yr, yra Range, xticks, yticks, yticksAlt []Tick) Box {
|
2016-07-12 19:47:52 -04:00
|
|
|
axesOuterBox := canvasBox.Clone()
|
2016-07-10 13:43:04 -04:00
|
|
|
if c.XAxis.Style.Show {
|
2016-08-05 19:55:55 -04:00
|
|
|
axesBounds := c.XAxis.Measure(r, canvasBox, xr, c.styleDefaultsAxes(), xticks)
|
2016-07-12 19:47:52 -04:00
|
|
|
axesOuterBox = axesOuterBox.Grow(axesBounds)
|
2016-07-10 13:43:04 -04:00
|
|
|
}
|
|
|
|
if c.YAxis.Style.Show {
|
2016-08-05 19:55:55 -04:00
|
|
|
axesBounds := c.YAxis.Measure(r, canvasBox, yr, c.styleDefaultsAxes(), yticks)
|
2016-07-12 19:47:52 -04:00
|
|
|
axesOuterBox = axesOuterBox.Grow(axesBounds)
|
2016-07-10 13:43:04 -04:00
|
|
|
}
|
|
|
|
if c.YAxisSecondary.Style.Show {
|
2016-08-05 19:55:55 -04:00
|
|
|
axesBounds := c.YAxisSecondary.Measure(r, canvasBox, yra, c.styleDefaultsAxes(), yticksAlt)
|
2016-07-12 19:47:52 -04:00
|
|
|
axesOuterBox = axesOuterBox.Grow(axesBounds)
|
2016-07-11 21:48:51 -04:00
|
|
|
}
|
2016-07-11 03:12:14 -04:00
|
|
|
|
2016-07-17 15:52:26 -04:00
|
|
|
return canvasBox.OuterConstrain(c.Box(), axesOuterBox)
|
2016-07-10 04:11:47 -04:00
|
|
|
}
|
|
|
|
|
2016-07-21 17:11:27 -04:00
|
|
|
func (c Chart) setRangeDomains(canvasBox Box, xr, yr, yra Range) (Range, Range, Range) {
|
|
|
|
xr.SetDomain(canvasBox.Width())
|
|
|
|
yr.SetDomain(canvasBox.Height())
|
|
|
|
yra.SetDomain(canvasBox.Height())
|
|
|
|
return xr, yr, yra
|
2016-07-07 17:44:03 -04:00
|
|
|
}
|
|
|
|
|
2016-07-10 19:26:18 -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 {
|
2016-07-12 23:34:59 -04:00
|
|
|
if as.Style.IsZero() || as.Style.Show {
|
2016-07-10 19:26:18 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-10 20:19:44 -04:00
|
|
|
return false
|
2016-07-10 19:26:18 -04:00
|
|
|
}
|
|
|
|
|
2016-07-13 01:55:46 -04:00
|
|
|
func (c Chart) hasSecondarySeries() bool {
|
|
|
|
for _, s := range c.Series {
|
|
|
|
if s.GetYAxis() == YAxisSecondary {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-07-10 19:26:18 -04:00
|
|
|
func (c Chart) getAnnotationAdjustedCanvasBox(r Renderer, canvasBox Box, xr, yr, yra Range, xf, yf, yfa ValueFormatter) Box {
|
2016-07-12 19:47:52 -04:00
|
|
|
annotationSeriesBox := canvasBox.Clone()
|
2016-07-10 19:26:18 -04:00
|
|
|
for seriesIndex, s := range c.Series {
|
2016-07-10 20:19:44 -04:00
|
|
|
if as, isAnnotationSeries := s.(AnnotationSeries); isAnnotationSeries {
|
2016-07-12 23:34:59 -04:00
|
|
|
if as.Style.IsZero() || as.Style.Show {
|
|
|
|
style := c.styleDefaultsSeries(seriesIndex)
|
2016-07-10 19:26:18 -04:00
|
|
|
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 19:26:18 -04:00
|
|
|
}
|
2016-07-10 20:19:44 -04:00
|
|
|
|
2016-07-12 19:47:52 -04:00
|
|
|
annotationSeriesBox = annotationSeriesBox.Grow(annotationBounds)
|
2016-07-10 19:26:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 21:29:06 -04:00
|
|
|
return canvasBox.OuterConstrain(c.Box(), annotationSeriesBox)
|
2016-07-10 19:26:18 -04:00
|
|
|
}
|
|
|
|
|
2016-07-17 01:56:12 -04:00
|
|
|
func (c Chart) getBackgroundStyle() Style {
|
2016-07-21 17:11:27 -04:00
|
|
|
return c.Background.InheritFrom(c.styleDefaultsBackground())
|
2016-07-17 01:56:12 -04:00
|
|
|
}
|
|
|
|
|
2016-07-06 21:54:00 -04:00
|
|
|
func (c Chart) drawBackground(r Renderer) {
|
2016-07-29 19:36:29 -04:00
|
|
|
Draw.Box(r, Box{
|
2016-07-17 15:52:26 -04:00
|
|
|
Right: c.GetWidth(),
|
|
|
|
Bottom: c.GetHeight(),
|
|
|
|
}, c.getBackgroundStyle())
|
2016-07-17 01:56:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c Chart) getCanvasStyle() Style {
|
2016-07-21 17:11:27 -04:00
|
|
|
return c.Canvas.InheritFrom(c.styleDefaultsCanvas())
|
2016-07-06 21:54:00 -04:00
|
|
|
}
|
|
|
|
|
2016-07-07 22:11:30 -04:00
|
|
|
func (c Chart) drawCanvas(r Renderer, canvasBox Box) {
|
2016-07-29 19:36:29 -04:00
|
|
|
Draw.Box(r, canvasBox, c.getCanvasStyle())
|
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-08-05 19:55:55 -04:00
|
|
|
c.XAxis.Render(r, canvasBox, xrange, c.styleDefaultsAxes(), xticks)
|
2016-07-08 12:17:28 -04:00
|
|
|
}
|
2016-07-10 04:11:47 -04:00
|
|
|
if c.YAxis.Style.Show {
|
2016-08-05 19:55:55 -04:00
|
|
|
c.YAxis.Render(r, canvasBox, yrange, c.styleDefaultsAxes(), yticks)
|
2016-07-07 23:26:07 -04:00
|
|
|
}
|
2016-07-10 04:11:47 -04:00
|
|
|
if c.YAxisSecondary.Style.Show {
|
2016-08-05 19:55:55 -04:00
|
|
|
c.YAxisSecondary.Render(r, canvasBox, yrangeAlt, c.styleDefaultsAxes(), 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) {
|
2016-07-12 23:34:59 -04:00
|
|
|
if s.GetStyle().IsZero() || s.GetStyle().Show {
|
|
|
|
if s.GetYAxis() == YAxisPrimary {
|
|
|
|
s.Render(r, canvasBox, xrange, yrange, c.styleDefaultsSeries(seriesIndex))
|
|
|
|
} else if s.GetYAxis() == YAxisSecondary {
|
|
|
|
s.Render(r, canvasBox, xrange, yrangeAlt, c.styleDefaultsSeries(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-12 23:34:59 -04:00
|
|
|
r.SetFont(c.TitleStyle.GetFont(c.GetFont()))
|
2017-04-14 20:43:52 -04:00
|
|
|
r.SetFontColor(c.TitleStyle.GetFontColor(c.GetColorPalette().TextColor()))
|
2016-07-10 13:43:04 -04:00
|
|
|
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
|
|
|
|
2016-07-12 19:47:52 -04:00
|
|
|
textWidth := textBox.Width()
|
|
|
|
textHeight := textBox.Height()
|
2016-07-10 13:43:04 -04:00
|
|
|
|
2016-07-12 23:34:59 -04:00
|
|
|
titleX := (c.GetWidth() >> 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)
|
|
|
|
}
|
|
|
|
}
|
2016-07-12 19:47:52 -04:00
|
|
|
|
2016-07-14 21:29:06 -04:00
|
|
|
func (c Chart) styleDefaultsBackground() Style {
|
|
|
|
return Style{
|
2017-04-14 20:43:52 -04:00
|
|
|
FillColor: c.GetColorPalette().BackgroundColor(),
|
|
|
|
StrokeColor: c.GetColorPalette().BackgroundStrokeColor(),
|
2017-02-22 20:44:44 -05:00
|
|
|
StrokeWidth: DefaultBackgroundStrokeWidth,
|
2016-07-14 21:29:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Chart) styleDefaultsCanvas() Style {
|
|
|
|
return Style{
|
2017-04-14 20:43:52 -04:00
|
|
|
FillColor: c.GetColorPalette().CanvasColor(),
|
|
|
|
StrokeColor: c.GetColorPalette().CanvasStrokeColor(),
|
2017-02-22 20:44:44 -05:00
|
|
|
StrokeWidth: DefaultCanvasStrokeWidth,
|
2016-07-14 21:29:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 23:34:59 -04:00
|
|
|
func (c Chart) styleDefaultsSeries(seriesIndex int) Style {
|
2016-07-12 19:47:52 -04:00
|
|
|
return Style{
|
2017-04-14 20:43:52 -04:00
|
|
|
DotColor: c.GetColorPalette().GetSeriesColor(seriesIndex),
|
|
|
|
StrokeColor: c.GetColorPalette().GetSeriesColor(seriesIndex),
|
2017-02-22 20:47:08 -05:00
|
|
|
StrokeWidth: DefaultSeriesLineWidth,
|
2016-07-12 23:34:59 -04:00
|
|
|
Font: c.GetFont(),
|
2016-07-12 19:47:52 -04:00
|
|
|
FontSize: DefaultFontSize,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 19:55:55 -04:00
|
|
|
func (c Chart) styleDefaultsAxes() Style {
|
2016-07-12 23:34:59 -04:00
|
|
|
return Style{
|
|
|
|
Font: c.GetFont(),
|
2017-04-14 20:43:52 -04:00
|
|
|
FontColor: c.GetColorPalette().TextColor(),
|
2016-07-12 23:34:59 -04:00
|
|
|
FontSize: DefaultAxisFontSize,
|
2017-04-14 20:43:52 -04:00
|
|
|
StrokeColor: c.GetColorPalette().AxisStrokeColor(),
|
2016-07-12 23:34:59 -04:00
|
|
|
StrokeWidth: DefaultAxisLineWidth,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 21:29:06 -04:00
|
|
|
func (c Chart) styleDefaultsElements() Style {
|
|
|
|
return Style{
|
|
|
|
Font: c.GetFont(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-14 20:43:52 -04:00
|
|
|
// GetColorPalette returns the color palette for the chart.
|
|
|
|
func (c Chart) GetColorPalette() ColorPalette {
|
|
|
|
if c.ColorPalette != nil {
|
|
|
|
return c.ColorPalette
|
|
|
|
}
|
|
|
|
return DefaultColorPalette
|
|
|
|
}
|
|
|
|
|
2016-07-14 21:29:06 -04:00
|
|
|
// Box returns the chart bounds as a box.
|
|
|
|
func (c Chart) Box() Box {
|
2016-07-17 15:52:26 -04:00
|
|
|
dpr := c.Background.Padding.GetRight(DefaultBackgroundPadding.Right)
|
|
|
|
dpb := c.Background.Padding.GetBottom(DefaultBackgroundPadding.Bottom)
|
|
|
|
|
|
|
|
return Box{
|
|
|
|
Top: c.Background.Padding.GetTop(DefaultBackgroundPadding.Top),
|
|
|
|
Left: c.Background.Padding.GetLeft(DefaultBackgroundPadding.Left),
|
|
|
|
Right: c.GetWidth() - dpr,
|
|
|
|
Bottom: c.GetHeight() - dpb,
|
|
|
|
}
|
2016-07-12 19:47:52 -04:00
|
|
|
}
|