From ad296bcd926c9bcccb8255d307f18c4688150fd0 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Mon, 13 Nov 2017 14:16:21 -0800 Subject: [PATCH 1/5] Replaced `ChartUtils.Math` in favour of an extension on `FloatingPoint` Increases readability, and in many cases removes a set of parentheses to ensure order of operations. --- Source/Charts/Charts/PieChartView.swift | 4 +- .../Charts/Charts/PieRadarChartViewBase.swift | 12 ++-- .../Charts/Renderers/PieChartRenderer.swift | 68 +++++++++---------- Source/Charts/Renderers/XAxisRenderer.swift | 2 +- .../XAxisRendererHorizontalBarChart.swift | 2 +- .../Renderers/XAxisRendererRadarChart.swift | 2 +- Source/Charts/Utils/ChartUtils.swift | 28 ++++---- Source/Charts/Utils/Fill.swift | 2 +- 8 files changed, 61 insertions(+), 59 deletions(-) diff --git a/Source/Charts/Charts/PieChartView.swift b/Source/Charts/Charts/PieChartView.swift index c6c17aaa93..90d923217a 100755 --- a/Source/Charts/Charts/PieChartView.swift +++ b/Source/Charts/Charts/PieChartView.swift @@ -172,8 +172,8 @@ open class PieChartView: PieRadarChartViewBase let offset = drawAngles[entryIndex] / 2.0 // calculate the text position - let x: CGFloat = (r * cos(((rotationAngle + absoluteAngles[entryIndex] - offset) * CGFloat(_animator.phaseY)) * ChartUtils.Math.FDEG2RAD) + center.x) - let y: CGFloat = (r * sin(((rotationAngle + absoluteAngles[entryIndex] - offset) * CGFloat(_animator.phaseY)) * ChartUtils.Math.FDEG2RAD) + center.y) + let x: CGFloat = (r * cos(((rotationAngle + absoluteAngles[entryIndex] - offset) * CGFloat(_animator.phaseY)).deg2rad) + center.x) + let y: CGFloat = (r * sin(((rotationAngle + absoluteAngles[entryIndex] - offset) * CGFloat(_animator.phaseY)).deg2rad) + center.y) return CGPoint(x: x, y: y) } diff --git a/Source/Charts/Charts/PieRadarChartViewBase.swift b/Source/Charts/Charts/PieRadarChartViewBase.swift index 87944f55f3..4902529afa 100755 --- a/Source/Charts/Charts/PieRadarChartViewBase.swift +++ b/Source/Charts/Charts/PieRadarChartViewBase.swift @@ -255,7 +255,7 @@ open class PieRadarChartViewBase: ChartViewBase let length = sqrt(tx * tx + ty * ty) let r = acos(ty / length) - var angle = r * ChartUtils.Math.RAD2DEG + var angle = r.rad2deg if x > c.x { @@ -278,8 +278,8 @@ open class PieRadarChartViewBase: ChartViewBase /// from the center, and the angle of the position around the center. @objc open func getPosition(center: CGPoint, dist: CGFloat, angle: CGFloat) -> CGPoint { - return CGPoint(x: center.x + dist * cos(angle * ChartUtils.Math.FDEG2RAD), - y: center.y + dist * sin(angle * ChartUtils.Math.FDEG2RAD)) + return CGPoint(x: center.x + dist * cos(angle.deg2rad), + y: center.y + dist * sin(angle.deg2rad)) } /// - returns: The distance of a certain point on the chart to the center of the chart. @@ -845,14 +845,14 @@ open class PieRadarChartViewBase: ChartViewBase if recognizer.state == NSUIGestureRecognizerState.began || recognizer.state == NSUIGestureRecognizerState.changed { - let angle = ChartUtils.Math.FRAD2DEG * recognizer.nsuiRotation + let angle = recognizer.nsuiRotation.rad2deg self.rotationAngle = _startAngle + angle setNeedsDisplay() } else if recognizer.state == NSUIGestureRecognizerState.ended { - let angle = ChartUtils.Math.FRAD2DEG * recognizer.nsuiRotation + let angle = recognizer.nsuiRotation.rad2deg self.rotationAngle = _startAngle + angle setNeedsDisplay() @@ -861,7 +861,7 @@ open class PieRadarChartViewBase: ChartViewBase { stopDeceleration() - _decelerationAngularVelocity = ChartUtils.Math.FRAD2DEG * recognizer.velocity + _decelerationAngularVelocity = recognizer.velocity.rad2deg if _decelerationAngularVelocity != 0.0 { diff --git a/Source/Charts/Renderers/PieChartRenderer.swift b/Source/Charts/Renderers/PieChartRenderer.swift index 2ad91ccddc..8f6f536d21 100755 --- a/Source/Charts/Renderers/PieChartRenderer.swift +++ b/Source/Charts/Renderers/PieChartRenderer.swift @@ -58,12 +58,12 @@ open class PieChartRenderer: DataRenderer let angleMiddle = startAngle + sweepAngle / 2.0 // Other point of the arc - let arcEndPointX = center.x + radius * cos((startAngle + sweepAngle) * ChartUtils.Math.FDEG2RAD) - let arcEndPointY = center.y + radius * sin((startAngle + sweepAngle) * ChartUtils.Math.FDEG2RAD) + let arcEndPointX = center.x + radius * cos((startAngle + sweepAngle).deg2rad) + let arcEndPointY = center.y + radius * sin((startAngle + sweepAngle).deg2rad) // Middle point on the arc - let arcMidPointX = center.x + radius * cos(angleMiddle * ChartUtils.Math.FDEG2RAD) - let arcMidPointY = center.y + radius * sin(angleMiddle * ChartUtils.Math.FDEG2RAD) + let arcMidPointX = center.x + radius * cos(angleMiddle.deg2rad) + let arcMidPointY = center.y + radius * sin(angleMiddle.deg2rad) // This is the base of the contained triangle let basePointsDistance = sqrt( @@ -74,7 +74,7 @@ open class PieChartRenderer: DataRenderer // the angle of the contained triangle should stay the same. // So let's find out the height of that triangle. let containedTriangleHeight = (basePointsDistance / 2.0 * - tan((180.0 - angle) / 2.0 * ChartUtils.Math.FDEG2RAD)) + tan((180.0 - angle).deg2rad / 2.0)) // Now we subtract that from the radius var spacedRadius = radius - containedTriangleHeight @@ -158,7 +158,7 @@ open class PieChartRenderer: DataRenderer let sliceSpaceAngleOuter = visibleAngleCount == 1 ? 0.0 : - sliceSpace / (ChartUtils.Math.FDEG2RAD * radius) + sliceSpace / radius.deg2rad let startAngleOuter = rotationAngle + (angle + sliceSpaceAngleOuter / 2.0) * CGFloat(phaseY) var sweepAngleOuter = (sliceAngle - sliceSpaceAngleOuter) * CGFloat(phaseY) if sweepAngleOuter < 0.0 @@ -166,15 +166,15 @@ open class PieChartRenderer: DataRenderer sweepAngleOuter = 0.0 } - let arcStartPointX = center.x + radius * cos(startAngleOuter * ChartUtils.Math.FDEG2RAD) - let arcStartPointY = center.y + radius * sin(startAngleOuter * ChartUtils.Math.FDEG2RAD) + let arcStartPointX = center.x + radius * cos(startAngleOuter.deg2rad) + let arcStartPointY = center.y + radius * sin(startAngleOuter.deg2rad) let path = CGMutablePath() path.move(to: CGPoint(x: arcStartPointX, y: arcStartPointY)) - path.addRelativeArc(center: center, radius: radius, startAngle: startAngleOuter * ChartUtils.Math.FDEG2RAD, delta: sweepAngleOuter * ChartUtils.Math.FDEG2RAD) + path.addRelativeArc(center: center, radius: radius, startAngle: startAngleOuter.deg2rad, delta: sweepAngleOuter.deg2rad) if drawInnerArc && (innerRadius > 0.0 || accountForSliceSpacing) @@ -198,7 +198,7 @@ open class PieChartRenderer: DataRenderer let sliceSpaceAngleInner = visibleAngleCount == 1 || innerRadius == 0.0 ? 0.0 : - sliceSpace / (ChartUtils.Math.FDEG2RAD * innerRadius) + sliceSpace / innerRadius.deg2rad let startAngleInner = rotationAngle + (angle + sliceSpaceAngleInner / 2.0) * CGFloat(phaseY) var sweepAngleInner = (sliceAngle - sliceSpaceAngleInner) * CGFloat(phaseY) if sweepAngleInner < 0.0 @@ -209,10 +209,10 @@ open class PieChartRenderer: DataRenderer path.addLine( to: CGPoint( - x: center.x + innerRadius * cos(endAngleInner * ChartUtils.Math.FDEG2RAD), - y: center.y + innerRadius * sin(endAngleInner * ChartUtils.Math.FDEG2RAD))) + x: center.x + innerRadius * cos(endAngleInner.deg2rad), + y: center.y + innerRadius * sin(endAngleInner.deg2rad))) - path.addRelativeArc(center: center, radius: innerRadius, startAngle: endAngleInner * ChartUtils.Math.FDEG2RAD, delta: -sweepAngleInner * ChartUtils.Math.FDEG2RAD) + path.addRelativeArc(center: center, radius: innerRadius, startAngle: endAngleInner.deg2rad, delta: -sweepAngleInner.deg2rad) } else { @@ -230,8 +230,8 @@ open class PieChartRenderer: DataRenderer startAngle: startAngleOuter, sweepAngle: sweepAngleOuter) - let arcEndPointX = center.x + sliceSpaceOffset * cos(angleMiddle * ChartUtils.Math.FDEG2RAD) - let arcEndPointY = center.y + sliceSpaceOffset * sin(angleMiddle * ChartUtils.Math.FDEG2RAD) + let arcEndPointX = center.x + sliceSpaceOffset * cos(angleMiddle.deg2rad) + let arcEndPointY = center.y + sliceSpaceOffset * sin(angleMiddle.deg2rad) path.addLine( to: CGPoint( @@ -339,7 +339,7 @@ open class PieChartRenderer: DataRenderer let sliceAngle = drawAngles[xIndex] let sliceSpace = getSliceSpace(dataSet: dataSet) - let sliceSpaceMiddleAngle = sliceSpace / (ChartUtils.Math.FDEG2RAD * labelRadius) + let sliceSpaceMiddleAngle = sliceSpace / labelRadius.deg2rad // offset needed to center the drawn text in the slice let angleOffset = (sliceAngle - sliceSpaceMiddleAngle / 2.0) / 2.0 @@ -355,8 +355,8 @@ open class PieChartRenderer: DataRenderer dataSetIndex: i, viewPortHandler: viewPortHandler) - let sliceXBase = cos(transformedAngle * ChartUtils.Math.FDEG2RAD) - let sliceYBase = sin(transformedAngle * ChartUtils.Math.FDEG2RAD) + let sliceXBase = cos(transformedAngle.deg2rad) + let sliceYBase = sin(transformedAngle.deg2rad) let drawXOutside = drawEntryLabels && xValuePosition == .outsideSlice let drawYOutside = drawValues && yValuePosition == .outsideSlice @@ -388,7 +388,7 @@ open class PieChartRenderer: DataRenderer } let polyline2Length = dataSet.valueLineVariableLength - ? labelRadius * valueLineLength2 * abs(sin(transformedAngle * ChartUtils.Math.FDEG2RAD)) + ? labelRadius * valueLineLength2 * abs(sin(transformedAngle.deg2rad)) : labelRadius * valueLineLength2 let pt0 = CGPoint( @@ -737,11 +737,11 @@ open class PieChartRenderer: DataRenderer let sliceSpaceAngleOuter = visibleAngleCount == 1 ? 0.0 : - sliceSpace / (ChartUtils.Math.FDEG2RAD * radius) + sliceSpace / radius.deg2rad let sliceSpaceAngleShifted = visibleAngleCount == 1 ? 0.0 : - sliceSpace / (ChartUtils.Math.FDEG2RAD * highlightedRadius) + sliceSpace / highlightedRadius.deg2rad let startAngleOuter = rotationAngle + (angle + sliceSpaceAngleOuter / 2.0) * CGFloat(phaseY) var sweepAngleOuter = (sliceAngle - sliceSpaceAngleOuter) * CGFloat(phaseY) @@ -759,11 +759,11 @@ open class PieChartRenderer: DataRenderer let path = CGMutablePath() - path.move(to: CGPoint(x: center.x + highlightedRadius * cos(startAngleShifted * ChartUtils.Math.FDEG2RAD), - y: center.y + highlightedRadius * sin(startAngleShifted * ChartUtils.Math.FDEG2RAD))) + path.move(to: CGPoint(x: center.x + highlightedRadius * cos(startAngleShifted.deg2rad), + y: center.y + highlightedRadius * sin(startAngleShifted.deg2rad))) - path.addRelativeArc(center: center, radius: highlightedRadius, startAngle: startAngleShifted * ChartUtils.Math.FDEG2RAD, - delta: sweepAngleShifted * ChartUtils.Math.FDEG2RAD) + path.addRelativeArc(center: center, radius: highlightedRadius, startAngle: startAngleShifted.deg2rad, + delta: sweepAngleShifted.deg2rad) var sliceSpaceRadius: CGFloat = 0.0 if accountForSliceSpacing @@ -772,8 +772,8 @@ open class PieChartRenderer: DataRenderer center: center, radius: radius, angle: sliceAngle * CGFloat(phaseY), - arcStartPointX: center.x + radius * cos(startAngleOuter * ChartUtils.Math.FDEG2RAD), - arcStartPointY: center.y + radius * sin(startAngleOuter * ChartUtils.Math.FDEG2RAD), + arcStartPointX: center.x + radius * cos(startAngleOuter.deg2rad), + arcStartPointY: center.y + radius * sin(startAngleOuter.deg2rad), startAngle: startAngleOuter, sweepAngle: sweepAngleOuter) } @@ -793,7 +793,7 @@ open class PieChartRenderer: DataRenderer let sliceSpaceAngleInner = visibleAngleCount == 1 || innerRadius == 0.0 ? 0.0 : - sliceSpace / (ChartUtils.Math.FDEG2RAD * innerRadius) + sliceSpace / innerRadius.deg2rad let startAngleInner = rotationAngle + (angle + sliceSpaceAngleInner / 2.0) * CGFloat(phaseY) var sweepAngleInner = (sliceAngle - sliceSpaceAngleInner) * CGFloat(phaseY) if sweepAngleInner < 0.0 @@ -804,12 +804,12 @@ open class PieChartRenderer: DataRenderer path.addLine( to: CGPoint( - x: center.x + innerRadius * cos(endAngleInner * ChartUtils.Math.FDEG2RAD), - y: center.y + innerRadius * sin(endAngleInner * ChartUtils.Math.FDEG2RAD))) + x: center.x + innerRadius * cos(endAngleInner.deg2rad), + y: center.y + innerRadius * sin(endAngleInner.deg2rad))) path.addRelativeArc(center: center, radius: innerRadius, - startAngle: endAngleInner * ChartUtils.Math.FDEG2RAD, - delta: -sweepAngleInner * ChartUtils.Math.FDEG2RAD) + startAngle: endAngleInner.deg2rad, + delta: -sweepAngleInner.deg2rad) } else { @@ -817,8 +817,8 @@ open class PieChartRenderer: DataRenderer { let angleMiddle = startAngleOuter + sweepAngleOuter / 2.0 - let arcEndPointX = center.x + sliceSpaceRadius * cos(angleMiddle * ChartUtils.Math.FDEG2RAD) - let arcEndPointY = center.y + sliceSpaceRadius * sin(angleMiddle * ChartUtils.Math.FDEG2RAD) + let arcEndPointX = center.x + sliceSpaceRadius * cos(angleMiddle.deg2rad) + let arcEndPointY = center.y + sliceSpaceRadius * sin(angleMiddle.deg2rad) path.addLine( to: CGPoint( diff --git a/Source/Charts/Renderers/XAxisRenderer.swift b/Source/Charts/Renderers/XAxisRenderer.swift index 85535b82a4..8b3f8f9063 100644 --- a/Source/Charts/Renderers/XAxisRenderer.swift +++ b/Source/Charts/Renderers/XAxisRenderer.swift @@ -193,7 +193,7 @@ open class XAxisRenderer: AxisRendererBase let labelAttrs: [NSAttributedStringKey : Any] = [NSAttributedStringKey.font: xAxis.labelFont, NSAttributedStringKey.foregroundColor: xAxis.labelTextColor, NSAttributedStringKey.paragraphStyle: paraStyle] - let labelRotationAngleRadians = xAxis.labelRotationAngle * ChartUtils.Math.FDEG2RAD + let labelRotationAngleRadians = xAxis.labelRotationAngle.deg2rad let centeringEnabled = xAxis.isCenterAxisLabelsEnabled diff --git a/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift b/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift index b277e0fa50..a0d90219ed 100644 --- a/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift +++ b/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift @@ -129,7 +129,7 @@ open class XAxisRendererHorizontalBarChart: XAxisRenderer let labelFont = xAxis.labelFont let labelTextColor = xAxis.labelTextColor - let labelRotationAngleRadians = xAxis.labelRotationAngle * ChartUtils.Math.FDEG2RAD + let labelRotationAngleRadians = xAxis.labelRotationAngle.deg2rad let centeringEnabled = xAxis.isCenterAxisLabelsEnabled diff --git a/Source/Charts/Renderers/XAxisRendererRadarChart.swift b/Source/Charts/Renderers/XAxisRendererRadarChart.swift index 2f78ce2303..949300c49e 100644 --- a/Source/Charts/Renderers/XAxisRendererRadarChart.swift +++ b/Source/Charts/Renderers/XAxisRendererRadarChart.swift @@ -41,7 +41,7 @@ open class XAxisRendererRadarChart: XAxisRenderer let labelFont = xAxis.labelFont let labelTextColor = xAxis.labelTextColor - let labelRotationAngleRadians = xAxis.labelRotationAngle * ChartUtils.Math.FDEG2RAD + let labelRotationAngleRadians = xAxis.labelRotationAngle.rad2deg let drawLabelAnchor = CGPoint(x: 0.5, y: 0.25) let sliceangle = chart.sliceAngle diff --git a/Source/Charts/Utils/ChartUtils.swift b/Source/Charts/Utils/ChartUtils.swift index 21601a7030..1275bfac9c 100755 --- a/Source/Charts/Utils/ChartUtils.swift +++ b/Source/Charts/Utils/ChartUtils.swift @@ -16,18 +16,20 @@ import CoreGraphics import UIKit #endif +extension FloatingPoint { + var deg2rad: Self { + return self * .pi / 180 + } + + var rad2deg: Self { + return self * 180 / .pi + } +} + open class ChartUtils { fileprivate static var _defaultValueFormatter: IValueFormatter = ChartUtils.generateDefaultValueFormatter() - - internal struct Math - { - internal static let FDEG2RAD = CGFloat(Double.pi / 180.0) - internal static let FRAD2DEG = CGFloat(180.0 / Double.pi) - internal static let DEG2RAD = Double.pi / 180.0 - internal static let RAD2DEG = 180.0 / Double.pi - } - + internal class func roundToNextSignificant(number: Double) -> Double { if number.isInfinite || number.isNaN || number == 0 @@ -75,8 +77,8 @@ open class ChartUtils internal class func getPosition(center: CGPoint, dist: CGFloat, angle: CGFloat) -> CGPoint { return CGPoint( - x: center.x + dist * cos(angle * Math.FDEG2RAD), - y: center.y + dist * sin(angle * Math.FDEG2RAD) + x: center.x + dist * cos(angle.deg2rad), + y: center.y + dist * sin(angle.deg2rad) ) } @@ -276,7 +278,7 @@ open class ChartUtils internal class func sizeOfRotatedRectangle(_ rectangleSize: CGSize, degrees: CGFloat) -> CGSize { - let radians = degrees * Math.FDEG2RAD + let radians = degrees.deg2rad return sizeOfRotatedRectangle(rectangleWidth: rectangleSize.width, rectangleHeight: rectangleSize.height, radians: radians) } @@ -287,7 +289,7 @@ open class ChartUtils internal class func sizeOfRotatedRectangle(rectangleWidth: CGFloat, rectangleHeight: CGFloat, degrees: CGFloat) -> CGSize { - let radians = degrees * Math.FDEG2RAD + let radians = degrees.deg2rad return sizeOfRotatedRectangle(rectangleWidth: rectangleWidth, rectangleHeight: rectangleHeight, radians: radians) } diff --git a/Source/Charts/Utils/Fill.swift b/Source/Charts/Utils/Fill.swift index 58c6fe693e..579c8da32b 100644 --- a/Source/Charts/Utils/Fill.swift +++ b/Source/Charts/Utils/Fill.swift @@ -273,7 +273,7 @@ open class Fill: NSObject case .linearGradient: - let radians = ChartUtils.Math.FDEG2RAD * (360.0 - _gradientAngle) + let radians = (360.0 - _gradientAngle).deg2rad let centerPoint = CGPoint(x: rect.midX, y: rect.midY) let xAngleDelta = cos(radians) * rect.width / 2.0 let yAngleDelta = sin(radians) * rect.height / 2.0 From 2d297f4d3221a9c0f98382a53c637f8a275f5534 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Mon, 13 Nov 2017 15:19:23 -0800 Subject: [PATCH 2/5] Moved `normalizedAngled` into `FloatingPoint` extension Improves readability --- Source/Charts/Charts/PieChartView.swift | 2 +- .../Charts/Charts/PieRadarChartViewBase.swift | 2 +- Source/Charts/Charts/RadarChartView.swift | 2 +- Source/Charts/Utils/ChartUtils.swift | 23 ++++++++----------- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/Source/Charts/Charts/PieChartView.swift b/Source/Charts/Charts/PieChartView.swift index 90d923217a..4d1ae7a5f0 100755 --- a/Source/Charts/Charts/PieChartView.swift +++ b/Source/Charts/Charts/PieChartView.swift @@ -264,7 +264,7 @@ open class PieChartView: PieRadarChartViewBase open override func indexForAngle(_ angle: CGFloat) -> Int { // take the current angle of the chart into consideration - let a = ChartUtils.normalizedAngleFromAngle(angle - self.rotationAngle) + let a = (angle - self.rotationAngle).normalizedAngle for i in 0 ..< _absoluteAngles.count { if _absoluteAngles[i] > a diff --git a/Source/Charts/Charts/PieRadarChartViewBase.swift b/Source/Charts/Charts/PieRadarChartViewBase.swift index 4902529afa..8f2ab35e27 100755 --- a/Source/Charts/Charts/PieRadarChartViewBase.swift +++ b/Source/Charts/Charts/PieRadarChartViewBase.swift @@ -336,7 +336,7 @@ open class PieRadarChartViewBase: ChartViewBase set { _rawRotationAngle = newValue - _rotationAngle = ChartUtils.normalizedAngleFromAngle(newValue) + _rotationAngle = newValue.normalizedAngle setNeedsDisplay() } } diff --git a/Source/Charts/Charts/RadarChartView.swift b/Source/Charts/Charts/RadarChartView.swift index ed74f75194..b563bb0171 100644 --- a/Source/Charts/Charts/RadarChartView.swift +++ b/Source/Charts/Charts/RadarChartView.swift @@ -166,7 +166,7 @@ open class RadarChartView: PieRadarChartViewBase open override func indexForAngle(_ angle: CGFloat) -> Int { // take the current angle of the chart into consideration - let a = ChartUtils.normalizedAngleFromAngle(angle - self.rotationAngle) + let a = (angle - self.rotationAngle).normalizedAngle let sliceAngle = self.sliceAngle diff --git a/Source/Charts/Utils/ChartUtils.swift b/Source/Charts/Utils/ChartUtils.swift index 1275bfac9c..9c29f3d6cf 100755 --- a/Source/Charts/Utils/ChartUtils.swift +++ b/Source/Charts/Utils/ChartUtils.swift @@ -24,6 +24,14 @@ extension FloatingPoint { var rad2deg: Self { return self * 180 / .pi } + + /// - returns: An angle between 0.0 < 360.0 (not less than zero, less than 360) + /// NOTE: Value must be in degrees + var normalizedAngle: Self + { + let angle = truncatingRemainder(dividingBy: 360) + return (sign == .minus) ? angle + 360 : angle + } } open class ChartUtils @@ -250,20 +258,7 @@ open class ChartUtils let rect = text.boundingRect(with: constrainedToSize, options: .usesLineFragmentOrigin, attributes: attributes, context: nil) drawMultilineText(context: context, text: text, knownTextSize: rect.size, point: point, attributes: attributes, constrainedToSize: constrainedToSize, anchor: anchor, angleRadians: angleRadians) } - - /// - returns: An angle between 0.0 < 360.0 (not less than zero, less than 360) - internal class func normalizedAngleFromAngle(_ angle: CGFloat) -> CGFloat - { - var angle = angle - - while (angle < 0.0) - { - angle += 360.0 - } - - return angle.truncatingRemainder(dividingBy: 360.0) - } - + fileprivate class func generateDefaultValueFormatter() -> IValueFormatter { let formatter = DefaultValueFormatter(decimals: 1) From 82f54e435be072486c789fee2983f713de9f7015 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Wed, 15 Nov 2017 10:42:22 -0800 Subject: [PATCH 3/5] Fix after merge --- Source/Charts/Utils/ChartUtils.swift | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Source/Charts/Utils/ChartUtils.swift b/Source/Charts/Utils/ChartUtils.swift index 3599b529ae..ac5c6c05ae 100755 --- a/Source/Charts/Utils/ChartUtils.swift +++ b/Source/Charts/Utils/ChartUtils.swift @@ -27,14 +27,15 @@ extension FloatingPoint { /// - returns: An angle between 0.0 < 360.0 (not less than zero, less than 360) /// NOTE: Value must be in degrees - var normalizedAngle: Self - { + var normalizedAngle: Self { let angle = truncatingRemainder(dividingBy: 360) return (sign == .minus) ? angle + 360 : angle + } +} extension CGSize { func rotatedBy(degrees: CGFloat) -> CGSize { - let radians = ChartUtils.Math.FDEG2RAD * degrees + let radians = degrees.deg2rad return rotatedBy(radians: radians) } From 846494ababe0397df7fba6e54413527f3f7ccd0d Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Fri, 8 Dec 2017 09:50:30 -0400 Subject: [PATCH 4/5] Pulled latest master --- Source/Charts/Utils/ChartUtils.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) mode change 100755 => 100644 Source/Charts/Utils/ChartUtils.swift diff --git a/Source/Charts/Utils/ChartUtils.swift b/Source/Charts/Utils/ChartUtils.swift old mode 100755 new mode 100644 index ac5c6c05ae..cdb926ef40 --- a/Source/Charts/Utils/ChartUtils.swift +++ b/Source/Charts/Utils/ChartUtils.swift @@ -49,7 +49,7 @@ extension CGSize { open class ChartUtils { - fileprivate static var _defaultValueFormatter: IValueFormatter = ChartUtils.generateDefaultValueFormatter() + private static var _defaultValueFormatter: IValueFormatter = ChartUtils.generateDefaultValueFormatter() internal class func roundToNextSignificant(number: Double) -> Double { @@ -272,7 +272,7 @@ open class ChartUtils drawMultilineText(context: context, text: text, knownTextSize: rect.size, point: point, attributes: attributes, constrainedToSize: constrainedToSize, anchor: anchor, angleRadians: angleRadians) } - fileprivate class func generateDefaultValueFormatter() -> IValueFormatter + private class func generateDefaultValueFormatter() -> IValueFormatter { let formatter = DefaultValueFormatter(decimals: 1) return formatter From 3aba32db5bd87f5a250e72b85d3fbf34e5f7c9fe Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sat, 9 Dec 2017 15:29:34 -0400 Subject: [PATCH 5/5] Renamed deg2rad/rad2deg --- Source/Charts/Charts/PieChartView.swift | 4 +- .../Charts/Charts/PieRadarChartViewBase.swift | 12 ++-- .../Charts/Renderers/PieChartRenderer.swift | 68 +++++++++---------- Source/Charts/Renderers/XAxisRenderer.swift | 2 +- .../XAxisRendererHorizontalBarChart.swift | 2 +- .../Renderers/XAxisRendererRadarChart.swift | 2 +- Source/Charts/Utils/ChartUtils.swift | 10 +-- Source/Charts/Utils/Fill.swift | 2 +- 8 files changed, 51 insertions(+), 51 deletions(-) diff --git a/Source/Charts/Charts/PieChartView.swift b/Source/Charts/Charts/PieChartView.swift index 4c8bd25434..046253b424 100755 --- a/Source/Charts/Charts/PieChartView.swift +++ b/Source/Charts/Charts/PieChartView.swift @@ -175,8 +175,8 @@ open class PieChartView: PieRadarChartViewBase let offset = drawAngles[entryIndex] / 2.0 // calculate the text position - let x: CGFloat = (r * cos(((rotationAngle + absoluteAngles[entryIndex] - offset) * CGFloat(_animator.phaseY)).deg2rad) + center.x) - let y: CGFloat = (r * sin(((rotationAngle + absoluteAngles[entryIndex] - offset) * CGFloat(_animator.phaseY)).deg2rad) + center.y) + let x: CGFloat = (r * cos(((rotationAngle + absoluteAngles[entryIndex] - offset) * CGFloat(_animator.phaseY)).DEG2RAD) + center.x) + let y: CGFloat = (r * sin(((rotationAngle + absoluteAngles[entryIndex] - offset) * CGFloat(_animator.phaseY)).DEG2RAD) + center.y) return CGPoint(x: x, y: y) } diff --git a/Source/Charts/Charts/PieRadarChartViewBase.swift b/Source/Charts/Charts/PieRadarChartViewBase.swift index c29384d3d9..5d6f22fac8 100755 --- a/Source/Charts/Charts/PieRadarChartViewBase.swift +++ b/Source/Charts/Charts/PieRadarChartViewBase.swift @@ -255,7 +255,7 @@ open class PieRadarChartViewBase: ChartViewBase let length = sqrt(tx * tx + ty * ty) let r = acos(ty / length) - var angle = r.rad2deg + var angle = r.RAD2DEG if x > c.x { @@ -278,8 +278,8 @@ open class PieRadarChartViewBase: ChartViewBase /// from the center, and the angle of the position around the center. @objc open func getPosition(center: CGPoint, dist: CGFloat, angle: CGFloat) -> CGPoint { - return CGPoint(x: center.x + dist * cos(angle.deg2rad), - y: center.y + dist * sin(angle.deg2rad)) + return CGPoint(x: center.x + dist * cos(angle.DEG2RAD), + y: center.y + dist * sin(angle.DEG2RAD)) } /// - returns: The distance of a certain point on the chart to the center of the chart. @@ -835,14 +835,14 @@ open class PieRadarChartViewBase: ChartViewBase if recognizer.state == NSUIGestureRecognizerState.began || recognizer.state == NSUIGestureRecognizerState.changed { - let angle = recognizer.nsuiRotation.rad2deg + let angle = recognizer.nsuiRotation.RAD2DEG self.rotationAngle = _startAngle + angle setNeedsDisplay() } else if recognizer.state == NSUIGestureRecognizerState.ended { - let angle = recognizer.nsuiRotation.rad2deg + let angle = recognizer.nsuiRotation.RAD2DEG self.rotationAngle = _startAngle + angle setNeedsDisplay() @@ -851,7 +851,7 @@ open class PieRadarChartViewBase: ChartViewBase { stopDeceleration() - _decelerationAngularVelocity = recognizer.velocity.rad2deg + _decelerationAngularVelocity = recognizer.velocity.RAD2DEG if _decelerationAngularVelocity != 0.0 { diff --git a/Source/Charts/Renderers/PieChartRenderer.swift b/Source/Charts/Renderers/PieChartRenderer.swift index 8db68a3a2e..a363da290d 100644 --- a/Source/Charts/Renderers/PieChartRenderer.swift +++ b/Source/Charts/Renderers/PieChartRenderer.swift @@ -58,12 +58,12 @@ open class PieChartRenderer: DataRenderer let angleMiddle = startAngle + sweepAngle / 2.0 // Other point of the arc - let arcEndPointX = center.x + radius * cos((startAngle + sweepAngle).deg2rad) - let arcEndPointY = center.y + radius * sin((startAngle + sweepAngle).deg2rad) + let arcEndPointX = center.x + radius * cos((startAngle + sweepAngle).DEG2RAD) + let arcEndPointY = center.y + radius * sin((startAngle + sweepAngle).DEG2RAD) // Middle point on the arc - let arcMidPointX = center.x + radius * cos(angleMiddle.deg2rad) - let arcMidPointY = center.y + radius * sin(angleMiddle.deg2rad) + let arcMidPointX = center.x + radius * cos(angleMiddle.DEG2RAD) + let arcMidPointY = center.y + radius * sin(angleMiddle.DEG2RAD) // This is the base of the contained triangle let basePointsDistance = sqrt( @@ -74,7 +74,7 @@ open class PieChartRenderer: DataRenderer // the angle of the contained triangle should stay the same. // So let's find out the height of that triangle. let containedTriangleHeight = (basePointsDistance / 2.0 * - tan((180.0 - angle).deg2rad / 2.0)) + tan((180.0 - angle).DEG2RAD / 2.0)) // Now we subtract that from the radius var spacedRadius = radius - containedTriangleHeight @@ -154,7 +154,7 @@ open class PieChartRenderer: DataRenderer let sliceSpaceAngleOuter = visibleAngleCount == 1 ? 0.0 : - sliceSpace / radius.deg2rad + sliceSpace / radius.DEG2RAD let startAngleOuter = rotationAngle + (angle + sliceSpaceAngleOuter / 2.0) * CGFloat(phaseY) var sweepAngleOuter = (sliceAngle - sliceSpaceAngleOuter) * CGFloat(phaseY) if sweepAngleOuter < 0.0 @@ -162,15 +162,15 @@ open class PieChartRenderer: DataRenderer sweepAngleOuter = 0.0 } - let arcStartPointX = center.x + radius * cos(startAngleOuter.deg2rad) - let arcStartPointY = center.y + radius * sin(startAngleOuter.deg2rad) + let arcStartPointX = center.x + radius * cos(startAngleOuter.DEG2RAD) + let arcStartPointY = center.y + radius * sin(startAngleOuter.DEG2RAD) let path = CGMutablePath() path.move(to: CGPoint(x: arcStartPointX, y: arcStartPointY)) - path.addRelativeArc(center: center, radius: radius, startAngle: startAngleOuter.deg2rad, delta: sweepAngleOuter.deg2rad) + path.addRelativeArc(center: center, radius: radius, startAngle: startAngleOuter.DEG2RAD, delta: sweepAngleOuter.DEG2RAD) if drawInnerArc && (innerRadius > 0.0 || accountForSliceSpacing) @@ -194,7 +194,7 @@ open class PieChartRenderer: DataRenderer let sliceSpaceAngleInner = visibleAngleCount == 1 || innerRadius == 0.0 ? 0.0 : - sliceSpace / innerRadius.deg2rad + sliceSpace / innerRadius.DEG2RAD let startAngleInner = rotationAngle + (angle + sliceSpaceAngleInner / 2.0) * CGFloat(phaseY) var sweepAngleInner = (sliceAngle - sliceSpaceAngleInner) * CGFloat(phaseY) if sweepAngleInner < 0.0 @@ -205,10 +205,10 @@ open class PieChartRenderer: DataRenderer path.addLine( to: CGPoint( - x: center.x + innerRadius * cos(endAngleInner.deg2rad), - y: center.y + innerRadius * sin(endAngleInner.deg2rad))) + 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) + path.addRelativeArc(center: center, radius: innerRadius, startAngle: endAngleInner.DEG2RAD, delta: -sweepAngleInner.DEG2RAD) } else { @@ -226,8 +226,8 @@ open class PieChartRenderer: DataRenderer startAngle: startAngleOuter, sweepAngle: sweepAngleOuter) - let arcEndPointX = center.x + sliceSpaceOffset * cos(angleMiddle.deg2rad) - let arcEndPointY = center.y + sliceSpaceOffset * sin(angleMiddle.deg2rad) + let arcEndPointX = center.x + sliceSpaceOffset * cos(angleMiddle.DEG2RAD) + let arcEndPointY = center.y + sliceSpaceOffset * sin(angleMiddle.DEG2RAD) path.addLine( to: CGPoint( @@ -334,7 +334,7 @@ open class PieChartRenderer: DataRenderer let sliceAngle = drawAngles[xIndex] let sliceSpace = getSliceSpace(dataSet: dataSet) - let sliceSpaceMiddleAngle = sliceSpace / labelRadius.deg2rad + let sliceSpaceMiddleAngle = sliceSpace / labelRadius.DEG2RAD // offset needed to center the drawn text in the slice let angleOffset = (sliceAngle - sliceSpaceMiddleAngle / 2.0) / 2.0 @@ -350,8 +350,8 @@ open class PieChartRenderer: DataRenderer dataSetIndex: i, viewPortHandler: viewPortHandler) - let sliceXBase = cos(transformedAngle.deg2rad) - let sliceYBase = sin(transformedAngle.deg2rad) + let sliceXBase = cos(transformedAngle.DEG2RAD) + let sliceYBase = sin(transformedAngle.DEG2RAD) let drawXOutside = drawEntryLabels && xValuePosition == .outsideSlice let drawYOutside = drawValues && yValuePosition == .outsideSlice @@ -383,7 +383,7 @@ open class PieChartRenderer: DataRenderer } let polyline2Length = dataSet.valueLineVariableLength - ? labelRadius * valueLineLength2 * abs(sin(transformedAngle.deg2rad)) + ? labelRadius * valueLineLength2 * abs(sin(transformedAngle.DEG2RAD)) : labelRadius * valueLineLength2 let pt0 = CGPoint( @@ -728,11 +728,11 @@ open class PieChartRenderer: DataRenderer let sliceSpaceAngleOuter = visibleAngleCount == 1 ? 0.0 : - sliceSpace / radius.deg2rad + sliceSpace / radius.DEG2RAD let sliceSpaceAngleShifted = visibleAngleCount == 1 ? 0.0 : - sliceSpace / highlightedRadius.deg2rad + sliceSpace / highlightedRadius.DEG2RAD let startAngleOuter = rotationAngle + (angle + sliceSpaceAngleOuter / 2.0) * CGFloat(phaseY) var sweepAngleOuter = (sliceAngle - sliceSpaceAngleOuter) * CGFloat(phaseY) @@ -750,11 +750,11 @@ open class PieChartRenderer: DataRenderer let path = CGMutablePath() - path.move(to: CGPoint(x: center.x + highlightedRadius * cos(startAngleShifted.deg2rad), - y: center.y + highlightedRadius * sin(startAngleShifted.deg2rad))) + path.move(to: CGPoint(x: center.x + highlightedRadius * cos(startAngleShifted.DEG2RAD), + y: center.y + highlightedRadius * sin(startAngleShifted.DEG2RAD))) - path.addRelativeArc(center: center, radius: highlightedRadius, startAngle: startAngleShifted.deg2rad, - delta: sweepAngleShifted.deg2rad) + path.addRelativeArc(center: center, radius: highlightedRadius, startAngle: startAngleShifted.DEG2RAD, + delta: sweepAngleShifted.DEG2RAD) var sliceSpaceRadius: CGFloat = 0.0 if accountForSliceSpacing @@ -763,8 +763,8 @@ open class PieChartRenderer: DataRenderer center: center, radius: radius, angle: sliceAngle * CGFloat(phaseY), - arcStartPointX: center.x + radius * cos(startAngleOuter.deg2rad), - arcStartPointY: center.y + radius * sin(startAngleOuter.deg2rad), + arcStartPointX: center.x + radius * cos(startAngleOuter.DEG2RAD), + arcStartPointY: center.y + radius * sin(startAngleOuter.DEG2RAD), startAngle: startAngleOuter, sweepAngle: sweepAngleOuter) } @@ -784,7 +784,7 @@ open class PieChartRenderer: DataRenderer let sliceSpaceAngleInner = visibleAngleCount == 1 || innerRadius == 0.0 ? 0.0 : - sliceSpace / innerRadius.deg2rad + sliceSpace / innerRadius.DEG2RAD let startAngleInner = rotationAngle + (angle + sliceSpaceAngleInner / 2.0) * CGFloat(phaseY) var sweepAngleInner = (sliceAngle - sliceSpaceAngleInner) * CGFloat(phaseY) if sweepAngleInner < 0.0 @@ -795,12 +795,12 @@ open class PieChartRenderer: DataRenderer path.addLine( to: CGPoint( - x: center.x + innerRadius * cos(endAngleInner.deg2rad), - y: center.y + innerRadius * sin(endAngleInner.deg2rad))) + 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) + startAngle: endAngleInner.DEG2RAD, + delta: -sweepAngleInner.DEG2RAD) } else { @@ -808,8 +808,8 @@ open class PieChartRenderer: DataRenderer { let angleMiddle = startAngleOuter + sweepAngleOuter / 2.0 - let arcEndPointX = center.x + sliceSpaceRadius * cos(angleMiddle.deg2rad) - let arcEndPointY = center.y + sliceSpaceRadius * sin(angleMiddle.deg2rad) + let arcEndPointX = center.x + sliceSpaceRadius * cos(angleMiddle.DEG2RAD) + let arcEndPointY = center.y + sliceSpaceRadius * sin(angleMiddle.DEG2RAD) path.addLine( to: CGPoint( diff --git a/Source/Charts/Renderers/XAxisRenderer.swift b/Source/Charts/Renderers/XAxisRenderer.swift index 5a73183b16..534e606dcf 100644 --- a/Source/Charts/Renderers/XAxisRenderer.swift +++ b/Source/Charts/Renderers/XAxisRenderer.swift @@ -182,7 +182,7 @@ open class XAxisRenderer: AxisRendererBase let labelAttrs: [NSAttributedStringKey : Any] = [NSAttributedStringKey.font: xAxis.labelFont, NSAttributedStringKey.foregroundColor: xAxis.labelTextColor, NSAttributedStringKey.paragraphStyle: paraStyle] - let labelRotationAngleRadians = xAxis.labelRotationAngle.deg2rad + let labelRotationAngleRadians = xAxis.labelRotationAngle.DEG2RAD let centeringEnabled = xAxis.isCenterAxisLabelsEnabled diff --git a/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift b/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift index eec2c3cca4..d2e0c29306 100644 --- a/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift +++ b/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift @@ -122,7 +122,7 @@ open class XAxisRendererHorizontalBarChart: XAxisRenderer let labelFont = xAxis.labelFont let labelTextColor = xAxis.labelTextColor - let labelRotationAngleRadians = xAxis.labelRotationAngle.deg2rad + let labelRotationAngleRadians = xAxis.labelRotationAngle.DEG2RAD let centeringEnabled = xAxis.isCenterAxisLabelsEnabled diff --git a/Source/Charts/Renderers/XAxisRendererRadarChart.swift b/Source/Charts/Renderers/XAxisRendererRadarChart.swift index f399a3b17f..ab57149cce 100644 --- a/Source/Charts/Renderers/XAxisRendererRadarChart.swift +++ b/Source/Charts/Renderers/XAxisRendererRadarChart.swift @@ -41,7 +41,7 @@ open class XAxisRendererRadarChart: XAxisRenderer let labelFont = xAxis.labelFont let labelTextColor = xAxis.labelTextColor - let labelRotationAngleRadians = xAxis.labelRotationAngle.rad2deg + let labelRotationAngleRadians = xAxis.labelRotationAngle.RAD2DEG let drawLabelAnchor = CGPoint(x: 0.5, y: 0.25) let sliceangle = chart.sliceAngle diff --git a/Source/Charts/Utils/ChartUtils.swift b/Source/Charts/Utils/ChartUtils.swift index cdb926ef40..d5b441e164 100644 --- a/Source/Charts/Utils/ChartUtils.swift +++ b/Source/Charts/Utils/ChartUtils.swift @@ -17,11 +17,11 @@ import CoreGraphics #endif extension FloatingPoint { - var deg2rad: Self { + var DEG2RAD: Self { return self * .pi / 180 } - var rad2deg: Self { + var RAD2DEG: Self { return self * 180 / .pi } @@ -35,7 +35,7 @@ extension FloatingPoint { extension CGSize { func rotatedBy(degrees: CGFloat) -> CGSize { - let radians = degrees.deg2rad + let radians = degrees.DEG2RAD return rotatedBy(radians: radians) } @@ -98,8 +98,8 @@ open class ChartUtils internal class func getPosition(center: CGPoint, dist: CGFloat, angle: CGFloat) -> CGPoint { return CGPoint( - x: center.x + dist * cos(angle.deg2rad), - y: center.y + dist * sin(angle.deg2rad) + x: center.x + dist * cos(angle.DEG2RAD), + y: center.y + dist * sin(angle.DEG2RAD) ) } diff --git a/Source/Charts/Utils/Fill.swift b/Source/Charts/Utils/Fill.swift index 22ed08c855..1294b3efc0 100644 --- a/Source/Charts/Utils/Fill.swift +++ b/Source/Charts/Utils/Fill.swift @@ -273,7 +273,7 @@ open class Fill: NSObject case .linearGradient: - let radians = (360.0 - _gradientAngle).deg2rad + let radians = (360.0 - _gradientAngle).DEG2RAD let centerPoint = CGPoint(x: rect.midX, y: rect.midY) let xAngleDelta = cos(radians) * rect.width / 2.0 let yAngleDelta = sin(radians) * rect.height / 2.0