From 6735e8990adb686811f5635e54840d2122d2dd15 Mon Sep 17 00:00:00 2001 From: Bernhard Reisenberger Date: Fri, 12 Oct 2018 02:21:06 +0200 Subject: [PATCH] draw circle if single value; do not position text on negative coordinates (#82) --- pie_chart.go | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/pie_chart.go b/pie_chart.go index 6d5d75e..cb8cd07 100644 --- a/pie_chart.go +++ b/pie_chart.go @@ -138,19 +138,26 @@ func (pc PieChart) drawSlices(r Renderer, canvasBox Box, values []Value) { // draw the pie slices var rads, delta, delta2, total float64 var lx, ly int - for index, v := range values { - v.Style.InheritFrom(pc.stylePieChartValue(index)).WriteToRenderer(r) + if len(values) == 1 { + pc.stylePieChartValue(0).WriteToRenderer(r) r.MoveTo(cx, cy) - rads = util.Math.PercentToRadians(total) - delta = util.Math.PercentToRadians(v.Value) + r.Circle(radius, cx, cy) + } else { + for index, v := range values { + v.Style.InheritFrom(pc.stylePieChartValue(index)).WriteToRenderer(r) - r.ArcTo(cx, cy, radius, radius, rads, delta) + r.MoveTo(cx, cy) + rads = util.Math.PercentToRadians(total) + delta = util.Math.PercentToRadians(v.Value) - r.LineTo(cx, cy) - r.Close() - r.FillStroke() - total = total + v.Value + r.ArcTo(cx, cy, radius, radius, rads, delta) + + r.LineTo(cx, cy) + r.Close() + r.FillStroke() + total = total + v.Value + } } // draw the labels @@ -166,6 +173,13 @@ func (pc PieChart) drawSlices(r Renderer, canvasBox Box, values []Value) { lx = lx - (tb.Width() >> 1) ly = ly + (tb.Height() >> 1) + if lx < 0 { + lx = 0 + } + if ly < 0 { + lx = 0 + } + r.Text(v.Label, lx, ly) } total = total + v.Value