From 7c45bee34b1fcde3f99a2fdc417a1f7acc84d78d Mon Sep 17 00:00:00 2001 From: Eduard Hasanaj Date: Fri, 29 Oct 2021 16:12:55 +0200 Subject: [PATCH] Fix label hidden in BarChart --- bar_chart.go | 33 +-------------------------------- yaxis.go | 10 +++++----- 2 files changed, 6 insertions(+), 37 deletions(-) diff --git a/bar_chart.go b/bar_chart.go index bc67113..e162f5e 100644 --- a/bar_chart.go +++ b/bar_chart.go @@ -146,7 +146,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.drawYAxis(r, canvasBox, yr, yt) + bc.YAxis.Render(r, canvasBox, yr, bc.styleDefaultsAxes(), yt) bc.drawTitle(r) for _, a := range bc.Elements { @@ -277,37 +277,6 @@ func (bc BarChart) drawXAxis(r Renderer, canvasBox Box) { } } -func (bc BarChart) drawYAxis(r Renderer, canvasBox Box, yr Range, ticks []Tick) { - if !bc.YAxis.Style.Hidden { - axisStyle := bc.YAxis.Style.InheritFrom(bc.styleDefaultsAxes()) - axisStyle.WriteToRenderer(r) - - r.MoveTo(canvasBox.Right, canvasBox.Top) - r.LineTo(canvasBox.Right, canvasBox.Bottom) - r.Stroke() - - r.MoveTo(canvasBox.Right, canvasBox.Bottom) - r.LineTo(canvasBox.Right+DefaultHorizontalTickWidth, canvasBox.Bottom) - r.Stroke() - - var ty int - var tb Box - for _, t := range ticks { - ty = canvasBox.Bottom - yr.Translate(t.Value) - - axisStyle.GetStrokeOptions().WriteToRenderer(r) - r.MoveTo(canvasBox.Right, ty) - r.LineTo(canvasBox.Right+DefaultHorizontalTickWidth, ty) - r.Stroke() - - axisStyle.GetTextOptions().WriteToRenderer(r) - tb = r.MeasureText(t.Label) - Draw.Text(r, t.Label, canvasBox.Right+DefaultYAxisMargin+5, ty+(tb.Height()>>1), axisStyle) - } - - } -} - func (bc BarChart) drawTitle(r Renderer) { if len(bc.Title) > 0 && !bc.TitleStyle.Hidden { r.SetFont(bc.TitleStyle.GetFont(bc.GetFont())) diff --git a/yaxis.go b/yaxis.go index 841fc86..eb1a762 100644 --- a/yaxis.go +++ b/yaxis.go @@ -114,10 +114,10 @@ func (ya YAxis) Measure(r Renderer, canvasBox Box, ra Range, defaults Style, tic if ya.AxisType == YAxisPrimary { minx = canvasBox.Right - maxx = MaxInt(maxx, tx+tb.Width()) + maxx = MaxInt(maxx, tx+tb.Width()+ya.NameStyle.Padding.Left+ya.NameStyle.Padding.Right) } else if ya.AxisType == YAxisSecondary { - minx = MinInt(minx, finalTextX) - maxx = MaxInt(maxx, tx) + minx = MinInt(minx, finalTextX-ya.NameStyle.Padding.Left) + maxx = MaxInt(maxx, tx+ya.NameStyle.Padding.Right) } miny = MinInt(miny, ly-tbh2) @@ -201,9 +201,9 @@ func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, defaults Style, tick var tx int if ya.AxisType == YAxisPrimary { - tx = canvasBox.Right + int(sw) + DefaultYAxisMargin + maxTextWidth + DefaultYAxisMargin + tx = canvasBox.Right + int(sw) + DefaultYAxisMargin + maxTextWidth + DefaultYAxisMargin + nameStyle.Padding.Left } else if ya.AxisType == YAxisSecondary { - tx = canvasBox.Left - (DefaultYAxisMargin + int(sw) + maxTextWidth + DefaultYAxisMargin) + tx = canvasBox.Left - (DefaultYAxisMargin + int(sw) + maxTextWidth + DefaultYAxisMargin + nameStyle.Padding.Right) } var ty int