Skip to content

Commit

Permalink
Remove misuse of count (#4461)
Browse files Browse the repository at this point in the history
* Remove misuse of `count`

* Fix protocol method name

* Update ChartColorTemplates.swift

use fallbackColor for `colorFromString()`

* Revert "Update ChartColorTemplates.swift"

This reverts commit b4111fd.

Co-authored-by: Xuan <[email protected]>
  • Loading branch information
jjatie and liuxuan30 authored Sep 25, 2020
1 parent 2e60d0c commit 2864cac
Show file tree
Hide file tree
Showing 26 changed files with 96 additions and 155 deletions.
2 changes: 1 addition & 1 deletion Source/Charts/Charts/ChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
guard
description.isEnabled,
let descriptionText = description.text,
descriptionText.count > 0
!descriptionText.isEmpty
else { return }

let position = description.position ?? CGPoint(x: bounds.width - viewPortHandler.offsetRight - description.xOffset,
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Charts/CombinedChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ open class CombinedChartView: BarLineChartViewBase, CombinedChartDataProvider
isDrawMarkersEnabled && valuesToHighlight()
else { return }

for i in 0 ..< highlighted.count
for i in highlighted.indices
{
let highlight = highlighted[i]

Expand Down
1 change: 0 additions & 1 deletion Source/Charts/Charts/PieRadarChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,6 @@ open class PieRadarChartViewBase: ChartViewBase

// Remove samples older than our sample time - 1 seconds
// while keeping at least one sample

var i = 0, count = velocitySamples.count
while (i < count - 2)
{
Expand Down
7 changes: 2 additions & 5 deletions Source/Charts/Components/AxisBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ open class AxisBase: ComponentBase
{
var longest = ""

for i in 0 ..< entries.count
for i in entries.indices
{
let text = getFormattedLabel(i)

Expand All @@ -151,10 +151,7 @@ open class AxisBase: ComponentBase
/// - Returns: The formatted label at the specified index. This will either use the auto-formatter or the custom formatter (if one is set).
@objc open func getFormattedLabel(_ index: Int) -> String
{
if index < 0 || index >= entries.count
{
return ""
}
guard entries.indices.contains(index) else { return "" }

return valueFormatter?.stringForValue(entries[index], axis: self) ?? ""
}
Expand Down
6 changes: 3 additions & 3 deletions Source/Charts/Components/Legend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ open class Legend: ComponentBase

var wasStacked = false

for i in 0 ..< entryCount
for i in entries.indices
{
let e = entries[i]
let drawingForm = e.form != .none
Expand Down Expand Up @@ -311,7 +311,7 @@ open class Legend: ComponentBase
var requiredWidth: CGFloat = 0.0
var stackedStartIndex: Int = -1

for i in 0 ..< entryCount
for i in entries.indices
{
let e = entries[i]
let drawingForm = e.form != .none
Expand Down Expand Up @@ -384,7 +384,7 @@ open class Legend: ComponentBase

neededWidth = maxLineWidth
neededHeight = labelLineHeight * CGFloat(calculatedLineSizes.count) +
yEntrySpace * CGFloat(calculatedLineSizes.count == 0 ? 0 : (calculatedLineSizes.count - 1))
yEntrySpace * CGFloat(calculatedLineSizes.isEmpty ? 0 : (calculatedLineSizes.count - 1))
}

neededWidth += xOffset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,14 @@ open class BarChartDataEntry: ChartDataEntry
self.data = data
}

@objc open func sumBelow(stackIndex :Int) -> Double
@objc open func sumBelow(stackIndex: Int) -> Double
{
guard let yVals = _yVals else
guard let yVals = _yVals, yVals.indices.contains(stackIndex) else
{
return 0
}

var remainder: Double = 0.0
var index = yVals.count - 1

while (index > stackIndex && index >= 0)
{
remainder += yVals[index]
index -= 1
}


let remainder = yVals[stackIndex...].reduce(into: 0.0) { $0 += $1 }
return remainder
}

Expand Down
10 changes: 5 additions & 5 deletions Source/Charts/Data/Implementations/Standard/ChartData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral
/// - Returns: The entry that is highlighted
@objc open func entry(for highlight: Highlight) -> ChartDataEntry?
{
guard highlight.dataSetIndex < dataSets.count else { return nil }
guard highlight.dataSetIndex < dataSets.endIndex else { return nil }
return self[highlight.dataSetIndex].entryForXValue(highlight.x, closestToY: highlight.y)
}

Expand All @@ -277,7 +277,7 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral
@objc(dataSetAtIndex:)
open func dataSet(at index: Index) -> Element?
{
guard index >= 0 && index < dataSets.count else { return nil }
guard dataSets.indices.contains(index) else { return nil }
return self[index]
}

Expand All @@ -295,7 +295,7 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral
@objc(addEntry:dataSetIndex:)
open func appendEntry(_ e: ChartDataEntry, toDataSet dataSetIndex: Index)
{
guard dataSets.count > dataSetIndex && dataSetIndex >= 0 else {
guard dataSets.indices.contains(dataSetIndex) else {
return print("ChartData.addEntry() - Cannot add Entry because dataSetIndex too high or too low.", terminator: "\n")
}

Expand All @@ -307,7 +307,7 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral
/// Removes the given Entry object from the DataSet at the specified index.
@objc @discardableResult open func removeEntry(_ entry: ChartDataEntry, dataSetIndex: Index) -> Bool
{
guard dataSetIndex < dataSets.count else { return false }
guard dataSets.indices.contains(dataSetIndex) else { return false }

// remove the entry from the dataset
let removed = self[dataSetIndex].removeEntry(entry)
Expand All @@ -327,7 +327,7 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral
@objc @discardableResult open func removeEntry(xValue: Double, dataSetIndex: Index) -> Bool
{
guard
dataSetIndex < dataSets.count,
dataSets.indices.contains(dataSetIndex),
let entry = self[dataSetIndex].entryForXValue(xValue, closestToY: .nan)
else { return false }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,20 +252,8 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData
/// - Returns: The entry that is highlighted
@objc override open func entry(for highlight: Highlight) -> ChartDataEntry?
{
if highlight.dataIndex >= allData.count
{
return nil
}

let data = dataByIndex(highlight.dataIndex)

if highlight.dataSetIndex >= data.endIndex
{
return nil
}

// The value of the highlighted entry could be NaN - if we are not interested in highlighting a specific value.
return data[highlight.dataSetIndex]
getDataSetByHighlight(highlight)?
.entriesForXValue(highlight.x)
.first { $0.y == highlight.y || highlight.y.isNaN }
}
Expand All @@ -276,20 +264,21 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData
/// - highlight: current highlight
/// - Returns: dataset related to highlight
@objc open func getDataSetByHighlight(_ highlight: Highlight) -> ChartDataSetProtocol!
{
if highlight.dataIndex >= allData.count
{
guard allData.indices.contains(highlight.dataIndex) else
{
return nil
}

let data = dataByIndex(highlight.dataIndex)
if highlight.dataSetIndex >= data.endIndex

guard data.indices.contains(highlight.dataSetIndex) else
{
return nil
}

return data.dataSets[highlight.dataSetIndex]

// The value of the highlighted entry could be NaN - if we are not interested in highlighting a specific value.
return data[highlight.dataSetIndex]
}

// MARK: Unsupported Collection Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ open class PieChartData: ChartData

open override func dataSet(forLabel label: String, ignorecase: Bool) -> ChartDataSetProtocol?
{
if dataSets.count == 0 || dataSets[0].label == nil
if dataSets.first?.label == nil
{
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions Source/Charts/Filters/DataApproximator+N.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ extension DataApproximator {
var keep = [Bool](repeating: false, count: points.count)

// first and last always stay
keep[0] = true
keep[points.count - 1] = true
keep[points.startIndex] = true
keep[points.endIndex - 1] = true
var currentStoredPoints = 2

var queue = [LineAlt]()
let line = LineAlt(start: 0, end: points.count - 1, points: points)
let line = LineAlt(start: points.startIndex, end: points.endIndex - 1, points: points)
queue.append(line)

repeat {
Expand Down
8 changes: 4 additions & 4 deletions Source/Charts/Filters/DataApproximator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ open class DataApproximator: NSObject
var keep = [Bool](repeating: false, count: points.count)

// first and last always stay
keep[0] = true
keep[points.count - 1] = true
keep[points.startIndex] = true
keep[points.endIndex - 1] = true

// first and last entry are entry point to recursion
reduceWithDouglasPeuker(points: points,
tolerance: tolerance,
start: 0,
end: points.count - 1,
start: points.startIndex,
end: points.endIndex - 1,
keep: &keep)

// create a new array with series, only take the kept ones
Expand Down
20 changes: 3 additions & 17 deletions Source/Charts/Formatters/IndexAxisValueFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,8 @@ import Foundation
@objc(ChartIndexAxisValueFormatter)
open class IndexAxisValueFormatter: NSObject, AxisValueFormatter
{
private var _values: [String] = [String]()
private var _valueCount: Int = 0

@objc public var values: [String]
{
get
{
return _values
}
set
{
_values = newValue
_valueCount = _values.count
}
}

@objc public var values: [String] = [String]()

public override init()
{
super.init()
Expand All @@ -54,6 +40,6 @@ open class IndexAxisValueFormatter: NSObject, AxisValueFormatter
{
let index = Int(value.rounded())
guard values.indices.contains(index), index == Int(value) else { return "" }
return _values[index]
return values[index]
}
}
4 changes: 2 additions & 2 deletions Source/Charts/Highlight/BarHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ open class BarHighlighter: ChartHighlighter

guard
let ranges = entry.ranges,
ranges.count > 0
!ranges.isEmpty
else { return nil }

let stackIndex = getClosestStackIndex(ranges: ranges, value: yValue)
Expand All @@ -101,7 +101,7 @@ open class BarHighlighter: ChartHighlighter
if let stackIndex = ranges.firstIndex(where: { $0.contains(value) }) {
return stackIndex
} else {
let length = max(ranges.count - 1, 0)
let length = max(ranges.endIndex - 1, 0)
return (value > ranges[length].to) ? length : 0
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Highlight/ChartHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ open class ChartHighlighter : NSObject, Highlighter
guard let chart = self.chart as? BarLineScatterCandleBubbleChartDataProvider else { return [] }

var entries = set.entriesForXValue(xValue)
if entries.count == 0, let closest = set.entryForXValue(xValue, closestToY: .nan, rounding: rounding)
if entries.isEmpty, let closest = set.entryForXValue(xValue, closestToY: .nan, rounding: rounding)
{
// Try to find closest x-value and take all entries for that x-value
entries = set.entriesForXValue(closest.x)
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Highlight/CombinedHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ open class CombinedHighlighter: ChartHighlighter
let dataObjects = chart.combinedData?.allData
else { return vals }

for i in 0..<dataObjects.count
for i in dataObjects.indices
{
let dataObject = dataObjects[i]

Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Highlight/HorizontalBarHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ open class HorizontalBarHighlighter: BarHighlighter
guard let chart = self.chart as? BarLineScatterCandleBubbleChartDataProvider else { return [] }

var entries = set.entriesForXValue(xValue)
if entries.count == 0, let closest = set.entryForXValue(xValue, closestToY: .nan, rounding: rounding)
if entries.isEmpty, let closest = set.entryForXValue(xValue, closestToY: .nan, rounding: rounding)
{
// Try to find closest x-value and take all entries for that x-value
entries = set.entriesForXValue(closest.x)
Expand Down
8 changes: 4 additions & 4 deletions Source/Charts/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
{
guard let barData = dataProvider?.barData else { return _buffers.removeAll() }

// Matche buffers count to dataset count
// Match buffers count to dataset count
if _buffers.count != barData.count
{
while _buffers.count < barData.count
Expand Down Expand Up @@ -558,7 +558,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
var posY = 0.0
var negY = -e.negativeSum

for k in 0 ..< vals.count
for k in vals.indices
{
let value = vals[k]
var y: Double
Expand Down Expand Up @@ -661,7 +661,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
}
}

bufferIndex = vals == nil ? (bufferIndex + 1) : (bufferIndex + vals!.count)
bufferIndex += vals?.count ?? 1
}
}
}
Expand Down Expand Up @@ -801,7 +801,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
let labelCount = min(dataSet.colors.count, stackSize)

let stackLabel: String?
if (dataSet.stackLabels.count > 0 && labelCount > 0) {
if (!dataSet.stackLabels.isEmpty && labelCount > 0) {
let labelIndex = idx % labelCount
stackLabel = dataSet.stackLabels.indices.contains(labelIndex) ? dataSet.stackLabels[labelIndex] : nil
} else {
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Renderers/BubbleChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer

var pt = CGPoint()

for i in 0..<dataSets.count
for i in dataSets.indices
{
let dataSet = dataSets[i]

Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Renderers/CandleStickChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ open class CandleStickChartRenderer: LineScatterCandleRadarRenderer

var pt = CGPoint()

for i in 0 ..< dataSets.count
for i in dataSets.indices
{
guard let
dataSet = dataSets[i] as? BarLineScatterCandleBubbleChartDataSetProtocol,
Expand Down
Loading

0 comments on commit 2864cac

Please sign in to comment.