Skip to content

Commit a930d59

Browse files
authored
Merge branch 'master' into release/5.0.0_updated
2 parents 34cae27 + 2bfd37f commit a930d59

File tree

2 files changed

+68
-110
lines changed

2 files changed

+68
-110
lines changed

Source/Charts/Renderers/PieChartRenderer.swift

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ open class PieChartRenderer: NSObject, DataRenderer
123123
let drawAngles = chart.drawAngles
124124
let center = chart.centerCircleBox
125125
let radius = chart.radius
126-
let drawInnerArc = chart.drawHoleEnabled && !chart.drawSlicesUnderHoleEnabled
127-
let userInnerRadius = drawInnerArc ? radius * chart.holeRadiusPercent : 0.0
128126

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

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

141141
context.saveGState()
142142

@@ -180,7 +180,7 @@ open class PieChartRenderer: NSObject, DataRenderer
180180
continue
181181
}
182182

183-
let accountForSliceSpacing = sliceSpace > 0.0 && sliceAngle <= 180.0
183+
let accountForSliceSpacing = sliceSpace > 0.0
184184

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

@@ -207,6 +207,8 @@ open class PieChartRenderer: NSObject, DataRenderer
207207
if drawInnerArc &&
208208
(innerRadius > 0.0 || accountForSliceSpacing)
209209
{
210+
var hasZeroInnerRadius = innerRadius == 0.0
211+
210212
if accountForSliceSpacing
211213
{
212214
var minSpacedRadius = calculateMinimumRadiusForSpacedSlice(
@@ -235,42 +237,29 @@ open class PieChartRenderer: NSObject, DataRenderer
235237
}
236238
let endAngleInner = startAngleInner + sweepAngleInner
237239

238-
path.addLine(
239-
to: CGPoint(
240-
x: center.x + innerRadius * cos(endAngleInner.DEG2RAD),
241-
y: center.y + innerRadius * sin(endAngleInner.DEG2RAD)))
242-
243-
path.addRelativeArc(center: center, radius: innerRadius, startAngle: endAngleInner.DEG2RAD, delta: -sweepAngleInner.DEG2RAD)
244-
}
245-
else
246-
{
247-
if accountForSliceSpacing
240+
if sliceAngle > 180.0 && hasZeroInnerRadius && accountForSliceSpacing
248241
{
249-
let angleMiddle = startAngleOuter + sweepAngleOuter / 2.0
250-
251-
let sliceSpaceOffset =
252-
calculateMinimumRadiusForSpacedSlice(
253-
center: center,
254-
radius: radius,
255-
angle: sliceAngle * CGFloat(phaseY),
256-
arcStartPointX: arcStartPointX,
257-
arcStartPointY: arcStartPointY,
258-
startAngle: startAngleOuter,
259-
sweepAngle: sweepAngleOuter)
260-
261-
let arcEndPointX = center.x + sliceSpaceOffset * cos(angleMiddle.DEG2RAD)
262-
let arcEndPointY = center.y + sliceSpaceOffset * sin(angleMiddle.DEG2RAD)
242+
let midAngleInner = (startAngleInner + endAngleInner) / 2
263243

264244
path.addLine(
265245
to: CGPoint(
266-
x: arcEndPointX,
267-
y: arcEndPointY))
246+
x: center.x + innerRadius * cos(midAngleInner.DEG2RAD),
247+
y: center.y + innerRadius * sin(midAngleInner.DEG2RAD)))
268248
}
269249
else
270250
{
271-
path.addLine(to: center)
251+
path.addLine(
252+
to: CGPoint(
253+
x: center.x + innerRadius * cos(endAngleInner.DEG2RAD),
254+
y: center.y + innerRadius * sin(endAngleInner.DEG2RAD)))
255+
256+
path.addRelativeArc(center: center, radius: innerRadius, startAngle: endAngleInner.DEG2RAD, delta: -sweepAngleInner.DEG2RAD)
272257
}
273258
}
259+
else
260+
{
261+
path.addLine(to: center)
262+
}
274263

275264
path.closeSubpath()
276265

@@ -518,7 +507,7 @@ open class PieChartRenderer: NSObject, DataRenderer
518507
{
519508
// calculate the text position
520509
let x = labelRadius * sliceXBase + center.x
521-
let y = labelRadius * sliceYBase + center.y - lineHeight
510+
let y = labelRadius * sliceYBase + center.y - lineHeight / 2.0
522511

523512
if drawXInside && drawYInside
524513
{
@@ -716,8 +705,6 @@ open class PieChartRenderer: NSObject, DataRenderer
716705
let absoluteAngles = chart.absoluteAngles
717706
let center = chart.centerCircleBox
718707
let radius = chart.radius
719-
let drawInnerArc = chart.drawHoleEnabled && !chart.drawSlicesUnderHoleEnabled
720-
let userInnerRadius = drawInnerArc ? radius * chart.holeRadiusPercent : 0.0
721708

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

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

759748
let sliceAngle = drawAngles[index]
760749
var innerRadius = userInnerRadius
761750

762751
let shift = set.selectionShift
763752
let highlightedRadius = radius + shift
764753

765-
let accountForSliceSpacing = sliceSpace > 0.0 && sliceAngle <= 180.0
754+
let accountForSliceSpacing = sliceSpace > 0.0
766755

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

@@ -812,6 +801,8 @@ open class PieChartRenderer: NSObject, DataRenderer
812801
if drawInnerArc &&
813802
(innerRadius > 0.0 || accountForSliceSpacing)
814803
{
804+
var hasZeroInnerRadius = innerRadius == 0.0
805+
815806
if accountForSliceSpacing
816807
{
817808
var minSpacedRadius = sliceSpaceRadius
@@ -833,34 +824,31 @@ open class PieChartRenderer: NSObject, DataRenderer
833824
}
834825
let endAngleInner = startAngleInner + sweepAngleInner
835826

836-
path.addLine(
837-
to: CGPoint(
838-
x: center.x + innerRadius * cos(endAngleInner.DEG2RAD),
839-
y: center.y + innerRadius * sin(endAngleInner.DEG2RAD)))
840-
841-
path.addRelativeArc(center: center, radius: innerRadius,
842-
startAngle: endAngleInner.DEG2RAD,
843-
delta: -sweepAngleInner.DEG2RAD)
844-
}
845-
else
846-
{
847-
if accountForSliceSpacing
827+
if sliceAngle > 180.0 && hasZeroInnerRadius && accountForSliceSpacing
848828
{
849-
let angleMiddle = startAngleOuter + sweepAngleOuter / 2.0
850-
851-
let arcEndPointX = center.x + sliceSpaceRadius * cos(angleMiddle.DEG2RAD)
852-
let arcEndPointY = center.y + sliceSpaceRadius * sin(angleMiddle.DEG2RAD)
829+
let midAngleInner = (startAngleInner + endAngleInner) / 2
853830

854831
path.addLine(
855832
to: CGPoint(
856-
x: arcEndPointX,
857-
y: arcEndPointY))
833+
x: center.x + innerRadius * cos(midAngleInner.DEG2RAD),
834+
y: center.y + innerRadius * sin(midAngleInner.DEG2RAD)))
858835
}
859836
else
860837
{
861-
path.addLine(to: center)
838+
path.addLine(
839+
to: CGPoint(
840+
x: center.x + innerRadius * cos(endAngleInner.DEG2RAD),
841+
y: center.y + innerRadius * sin(endAngleInner.DEG2RAD)))
842+
843+
path.addRelativeArc(center: center, radius: innerRadius,
844+
startAngle: endAngleInner.DEG2RAD,
845+
delta: -sweepAngleInner.DEG2RAD)
862846
}
863847
}
848+
else
849+
{
850+
path.addLine(to: center)
851+
}
864852

865853
path.closeSubpath()
866854

Source/Charts/Utils/ChartUtils.swift

Lines changed: 27 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,105 +12,75 @@
1212
import Foundation
1313
import CoreGraphics
1414

15-
extension Comparable
16-
{
17-
func clamped(to range: ClosedRange<Self>) -> Self
18-
{
19-
if self > range.upperBound
20-
{
15+
extension Comparable {
16+
func clamped(to range: ClosedRange<Self>) -> Self {
17+
if self > range.upperBound {
2118
return range.upperBound
22-
}
23-
else if self < range.lowerBound
24-
{
19+
} else if self < range.lowerBound {
2520
return range.lowerBound
26-
}
27-
else
28-
{
21+
} else {
2922
return self
3023
}
3124
}
3225
}
3326

34-
extension FloatingPoint
35-
{
36-
var DEG2RAD: Self
37-
{
27+
extension FloatingPoint {
28+
var DEG2RAD: Self {
3829
return self * .pi / 180
3930
}
4031

41-
var RAD2DEG: Self
42-
{
32+
var RAD2DEG: Self {
4333
return self * 180 / .pi
4434
}
4535

46-
/// - Note: Value must be in degrees
47-
/// - Returns: An angle between 0.0 < 360.0 (not less than zero, less than 360)
48-
var normalizedAngle: Self
49-
{
36+
var normalizedAngle: Self {
5037
let angle = truncatingRemainder(dividingBy: 360)
5138
return (sign == .minus) ? angle + 360 : angle
5239
}
5340
}
5441

55-
extension CGSize
56-
{
57-
func rotatedBy(degrees: CGFloat) -> CGSize
58-
{
42+
extension CGSize {
43+
func rotatedBy(degrees: CGFloat) -> CGSize {
5944
let radians = degrees.DEG2RAD
6045
return rotatedBy(radians: radians)
6146
}
6247

63-
func rotatedBy(radians: CGFloat) -> CGSize
64-
{
48+
func rotatedBy(radians: CGFloat) -> CGSize {
6549
return CGSize(
6650
width: abs(width * cos(radians)) + abs(height * sin(radians)),
6751
height: abs(width * sin(radians)) + abs(height * cos(radians))
6852
)
6953
}
7054
}
7155

72-
extension Double
73-
{
74-
/// Rounds the number to the nearest multiple of it's order of magnitude, rounding away from zero if halfway.
75-
func roundedToNextSignificant() -> Double
76-
{
77-
guard
78-
!isInfinite,
79-
!isNaN,
80-
self != 0
81-
else { return self }
56+
extension Double {
57+
func roundedToNextSignificant() -> Double {
58+
guard !isInfinite, !isNaN, self != 0 else { return self }
8259

83-
let d = ceil(log10(self < 0 ? -self : self))
60+
// Use Foundation.log10
61+
let d = ceil(Foundation.log10(self < 0 ? -self : self))
8462
let pw = 1 - Int(d)
85-
let magnitude = pow(10.0, Double(pw))
63+
64+
// Use Foundation.pow
65+
let magnitude = Foundation.pow(10.0, Double(pw))
8666
let shifted = (self * magnitude).rounded()
8767
return shifted / magnitude
8868
}
8969

90-
var decimalPlaces: Int
91-
{
92-
guard
93-
!isNaN,
94-
!isInfinite,
95-
self != 0.0
96-
else { return 0 }
70+
var decimalPlaces: Int {
71+
guard !isNaN, !isInfinite, self != 0.0 else { return 0 }
9772

9873
let i = roundedToNextSignificant()
9974

100-
guard
101-
!i.isInfinite,
102-
!i.isNaN
103-
else { return 0 }
75+
guard !i.isInfinite, !i.isNaN else { return 0 }
10476

105-
return Int(ceil(-log10(i))) + 2
77+
// Use Foundation.log10
78+
return Int(ceil(-Foundation.log10(i))) + 2
10679
}
10780
}
10881

109-
extension CGPoint
110-
{
111-
/// Calculates the position around a center point, depending on the distance from the center, and the angle of the position around the center.
112-
func moving(distance: CGFloat, atAngle angle: CGFloat) -> CGPoint
113-
{
82+
extension CGPoint {
83+
func moving(distance: CGFloat, atAngle angle: CGFloat) -> CGPoint {
11484
return CGPoint(x: x + distance * cos(angle.DEG2RAD),
11585
y: y + distance * sin(angle.DEG2RAD))
11686
}

0 commit comments

Comments
 (0)