diff --git a/_examples/bar_chart/main.go b/_examples/bar_chart/main.go index 6afc886..6b8db61 100644 --- a/_examples/bar_chart/main.go +++ b/_examples/bar_chart/main.go @@ -11,6 +11,13 @@ import ( func drawChart(res http.ResponseWriter, req *http.Request) { sbc := chart.BarChart{ + Title: "Test Bar Chart", + TitleStyle: chart.StyleShow(), + Background: chart.Style{ + Padding: chart.Box{ + Top: 40, + }, + }, Height: 512, BarWidth: 60, XAxis: chart.Style{ diff --git a/_examples/bar_chart/output.png b/_examples/bar_chart/output.png index 59c6c89..321a341 100644 Binary files a/_examples/bar_chart/output.png and b/_examples/bar_chart/output.png differ diff --git a/bar_chart.go b/bar_chart.go index d6a8f7b..0c24d92 100644 --- a/bar_chart.go +++ b/bar_chart.go @@ -126,7 +126,7 @@ func (bc BarChart) Render(rp RendererProvider, w io.Writer) error { canvasBox = bc.getAdjustedCanvasBox(r, canvasBox, yr, yt) yr = bc.setRangeDomains(canvasBox, yr) } - + bc.drawCanvas(r, canvasBox) bc.drawBars(r, canvasBox, yr) bc.drawXAxis(r, canvasBox) bc.drawYAxis(r, canvasBox, yr, yt) @@ -139,6 +139,10 @@ func (bc BarChart) Render(rp RendererProvider, w io.Writer) error { return r.Save(w) } +func (bc BarChart) drawCanvas(r Renderer, canvasBox Box) { + Draw.Box(r, canvasBox, bc.getCanvasStyle()) +} + func (bc BarChart) getRanges() Range { var yrange Range if bc.YAxis.Range != nil && !bc.YAxis.Range.IsZero() { @@ -280,7 +284,32 @@ func (bc BarChart) drawYAxis(r Renderer, canvasBox Box, yr Range, ticks []Tick) func (bc BarChart) drawTitle(r Renderer) { if len(bc.Title) > 0 && bc.TitleStyle.Show { - Draw.TextWithin(r, bc.Title, bc.box(), bc.styleDefaultsTitle()) + r.SetFont(bc.TitleStyle.GetFont(bc.GetFont())) + r.SetFontColor(bc.TitleStyle.GetFontColor(bc.GetColorPalette().TextColor())) + titleFontSize := bc.TitleStyle.GetFontSize(bc.getTitleFontSize()) + r.SetFontSize(titleFontSize) + + textBox := r.MeasureText(bc.Title) + + textWidth := textBox.Width() + textHeight := textBox.Height() + + titleX := (bc.GetWidth() >> 1) - (textWidth >> 1) + titleY := bc.TitleStyle.Padding.GetTop(DefaultTitleTop) + textHeight + + r.Text(bc.Title, titleX, titleY) + } +} + +func (bc BarChart) getCanvasStyle() Style { + return bc.Canvas.InheritFrom(bc.styleDefaultsCanvas()) +} + +func (bc BarChart) styleDefaultsCanvas() Style { + return Style{ + FillColor: bc.GetColorPalette().CanvasColor(), + StrokeColor: bc.GetColorPalette().CanvasStrokeColor(), + StrokeWidth: DefaultCanvasStrokeWidth, } } @@ -397,8 +426,8 @@ func (bc BarChart) box() Box { dpb := bc.Background.Padding.GetBottom(50) return Box{ - Top: 20, - Left: 20, + Top: bc.Background.Padding.GetTop(20), + Left: bc.Background.Padding.GetLeft(20), Right: bc.GetWidth() - dpr, Bottom: bc.GetHeight() - dpb, }