diff --git a/chart.go b/chart.go index 2ddbd8a..e7478ed 100644 --- a/chart.go +++ b/chart.go @@ -288,17 +288,18 @@ func (c Chart) getYAxisWidth(r Renderer, ticks []Tick) int { } func (c Chart) getYAxisSecondaryWidth(r Renderer, ticks []Tick) int { - var ll string + r.SetFontSize(c.YAxisSecondary.Style.GetFontSize(DefaultFontSize)) + r.SetFont(c.YAxisSecondary.Style.GetFont(c.Font)) + + var textWidth int for _, t := range ticks { - if len(t.Label) > len(ll) { - ll = t.Label + tw, _ := r.MeasureText(t.Label) + if tw > textWidth { + textWidth = tw } } - r.SetFontSize(c.YAxisSecondary.Style.GetFontSize(DefaultFontSize)) - r.SetFont(c.YAxisSecondary.Style.GetFont(c.Font)) - tw, _ := r.MeasureText(ll) - return tw + DefaultYAxisMargin + return textWidth + DefaultYAxisMargin } func (c Chart) setRangeDomains(canvasBox Box, xrange, yrange, yrangeAlt Range) (Range, Range, Range) { diff --git a/yaxis.go b/yaxis.go index 5ad2619..544b22a 100644 --- a/yaxis.go +++ b/yaxis.go @@ -77,8 +77,8 @@ func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, axisType YAxisType, v := t.Value ly := ra.Translate(v) + canvasBox.Top - th := int(fontSize) >> 1 - ty := ly + th + _, pth := r.MeasureText(t.Label) + ty := ly + pth>>1 r.Text(t.Label, tx, ty) @@ -97,13 +97,12 @@ func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, axisType YAxisType, v := t.Value ly := ra.Translate(v) + canvasBox.Top - ptw, _ := r.MeasureText(t.Label) + ptw, pth := r.MeasureText(t.Label) tw := ptw - th := int(fontSize) - tx = lx - (int(tw) + (DefaultYAxisMargin >> 1)) - ty := ly + th>>1 + tx = lx - (tw + DefaultYAxisMargin) + ty := ly + pth>>1 r.Text(t.Label, tx, ty)