diff --git a/Source/Charts/Data/Implementations/ChartBaseDataSet.swift b/Source/Charts/Data/Implementations/ChartBaseDataSet.swift index 190241efa3..c8515aa7c5 100644 --- a/Source/Charts/Data/Implementations/ChartBaseDataSet.swift +++ b/Source/Charts/Data/Implementations/ChartBaseDataSet.swift @@ -24,7 +24,7 @@ open class ChartBaseDataSet: NSObject, ChartDataSetProtocol valueColors.append(NSUIColor.black) } - @objc public init(label: String?) + @objc public init(label: String) { super.init() diff --git a/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift index 6f88bf95cb..3ff93b004b 100644 --- a/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift @@ -29,7 +29,7 @@ open class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, BarChartData initialize() } - public override init(values: [ChartDataEntry]?, label: String?) + public override init(values: [ChartDataEntry], label: String) { super.init(values: values, label: label) initialize() @@ -50,68 +50,36 @@ open class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, BarChartData { _entryCountStacks = 0 - for i in 0 ..< entries.count - { - if let vals = entries[i].yValues - { - _entryCountStacks += vals.count - } - else - { - _entryCountStacks += 1 - } - } + entries.forEach { _entryCountStacks += $0.yValues?.count ?? 1 } } /// calculates the maximum stacksize that occurs in the Entries array of this DataSet private func calcStackSize(entries: [BarChartDataEntry]) { - for i in 0 ..< entries.count + for e in entries where (e.yValues?.count ?? 0) > _stackSize { - if let vals = entries[i].yValues - { - if vals.count > _stackSize - { - _stackSize = vals.count - } - } + _stackSize = e.yValues!.count } } open override func calcMinMax(entry e: ChartDataEntry) { - guard let e = e as? BarChartDataEntry + guard let e = e as? BarChartDataEntry, + !e.y.isNaN else { return } - if !e.y.isNaN + if e.yValues == nil { - if e.yValues == nil - { - if e.y < _yMin - { - _yMin = e.y - } - - if e.y > _yMax - { - _yMax = e.y - } - } - else - { - if -e.negativeSum < _yMin - { - _yMin = -e.negativeSum - } - - if e.positiveSum > _yMax - { - _yMax = e.positiveSum - } - } - - calcMinMaxX(entry: e) + _yMin = min(e.y, _yMin) + _yMax = max(e.y, _yMax) } + else + { + _yMin = min(-e.negativeSum, _yMin) + _yMax = max(e.positiveSum, _yMax) + } + + calcMinMaxX(entry: e) } /// - returns: The maximum number of bars that can be stacked upon another in this DataSet. diff --git a/Source/Charts/Data/Implementations/Standard/BubbleChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/BubbleChartDataSet.swift index d93eef6645..2ccb540cb2 100644 --- a/Source/Charts/Data/Implementations/Standard/BubbleChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/BubbleChartDataSet.swift @@ -30,12 +30,7 @@ open class BubbleChartDataSet: BarLineScatterCandleBubbleChartDataSet, BubbleCha super.calcMinMax(entry: e) - let size = e.size - - if size > _maxSize - { - _maxSize = size - } + _maxSize = max(e.size, maxSize) } // MARK: - Styling functions and accessors diff --git a/Source/Charts/Data/Implementations/Standard/CandleChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/CandleChartDataSet.swift index c04a5edceb..a18078afb5 100644 --- a/Source/Charts/Data/Implementations/Standard/CandleChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/CandleChartDataSet.swift @@ -21,7 +21,7 @@ open class CandleChartDataSet: LineScatterCandleRadarChartDataSet, CandleChartDa super.init() } - public override init(values: [ChartDataEntry]?, label: String?) + public override init(values: [ChartDataEntry], label: String) { super.init(values: values, label: label) } @@ -32,17 +32,10 @@ open class CandleChartDataSet: LineScatterCandleRadarChartDataSet, CandleChartDa { guard let e = e as? CandleChartDataEntry else { return } - - if e.low < _yMin - { - _yMin = e.low - } - - if e.high > _yMax - { - _yMax = e.high - } - + + _yMin = min(e.low, _yMin) + _yMax = max(e.high, _yMax) + calcMinMaxX(entry: e) } @@ -50,24 +43,12 @@ open class CandleChartDataSet: LineScatterCandleRadarChartDataSet, CandleChartDa { guard let e = e as? CandleChartDataEntry else { return } - - if e.high < _yMin - { - _yMin = e.high - } - if e.high > _yMax - { - _yMax = e.high - } - - if e.low < _yMin - { - _yMin = e.low - } - if e.low > _yMax - { - _yMax = e.low - } + + _yMin = min(e.low, _yMin) + _yMax = max(e.high, _yMin) + + _yMin = min(e.low, _yMax) + _yMax = max(e.high, _yMax) } // MARK: - Styling functions and accessors @@ -75,25 +56,18 @@ open class CandleChartDataSet: LineScatterCandleRadarChartDataSet, CandleChartDa /// the space between the candle entries /// /// **default**: 0.1 (10%) - private var _barSpace = CGFloat(0.1) - + private var _barSpace: CGFloat = 0.1 + /// the space that is left out on the left and right side of each candle, /// **default**: 0.1 (10%), max 0.45, min 0.0 open var barSpace: CGFloat { set { - if newValue < 0.0 - { - _barSpace = 0.0 - } - else if newValue > 0.45 - { - _barSpace = 0.45 - } - else - { - _barSpace = newValue + switch newValue { + case ..<0: _barSpace = 0.0 + case 0.45...: _barSpace = 0.45 + default: _barSpace = newValue } } get diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index ac9eb3aeda..69568fc725 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -296,7 +296,7 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral } /// - returns: All colors used across all DataSet objects this object represents. - @objc open func getColors() -> [NSUIColor] + @objc open var colors: [NSUIColor] { return reduce(into: []) { $0 += $1.colors } } diff --git a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift index fa21e6b02e..8abbf3fe71 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift @@ -27,33 +27,33 @@ open class ChartDataSet: ChartBaseDataSet public required init() { values = [] - + super.init() } - public override init(label: String?) + public override init(label: String) { values = [] - + super.init(label: label) } - @objc public init(values: [ChartDataEntry]?, label: String?) + @objc public init(values: [ChartDataEntry], label: String) { - self.values = values ?? [] - + self.values = values + super.init(label: label) - + self.calcMinMax() } - @objc public convenience init(values: [ChartDataEntry]?) + @objc public convenience init(values: [ChartDataEntry]) { self.init(values: values, label: "DataSet") } // MARK: - Data functions and accessors - + /// * /// - note: Calls `notifyDataSetChanged()` after setting a new value. /// - returns: The array of y-values that this DataSet represents. @@ -69,6 +69,7 @@ open class ChartDataSet: ChartBaseDataSet notifyDataSetChanged() } } + // TODO: Temporary fix for performance. Will be removed in 4.0 private var isIndirectValuesCall = false @@ -103,39 +104,23 @@ open class ChartDataSet: ChartBaseDataSet guard !values.isEmpty else { return } - let indexFrom = entryIndex(x: fromX, closestToY: Double.nan, rounding: .down) - let indexTo = entryIndex(x: toX, closestToY: Double.nan, rounding: .up) - - guard !(indexTo < indexFrom) else { return } + let indexFrom = entryIndex(x: fromX, closestToY: .nan, rounding: .down) + let indexTo = entryIndex(x: toX, closestToY: .nan, rounding: .up) - (indexFrom...indexTo).forEach { - // only recalculate y - calcMinMaxY(entry: values[$0]) - } + guard indexTo >= indexFrom else { return } + (indexFrom...indexTo).forEach { calcMinMaxY(entry: values[$0]) } // only recalculate y } @objc open func calcMinMaxX(entry e: ChartDataEntry) { - if e.x < _xMin - { - _xMin = e.x - } - if e.x > _xMax - { - _xMax = e.x - } + _xMin = min(e.x, _xMin) + _xMax = max(e.x, _xMax) } @objc open func calcMinMaxY(entry e: ChartDataEntry) { - if e.y < _yMin - { - _yMin = e.y - } - if e.y > _yMax - { - _yMax = e.y - } + _yMin = min(e.y, _yMin) + _yMax = max(e.y, _yMax) } /// Updates the min and max x and y value of this DataSet based on the given Entry. @@ -148,16 +133,16 @@ open class ChartDataSet: ChartBaseDataSet } /// - returns: The minimum y-value this DataSet holds - open override var yMin: Double { return _yMin } + @objc open override var yMin: Double { return _yMin } /// - returns: The maximum y-value this DataSet holds - open override var yMax: Double { return _yMax } + @objc open override var yMax: Double { return _yMax } /// - returns: The minimum x-value this DataSet holds - open override var xMin: Double { return _xMin } + @objc open override var xMin: Double { return _xMin } /// - returns: The maximum x-value this DataSet holds - open override var xMax: Double { return _xMax } + @objc open override var xMax: Double { return _xMax } /// - returns: The number of y-values this DataSet represents open override var entryCount: Int { return values.count } @@ -268,6 +253,7 @@ open class ChartDataSet: ChartBaseDataSet /// - parameter xValue: x-value of the entry to search for /// - parameter closestToY: If there are multiple y-values for the specified x-value, /// - parameter rounding: Rounding method if exact value was not found + // TODO: This should return `nil` to follow Swift convention open override func entryIndex( x xValue: Double, closestToY yValue: Double, @@ -320,21 +306,15 @@ open class ChartDataSet: ChartBaseDataSet { let closestXValue = values[closest].x - if rounding == .up + if rounding == .up, closestXValue < xValue, closest < values.endIndex - 1 { // If rounding up, and found x-value is lower than specified x, and we can go upper... - if closestXValue < xValue && closest < values.endIndex - 1 - { - closest += 1 - } + closest += 1 } - else if rounding == .down + else if rounding == .down, closestXValue > xValue, closest > values.startIndex { // If rounding down, and found x-value is upper than specified x, and we can go lower... - if closestXValue > xValue && closest > 0 - { - closest -= 1 - } + closest -= 1 } // Search by closest to y-value @@ -373,17 +353,10 @@ open class ChartDataSet: ChartBaseDataSet /// - returns: The array-index of the specified entry /// /// - parameter e: the entry to search for + // TODO: Should be returning `nil` to follow Swift convention open override func entryIndex(entry e: ChartDataEntry) -> Int { - for i in 0 ..< values.count - { - if values[i] === e - { - return i - } - } - - return -1 + return values.index { $0 === e } ?? -1 } /// Adds an Entry to the DataSet dynamically. @@ -391,6 +364,7 @@ open class ChartDataSet: ChartBaseDataSet /// This will also recalculate the current minimum and maximum values of the DataSet and the value-sum. /// - parameter e: the entry to add /// - returns: True + // TODO: This should return `Void` to follow Swift convention open override func addEntry(_ e: ChartDataEntry) -> Bool { calcMinMax(entry: e) @@ -406,6 +380,7 @@ open class ChartDataSet: ChartBaseDataSet /// This will also recalculate the current minimum and maximum values of the DataSet and the value-sum. /// - parameter e: the entry to add /// - returns: True + // TODO: This should return `Void` to follow Swift convention open override func addEntryOrdered(_ e: ChartDataEntry) -> Bool { calcMinMax(entry: e) @@ -432,29 +407,22 @@ open class ChartDataSet: ChartBaseDataSet /// This will also recalculate the current minimum and maximum values of the DataSet and the value-sum. /// - parameter entry: the entry to remove /// - returns: `true` if the entry was removed successfully, else if the entry does not exist + // TODO: This should return the removed entry to follow Swift convention. open override func removeEntry(_ entry: ChartDataEntry) -> Bool { - var removed = false isIndirectValuesCall = true - for i in 0 ..< values.count - { - if values[i] === entry - { - values.remove(at: i) - removed = true - break - } - } + guard let i = values.index(where: { $0 === entry }) else { return false } + values.remove(at: i) notifyDataSetChanged() - - return removed + return true } /// Removes the first Entry (at index 0) of this DataSet from the entries array. /// /// - returns: `true` if successful, `false` if not. + // TODO: This should return the removed entry to follow Swift convention. open override func removeFirst() -> Bool { let entry: ChartDataEntry? = values.isEmpty ? nil : values.removeFirst() @@ -464,6 +432,7 @@ open class ChartDataSet: ChartBaseDataSet /// Removes the last Entry (at index size-1) of this DataSet from the entries array. /// /// - returns: `true` if successful, `false` if not. + // TODO: This should return the removed entry to follow Swift convention. open override func removeLast() -> Bool { let entry: ChartDataEntry? = values.isEmpty ? nil : values.removeLast() @@ -474,15 +443,7 @@ open class ChartDataSet: ChartBaseDataSet /// - returns: `true` if contains the entry, `false` if not. open override func contains(_ e: ChartDataEntry) -> Bool { - for entry in values - { - if entry == e - { - return true - } - } - - return false + return values.contains(e) } /// Removes all values from this DataSet and recalculates min and max value. @@ -492,7 +453,7 @@ open class ChartDataSet: ChartBaseDataSet } // MARK: - Data functions and accessors - + // MARK: - NSCopying open override func copyWithZone(_ zone: NSZone?) -> AnyObject @@ -502,7 +463,7 @@ open class ChartDataSet: ChartBaseDataSet copy.values = values copy._yMax = _yMax copy._yMin = _yMin - + return copy } } diff --git a/Source/Charts/Data/Implementations/Standard/LineChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/LineChartDataSet.swift index 8c6f50b367..afee642da6 100644 --- a/Source/Charts/Data/Implementations/Standard/LineChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/LineChartDataSet.swift @@ -36,7 +36,7 @@ open class LineChartDataSet: LineRadarChartDataSet, LineChartDataSetProtocol initialize() } - public override init(values: [ChartDataEntry]?, label: String?) + public override init(values: [ChartDataEntry], label: String) { super.init(values: values, label: label) initialize() @@ -64,14 +64,10 @@ open class LineChartDataSet: LineRadarChartDataSet, LineChartDataSetProtocol } set { - _cubicIntensity = newValue - if _cubicIntensity > 1.0 - { - _cubicIntensity = 1.0 - } - if _cubicIntensity < 0.05 - { - _cubicIntensity = 0.05 + switch newValue { + case ..<0.05: _cubicIntensity = 0.05 + case 1.0...: _cubicIntensity = 1.0 + default: _cubicIntensity = newValue } } } diff --git a/Source/Charts/Data/Implementations/Standard/LineRadarChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/LineRadarChartDataSet.swift index f254ea6514..709655da44 100644 --- a/Source/Charts/Data/Implementations/Standard/LineRadarChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/LineRadarChartDataSet.swift @@ -54,17 +54,10 @@ open class LineRadarChartDataSet: LineScatterCandleRadarChartDataSet, LineRadarC } set { - if newValue < 0.0 - { - _lineWidth = 0.0 - } - else if newValue > 10.0 - { - _lineWidth = 10.0 - } - else - { - _lineWidth = newValue + switch newValue { + case ..<0.0: _lineWidth = 0.0 + case 10.0...: _lineWidth = 10.0 + default: _lineWidth = newValue } } } diff --git a/Source/Charts/Data/Implementations/Standard/PieChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/PieChartDataSet.swift index ea53d8666f..8d5154b64f 100644 --- a/Source/Charts/Data/Implementations/Standard/PieChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/PieChartDataSet.swift @@ -33,7 +33,7 @@ open class PieChartDataSet: ChartDataSet, PieChartDataSetProtocol initialize() } - public override init(values: [ChartDataEntry]?, label: String?) + public override init(values: [ChartDataEntry], label: String) { super.init(values: values, label: label) initialize() @@ -59,16 +59,11 @@ open class PieChartDataSet: ChartDataSet, PieChartDataSetProtocol } set { - var space = newValue - if space > 20.0 - { - space = 20.0 + switch newValue { + case ..<0.0: _sliceSpace = 0.0 + case 20.0...: _sliceSpace = 20.0 + default: _sliceSpace = newValue } - if space < 0.0 - { - space = 0.0 - } - _sliceSpace = space } } diff --git a/Source/Charts/Data/Implementations/Standard/RadarChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/RadarChartDataSet.swift index b6da45866e..81ffd9be7a 100644 --- a/Source/Charts/Data/Implementations/Standard/RadarChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/RadarChartDataSet.swift @@ -26,7 +26,7 @@ open class RadarChartDataSet: LineRadarChartDataSet, RadarChartDataSetProtocol initialize() } - public required override init(values: [ChartDataEntry]?, label: String?) + public required override init(values: [ChartDataEntry], label: String) { super.init(values: values, label: label) initialize()