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 @@ -158,23 +158,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 @@ -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
Expand All @@ -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
{
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
}
Expand All @@ -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
Expand All @@ -804,21 +804,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 @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,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
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 {
Copy link
Member

Choose a reason for hiding this comment

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

Generally it's fine. But do you think we should follow the Camel-Case naming? like deg2Rad

Copy link
Collaborator Author

@jjatie jjatie Dec 8, 2017

Choose a reason for hiding this comment

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

Up to you. I tried deg2Rad and deg2RAD and they both look silly to me. I also considered inRadians and inDegrees

Copy link
Member

Choose a reason for hiding this comment

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

OK. How about we keep old FDEG2RAD to keep consitent?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll drop the F since it's implied as it's an extension on FloatingPoint

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
{
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
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)
}


fileprivate class func generateDefaultValueFormatter() -> IValueFormatter
{
let formatter = DefaultValueFormatter(decimals: 1)
Expand Down
Loading