From 8310896cd0cd0233cb26c521caa4156290ed7ad4 Mon Sep 17 00:00:00 2001 From: Alan Bernstein Date: Thu, 20 Aug 2020 23:29:13 -0500 Subject: [PATCH] Add ability to draw bar chart X-axis at BaseValue --- bar_chart.go | 27 ++++++++++++++++----------- examples/bar_chart_base_value/main.go | 6 ++++-- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/bar_chart.go b/bar_chart.go index d61f3db..f41e1eb 100644 --- a/bar_chart.go +++ b/bar_chart.go @@ -30,8 +30,9 @@ type BarChart struct { BarSpacing int - UseBaseValue bool - BaseValue float64 + UseBaseValue bool + BaseValue float64 + UseBaseValueXAxis bool Font *truetype.Font defaultFont *truetype.Font @@ -130,7 +131,7 @@ func (bc BarChart) Render(rp RendererProvider, w io.Writer) error { } bc.drawCanvas(r, canvasBox) bc.drawBars(r, canvasBox, yr) - bc.drawXAxis(r, canvasBox) + bc.drawXAxis(r, canvasBox, yr) bc.drawYAxis(r, canvasBox, yr, yt) bc.drawTitle(r) @@ -223,25 +224,29 @@ func (bc BarChart) drawBars(r Renderer, canvasBox Box, yr Range) { } } -func (bc BarChart) drawXAxis(r Renderer, canvasBox Box) { +func (bc BarChart) drawXAxis(r Renderer, canvasBox Box, yr Range) { if !bc.XAxis.Hidden { axisStyle := bc.XAxis.InheritFrom(bc.styleDefaultsAxes()) axisStyle.WriteToRenderer(r) width, spacing, _ := bc.calculateScaledTotalWidth(canvasBox) - r.MoveTo(canvasBox.Left, canvasBox.Bottom) - r.LineTo(canvasBox.Right, canvasBox.Bottom) + axisYValue := canvasBox.Bottom + if bc.UseBaseValueXAxis { + axisYValue -= yr.Translate(bc.BaseValue) + } + r.MoveTo(canvasBox.Left, axisYValue) + r.LineTo(canvasBox.Right, axisYValue) r.Stroke() - r.MoveTo(canvasBox.Left, canvasBox.Bottom) - r.LineTo(canvasBox.Left, canvasBox.Bottom+DefaultVerticalTickHeight) + r.MoveTo(canvasBox.Left, axisYValue) + r.LineTo(canvasBox.Left, axisYValue+DefaultVerticalTickHeight) r.Stroke() cursor := canvasBox.Left for index, bar := range bc.Bars { barLabelBox := Box{ - Top: canvasBox.Bottom + DefaultXAxisMargin, + Top: axisYValue + DefaultXAxisMargin, Left: cursor, Right: cursor + width + spacing, Bottom: bc.GetHeight(), @@ -253,8 +258,8 @@ func (bc BarChart) drawXAxis(r Renderer, canvasBox Box) { axisStyle.WriteToRenderer(r) if index < len(bc.Bars)-1 { - r.MoveTo(barLabelBox.Right, canvasBox.Bottom) - r.LineTo(barLabelBox.Right, canvasBox.Bottom+DefaultVerticalTickHeight) + r.MoveTo(barLabelBox.Right, axisYValue) + r.LineTo(barLabelBox.Right, axisYValue+DefaultVerticalTickHeight) r.Stroke() } cursor += width + spacing diff --git a/examples/bar_chart_base_value/main.go b/examples/bar_chart_base_value/main.go index 84ad9c4..14adbe8 100644 --- a/examples/bar_chart_base_value/main.go +++ b/examples/bar_chart_base_value/main.go @@ -31,6 +31,7 @@ func main() { }, Height: 512, BarWidth: 60, + XAxis: chart.Style{StrokeColor: drawing.ColorBlack, StrokeWidth: 1.0}, YAxis: chart.YAxis{ Ticks: []chart.Tick{ {Value: -4.0, Label: "-4"}, @@ -44,8 +45,9 @@ func main() { {Value: 12.0, Label: "12"}, }, }, - UseBaseValue: true, - BaseValue: 0.0, + UseBaseValue: true, + BaseValueXAxis: true, + BaseValue: 0.0, Bars: []chart.Value{ {Value: 10.0, Style: profitStyle, Label: "Profit"}, {Value: 12.0, Style: profitStyle, Label: "More Profit"},