Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 41 additions & 53 deletions Source/Charts/Renderers/PieChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ open class PieChartRenderer: NSObject, DataRenderer
let drawAngles = chart.drawAngles
let center = chart.centerCircleBox
let radius = chart.radius
let drawInnerArc = chart.drawHoleEnabled && !chart.drawSlicesUnderHoleEnabled
let userInnerRadius = drawInnerArc ? radius * chart.holeRadiusPercent : 0.0

var visibleAngleCount = 0
for j in 0 ..< entryCount
Expand All @@ -137,6 +135,8 @@ open class PieChartRenderer: NSObject, DataRenderer
}

let sliceSpace = visibleAngleCount <= 1 ? 0.0 : getSliceSpace(dataSet: dataSet)
let drawInnerArc = chart.drawHoleEnabled && !chart.drawSlicesUnderHoleEnabled || sliceSpace > 0.0
let userInnerRadius = drawInnerArc ? radius * chart.holeRadiusPercent : 0.0

context.saveGState()

Expand Down Expand Up @@ -180,7 +180,7 @@ open class PieChartRenderer: NSObject, DataRenderer
continue
}

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

context.setFillColor(dataSet.color(atIndex: j).cgColor)

Expand All @@ -207,6 +207,8 @@ open class PieChartRenderer: NSObject, DataRenderer
if drawInnerArc &&
(innerRadius > 0.0 || accountForSliceSpacing)
{
var hasZeroInnerRadius = innerRadius == 0.0

if accountForSliceSpacing
{
var minSpacedRadius = calculateMinimumRadiusForSpacedSlice(
Expand Down Expand Up @@ -235,42 +237,29 @@ open class PieChartRenderer: NSObject, DataRenderer
}
let endAngleInner = startAngleInner + sweepAngleInner

path.addLine(
to: CGPoint(
x: center.x + innerRadius * cos(endAngleInner.DEG2RAD),
y: center.y + innerRadius * sin(endAngleInner.DEG2RAD)))

path.addRelativeArc(center: center, radius: innerRadius, startAngle: endAngleInner.DEG2RAD, delta: -sweepAngleInner.DEG2RAD)
}
else
{
if accountForSliceSpacing
if sliceAngle > 180.0 && hasZeroInnerRadius && accountForSliceSpacing
{
let angleMiddle = startAngleOuter + sweepAngleOuter / 2.0

let sliceSpaceOffset =
calculateMinimumRadiusForSpacedSlice(
center: center,
radius: radius,
angle: sliceAngle * CGFloat(phaseY),
arcStartPointX: arcStartPointX,
arcStartPointY: arcStartPointY,
startAngle: startAngleOuter,
sweepAngle: sweepAngleOuter)

let arcEndPointX = center.x + sliceSpaceOffset * cos(angleMiddle.DEG2RAD)
let arcEndPointY = center.y + sliceSpaceOffset * sin(angleMiddle.DEG2RAD)
let midAngleInner = (startAngleInner + endAngleInner) / 2

path.addLine(
to: CGPoint(
x: arcEndPointX,
y: arcEndPointY))
x: center.x + innerRadius * cos(midAngleInner.DEG2RAD),
y: center.y + innerRadius * sin(midAngleInner.DEG2RAD)))
}
else
{
path.addLine(to: center)
path.addLine(
to: CGPoint(
x: center.x + innerRadius * cos(endAngleInner.DEG2RAD),
y: center.y + innerRadius * sin(endAngleInner.DEG2RAD)))

path.addRelativeArc(center: center, radius: innerRadius, startAngle: endAngleInner.DEG2RAD, delta: -sweepAngleInner.DEG2RAD)
}
}
else
{
path.addLine(to: center)
}

path.closeSubpath()

Expand Down Expand Up @@ -518,7 +507,7 @@ open class PieChartRenderer: NSObject, DataRenderer
{
// calculate the text position
let x = labelRadius * sliceXBase + center.x
let y = labelRadius * sliceYBase + center.y - lineHeight
let y = labelRadius * sliceYBase + center.y - lineHeight / 2.0

if drawXInside && drawYInside
{
Expand Down Expand Up @@ -716,8 +705,6 @@ open class PieChartRenderer: NSObject, DataRenderer
let absoluteAngles = chart.absoluteAngles
let center = chart.centerCircleBox
let radius = chart.radius
let drawInnerArc = chart.drawHoleEnabled && !chart.drawSlicesUnderHoleEnabled
let userInnerRadius = drawInnerArc ? radius * chart.holeRadiusPercent : 0.0

// Append highlighted accessibility slices into this array, so we can prioritize them over unselected slices
var highlightedAccessibleElements: [NSUIAccessibilityElement] = []
Expand Down Expand Up @@ -755,14 +742,16 @@ open class PieChartRenderer: NSObject, DataRenderer
}

let sliceSpace = visibleAngleCount <= 1 ? 0.0 : set.sliceSpace
let drawInnerArc = chart.drawHoleEnabled && !chart.drawSlicesUnderHoleEnabled || sliceSpace > 0.0
let userInnerRadius = drawInnerArc ? radius * chart.holeRadiusPercent : 0.0

let sliceAngle = drawAngles[index]
var innerRadius = userInnerRadius

let shift = set.selectionShift
let highlightedRadius = radius + shift

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

context.setFillColor(set.highlightColor?.cgColor ?? set.color(atIndex: index).cgColor)

Expand Down Expand Up @@ -812,6 +801,8 @@ open class PieChartRenderer: NSObject, DataRenderer
if drawInnerArc &&
(innerRadius > 0.0 || accountForSliceSpacing)
{
var hasZeroInnerRadius = innerRadius == 0.0

if accountForSliceSpacing
{
var minSpacedRadius = sliceSpaceRadius
Expand All @@ -833,34 +824,31 @@ open class PieChartRenderer: NSObject, DataRenderer
}
let endAngleInner = startAngleInner + sweepAngleInner

path.addLine(
to: CGPoint(
x: center.x + innerRadius * cos(endAngleInner.DEG2RAD),
y: center.y + innerRadius * sin(endAngleInner.DEG2RAD)))

path.addRelativeArc(center: center, radius: innerRadius,
startAngle: endAngleInner.DEG2RAD,
delta: -sweepAngleInner.DEG2RAD)
}
else
{
if accountForSliceSpacing
if sliceAngle > 180.0 && hasZeroInnerRadius && accountForSliceSpacing
{
let angleMiddle = startAngleOuter + sweepAngleOuter / 2.0

let arcEndPointX = center.x + sliceSpaceRadius * cos(angleMiddle.DEG2RAD)
let arcEndPointY = center.y + sliceSpaceRadius * sin(angleMiddle.DEG2RAD)
let midAngleInner = (startAngleInner + endAngleInner) / 2

path.addLine(
to: CGPoint(
x: arcEndPointX,
y: arcEndPointY))
x: center.x + innerRadius * cos(midAngleInner.DEG2RAD),
y: center.y + innerRadius * sin(midAngleInner.DEG2RAD)))
}
else
{
path.addLine(to: center)
path.addLine(
to: CGPoint(
x: center.x + innerRadius * cos(endAngleInner.DEG2RAD),
y: center.y + innerRadius * sin(endAngleInner.DEG2RAD)))

path.addRelativeArc(center: center, radius: innerRadius,
startAngle: endAngleInner.DEG2RAD,
delta: -sweepAngleInner.DEG2RAD)
}
}
else
{
path.addLine(to: center)
}

path.closeSubpath()

Expand Down
Loading