From 7bce09c8594a0bbabc76f0c165d12ca6cc4452b9 Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Thu, 14 Jul 2016 18:29:06 -0700 Subject: [PATCH] api tweaks. --- chart.go | 43 +++++++++++++++++++++++++++++-------------- renderable.go | 2 +- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/chart.go b/chart.go index c32696f..dd96c14 100644 --- a/chart.go +++ b/chart.go @@ -122,7 +122,7 @@ func (c Chart) Render(rp RendererProvider, w io.Writer) error { c.drawTitle(r) for _, a := range c.Elements { - a(r, canvasBox) + a(r, canvasBox, c.styleDefaultsElements()) } return r.Save(w) @@ -273,7 +273,7 @@ func (c Chart) getAxisAdjustedCanvasBox(r Renderer, canvasBox Box, xr, yr, yra R axesOuterBox = axesOuterBox.Grow(axesBounds) } - return canvasBox.OuterConstrain(c.asBox(), axesOuterBox) + return canvasBox.OuterConstrain(c.Box(), axesOuterBox) } func (c Chart) setRangeDomains(canvasBox Box, xr, yr, yra Range) (xr2, yr2, yra2 Range) { @@ -324,23 +324,15 @@ func (c Chart) getAnnotationAdjustedCanvasBox(r Renderer, canvasBox Box, xr, yr, } } - return canvasBox.OuterConstrain(c.asBox(), annotationSeriesBox) + return canvasBox.OuterConstrain(c.Box(), annotationSeriesBox) } func (c Chart) drawBackground(r Renderer) { - DrawBox(r, c.asBox(), c.Canvas.WithDefaultsFrom(Style{ - FillColor: DefaultBackgroundColor, - StrokeColor: DefaultBackgroundStrokeColor, - StrokeWidth: DefaultStrokeWidth, - })) + DrawBox(r, c.Box(), c.Canvas.WithDefaultsFrom(c.styleDefaultsBackground())) } func (c Chart) drawCanvas(r Renderer, canvasBox Box) { - DrawBox(r, canvasBox, c.Canvas.WithDefaultsFrom(Style{ - FillColor: DefaultCanvasColor, - StrokeColor: DefaultCanvasStrokeColor, - StrokeWidth: DefaultStrokeWidth, - })) + DrawBox(r, canvasBox, c.Canvas.WithDefaultsFrom(c.styleDefaultsCanvas())) } func (c Chart) drawAxes(r Renderer, canvasBox Box, xrange, yrange, yrangeAlt Range, xticks, yticks, yticksAlt []Tick) { @@ -384,6 +376,22 @@ func (c Chart) drawTitle(r Renderer) { } } +func (c Chart) styleDefaultsBackground() Style { + return Style{ + FillColor: DefaultBackgroundColor, + StrokeColor: DefaultBackgroundStrokeColor, + StrokeWidth: DefaultStrokeWidth, + } +} + +func (c Chart) styleDefaultsCanvas() Style { + return Style{ + FillColor: DefaultCanvasColor, + StrokeColor: DefaultCanvasStrokeColor, + StrokeWidth: DefaultStrokeWidth, + } +} + func (c Chart) styleDefaultsSeries(seriesIndex int) Style { strokeColor := GetDefaultSeriesStrokeColor(seriesIndex) return Style{ @@ -403,6 +411,13 @@ func (c Chart) styleDefaultsAxis() Style { } } -func (c Chart) asBox() Box { +func (c Chart) styleDefaultsElements() Style { + return Style{ + Font: c.GetFont(), + } +} + +// Box returns the chart bounds as a box. +func (c Chart) Box() Box { return Box{Right: c.GetWidth(), Bottom: c.GetHeight()} } diff --git a/renderable.go b/renderable.go index c1205ee..6efb3ad 100644 --- a/renderable.go +++ b/renderable.go @@ -1,4 +1,4 @@ package chart // Renderable is a function that can be called to render custom elements on the chart. -type Renderable func(r Renderer, canvasBox Box) +type Renderable func(r Renderer, canvasBox Box, defaults Style)