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

Migrating to built-in algorithms #3892

Merged
merged 1 commit into from
Mar 9, 2019
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/Charts/ChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
/// - Returns: `true` if there are values to highlight, `false` ifthere are no values to highlight.
@objc open func valuesToHighlight() -> Bool
{
return _indicesToHighlight.count > 0
return !_indicesToHighlight.isEmpty
}

/// Highlights the values at the given indices in the given DataSets. Provide
Expand Down
54 changes: 10 additions & 44 deletions Source/Charts/Charts/PieChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,12 @@ open class PieChartView: PieRadarChartViewBase
_absoluteAngles.reserveCapacity(entryCount)

let yValueSum = (_data as! PieChartData).yValueSum

var dataSets = data.dataSets

var cnt = 0

for i in 0 ..< data.dataSetCount
for set in data.dataSets
{
let set = dataSets[i]
let entryCount = set.entryCount

for j in 0 ..< entryCount
for j in 0 ..< set.entryCount
{
guard let e = set.entryForIndex(j) else { continue }

Expand All @@ -224,22 +219,7 @@ open class PieChartView: PieRadarChartViewBase
/// Checks if the given index is set to be highlighted.
@objc open func needsHighlight(index: Int) -> Bool
{
// no highlight
if !valuesToHighlight()
{
return false
}

for i in 0 ..< _indicesToHighlight.count
{
// check if the xvalue for the given dataset needs highlight
if Int(_indicesToHighlight[i].x) == index
{
return true
}
}

return false
return _indicesToHighlight.contains { Int($0.x) == index }
}

/// calculates the needed angle for a given value
Expand All @@ -259,36 +239,22 @@ open class PieChartView: PieRadarChartViewBase
{
fatalError("PieChart has no XAxis")
}

open override func indexForAngle(_ angle: CGFloat) -> Int
{
// TODO: Return nil instead of -1
// take the current angle of the chart into consideration
let a = (angle - self.rotationAngle).normalizedAngle
for i in 0 ..< _absoluteAngles.count
{
if _absoluteAngles[i] > a
{
return i
}
}

return -1 // return -1 if no index found
return _absoluteAngles.firstIndex { $0 > a } ?? -1
}

/// - Returns: The index of the DataSet this x-index belongs to.
@objc open func dataSetIndexForIndex(_ xValue: Double) -> Int
{
var dataSets = _data?.dataSets ?? []

for i in 0 ..< dataSets.count
{
if (dataSets[i].entryForXValue(xValue, closestToY: Double.nan) !== nil)
{
return i
}
}

return -1
// TODO: Return nil instead of -1
return _data?.dataSets.firstIndex {
$0.entryForXValue(xValue, closestToY: .nan) != nil
} ?? -1
}

/// - Returns: An integer array of all the different angles the chart slices
Expand Down
18 changes: 3 additions & 15 deletions Source/Charts/Charts/RadarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,21 +168,9 @@ open class RadarChartView: PieRadarChartViewBase
let sliceAngle = self.sliceAngle

let max = _data?.maxEntryCountSet?.entryCount ?? 0

var index = 0

for i in 0..<max
{
let referenceAngle = sliceAngle * CGFloat(i + 1) - sliceAngle / 2.0

if referenceAngle > a
{
index = i
break
}
}

return index
return (0..<max).firstIndex {
sliceAngle * CGFloat($0 + 1) - sliceAngle / 2.0 > a
} ?? max
Copy link
Member

Choose a reason for hiding this comment

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

@jjatie while I am working merging 4.0, I found this imp is different than the old one.

The old one would return 0 if no match, but the new logic returns max.

Is this a mistake or intended?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

My mistake. Should be ?? 0
It might be a bit more readable to write it as:

return (0..<max).firstIndex {
    let referenceAngle = sliceAngle * CGFloat($0 + 1) - sliceAngle / 2.0
    return referenceAngle > a
} ?? 0

Copy link
Member

Choose a reason for hiding this comment

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

I have changed to ?? 0.
I will let you know when I resolve all conflicts and we could continue from there.

}

/// The object that represents all y-labels of the RadarChart.
Expand Down
10 changes: 2 additions & 8 deletions Source/Charts/Components/AxisBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,8 @@ open class AxisBase: ComponentBase
/// Removes the specified ChartLimitLine from the axis.
@objc open func removeLimitLine(_ line: ChartLimitLine)
{
for i in 0 ..< _limitLines.count
{
if _limitLines[i] === line
{
_limitLines.remove(at: i)
return
}
}
guard let i = _limitLines.firstIndex(of: line) else { return }
_limitLines.remove(at: i)
}

/// Removes all LimitLines from the axis.
Expand Down
18 changes: 3 additions & 15 deletions Source/Charts/Data/Implementations/ChartBaseDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,7 @@ open class ChartBaseDataSet: NSObject, IChartDataSet, NSCopying
/// - alpha: alpha to apply to the set `colors`
@objc open func setColors(_ colors: [NSUIColor], alpha: CGFloat)
{
var colorsWithAlpha = colors

for i in 0 ..< colorsWithAlpha.count
{
colorsWithAlpha[i] = colorsWithAlpha[i] .withAlphaComponent(alpha)
}

self.colors = colorsWithAlpha
self.colors = colors.map { $0.withAlphaComponent(alpha) }
}

/// Sets colors with a specific alpha value.
Expand Down Expand Up @@ -409,14 +402,9 @@ open class ChartBaseDataSet: NSObject, IChartDataSet, NSCopying

open override var debugDescription: String
{
var desc = description + ":"

for i in 0 ..< self.entryCount
{
desc += "\n" + (self.entryForIndex(i)?.description ?? "")
return (0..<entryCount).reduce(description + ":") {
$0 + "\n" + (self.entryForIndex($1)?.description ?? "")
}

return desc
}

// MARK: - NSCopying
Expand Down
Loading