draw circle if single value; do not position text on negative coordinates (#82)
This commit is contained in:
parent
865ff54ab9
commit
6735e8990a
1 changed files with 23 additions and 9 deletions
32
pie_chart.go
32
pie_chart.go
|
@ -138,19 +138,26 @@ func (pc PieChart) drawSlices(r Renderer, canvasBox Box, values []Value) {
|
||||||
// draw the pie slices
|
// draw the pie slices
|
||||||
var rads, delta, delta2, total float64
|
var rads, delta, delta2, total float64
|
||||||
var lx, ly int
|
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)
|
r.MoveTo(cx, cy)
|
||||||
rads = util.Math.PercentToRadians(total)
|
r.Circle(radius, cx, cy)
|
||||||
delta = util.Math.PercentToRadians(v.Value)
|
} 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.ArcTo(cx, cy, radius, radius, rads, delta)
|
||||||
r.Close()
|
|
||||||
r.FillStroke()
|
r.LineTo(cx, cy)
|
||||||
total = total + v.Value
|
r.Close()
|
||||||
|
r.FillStroke()
|
||||||
|
total = total + v.Value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw the labels
|
// draw the labels
|
||||||
|
@ -166,6 +173,13 @@ func (pc PieChart) drawSlices(r Renderer, canvasBox Box, values []Value) {
|
||||||
lx = lx - (tb.Width() >> 1)
|
lx = lx - (tb.Width() >> 1)
|
||||||
ly = ly + (tb.Height() >> 1)
|
ly = ly + (tb.Height() >> 1)
|
||||||
|
|
||||||
|
if lx < 0 {
|
||||||
|
lx = 0
|
||||||
|
}
|
||||||
|
if ly < 0 {
|
||||||
|
lx = 0
|
||||||
|
}
|
||||||
|
|
||||||
r.Text(v.Label, lx, ly)
|
r.Text(v.Label, lx, ly)
|
||||||
}
|
}
|
||||||
total = total + v.Value
|
total = total + v.Value
|
||||||
|
|
Loading…
Reference in a new issue