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

Refactored ChartUtils method into CGPoint extension #3087

Merged
merged 4 commits into from
Jan 3, 2018
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: 0 additions & 2 deletions Source/Charts/Data/Implementations/ChartBaseDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -422,5 +422,3 @@ open class ChartBaseDataSet: NSObject, IChartDataSet
return copy
}
}


6 changes: 2 additions & 4 deletions Source/Charts/Highlight/RadarHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,8 @@ open class RadarHighlighter: PieRadarHighlighter

let y = (entry.y - chart.chartYMin)

let p = ChartUtils.getPosition(
center: chart.centerOffsets,
dist: CGFloat(y) * factor * CGFloat(phaseY),
angle: sliceangle * CGFloat(index) * CGFloat(phaseX) + chart.rotationAngle)
let p = chart.centerOffsets.moving(distance: CGFloat(y) * factor * CGFloat(phaseY),
atAngle: sliceangle * CGFloat(index) * CGFloat(phaseX) + chart.rotationAngle)

vals.append(Highlight(x: Double(index), y: entry.y, xPx: p.x, yPx: p.y, dataSetIndex: i, axis: dataSet.axisDependency))
}
Expand Down
34 changes: 12 additions & 22 deletions Source/Charts/Renderers/RadarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@ open class RadarChartRenderer: LineRadarRenderer
{
guard let e = dataSet.entryForIndex(j) else { continue }

let p = ChartUtils.getPosition(
center: center,
dist: CGFloat((e.y - chart.chartYMin) * Double(factor) * phaseY),
angle: sliceangle * CGFloat(j) * CGFloat(phaseX) + chart.rotationAngle)
let p = center.moving(distance: CGFloat((e.y - chart.chartYMin) * Double(factor) * phaseY),
atAngle: sliceangle * CGFloat(j) * CGFloat(phaseX) + chart.rotationAngle)

if p.x.isNaN
{
Expand Down Expand Up @@ -170,10 +168,8 @@ open class RadarChartRenderer: LineRadarRenderer
{
guard let e = dataSet.entryForIndex(j) else { continue }

let p = ChartUtils.getPosition(
center: center,
dist: CGFloat(e.y - chart.chartYMin) * factor * CGFloat(phaseY),
angle: sliceangle * CGFloat(j) * CGFloat(phaseX) + chart.rotationAngle)
let p = center.moving(distance: CGFloat(e.y - chart.chartYMin) * factor * CGFloat(phaseY),
atAngle: sliceangle * CGFloat(j) * CGFloat(phaseX) + chart.rotationAngle)

let valueFont = dataSet.valueFont

Expand All @@ -197,10 +193,8 @@ open class RadarChartRenderer: LineRadarRenderer

if let icon = e.icon, dataSet.isDrawIconsEnabled
{
var pIcon = ChartUtils.getPosition(
center: center,
dist: CGFloat(e.y) * factor * CGFloat(phaseY) + iconsOffset.y,
angle: sliceangle * CGFloat(j) * CGFloat(phaseX) + chart.rotationAngle)
var pIcon = center.moving(distance: CGFloat(e.y) * factor * CGFloat(phaseY) + iconsOffset.y,
atAngle: sliceangle * CGFloat(j) * CGFloat(phaseX) + chart.rotationAngle)
pIcon.y += iconsOffset.x

ChartUtils.drawImage(context: context,
Expand Down Expand Up @@ -248,10 +242,8 @@ open class RadarChartRenderer: LineRadarRenderer

for i in stride(from: 0, to: maxEntryCount, by: xIncrements)
{
let p = ChartUtils.getPosition(
center: center,
dist: CGFloat(chart.yRange) * factor,
angle: sliceangle * CGFloat(i) + rotationangle)
let p = center.moving(distance: CGFloat(chart.yRange) * factor,
atAngle: sliceangle * CGFloat(i) + rotationangle)

_webLineSegmentsBuffer[0].x = center.x
_webLineSegmentsBuffer[0].y = center.y
Expand All @@ -274,8 +266,8 @@ open class RadarChartRenderer: LineRadarRenderer
{
let r = CGFloat(chart.yAxis.entries[j] - chart.chartYMin) * factor

let p1 = ChartUtils.getPosition(center: center, dist: r, angle: sliceangle * CGFloat(i) + rotationangle)
let p2 = ChartUtils.getPosition(center: center, dist: r, angle: sliceangle * CGFloat(i + 1) + rotationangle)
let p1 = center.moving(distance: r, atAngle: sliceangle * CGFloat(i) + rotationangle)
let p2 = center.moving(distance: r, atAngle: sliceangle * CGFloat(i + 1) + rotationangle)

_webLineSegmentsBuffer[0].x = p1.x
_webLineSegmentsBuffer[0].y = p1.y
Expand Down Expand Up @@ -336,10 +328,8 @@ open class RadarChartRenderer: LineRadarRenderer

let y = e.y - chart.chartYMin

_highlightPointBuffer = ChartUtils.getPosition(
center: center,
dist: CGFloat(y) * factor * CGFloat(animator.phaseY),
angle: sliceangle * CGFloat(high.x) * CGFloat(animator.phaseX) + chart.rotationAngle)
_highlightPointBuffer = center.moving(distance: CGFloat(y) * factor * CGFloat(animator.phaseY),
atAngle: sliceangle * CGFloat(high.x) * CGFloat(animator.phaseX) + chart.rotationAngle)

high.setDraw(pt: _highlightPointBuffer)

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 @@ -58,7 +58,7 @@ open class XAxisRendererRadarChart: XAxisRenderer

let angle = (sliceangle * CGFloat(i) + chart.rotationAngle).truncatingRemainder(dividingBy: 360.0)

let p = ChartUtils.getPosition(center: center, dist: CGFloat(chart.yRange) * factor + xAxis.labelRotatedWidth / 2.0, angle: angle)
let p = center.moving(distance: CGFloat(chart.yRange) * factor + xAxis.labelRotatedWidth / 2.0, atAngle: angle)

drawLabel(context: context,
formattedLabel: label,
Expand Down
4 changes: 2 additions & 2 deletions Source/Charts/Renderers/YAxisRendererRadarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ open class YAxisRendererRadarChart: YAxisRenderer
{
let r = CGFloat(yAxis.entries[j] - yAxis._axisMinimum) * factor

let p = ChartUtils.getPosition(center: center, dist: r, angle: chart.rotationAngle)
let p = center.moving(distance: r, atAngle: chart.rotationAngle)

let label = yAxis.getFormattedLabel(j)

Expand Down Expand Up @@ -252,7 +252,7 @@ open class YAxisRendererRadarChart: YAxisRenderer

for j in 0 ..< (data.maxEntryCountSet?.entryCount ?? 0)
{
let p = ChartUtils.getPosition(center: center, dist: r, angle: sliceangle * CGFloat(j) + chart.rotationAngle)
let p = center.moving(distance: r, atAngle: sliceangle * CGFloat(j) + chart.rotationAngle)

if j == 0
{
Expand Down
51 changes: 29 additions & 22 deletions Source/Charts/Utils/ChartUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,49 @@
import Foundation
import CoreGraphics

#if !os(OSX)
Copy link
Member

Choose a reason for hiding this comment

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

why delete this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

UIKit is not used in the file.

import UIKit
#endif

extension FloatingPoint {
var DEG2RAD: Self {
extension FloatingPoint
{
var DEG2RAD: Self
{
return self * .pi / 180
}

var RAD2DEG: Self {
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 {
var normalizedAngle: Self
{
let angle = truncatingRemainder(dividingBy: 360)
return (sign == .minus) ? angle + 360 : angle
}
}

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

func rotatedBy(radians: CGFloat) -> CGSize {
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))
)
}
}

extension Double {
extension Double
{
/// Rounds the number to the nearest multiple of it's order of magnitude, rounding away from zero if halfway.
func roundedToNextSignficant() -> Double {
func roundedToNextSignficant() -> Double
{
guard
!isInfinite,
!isNaN,
Expand All @@ -63,7 +68,8 @@ extension Double {
return shifted / magnitude
}

var decimalPlaces: Int {
var decimalPlaces: Int
{
guard
!isNaN,
!isInfinite,
Expand All @@ -81,18 +87,19 @@ extension Double {
}
}

open class ChartUtils
extension CGPoint
{
private static var _defaultValueFormatter: IValueFormatter = ChartUtils.generateDefaultValueFormatter()

/// Calculates the position around a center point, depending on the distance from the center, and the angle of the position around the center.
internal class func getPosition(center: CGPoint, dist: CGFloat, angle: CGFloat) -> CGPoint
func moving(distance: CGFloat, atAngle angle: CGFloat) -> CGPoint
{
return CGPoint(
x: center.x + dist * cos(angle.DEG2RAD),
y: center.y + dist * sin(angle.DEG2RAD)
)
return CGPoint(x: x + distance * cos(angle.DEG2RAD),
y: y + distance * sin(angle.DEG2RAD))
}
}

open class ChartUtils
{
private static var _defaultValueFormatter: IValueFormatter = ChartUtils.generateDefaultValueFormatter()

open class func drawImage(
context: CGContext,
Expand Down