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"
|
|
|
|
|
|
|
|
"github.com/golang/freetype/truetype"
|
2016-07-10 19:43:04 +02:00
|
|
|
"github.com/wcharczuk/go-chart/drawing"
|
2016-07-07 03:54:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2016-07-08 02:14:25 +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
|
2016-07-07 03:54:00 +02:00
|
|
|
|
2016-07-10 10:11:47 +02:00
|
|
|
XAxis XAxis
|
|
|
|
YAxis YAxis
|
|
|
|
YAxisSecondary YAxis
|
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 19:43:04 +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-08 02:14:25 +02:00
|
|
|
}
|
2016-07-10 19:43:04 +02:00
|
|
|
c.Font = font
|
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
|
|
|
|
2016-07-11 01:26:18 +02:00
|
|
|
c.drawBackground(r)
|
|
|
|
|
|
|
|
var xt, yt, yta []Tick
|
|
|
|
xr, yr, yra := c.getRanges()
|
2016-07-10 19:43:04 +02:00
|
|
|
canvasBox := c.getDefaultCanvasBox()
|
|
|
|
xf, yf, yfa := c.getValueFormatters()
|
2016-07-11 01:26:18 +02:00
|
|
|
xr, yr, yra = c.setRangeDomains(canvasBox, xr, yr, yra)
|
2016-07-10 19:43:04 +02:00
|
|
|
|
2016-07-11 01:26:18 +02:00
|
|
|
if c.hasAxes() {
|
|
|
|
xt, yt, yta = c.getAxesTicks(r, xr, yr, yra, xf, yf, yfa)
|
|
|
|
canvasBox = c.getAxisAdjustedCanvasBox(r, canvasBox, 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 19:43:04 +02:00
|
|
|
|
2016-07-11 01:26:18 +02: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-08 04:11:30 +02:00
|
|
|
|
|
|
|
c.drawCanvas(r, canvasBox)
|
2016-07-11 01:26:18 +02:00
|
|
|
c.drawAxes(r, canvasBox, xr, yr, yra, xt, yt, yta)
|
|
|
|
|
2016-07-08 02:50:16 +02:00
|
|
|
for index, series := range c.Series {
|
2016-07-11 01:26:18 +02:00
|
|
|
c.drawSeries(r, canvasBox, xr, yr, yra, series, index)
|
2016-07-07 03:54:00 +02:00
|
|
|
}
|
2016-07-08 02:14:25 +02:00
|
|
|
c.drawTitle(r)
|
2016-07-07 03:54:00 +02:00
|
|
|
return r.Save(w)
|
|
|
|
}
|
|
|
|
|
2016-07-10 10:11:47 +02:00
|
|
|
func (c Chart) getRanges() (xrange, yrange, yrangeAlt Range) {
|
2016-07-07 23:44:03 +02:00
|
|
|
//iterate over each series, pull out the min/max for x,y
|
|
|
|
var didSetFirstValues bool
|
2016-07-10 10:11:47 +02:00
|
|
|
|
|
|
|
var globalMinX, globalMaxX float64
|
|
|
|
var globalMinY, globalMaxY float64
|
|
|
|
var globalMinYA, globalMaxYA float64
|
|
|
|
|
2016-07-07 23:44:03 +02:00
|
|
|
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 globalMaxX < vx {
|
|
|
|
globalMaxX = vx
|
|
|
|
}
|
2016-07-10 10:11:47 +02:00
|
|
|
if s.GetYAxis() == YAxisPrimary {
|
|
|
|
if globalMinY > vy {
|
|
|
|
globalMinY = vy
|
|
|
|
}
|
|
|
|
if globalMaxY < vy {
|
|
|
|
globalMaxY = vy
|
|
|
|
}
|
|
|
|
} else if s.GetYAxis() == YAxisSecondary {
|
|
|
|
if globalMinYA > vy {
|
|
|
|
globalMinYA = vy
|
|
|
|
}
|
|
|
|
if globalMaxYA < vy {
|
|
|
|
globalMaxYA = vy
|
|
|
|
}
|
2016-07-10 05:14:11 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
globalMinX, globalMaxX = vx, vx
|
2016-07-10 10:11:47 +02:00
|
|
|
if s.GetYAxis() == YAxisPrimary {
|
|
|
|
globalMinY, globalMaxY = vy, vy
|
|
|
|
} else if s.GetYAxis() == YAxisSecondary {
|
|
|
|
globalMinYA, globalMaxYA = vy, vy
|
|
|
|
}
|
2016-07-10 05:14:11 +02:00
|
|
|
didSetFirstValues = true
|
2016-07-07 23:44:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-10 10:11:47 +02:00
|
|
|
if !c.XAxis.Range.IsZero() {
|
|
|
|
xrange.Min = c.XAxis.Range.Min
|
|
|
|
xrange.Max = c.XAxis.Range.Max
|
|
|
|
} else {
|
2016-07-07 23:44:03 +02:00
|
|
|
xrange.Min = globalMinX
|
|
|
|
xrange.Max = globalMaxX
|
2016-07-10 10:11:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !c.YAxis.Range.IsZero() {
|
|
|
|
yrange.Min = c.YAxis.Range.Min
|
|
|
|
yrange.Max = c.YAxis.Range.Max
|
2016-07-07 23:44:03 +02:00
|
|
|
} else {
|
2016-07-10 10:11:47 +02:00
|
|
|
yrange.Min = globalMinY
|
|
|
|
yrange.Max = globalMaxY
|
2016-07-07 23:44:03 +02:00
|
|
|
}
|
2016-07-10 20:19:56 +02:00
|
|
|
|
|
|
|
if !c.YAxisSecondary.Range.IsZero() {
|
|
|
|
yrangeAlt.Min = c.YAxisSecondary.Range.Min
|
|
|
|
yrangeAlt.Max = c.YAxisSecondary.Range.Max
|
|
|
|
} else {
|
|
|
|
yrangeAlt.Min = globalMinYA
|
|
|
|
yrangeAlt.Max = globalMaxYA
|
|
|
|
}
|
2016-07-10 10:11:47 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-10 19:43:04 +02:00
|
|
|
func (c Chart) getDefaultCanvasBox() Box {
|
2016-07-10 10:11:47 +02:00
|
|
|
dpl := c.Background.Padding.GetLeft(DefaultBackgroundPadding.Left)
|
|
|
|
dpr := c.Background.Padding.GetRight(DefaultBackgroundPadding.Right)
|
|
|
|
dpb := c.Background.Padding.GetBottom(DefaultBackgroundPadding.Bottom)
|
|
|
|
|
|
|
|
cb := Box{
|
|
|
|
Top: c.Background.Padding.GetTop(DefaultBackgroundPadding.Top),
|
|
|
|
Left: dpl,
|
|
|
|
Right: c.Width - dpr,
|
|
|
|
Bottom: c.Height - dpb,
|
2016-07-07 23:44:03 +02:00
|
|
|
}
|
2016-07-10 10:11:47 +02:00
|
|
|
cb.Height = cb.Bottom - cb.Top
|
|
|
|
cb.Width = cb.Right - cb.Left
|
|
|
|
return cb
|
|
|
|
}
|
|
|
|
|
2016-07-10 19:43:04 +02: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-11 01:26:18 +02:00
|
|
|
func (c Chart) hasAxes() bool {
|
|
|
|
return c.XAxis.Style.Show || c.YAxis.Style.Show || c.YAxisSecondary.Style.Show
|
|
|
|
}
|
|
|
|
|
2016-07-10 19:43:04 +02: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 20:19:56 +02:00
|
|
|
yticksAlt = c.YAxisSecondary.GetTicks(r, yar, yf)
|
2016-07-10 19:43:04 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-11 01:26:18 +02:00
|
|
|
func (c Chart) getAxisAdjustedCanvasBox(r Renderer, defaults Box, xticks, yticks, yticksAlt []Tick) Box {
|
2016-07-10 19:43:04 +02:00
|
|
|
canvasBox := Box{}
|
|
|
|
|
|
|
|
var dpl, dpr, dpb int
|
|
|
|
if c.XAxis.Style.Show {
|
|
|
|
dpb = c.getXAxisHeight(r, xticks)
|
|
|
|
}
|
|
|
|
if c.YAxis.Style.Show {
|
|
|
|
dpr = c.getYAxisWidth(r, yticks)
|
|
|
|
}
|
|
|
|
if c.YAxisSecondary.Style.Show {
|
|
|
|
dpl = c.getYAxisSecondaryWidth(r, yticksAlt)
|
|
|
|
}
|
|
|
|
|
|
|
|
canvasBox.Top = defaults.Top
|
|
|
|
if dpl != 0 {
|
|
|
|
canvasBox.Left = c.Canvas.Padding.GetLeft(dpl)
|
|
|
|
} else {
|
|
|
|
canvasBox.Left = defaults.Left
|
|
|
|
}
|
|
|
|
if dpr != 0 {
|
|
|
|
canvasBox.Right = c.Width - c.Canvas.Padding.GetRight(dpr)
|
|
|
|
} else {
|
|
|
|
canvasBox.Right = defaults.Right
|
|
|
|
}
|
|
|
|
if dpb != 0 {
|
|
|
|
canvasBox.Bottom = c.Height - c.Canvas.Padding.GetBottom(dpb)
|
|
|
|
} else {
|
|
|
|
canvasBox.Bottom = defaults.Bottom
|
|
|
|
}
|
|
|
|
|
|
|
|
canvasBox.Width = canvasBox.Right - canvasBox.Left
|
|
|
|
canvasBox.Height = canvasBox.Bottom - canvasBox.Top
|
|
|
|
return canvasBox
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Chart) getXAxisHeight(r Renderer, ticks []Tick) int {
|
|
|
|
r.SetFontSize(c.XAxis.Style.GetFontSize(DefaultFontSize))
|
|
|
|
r.SetFont(c.XAxis.Style.GetFont(c.Font))
|
|
|
|
var tl int
|
2016-07-10 10:11:47 +02:00
|
|
|
for _, t := range ticks {
|
2016-07-10 19:43:04 +02:00
|
|
|
_, lh := r.MeasureText(t.Label)
|
|
|
|
if lh > tl {
|
|
|
|
tl = lh
|
2016-07-10 10:11:47 +02:00
|
|
|
}
|
2016-07-08 09:16:02 +02:00
|
|
|
}
|
2016-07-10 19:43:04 +02:00
|
|
|
return tl + DefaultXAxisMargin
|
2016-07-10 10:11:47 +02:00
|
|
|
}
|
|
|
|
|
2016-07-10 19:43:04 +02:00
|
|
|
func (c Chart) getYAxisWidth(r Renderer, ticks []Tick) int {
|
2016-07-10 10:11:47 +02:00
|
|
|
var ll string
|
|
|
|
for _, t := range ticks {
|
|
|
|
if len(t.Label) > len(ll) {
|
|
|
|
ll = t.Label
|
|
|
|
}
|
2016-07-08 18:17:28 +02:00
|
|
|
}
|
2016-07-10 10:11:47 +02:00
|
|
|
r.SetFontSize(c.YAxis.Style.GetFontSize(DefaultFontSize))
|
|
|
|
r.SetFont(c.YAxis.Style.GetFont(c.Font))
|
|
|
|
tw, _ := r.MeasureText(ll)
|
|
|
|
return tw + DefaultYAxisMargin
|
|
|
|
}
|
2016-07-07 23:44:03 +02:00
|
|
|
|
2016-07-10 19:43:04 +02:00
|
|
|
func (c Chart) getYAxisSecondaryWidth(r Renderer, ticks []Tick) int {
|
|
|
|
var ll string
|
2016-07-10 10:11:47 +02:00
|
|
|
for _, t := range ticks {
|
2016-07-10 19:43:04 +02:00
|
|
|
if len(t.Label) > len(ll) {
|
|
|
|
ll = t.Label
|
2016-07-10 10:11:47 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-10 19:43:04 +02:00
|
|
|
|
|
|
|
r.SetFontSize(c.YAxisSecondary.Style.GetFontSize(DefaultFontSize))
|
|
|
|
r.SetFont(c.YAxisSecondary.Style.GetFont(c.Font))
|
|
|
|
tw, _ := r.MeasureText(ll)
|
|
|
|
return tw + DefaultYAxisMargin
|
2016-07-10 10:11:47 +02:00
|
|
|
}
|
|
|
|
|
2016-07-10 19:43:04 +02:00
|
|
|
func (c Chart) setRangeDomains(canvasBox Box, xrange, yrange, yrangeAlt Range) (Range, Range, Range) {
|
2016-07-10 10:11:47 +02:00
|
|
|
xrange.Domain = canvasBox.Width
|
|
|
|
yrange.Domain = canvasBox.Height
|
|
|
|
yrangeAlt.Domain = canvasBox.Height
|
|
|
|
return xrange, yrange, yrangeAlt
|
2016-07-07 23:44:03 +02:00
|
|
|
}
|
|
|
|
|
2016-07-11 01:26:18 +02:00
|
|
|
func (c Chart) hasAnnotationSeries() bool {
|
|
|
|
for _, s := range c.Series {
|
|
|
|
if as, isAnnotationSeries:= s.(AnnotationSeries); isAnnotationSeries {
|
|
|
|
if as.Style.Show {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Chart) getAnnotationAdjustedCanvasBox(r Renderer, canvasBox Box, xr, yr, yra Range, xf, yf, yfa ValueFormatter) Box {
|
|
|
|
annotationMinX, annotationMaxX, annotationMinY, annotationMaxY := canvasBox.Right, canvasBox.Left, canvasBox.Bottom, canvasBox.Top
|
|
|
|
for seriesIndex, s := range c.Series {
|
|
|
|
if as, isAnnotationSeries:= s.(AnnotationSeries); isAnnotationSeries {
|
|
|
|
if as.Style.Show {
|
|
|
|
style := c.getSeriesStyleDefaults(seriesIndex)
|
|
|
|
var annotationBounds Box
|
|
|
|
if as.YAxis == YAxisPrimary {
|
|
|
|
annotationBounds = as.Measure(r, canvasBox, xr, yr, style)
|
|
|
|
} else if as.YAxis == YAxisSecondary {
|
|
|
|
annotationBounds = as.Measure(r, canvasBox, xr, yr, style)
|
|
|
|
}
|
|
|
|
|
|
|
|
if annotationMinY > annotationBounds.Top {
|
|
|
|
annotationMinY = annotationBounds.Top
|
|
|
|
}
|
|
|
|
|
|
|
|
if annotationMinX > annotationBounds.Left {
|
|
|
|
annotationMinX = annotationBounds.Left
|
|
|
|
}
|
|
|
|
|
|
|
|
if annotationMaxX < annotationBounds.Right {
|
|
|
|
annotationMaxX = annotationBounds.Right
|
|
|
|
}
|
|
|
|
|
|
|
|
if annotationMaxY < annotationBounds.Bottom {
|
|
|
|
annotationMaxY = annotationBounds.Bottom
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newBox := Box{
|
|
|
|
Top: canvasBox.Top,
|
|
|
|
Left: canvasBox.Left,
|
|
|
|
Right: canvasBox.Right,
|
|
|
|
Bottom: canvasBox.Bottom,
|
|
|
|
}
|
|
|
|
if annotationMinY < 0 {
|
|
|
|
// figure out how much top padding to add
|
|
|
|
delta := -1*annotationMinY
|
|
|
|
newBox.Top = canvasBox.Top+delta
|
|
|
|
}
|
|
|
|
|
|
|
|
if annotationMaxX > c.Width {
|
|
|
|
// figure out how much right padding to add
|
|
|
|
delta := annotationMaxX - c.Width
|
|
|
|
newBox.Right = canvasBox.Right - delta
|
|
|
|
}
|
|
|
|
|
|
|
|
if annotationMinX < 0 {
|
|
|
|
// figure out how much left padding to add
|
|
|
|
delta := -1*annotationMinX
|
|
|
|
newBox.Left = canvasBox.Left + delta
|
|
|
|
}
|
|
|
|
|
|
|
|
if annotationMaxY > c.Height {
|
|
|
|
//figure out how much bottom padding to add
|
|
|
|
delta := annotationMaxY - c.Height
|
|
|
|
newBox.Bottom = canvasBox.Bottom - delta
|
|
|
|
}
|
|
|
|
|
|
|
|
newBox.Height = newBox.Bottom - newBox.Top
|
|
|
|
newBox.Width = newBox.Right - newBox.Left
|
|
|
|
|
|
|
|
return newBox
|
|
|
|
}
|
|
|
|
|
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))
|
2016-07-08 04:11:30 +02:00
|
|
|
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()
|
2016-07-08 04:11:30 +02:00
|
|
|
r.FillStroke()
|
2016-07-07 03:54:00 +02:00
|
|
|
}
|
|
|
|
|
2016-07-08 04:11:30 +02:00
|
|
|
func (c Chart) drawCanvas(r Renderer, canvasBox Box) {
|
2016-07-07 23:44:03 +02:00
|
|
|
r.SetFillColor(c.Canvas.GetFillColor(DefaultCanvasColor))
|
2016-07-08 04:11:30 +02:00
|
|
|
r.SetStrokeColor(c.Canvas.GetStrokeColor(DefaultCanvasStrokColor))
|
2016-07-08 07:18:53 +02:00
|
|
|
r.SetStrokeWidth(c.Canvas.GetStrokeWidth(DefaultStrokeWidth))
|
2016-07-08 04:11:30 +02:00
|
|
|
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()
|
2016-07-08 04:11:30 +02:00
|
|
|
r.FillStroke()
|
2016-07-07 03:54:00 +02:00
|
|
|
}
|
|
|
|
|
2016-07-10 19:43:04 +02:00
|
|
|
func (c Chart) drawAxes(r Renderer, canvasBox Box, xrange, yrange, yrangeAlt Range, xticks, yticks, yticksAlt []Tick) {
|
2016-07-10 10:11:47 +02:00
|
|
|
if c.XAxis.Style.Show {
|
2016-07-10 19:43:04 +02:00
|
|
|
c.XAxis.Render(r, canvasBox, xrange, xticks)
|
2016-07-08 18:17:28 +02:00
|
|
|
}
|
2016-07-10 10:11:47 +02:00
|
|
|
if c.YAxis.Style.Show {
|
2016-07-10 19:43:04 +02:00
|
|
|
c.YAxis.Render(r, canvasBox, yrange, YAxisPrimary, yticks)
|
2016-07-08 05:26:07 +02:00
|
|
|
}
|
2016-07-10 10:11:47 +02:00
|
|
|
if c.YAxisSecondary.Style.Show {
|
2016-07-10 19:43:04 +02:00
|
|
|
c.YAxisSecondary.Render(r, canvasBox, yrangeAlt, YAxisSecondary, yticksAlt)
|
2016-07-08 05:26:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-10 19:43:04 +02:00
|
|
|
func (c Chart) getSeriesStyleDefaults(seriesIndex int) Style {
|
2016-07-10 10:11:47 +02:00
|
|
|
strokeColor := GetDefaultSeriesStrokeColor(seriesIndex)
|
|
|
|
return Style{
|
|
|
|
StrokeColor: strokeColor,
|
|
|
|
StrokeWidth: DefaultStrokeWidth,
|
|
|
|
FillColor: strokeColor.WithAlpha(100),
|
|
|
|
Font: c.Font,
|
|
|
|
FontSize: DefaultFontSize,
|
2016-07-08 05:26:07 +02:00
|
|
|
}
|
2016-07-08 02:14:25 +02:00
|
|
|
}
|
|
|
|
|
2016-07-10 10:11:47 +02:00
|
|
|
func (c Chart) drawSeries(r Renderer, canvasBox Box, xrange, yrange, yrangeAlt Range, s Series, seriesIndex int) {
|
|
|
|
if s.GetYAxis() == YAxisPrimary {
|
2016-07-10 19:43:04 +02:00
|
|
|
s.Render(r, canvasBox, xrange, yrange, c.getSeriesStyleDefaults(seriesIndex))
|
2016-07-10 10:11:47 +02:00
|
|
|
} else if s.GetYAxis() == YAxisSecondary {
|
2016-07-10 20:19:56 +02:00
|
|
|
s.Render(r, canvasBox, xrange, yrangeAlt, c.getSeriesStyleDefaults(seriesIndex))
|
2016-07-10 10:11:47 +02:00
|
|
|
}
|
2016-07-07 03:54:00 +02:00
|
|
|
}
|
|
|
|
|
2016-07-10 19:43:04 +02:00
|
|
|
func (c Chart) drawTitle(r Renderer) {
|
2016-07-07 23:44:03 +02:00
|
|
|
if len(c.Title) > 0 && c.TitleStyle.Show {
|
2016-07-10 19:43:04 +02:00
|
|
|
r.SetFont(c.TitleStyle.GetFont(c.Font))
|
|
|
|
r.SetFontColor(c.TitleStyle.GetFontColor(DefaultTextColor))
|
|
|
|
titleFontSize := c.TitleStyle.GetFontSize(DefaultTitleFontSize)
|
2016-07-07 23:44:03 +02:00
|
|
|
r.SetFontSize(titleFontSize)
|
2016-07-10 19:43:04 +02:00
|
|
|
|
|
|
|
textWidthPoints, textHeightPoints := r.MeasureText(c.Title)
|
|
|
|
|
|
|
|
textWidth := int(drawing.PointsToPixels(r.GetDPI(), float64(textWidthPoints)))
|
|
|
|
textHeight := int(drawing.PointsToPixels(r.GetDPI(), float64(textHeightPoints)))
|
|
|
|
|
2016-07-07 03:54:00 +02:00
|
|
|
titleX := (c.Width >> 1) - (textWidth >> 1)
|
2016-07-10 19:43:04 +02:00
|
|
|
titleY := c.TitleStyle.Padding.GetTop(DefaultTitleTop) + textHeight
|
|
|
|
|
2016-07-07 03:54:00 +02:00
|
|
|
r.Text(c.Title, titleX, titleY)
|
|
|
|
}
|
|
|
|
}
|