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 methods with CGSize extensions #2995

Merged
merged 1 commit into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/Charts/Renderers/XAxisRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ open class XAxisRenderer: AxisRendererBase
let labelWidth = labelSize.width
let labelHeight = labelSize.height

let labelRotatedSize = ChartUtils.sizeOfRotatedRectangle(labelSize, degrees: xAxis.labelRotationAngle)
let labelRotatedSize = labelSize.rotatedBy(degrees: xAxis.labelRotationAngle)

xAxis.labelWidth = labelWidth
xAxis.labelHeight = labelHeight
Expand Down
5 changes: 2 additions & 3 deletions Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ open class XAxisRendererHorizontalBarChart: XAxisRenderer

let labelWidth = floor(labelSize.width + xAxis.xOffset * 3.5)
let labelHeight = labelSize.height

let labelRotatedSize = ChartUtils.sizeOfRotatedRectangle(rectangleWidth: labelSize.width, rectangleHeight: labelHeight, degrees: xAxis.labelRotationAngle)

let labelRotatedSize = CGSize(width: labelSize.width, height: labelHeight).rotatedBy(degrees: xAxis.labelRotationAngle)

xAxis.labelWidth = labelWidth
xAxis.labelHeight = labelHeight
xAxis.labelRotatedWidth = round(labelRotatedSize.width + xAxis.xOffset * 3.5)
Expand Down
45 changes: 17 additions & 28 deletions Source/Charts/Utils/ChartUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ import CoreGraphics
import UIKit
#endif

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

func rotatedBy(radians: CGFloat) -> CGSize {
return CGSize(
width: abs(width * cos(radians)) + abs(height * sin(radians)),
height: abs(width * sin(radians)) + abs(height * cos(radians))
)
}
}

open class ChartUtils
{
fileprivate static var _defaultValueFormatter: IValueFormatter = ChartUtils.generateDefaultValueFormatter()
Expand Down Expand Up @@ -162,7 +176,7 @@ open class ChartUtils
// Move the "outer" rect relative to the anchor, assuming its centered
if anchor.x != 0.5 || anchor.y != 0.5
{
let rotatedSize = sizeOfRotatedRectangle(size, radians: angleRadians)
let rotatedSize = size.rotatedBy(radians: angleRadians)

translate.x -= rotatedSize.width * (anchor.x - 0.5)
translate.y -= rotatedSize.height * (anchor.y - 0.5)
Expand Down Expand Up @@ -212,7 +226,7 @@ open class ChartUtils
// Move the "outer" rect relative to the anchor, assuming its centered
if anchor.x != 0.5 || anchor.y != 0.5
{
let rotatedSize = sizeOfRotatedRectangle(knownTextSize, radians: angleRadians)
let rotatedSize = knownTextSize.rotatedBy(radians: angleRadians)

translate.x -= rotatedSize.width * (anchor.x - 0.5)
translate.y -= rotatedSize.height * (anchor.y - 0.5)
Expand Down Expand Up @@ -273,32 +287,7 @@ open class ChartUtils
{
return _defaultValueFormatter
}

internal class func sizeOfRotatedRectangle(_ rectangleSize: CGSize, degrees: CGFloat) -> CGSize
{
let radians = degrees * Math.FDEG2RAD
return sizeOfRotatedRectangle(rectangleWidth: rectangleSize.width, rectangleHeight: rectangleSize.height, radians: radians)
}

internal class func sizeOfRotatedRectangle(_ rectangleSize: CGSize, radians: CGFloat) -> CGSize
{
return sizeOfRotatedRectangle(rectangleWidth: rectangleSize.width, rectangleHeight: rectangleSize.height, radians: radians)
}

internal class func sizeOfRotatedRectangle(rectangleWidth: CGFloat, rectangleHeight: CGFloat, degrees: CGFloat) -> CGSize
{
let radians = degrees * Math.FDEG2RAD
return sizeOfRotatedRectangle(rectangleWidth: rectangleWidth, rectangleHeight: rectangleHeight, radians: radians)
}

internal class func sizeOfRotatedRectangle(rectangleWidth: CGFloat, rectangleHeight: CGFloat, radians: CGFloat) -> CGSize
{
return CGSize(
width: abs(rectangleWidth * cos(radians)) + abs(rectangleHeight * sin(radians)),
height: abs(rectangleWidth * sin(radians)) + abs(rectangleHeight * cos(radians))
)
}


/// MARK: - Bridging functions

internal class func bridgedObjCGetNSUIColorArray (swift array: [NSUIColor?]) -> [NSObject]
Expand Down