Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced ChartUtils.Math in favour of an extension on FloatingPoint #2993

Merged
merged 8 commits into from
Dec 11, 2017
Merged
6 changes: 3 additions & 3 deletions Source/Charts/Charts/PieChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions Source/Charts/Charts/PieRadarChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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.
Expand Down Expand Up @@ -336,7 +336,7 @@ open class PieRadarChartViewBase: ChartViewBase
set
{
_rawRotationAngle = newValue
_rotationAngle = ChartUtils.normalizedAngleFromAngle(newValue)
_rotationAngle = newValue.normalizedAngle
setNeedsDisplay()
}
}
Expand Down Expand Up @@ -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()
Expand All @@ -851,7 +851,7 @@ open class PieRadarChartViewBase: ChartViewBase
{
stopDeceleration()

_decelerationAngularVelocity = ChartUtils.Math.FRAD2DEG * recognizer.velocity
_decelerationAngularVelocity = recognizer.velocity.RAD2DEG

if _decelerationAngularVelocity != 0.0
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Charts/RadarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
68 changes: 34 additions & 34 deletions Source/Charts/Renderers/PieChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -154,23 +154,23 @@ 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
{
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)
Expand All @@ -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
Expand All @@ -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
{
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
}
Expand All @@ -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
Expand All @@ -795,21 +795,21 @@ 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
{
if accountForSliceSpacing
{
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(
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Renderers/XAxisRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Renderers/XAxisRendererRadarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 22 additions & 26 deletions Source/Charts/Utils/ChartUtils.swift
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the order is flipped. I need to refresh my mind first to see if they are equivalent. Or you already did that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They’re equivalent.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made some stupid equations :P

}
}

extension CGSize {
func rotatedBy(degrees: CGFloat) -> CGSize {
let radians = ChartUtils.Math.FDEG2RAD * degrees
let radians = degrees.DEG2RAD
return rotatedBy(radians: radians)
}

Expand All @@ -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
Expand Down Expand Up @@ -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)
)
}

Expand Down Expand Up @@ -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)
Expand Down
Loading