Skip to content

Commit

Permalink
Do not let slice spacing make the chart go haywire. (Fixes #819)
Browse files Browse the repository at this point in the history
If the user provides data that does not allow the spacing to stay even -
he will need to manually fix that (i.e by giving a larger inner hole)

Even with a hole - with very small slices it will not help.
Spacing could be reduces, and small slices can be removed from the dataset.
  • Loading branch information
danielgindi committed Mar 17, 2016
1 parent 368e251 commit 4f87c7c
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions Charts/Classes/Renderers/PieChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ public class PieChartRenderer: ChartDataRendererBase
if (!chart.needsHighlight(xIndex: e.xIndex,
dataSetIndex: data.indexOfDataSet(dataSet)))
{
let accountForSliceSpacing = sliceSpace > 0.0 && sliceAngle <= 180.0

CGContextSetFillColorWithColor(context, dataSet.colorAt(j).CGColor)

let sliceSpaceAngleOuter = visibleAngleCount == 1 ?
Expand Down Expand Up @@ -169,9 +171,9 @@ public class PieChartRenderer: ChartDataRendererBase
sweepAngleOuter * ChartUtils.Math.FDEG2RAD)

if drawInnerArc &&
(innerRadius > 0.0 || sliceSpace > 0.0)
(innerRadius > 0.0 || accountForSliceSpacing)
{
if sliceSpace > 0.0
if accountForSliceSpacing
{
var minSpacedRadius = calculateMinimumRadiusForSpacedSlice(
center: center,
Expand All @@ -185,7 +187,7 @@ public class PieChartRenderer: ChartDataRendererBase
{
minSpacedRadius = -minSpacedRadius
}
innerRadius = max(innerRadius, minSpacedRadius)
innerRadius = min(max(innerRadius, minSpacedRadius), radius)
}

let sliceSpaceAngleInner = visibleAngleCount == 1 || innerRadius == 0.0 ?
Expand Down Expand Up @@ -215,7 +217,7 @@ public class PieChartRenderer: ChartDataRendererBase
}
else
{
if sliceSpace > 0.0
if accountForSliceSpacing
{
let angleMiddle = startAngleOuter + sweepAngleOuter / 2.0

Expand Down Expand Up @@ -577,6 +579,8 @@ public class PieChartRenderer: ChartDataRendererBase
let shift = set.selectionShift
let highlightedRadius = radius + shift

let accountForSliceSpacing = sliceSpace > 0.0 && sliceAngle <= 180.0

CGContextSetFillColorWithColor(context, set.colorAt(xIndex).CGColor)

let sliceSpaceAngleOuter = visibleAngleCount == 1 ?
Expand Down Expand Up @@ -618,7 +622,7 @@ public class PieChartRenderer: ChartDataRendererBase
sweepAngleShifted * ChartUtils.Math.FDEG2RAD)

var sliceSpaceRadius: CGFloat = 0.0
if sliceSpace > 0.0
if accountForSliceSpacing
{
sliceSpaceRadius = calculateMinimumRadiusForSpacedSlice(
center: center,
Expand All @@ -631,16 +635,16 @@ public class PieChartRenderer: ChartDataRendererBase
}

if drawInnerArc &&
(innerRadius > 0.0 || sliceSpace > 0.0)
(innerRadius > 0.0 || accountForSliceSpacing)
{
if sliceSpace > 0.0
if accountForSliceSpacing
{
var minSpacedRadius = sliceSpaceRadius
if minSpacedRadius < 0.0
{
minSpacedRadius = -minSpacedRadius
}
innerRadius = max(innerRadius, minSpacedRadius)
innerRadius = min(max(innerRadius, minSpacedRadius), radius)
}

let sliceSpaceAngleInner = visibleAngleCount == 1 || innerRadius == 0.0 ?
Expand Down Expand Up @@ -670,7 +674,7 @@ public class PieChartRenderer: ChartDataRendererBase
}
else
{
if sliceSpace > 0.0
if accountForSliceSpacing
{
let angleMiddle = startAngleOuter + sweepAngleOuter / 2.0

Expand Down

0 comments on commit 4f87c7c

Please sign in to comment.