diff --git a/Source/Charts/Charts/PieChartView.swift b/Source/Charts/Charts/PieChartView.swift index d67ac92b99..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)) * 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) } @@ -267,7 +267,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 2480556e58..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 * 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. @@ -336,7 +336,7 @@ open class PieRadarChartViewBase: ChartViewBase set { _rawRotationAngle = newValue - _rotationAngle = ChartUtils.normalizedAngleFromAngle(newValue) + _rotationAngle = newValue.normalizedAngle setNeedsDisplay() } } @@ -835,14 +835,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() @@ -851,7 +851,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/Charts/RadarChartView.swift b/Source/Charts/Charts/RadarChartView.swift index 766cdfb1af..2182df6e15 100644 --- a/Source/Charts/Charts/RadarChartView.swift +++ b/Source/Charts/Charts/RadarChartView.swift @@ -163,7 +163,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/Renderers/PieChartRenderer.swift b/Source/Charts/Renderers/PieChartRenderer.swift index b877652c96..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) * 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 @@ -154,7 +154,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 @@ -162,15 +162,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) @@ -194,7 +194,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 @@ -205,10 +205,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 { @@ -226,8 +226,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( @@ -334,7 +334,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 @@ -350,8 +350,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 @@ -383,7 +383,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( @@ -728,11 +728,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) @@ -750,11 +750,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 @@ -763,8 +763,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) } @@ -784,7 +784,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 @@ -795,12 +795,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 { @@ -808,8 +808,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 0c84cf4aef..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 * 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 8faf863771..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 * 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 8b98791c23..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 * 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 old mode 100755 new mode 100644 index 3169e1b75c..d5b441e164 --- a/Source/Charts/Utils/ChartUtils.swift +++ b/Source/Charts/Utils/ChartUtils.swift @@ -16,9 +16,26 @@ import CoreGraphics import UIKit #endif +extension FloatingPoint { + var DEG2RAD: Self { + return self * .pi / 180 + } + + 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 + } +} + extension CGSize { func rotatedBy(degrees: CGFloat) -> CGSize { - let radians = ChartUtils.Math.FDEG2RAD * degrees + let radians = degrees.DEG2RAD return rotatedBy(radians: radians) } @@ -33,15 +50,7 @@ extension CGSize { open class ChartUtils { private 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 @@ -89,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 * Math.FDEG2RAD), - y: center.y + dist * sin(angle * Math.FDEG2RAD) + x: center.x + dist * cos(angle.DEG2RAD), + y: center.y + dist * sin(angle.DEG2RAD) ) } @@ -262,20 +271,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) - } - + private class func generateDefaultValueFormatter() -> IValueFormatter { let formatter = DefaultValueFormatter(decimals: 1) diff --git a/Source/Charts/Utils/Fill.swift b/Source/Charts/Utils/Fill.swift index 88f6f71109..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 = 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