From 5fbf9b495d5dfe20fd2b376783c114435470bba3 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Mon, 13 Nov 2017 17:45:45 -0800 Subject: [PATCH 01/34] Cleaned up `ChartDataSet` logic Added TODOs for areas where simple changes can help improve Swift consistency. --- .../Implementations/ChartBaseDataSet.swift | 2 +- .../Standard/BarChartDataSet.swift | 2 +- .../Standard/CandleChartDataSet.swift | 2 +- .../Standard/ChartDataSet.swift | 197 ++++++------------ .../Standard/LineChartDataSet.swift | 2 +- .../Standard/PieChartDataSet.swift | 2 +- .../Standard/RadarChartDataSet.swift | 2 +- 7 files changed, 69 insertions(+), 140 deletions(-) diff --git a/Source/Charts/Data/Implementations/ChartBaseDataSet.swift b/Source/Charts/Data/Implementations/ChartBaseDataSet.swift index ea282ffdd1..1084f4753a 100644 --- a/Source/Charts/Data/Implementations/ChartBaseDataSet.swift +++ b/Source/Charts/Data/Implementations/ChartBaseDataSet.swift @@ -24,7 +24,7 @@ open class ChartBaseDataSet: NSObject, IChartDataSet 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 ceb5ccb8af..d2829cd4a4 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, IBarChartDat initialize() } - public override init(values: [ChartDataEntry]?, label: String?) + public override init(values: [ChartDataEntry], label: String) { super.init(values: values, label: label) initialize() diff --git a/Source/Charts/Data/Implementations/Standard/CandleChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/CandleChartDataSet.swift index d4da90d69f..019d5c6436 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, ICandleChartD super.init() } - public override init(values: [ChartDataEntry]?, label: String?) + public override init(values: [ChartDataEntry], label: String) { super.init(values: values, label: label) } diff --git a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift index b829f630e1..a2b2f2c901 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift @@ -26,28 +26,28 @@ open class ChartDataSet: ChartBaseDataSet { public required init() { - super.init() - _values = [ChartDataEntry]() + + super.init() } - public override init(label: String?) + public override init(label: String) { - super.init(label: label) - _values = [ChartDataEntry]() + + super.init(label: label) } - @objc public init(values: [ChartDataEntry]?, label: String?) + @objc public init(values: [ChartDataEntry], label: String) { + _values = values + super.init(label: label) - - _values = values == nil ? [ChartDataEntry]() : values - + self.calcMinMax() } - @objc public convenience init(values: [ChartDataEntry]?) + @objc public convenience init(values: [ChartDataEntry]) { self.init(values: values, label: "DataSet") } @@ -55,19 +55,19 @@ open class ChartDataSet: ChartBaseDataSet // MARK: - Data functions and accessors /// the entries that this dataset represents / holds together - @objc internal var _values: [ChartDataEntry]! + internal var _values: [ChartDataEntry] /// maximum y-value in the value array - @objc internal var _yMax: Double = -Double.greatestFiniteMagnitude + internal var _yMax: Double = -Double.greatestFiniteMagnitude /// minimum y-value in the value array - @objc internal var _yMin: Double = Double.greatestFiniteMagnitude + internal var _yMin: Double = Double.greatestFiniteMagnitude /// maximum x-value in the value array - @objc internal var _xMax: Double = -Double.greatestFiniteMagnitude + internal var _xMax: Double = -Double.greatestFiniteMagnitude /// minimum x-value in the value array - @objc internal var _xMin: Double = Double.greatestFiniteMagnitude + internal var _xMin: Double = Double.greatestFiniteMagnitude /// * /// - note: Calls `notifyDataSetChanged()` after setting a new value. @@ -93,66 +93,40 @@ open class ChartDataSet: ChartBaseDataSet open override func calcMinMax() { - if _values.count == 0 - { - return - } + guard !_values.isEmpty else { return } _yMax = -Double.greatestFiniteMagnitude _yMin = Double.greatestFiniteMagnitude _xMax = -Double.greatestFiniteMagnitude _xMin = Double.greatestFiniteMagnitude - - for e in _values - { - calcMinMax(entry: e) - } + + _values.forEach { calcMinMax(entry: $0) } } open override func calcMinMaxY(fromX: Double, toX: Double) { - if _values.count == 0 - { - return - } - + guard !_values.isEmpty else { return } + _yMax = -Double.greatestFiniteMagnitude _yMin = Double.greatestFiniteMagnitude let indexFrom = entryIndex(x: fromX, closestToY: Double.nan, rounding: .down) let indexTo = entryIndex(x: toX, closestToY: Double.nan, rounding: .up) - if indexTo < indexFrom { return } - - for i in indexFrom...indexTo - { - // only recalculate y - calcMinMaxY(entry: _values[i]) - } + 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. @@ -165,26 +139,26 @@ 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 ?? 0 } + @objc open override var entryCount: Int { return _values.count } /// - returns: The entry object found at the given index (not x-value!) /// - throws: out of bounds /// if `i` is out of bounds, it may throw an out-of-bounds exception open override func entryForIndex(_ i: Int) -> ChartDataEntry? { - guard i >= 0 && i < _values.count else { + guard _values.indices.contains(i) else { return nil } return _values[i] @@ -285,6 +259,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, @@ -337,21 +312,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.count - 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 @@ -390,17 +359,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. @@ -408,13 +370,9 @@ 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 { - if _values == nil - { - _values = [ChartDataEntry]() - } - calcMinMax(entry: e) _values.append(e) @@ -427,13 +385,9 @@ 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 { - if _values == nil - { - _values = [ChartDataEntry]() - } - calcMinMax(entry: e) if _values.count > 0 && _values.last!.x > e.x @@ -457,75 +411,50 @@ 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 - - for i in 0 ..< _values.count - { - if _values[i] === entry - { - _values.remove(at: i) - removed = true - break - } - } - - if removed - { - calcMinMax() - } - - return removed + guard let i = _values.index(where: { $0 === entry }) else { return false } + + _values.remove(at: i) + calcMinMax() + + return true } /// Removes the first Entry (at index 0) of this DataSet from the entries array. /// /// - returns: `true` if successful, `false` ifnot. + // TODO: This should return the removed entry to follow Swift convention. open override func removeFirst() -> Bool { - let entry: ChartDataEntry? = _values.isEmpty ? nil : _values.removeFirst() - - let removed = entry != nil - - if removed - { - calcMinMax() - } + guard !_values.isEmpty else { return false } + + _values.removeFirst() + calcMinMax() - return removed + return true } /// Removes the last Entry (at index size-1) of this DataSet from the entries array. /// /// - returns: `true` if successful, `false` ifnot. - open override func removeLast() -> Bool + // TODO: This should return the removed entry to follow Swift convention. + open override func removeLast() -> Bool { - let entry: ChartDataEntry? = _values.isEmpty ? nil : _values.removeLast() - - let removed = entry != nil - - if removed - { - calcMinMax() - } - - return removed + guard !_values.isEmpty else { return false } + + _values.removeLast() + calcMinMax() + + return true } /// Checks if this DataSet contains the specified Entry. /// - returns: `true` if contains the entry, `false` ifnot. open override func contains(_ e: ChartDataEntry) -> Bool { - for entry in _values - { - if (entry.isEqual(e)) - { - return true - } - } - - return false + return _values.contains(e) } /// Removes all values from this DataSet and recalculates min and max value. diff --git a/Source/Charts/Data/Implementations/Standard/LineChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/LineChartDataSet.swift index 391bba48a1..69e3d6534b 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, ILineChartDataSet initialize() } - public override init(values: [ChartDataEntry]?, label: String?) + public override init(values: [ChartDataEntry], label: String) { super.init(values: values, label: label) initialize() diff --git a/Source/Charts/Data/Implementations/Standard/PieChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/PieChartDataSet.swift index 0a5d9a4029..caac9b0e5c 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, IPieChartDataSet initialize() } - public override init(values: [ChartDataEntry]?, label: String?) + public override init(values: [ChartDataEntry], label: String) { super.init(values: values, label: label) initialize() diff --git a/Source/Charts/Data/Implementations/Standard/RadarChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/RadarChartDataSet.swift index fcb3134fba..5d40e43d83 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, IRadarChartDataSet initialize() } - public required override init(values: [ChartDataEntry]?, label: String?) + public required override init(values: [ChartDataEntry], label: String) { super.init(values: values, label: label) initialize() From 1feda881352eaab6919e9b8e1b606c3ccb202c39 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Mon, 13 Nov 2017 18:00:36 -0800 Subject: [PATCH 02/34] Tidied up logic for `ChartDataSet` subclasses Minor changes to take advantage of Swift features and help improve readability. --- .../Standard/BarChartDataSet.swift | 63 +++++-------------- .../Standard/BubbleChartDataSet.swift | 7 +-- .../Standard/CandleChartDataSet.swift | 56 +++++------------ .../Standard/LineChartDataSet.swift | 12 ++-- .../Standard/LineRadarChartDataSet.swift | 15 ++--- .../Standard/PieChartDataSet.swift | 13 ++-- 6 files changed, 45 insertions(+), 121 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift index d2829cd4a4..585f0b495b 100644 --- a/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift @@ -50,68 +50,39 @@ open class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, IBarChartDat { _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 fileprivate func calcStackSize(entries: [BarChartDataEntry]) { - for i in 0 ..< entries.count + for e in entries { - if let vals = entries[i].yValues - { - if vals.count > _stackSize - { - _stackSize = vals.count - } + guard let vals = e.yValues, vals.count > _stackSize else { + continue } + _stackSize = vals.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 6041bb94d3..2ad7677cb4 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, IBubbleCh 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 019d5c6436..6391a758d5 100644 --- a/Source/Charts/Data/Implementations/Standard/CandleChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/CandleChartDataSet.swift @@ -32,17 +32,10 @@ open class CandleChartDataSet: LineScatterCandleRadarChartDataSet, ICandleChartD { 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, ICandleChartD { 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,7 +56,7 @@ open class CandleChartDataSet: LineScatterCandleRadarChartDataSet, ICandleChartD /// the space between the candle entries /// /// **default**: 0.1 (10%) - fileprivate var _barSpace = CGFloat(0.1) + fileprivate 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 @@ -83,17 +64,10 @@ open class CandleChartDataSet: LineScatterCandleRadarChartDataSet, ICandleChartD { 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/LineChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/LineChartDataSet.swift index 69e3d6534b..c68ee37126 100644 --- a/Source/Charts/Data/Implementations/Standard/LineChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/LineChartDataSet.swift @@ -64,14 +64,10 @@ open class LineChartDataSet: LineRadarChartDataSet, ILineChartDataSet } 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 4fe67697e9..f5ad8eec2c 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, ILineRadar } 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 caac9b0e5c..397616e152 100644 --- a/Source/Charts/Data/Implementations/Standard/PieChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/PieChartDataSet.swift @@ -59,16 +59,11 @@ open class PieChartDataSet: ChartDataSet, IPieChartDataSet } 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 } } From 6e80eb0f2ef58a2d6b28123d6a42ddf2ea0024ad Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sat, 18 Nov 2017 14:25:17 -0800 Subject: [PATCH 03/34] Added Collection conformances MutableCollection RandomAccessCollection RangeReplaceableCollection --- .../Implementations/Standard/ChartData.swift | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index 411643d67f..e40b95d3fd 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -757,3 +757,118 @@ open class ChartData: NSObject return max } } + +// MARK: MutableCollection +extension ChartData: MutableCollection { + public typealias Index = Int + public typealias Element = IChartDataSet + + public var startIndex: Index { + return _dataSets.startIndex + } + + public var endIndex: Index { + return _dataSets.endIndex + } + + public func index(after: Index) -> Index { + return _dataSets.index(after: after) + } + + public subscript(position: Index) -> Element { + get{ return _dataSets[position] } + set{ self._dataSets[position] = newValue } + } +} + +// MARK: RandomAccessCollection +extension ChartData: RandomAccessCollection { + public func index(before: Index) -> Index { + return _dataSets.index(before: before) + } +} + +// MARK: RangeReplaceableCollection +extension ChartData: RangeReplaceableCollection { + public func append(_ newElement: Element) { + self._dataSets.append(newElement) + calcMinMax(dataSet: newElement) + } + + public func remove(at position: Index) -> Element { + let element = self._dataSets.remove(at: position) + calcMinMax() + return element + } + + public func removeFirst() -> Element { + let element = self._dataSets.removeFirst() + notifyDataChanged() + return element + } + + public func removeFirst(_ n: Int) { + self._dataSets.removeFirst(n) + notifyDataChanged() + } + + public func removeLast() -> Element { + let element = self._dataSets.removeLast() + notifyDataChanged() + return element + } + + public func removeLast(_ n: Int) { + self._dataSets.removeLast(n) + notifyDataChanged() + } + +// public func removeSubrange(_ bounds: Range) { +// self.dataSets.removeSubrange(bounds) +// notifyDataChanged() +// } +} + +// MARK: Swift Accessors +extension ChartData { + //TODO: Reevaluate if warning is still true + /// Retrieve the index of a ChartDataSet with a specific label from the ChartData. Search can be case sensitive or not. + /// **IMPORTANT: This method does calculations at runtime, do not over-use in performance critical situations.** + /// + /// - Parameters: + /// - label: The label to search for + /// - ignoreCase: if true, the search is not case-sensitive + /// - Returns: The index of the DataSet Object with the given label. `nil` if not found + public func index(forLabel label: String, ignoreCase: Bool) -> Index? { + return ignoreCase + ? index { $0.label?.caseInsensitiveCompare(label) == .orderedSame } + : index { $0.label == label } + } + + public subscript(label: String, ignoreCase: Bool) -> Element? { + get { + guard let index = index(forLabel: label, ignoreCase: ignoreCase) else { return nil } + return self[index] + } + } + + public subscript(entry: ChartDataEntry) -> Element? { + get { + guard let index = index(where: { $0.entryForXValue(entry.x, closestToY: entry.y) === entry }) else { + return nil + } + return self[index] + } + } + + public func appendEntry(_ e: ChartDataEntry, toDataSet dataSetIndex: Index) { + guard indices.contains(dataSetIndex) else { + print("ChartData.addEntry() - Cannot add Entry because dataSetIndex too high or too low.", terminator: "\n") + } + + let set = self[dataSetIndex] + if !set.addEntry(e) { return } + calcMinMax(entry: e, axis: set.axisDependency) + } + +} From d36d11b5ea054b78363a30699fef0ed2c2af01e3 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sat, 18 Nov 2017 16:04:02 -0800 Subject: [PATCH 04/34] [#3018] Refactored use of `ChartData` to use new `Collection` conformances --- Source/Charts/Charts/ChartViewBase.swift | 48 +-- .../Standard/BarChartData.swift | 13 +- .../BarLineScatterCandleBubbleChartData.swift | 2 +- .../Standard/BubbleChartData.swift | 2 +- .../Standard/CandleChartData.swift | 2 +- .../Implementations/Standard/ChartData.swift | 348 ++++-------------- .../Standard/CombinedChartData.swift | 39 +- .../Standard/LineChartData.swift | 2 +- .../Standard/PieChartData.swift | 2 +- .../Standard/RadarChartData.swift | 2 +- .../Standard/ScatterChartData.swift | 22 +- .../Charts/Highlight/ChartHighlighter.swift | 17 +- 12 files changed, 126 insertions(+), 373 deletions(-) diff --git a/Source/Charts/Charts/ChartViewBase.swift b/Source/Charts/Charts/ChartViewBase.swift index 0c79db9d74..7f6616edfa 100755 --- a/Source/Charts/Charts/ChartViewBase.swift +++ b/Source/Charts/Charts/ChartViewBase.swift @@ -138,7 +138,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate @objc open var noDataFont: NSUIFont! = NSUIFont(name: "HelveticaNeue", size: 12.0) /// color of the no data text - @objc open var noDataTextColor: NSUIColor = NSUIColor.black + @objc open var noDataTextColor: NSUIColor = .black @objc internal var _legendRenderer: LegendRenderer! @@ -257,12 +257,10 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate // calculate how many digits are needed setupDefaultFormatter(min: _data.getYMin(), max: _data.getYMax()) - for set in _data.dataSets + for set in _data where + set.needsFormatter || set.valueFormatter === _defaultValueFormatter { - if set.needsFormatter || set.valueFormatter === _defaultValueFormatter - { - set.valueFormatter = _defaultValueFormatter - } + set.valueFormatter = _defaultValueFormatter } // let the chart know there is new data @@ -291,16 +289,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate /// - returns: `true` if the chart is empty (meaning it's data object is either null or contains no entries). @objc open func isEmpty() -> Bool { - guard let data = _data else { return true } - - if data.entryCount <= 0 - { - return true - } - else - { - return false - } + return _data?.isEmpty ?? true } /// Lets the chart know its underlying data has changed and should perform all necessary recalculations. @@ -340,13 +329,11 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate } - if _defaultValueFormatter is DefaultValueFormatter + if let formatter = _defaultValueFormatter as? DefaultValueFormatter { // setup the formatter with a new number of digits let digits = ChartUtils.decimals(reference) - - (_defaultValueFormatter as? DefaultValueFormatter)?.decimals - = digits + formatter.decimals = digits } } @@ -591,9 +578,9 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate { // if there is no marker view or drawing marker is disabled guard - let marker = marker - , isDrawMarkersEnabled && - valuesToHighlight() + let marker = marker, + isDrawMarkersEnabled, + valuesToHighlight() else { return } for i in 0 ..< _indicesToHighlight.count @@ -601,23 +588,20 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate let highlight = _indicesToHighlight[i] guard let - set = data?.getDataSetByIndex(highlight.dataSetIndex), + set = data?[highlight.dataSetIndex], let e = _data?.entryForHighlight(highlight) else { continue } let entryIndex = set.entryIndex(entry: e) - if entryIndex > Int(Double(set.entryCount) * _animator.phaseX) - { - continue - } + + guard + entryIndex <= Int(Double(set.entryCount) * _animator.phaseX) + else { continue } let pos = getMarkerPosition(highlight: highlight) // check bounds - if !_viewPortHandler.isInBounds(x: pos.x, y: pos.y) - { - continue - } + guard _viewPortHandler.isInBounds(x: pos.x, y: pos.y) else { continue } // callbacks to update the content marker.refreshContent(entry: e, highlight: highlight) diff --git a/Source/Charts/Data/Implementations/Standard/BarChartData.swift b/Source/Charts/Data/Implementations/Standard/BarChartData.swift index 0317fc8a6b..6f5792fc87 100644 --- a/Source/Charts/Data/Implementations/Standard/BarChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/BarChartData.swift @@ -14,7 +14,7 @@ import CoreGraphics open class BarChartData: BarLineScatterCandleBubbleChartData { - public override init() + public required init() { super.init() } @@ -38,13 +38,11 @@ open class BarChartData: BarLineScatterCandleBubbleChartData /// - parameter barSpace: The space between individual bars in values (not pixels) e.g. 0.1f for bar width 1f @objc open func groupBars(fromX: Double, groupSpace: Double, barSpace: Double) { - let setCount = _dataSets.count - if setCount <= 1 - { + guard !isEmpty else { print("BarData needs to hold at least 2 BarDataSets to allow grouping.", terminator: "\n") return } - + let max = maxEntryCountSet let maxEntryCount = max?.entryCount ?? 0 @@ -56,7 +54,7 @@ open class BarChartData: BarLineScatterCandleBubbleChartData let interval = groupWidth(groupSpace: groupSpace, barSpace: barSpace) - for i in stride(from: 0, to: maxEntryCount, by: 1) + for i in 0.. Double { - return Double(_dataSets.count) * (self.barWidth + barSpace) + groupSpace + return Double(count) * (self.barWidth + barSpace) + groupSpace } - } diff --git a/Source/Charts/Data/Implementations/Standard/BarLineScatterCandleBubbleChartData.swift b/Source/Charts/Data/Implementations/Standard/BarLineScatterCandleBubbleChartData.swift index c98bb1d075..b5e0d6d421 100644 --- a/Source/Charts/Data/Implementations/Standard/BarLineScatterCandleBubbleChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/BarLineScatterCandleBubbleChartData.swift @@ -13,7 +13,7 @@ import Foundation open class BarLineScatterCandleBubbleChartData: ChartData { - public override init() + public required init() { super.init() } diff --git a/Source/Charts/Data/Implementations/Standard/BubbleChartData.swift b/Source/Charts/Data/Implementations/Standard/BubbleChartData.swift index 433f384f75..6cf729f152 100644 --- a/Source/Charts/Data/Implementations/Standard/BubbleChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/BubbleChartData.swift @@ -14,7 +14,7 @@ import CoreGraphics open class BubbleChartData: BarLineScatterCandleBubbleChartData { - public override init() + public required init() { super.init() } diff --git a/Source/Charts/Data/Implementations/Standard/CandleChartData.swift b/Source/Charts/Data/Implementations/Standard/CandleChartData.swift index 5158668ad9..401159e043 100644 --- a/Source/Charts/Data/Implementations/Standard/CandleChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/CandleChartData.swift @@ -13,7 +13,7 @@ import Foundation open class CandleChartData: BarLineScatterCandleBubbleChartData { - public override init() + public required init() { super.init() } diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index e40b95d3fd..168d39fc24 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -24,7 +24,7 @@ open class ChartData: NSObject @objc internal var _dataSets = [IChartDataSet]() - public override init() + public override required init() { super.init() @@ -59,10 +59,7 @@ open class ChartData: NSObject @objc open func calcMinMaxY(fromX: Double, toX: Double) { - for set in _dataSets - { - set.calcMinMaxY(fromX: fromX, toX: toX) - } + forEach { $0.calcMinMaxY(fromX: fromX, toX: toX) } // apply the new data calcMinMax() @@ -76,10 +73,7 @@ open class ChartData: NSObject _xMax = -Double.greatestFiniteMagnitude _xMin = Double.greatestFiniteMagnitude - for set in _dataSets - { - calcMinMax(dataSet: set) - } + forEach { calcMinMax(dataSet: $0) } _leftAxisMax = -Double.greatestFiniteMagnitude _leftAxisMin = Double.greatestFiniteMagnitude @@ -93,20 +87,17 @@ open class ChartData: NSObject { _leftAxisMax = firstLeft!.yMax _leftAxisMin = firstLeft!.yMin - - for dataSet in _dataSets + + for dataSet in _dataSets where dataSet.axisDependency == .left { - if dataSet.axisDependency == .left + if dataSet.yMin < _leftAxisMin { - if dataSet.yMin < _leftAxisMin - { - _leftAxisMin = dataSet.yMin - } - - if dataSet.yMax > _leftAxisMax - { - _leftAxisMax = dataSet.yMax - } + _leftAxisMin = dataSet.yMin + } + + if dataSet.yMax > _leftAxisMax + { + _leftAxisMax = dataSet.yMax } } } @@ -119,19 +110,16 @@ open class ChartData: NSObject _rightAxisMax = firstRight!.yMax _rightAxisMin = firstRight!.yMin - for dataSet in _dataSets + for dataSet in _dataSets where dataSet.axisDependency == .right { - if dataSet.axisDependency == .right + if dataSet.yMin < _rightAxisMin + { + _rightAxisMin = dataSet.yMin + } + + if dataSet.yMax > _rightAxisMax { - if dataSet.yMin < _rightAxisMin - { - _rightAxisMin = dataSet.yMin - } - - if dataSet.yMax > _rightAxisMax - { - _rightAxisMax = dataSet.yMax - } + _rightAxisMax = dataSet.yMax } } } @@ -352,50 +340,15 @@ open class ChartData: NSObject /// - returns: The index of the DataSet Object with the given label. Sensitive or not. @objc internal func getDataSetIndexByLabel(_ label: String, ignorecase: Bool) -> Int { - if ignorecase - { - for i in 0 ..< dataSets.count - { - if dataSets[i].label == nil - { - continue - } - if (label.caseInsensitiveCompare(dataSets[i].label!) == ComparisonResult.orderedSame) - { - return i - } - } - } - else - { - for i in 0 ..< dataSets.count - { - if label == dataSets[i].label - { - return i - } - } - } - - return -1 + return ignorecase + ? index { $0.label?.caseInsensitiveCompare(label) == .orderedSame } ?? -1 + : index { $0.label == label } ?? -1 } /// - returns: The labels of all DataSets as a string array. @objc internal func dataSetLabels() -> [String] { - var types = [String]() - - for i in 0 ..< _dataSets.count - { - if dataSets[i].label == nil - { - continue - } - - types[i] = _dataSets[i].label! - } - - return types + return flatMap { $0.label } } /// Get the Entry for a corresponding highlight object @@ -404,14 +357,8 @@ open class ChartData: NSObject /// - returns: The entry that is highlighted @objc open func entryForHighlight(_ highlight: Highlight) -> ChartDataEntry? { - if highlight.dataSetIndex >= dataSets.count - { - return nil - } - else - { - return dataSets[highlight.dataSetIndex].entryForXValue(highlight.x, closestToY: highlight.y) - } + guard indices.contains(highlight.dataSetIndex) else { return nil } + return self[highlight.dataSetIndex].entryForXValue(highlight.x, closestToY: highlight.y) } /// **IMPORTANT: This method does calculations at runtime. Use with care in performance critical situations.** @@ -419,31 +366,23 @@ open class ChartData: NSObject /// - parameter label: /// - parameter ignorecase: /// - returns: The DataSet Object with the given label. Sensitive or not. - @objc open func getDataSetByLabel(_ label: String, ignorecase: Bool) -> IChartDataSet? + @objc open func getDataSetByLabel(_ label: String, ignorecase: Bool) -> Element? { - let index = getDataSetIndexByLabel(label, ignorecase: ignorecase) - - if index < 0 || index >= _dataSets.count - { - return nil - } - else - { - return _dataSets[index] - } + guard let index = index(forLabel: label, ignoreCase: ignorecase) else { return nil } + return self[index] } - @objc open func getDataSetByIndex(_ index: Int) -> IChartDataSet! + @objc open func getDataSetByIndex(_ index: Index) -> Element! { if index < 0 || index >= _dataSets.count { return nil } - return _dataSets[index] + return self[index] } - @objc open func addDataSet(_ dataSet: IChartDataSet!) + @objc open func addDataSet(_ dataSet: Element!) { calcMinMax(dataSet: dataSet) @@ -454,22 +393,15 @@ open class ChartData: NSObject /// Also recalculates all minimum and maximum values. /// /// - returns: `true` if a DataSet was removed, `false` ifno DataSet could be removed. - @objc @discardableResult open func removeDataSet(_ dataSet: IChartDataSet!) -> Bool + @objc @discardableResult open func removeDataSet(_ dataSet: Element!) -> Bool { - if dataSet === nil - { - return false - } - - for i in 0 ..< _dataSets.count - { - if _dataSets[i] === dataSet - { - return removeDataSetByIndex(i) - } - } - - return false + guard + dataSet != nil, + let index = index(where: { $0 === dataSet }) + else { return false } + + remove(at: index) + return true } /// Removes the DataSet at the given index in the DataSet array from the data object. @@ -478,13 +410,9 @@ open class ChartData: NSObject /// - returns: `true` if a DataSet was removed, `false` ifno DataSet could be removed. @objc @discardableResult open func removeDataSetByIndex(_ index: Int) -> Bool { - if index >= _dataSets.count || index < 0 - { - return false - } - - _dataSets.remove(at: index) - + guard indices.contains(index) else { return false } + + _ = remove(at: index) calcMinMax() return true @@ -493,31 +421,22 @@ open class ChartData: NSObject /// Adds an Entry to the DataSet at the specified index. Entries are added to the end of the list. @objc open func addEntry(_ e: ChartDataEntry, dataSetIndex: Int) { - if _dataSets.count > dataSetIndex && dataSetIndex >= 0 - { - let set = _dataSets[dataSetIndex] - - if !set.addEntry(e) { return } - - calcMinMax(entry: e, axis: set.axisDependency) - } - else - { - print("ChartData.addEntry() - Cannot add Entry because dataSetIndex too high or too low.", terminator: "\n") + guard indices.contains(dataSetIndex) else { + return print("ChartData.addEntry() - Cannot add Entry because dataSetIndex too high or too low.", terminator: "\n") } + + let set = self[dataSetIndex] + if !set.addEntry(e) { return } + calcMinMax(entry: e, axis: set.axisDependency) } /// Removes the given Entry object from the DataSet at the specified index. @objc @discardableResult open func removeEntry(_ entry: ChartDataEntry, dataSetIndex: Int) -> Bool { - // entry outofbounds - if dataSetIndex >= _dataSets.count - { - return false - } - + guard indices.contains(dataSetIndex) else { return false } + // remove the entry from the dataset - let removed = _dataSets[dataSetIndex].removeEntry(entry) + let removed = self[dataSetIndex].removeEntry(entry) if removed { @@ -532,105 +451,45 @@ open class ChartData: NSObject /// - returns: `true` if an entry was removed, `false` ifno Entry was found that meets the specified requirements. @objc @discardableResult open func removeEntry(xValue: Double, dataSetIndex: Int) -> Bool { - if dataSetIndex >= _dataSets.count - { - return false - } - - if let entry = _dataSets[dataSetIndex].entryForXValue(xValue, closestToY: Double.nan) - { - return removeEntry(entry, dataSetIndex: dataSetIndex) - } - - return false + guard + indices.contains(dataSetIndex), + let entry = self[dataSetIndex].entryForXValue(xValue, closestToY: .nan) + else { return false } + + return removeEntry(entry, dataSetIndex: dataSetIndex) } /// - returns: The DataSet that contains the provided Entry, or null, if no DataSet contains this entry. @objc open func getDataSetForEntry(_ e: ChartDataEntry!) -> IChartDataSet? { - if e == nil - { - return nil - } - - for i in 0 ..< _dataSets.count - { - let set = _dataSets[i] - - if e === set.entryForXValue(e.x, closestToY: e.y) - { - return set - } - } - - return nil + guard e != nil else { return nil } + + return first { $0.entryForXValue(e.x, closestToY: e.y) === e } } /// - returns: The index of the provided DataSet in the DataSet array of this data object, or -1 if it does not exist. @objc open func indexOfDataSet(_ dataSet: IChartDataSet) -> Int { - for i in 0 ..< _dataSets.count - { - if _dataSets[i] === dataSet - { - return i - } - } - - return -1 + return index(where: { $0 === dataSet }) ?? -1 } /// - returns: The first DataSet from the datasets-array that has it's dependency on the left axis. Returns null if no DataSet with left dependency could be found. @objc open func getFirstLeft(dataSets: [IChartDataSet]) -> IChartDataSet? { - for dataSet in dataSets - { - if dataSet.axisDependency == .left - { - return dataSet - } - } - - return nil + return first { $0.axisDependency == .left } } /// - returns: The first DataSet from the datasets-array that has it's dependency on the right axis. Returns null if no DataSet with right dependency could be found. @objc open func getFirstRight(dataSets: [IChartDataSet]) -> IChartDataSet? { - for dataSet in _dataSets - { - if dataSet.axisDependency == .right - { - return dataSet - } - } - - return nil + return first { $0.axisDependency == .right } } /// - returns: All colors used across all DataSet objects this object represents. + // TODO: This should return a non-optional array @objc open func getColors() -> [NSUIColor]? { - var clrcnt = 0 - - for i in 0 ..< _dataSets.count - { - clrcnt += _dataSets[i].colors.count - } - - var colors = [NSUIColor]() - - for i in 0 ..< _dataSets.count - { - let clrs = _dataSets[i].colors - - for clr in clrs - { - colors.append(clr) - } - } - - return colors + return flatMap { $0.colors.map { $0 } } } /// Sets a custom IValueFormatter for all DataSets this data object contains. @@ -638,38 +497,26 @@ open class ChartData: NSObject { guard let formatter = formatter else { return } - - for set in dataSets - { - set.valueFormatter = formatter - } + + forEach { $0.valueFormatter = formatter } } /// Sets the color of the value-text (color in which the value-labels are drawn) for all DataSets this data object contains. @objc open func setValueTextColor(_ color: NSUIColor!) { - for set in dataSets - { - set.valueTextColor = color ?? set.valueTextColor - } + forEach { $0.valueTextColor = color ?? $0.valueTextColor } } /// Sets the font for all value-labels for all DataSets this data object contains. @objc open func setValueFont(_ font: NSUIFont!) { - for set in dataSets - { - set.valueFont = font ?? set.valueFont - } + forEach { $0.valueFont = font ?? $0.valueFont } } /// Enables / disables drawing values (value-text) for all DataSets this data object contains. @objc open func setDrawValues(_ enabled: Bool) { - for set in dataSets - { - set.drawValuesEnabled = enabled - } + forEach { $0.drawValuesEnabled = enabled } } /// Enables / disables highlighting values for all DataSets this data object contains. @@ -678,22 +525,11 @@ open class ChartData: NSObject { get { - for set in dataSets - { - if !set.highlightEnabled - { - return false - } - } - - return true + return first { $0.highlightEnabled == false } == nil } set { - for set in dataSets - { - set.highlightEnabled = newValue - } + forEach { $0.highlightEnabled = newValue } } } @@ -712,49 +548,19 @@ open class ChartData: NSObject /// - returns: `true` if so, `false` ifnot. @objc open func contains(dataSet: IChartDataSet) -> Bool { - for set in dataSets - { - if set === dataSet - { - return true - } - } - - return false + return contains { $0 === dataSet } } /// - returns: The total entry count across all DataSet objects this data object contains. @objc open var entryCount: Int { - var count = 0 - - for set in _dataSets - { - count += set.entryCount - } - - return count + return reduce(0) { return $0 + $1.entryCount } } /// - returns: The DataSet object with the maximum number of entries or null if there are no DataSets. @objc open var maxEntryCountSet: IChartDataSet? { - if _dataSets.count == 0 - { - return nil - } - - var max = _dataSets[0] - - for set in _dataSets - { - if set.entryCount > max.entryCount - { - max = set - } - } - - return max + return self.max { $0.entryCount > $1.entryCount } } } @@ -863,7 +669,7 @@ extension ChartData { public func appendEntry(_ e: ChartDataEntry, toDataSet dataSetIndex: Index) { guard indices.contains(dataSetIndex) else { - print("ChartData.addEntry() - Cannot add Entry because dataSetIndex too high or too low.", terminator: "\n") + return print("ChartData.addEntry() - Cannot add Entry because dataSetIndex too high or too low.", terminator: "\n") } let set = self[dataSetIndex] diff --git a/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift b/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift index 9492f64de9..b9055260b4 100644 --- a/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift @@ -13,13 +13,13 @@ import Foundation open class CombinedChartData: BarLineScatterCandleBubbleChartData { - fileprivate var _lineData: LineChartData! - fileprivate var _barData: BarChartData! - fileprivate var _scatterData: ScatterChartData! - fileprivate var _candleData: CandleChartData! - fileprivate var _bubbleData: BubbleChartData! + private var _lineData: LineChartData! + private var _barData: BarChartData! + private var _scatterData: ScatterChartData! + private var _candleData: CandleChartData! + private var _bubbleData: BubbleChartData! - public override init() + public required init() { super.init() } @@ -237,27 +237,12 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData open override func notifyDataChanged() { - if _lineData !== nil - { - _lineData.notifyDataChanged() - } - if _barData !== nil - { - _barData.notifyDataChanged() - } - if _scatterData !== nil - { - _scatterData.notifyDataChanged() - } - if _candleData !== nil - { - _candleData.notifyDataChanged() - } - if _bubbleData !== nil - { - _bubbleData.notifyDataChanged() - } - + _lineData?.notifyDataChanged() + _barData?.notifyDataChanged() + _scatterData?.notifyDataChanged() + _candleData?.notifyDataChanged() + _bubbleData?.notifyDataChanged() + super.notifyDataChanged() // recalculate everything } diff --git a/Source/Charts/Data/Implementations/Standard/LineChartData.swift b/Source/Charts/Data/Implementations/Standard/LineChartData.swift index 2ebd6b42a9..c813b12030 100644 --- a/Source/Charts/Data/Implementations/Standard/LineChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/LineChartData.swift @@ -14,7 +14,7 @@ import Foundation /// Data object that encapsulates all data associated with a LineChart. open class LineChartData: ChartData { - public override init() + public required init() { super.init() } diff --git a/Source/Charts/Data/Implementations/Standard/PieChartData.swift b/Source/Charts/Data/Implementations/Standard/PieChartData.swift index f2dc35a948..8e31f1523d 100644 --- a/Source/Charts/Data/Implementations/Standard/PieChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/PieChartData.swift @@ -13,7 +13,7 @@ import Foundation open class PieChartData: ChartData { - public override init() + public required init() { super.init() } diff --git a/Source/Charts/Data/Implementations/Standard/RadarChartData.swift b/Source/Charts/Data/Implementations/Standard/RadarChartData.swift index 31fd7d2ba0..87bcf56e7c 100644 --- a/Source/Charts/Data/Implementations/Standard/RadarChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/RadarChartData.swift @@ -29,7 +29,7 @@ open class RadarChartData: ChartData self.labels = labels } - public override init() + public required init() { super.init() } diff --git a/Source/Charts/Data/Implementations/Standard/ScatterChartData.swift b/Source/Charts/Data/Implementations/Standard/ScatterChartData.swift index ff8ccaf93e..7be443a1e1 100644 --- a/Source/Charts/Data/Implementations/Standard/ScatterChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ScatterChartData.swift @@ -14,7 +14,7 @@ import CoreGraphics open class ScatterChartData: BarLineScatterCandleBubbleChartData { - public override init() + public required init() { super.init() } @@ -27,22 +27,14 @@ open class ScatterChartData: BarLineScatterCandleBubbleChartData /// - returns: The maximum shape-size across all DataSets. @objc open func getGreatestShapeSize() -> CGFloat { - var max = CGFloat(0.0) - - for set in _dataSets - { - let scatterDataSet = set as? IScatterChartDataSet - - if scatterDataSet == nil - { + return reduce(0) { (max, set) -> CGFloat in + guard let set = set as? IScatterChartDataSet else { print("ScatterChartData: Found a DataSet which is not a ScatterChartDataSet", terminator: "\n") + + return max } - else if let size = scatterDataSet?.scatterShapeSize, size > max - { - max = size - } + + return Swift.max(max, set.scatterShapeSize) } - - return max } } diff --git a/Source/Charts/Highlight/ChartHighlighter.swift b/Source/Charts/Highlight/ChartHighlighter.swift index d8e1d0bc4d..d001a47cb4 100644 --- a/Source/Charts/Highlight/ChartHighlighter.swift +++ b/Source/Charts/Highlight/ChartHighlighter.swift @@ -77,25 +77,14 @@ open class ChartHighlighter : NSObject, IHighlighter { var vals = [Highlight]() - guard let - data = self.data - else { return vals } + guard let data = self.data else { return vals } - for i in 0 ..< data.dataSetCount + for (i, set) in zip(data.indices, data) where set.isHighlightEnabled { - guard let dataSet = data.getDataSetByIndex(i) - else { continue } - - // don't include datasets that cannot be highlighted - if !dataSet.isHighlightEnabled - { - continue - } // extract all y-values from all DataSets at the given x-value. // some datasets (i.e bubble charts) make sense to have multiple values for an x-value. We'll have to find a way to handle that later on. It's more complicated now when x-indices are floating point. - - vals.append(contentsOf: buildHighlights(dataSet: dataSet, dataSetIndex: i, xValue: xValue, rounding: .closest)) + vals.append(contentsOf: buildHighlights(dataSet: set, dataSetIndex: i, xValue: xValue, rounding: .closest)) } return vals From 36ca566d70732772bf4225222b28575710a3a78e Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sun, 19 Nov 2017 21:10:47 -0500 Subject: [PATCH 05/34] Fixed required initializers --- Source/Charts/Data/Implementations/Standard/BarChartData.swift | 2 +- .../Standard/BarLineScatterCandleBubbleChartData.swift | 2 +- .../Charts/Data/Implementations/Standard/BubbleChartData.swift | 2 +- .../Charts/Data/Implementations/Standard/CandleChartData.swift | 2 +- Source/Charts/Data/Implementations/Standard/ChartData.swift | 3 ++- .../Data/Implementations/Standard/CombinedChartData.swift | 2 +- .../Charts/Data/Implementations/Standard/LineChartData.swift | 2 +- Source/Charts/Data/Implementations/Standard/PieChartData.swift | 2 +- .../Charts/Data/Implementations/Standard/RadarChartData.swift | 2 +- .../Data/Implementations/Standard/ScatterChartData.swift | 2 +- 10 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/BarChartData.swift b/Source/Charts/Data/Implementations/Standard/BarChartData.swift index 0317fc8a6b..050ea3a6fc 100644 --- a/Source/Charts/Data/Implementations/Standard/BarChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/BarChartData.swift @@ -14,7 +14,7 @@ import CoreGraphics open class BarChartData: BarLineScatterCandleBubbleChartData { - public override init() + public required init() { super.init() } diff --git a/Source/Charts/Data/Implementations/Standard/BarLineScatterCandleBubbleChartData.swift b/Source/Charts/Data/Implementations/Standard/BarLineScatterCandleBubbleChartData.swift index c98bb1d075..b5e0d6d421 100644 --- a/Source/Charts/Data/Implementations/Standard/BarLineScatterCandleBubbleChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/BarLineScatterCandleBubbleChartData.swift @@ -13,7 +13,7 @@ import Foundation open class BarLineScatterCandleBubbleChartData: ChartData { - public override init() + public required init() { super.init() } diff --git a/Source/Charts/Data/Implementations/Standard/BubbleChartData.swift b/Source/Charts/Data/Implementations/Standard/BubbleChartData.swift index 433f384f75..6cf729f152 100644 --- a/Source/Charts/Data/Implementations/Standard/BubbleChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/BubbleChartData.swift @@ -14,7 +14,7 @@ import CoreGraphics open class BubbleChartData: BarLineScatterCandleBubbleChartData { - public override init() + public required init() { super.init() } diff --git a/Source/Charts/Data/Implementations/Standard/CandleChartData.swift b/Source/Charts/Data/Implementations/Standard/CandleChartData.swift index 5158668ad9..401159e043 100644 --- a/Source/Charts/Data/Implementations/Standard/CandleChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/CandleChartData.swift @@ -13,7 +13,7 @@ import Foundation open class CandleChartData: BarLineScatterCandleBubbleChartData { - public override init() + public required init() { super.init() } diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index e40b95d3fd..42cc4941a2 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -24,7 +24,7 @@ open class ChartData: NSObject @objc internal var _dataSets = [IChartDataSet]() - public override init() + public override required init() { super.init() @@ -864,6 +864,7 @@ extension ChartData { public func appendEntry(_ e: ChartDataEntry, toDataSet dataSetIndex: Index) { guard indices.contains(dataSetIndex) else { print("ChartData.addEntry() - Cannot add Entry because dataSetIndex too high or too low.", terminator: "\n") + return } let set = self[dataSetIndex] diff --git a/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift b/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift index 9492f64de9..523c729318 100644 --- a/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift @@ -19,7 +19,7 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData fileprivate var _candleData: CandleChartData! fileprivate var _bubbleData: BubbleChartData! - public override init() + public required init() { super.init() } diff --git a/Source/Charts/Data/Implementations/Standard/LineChartData.swift b/Source/Charts/Data/Implementations/Standard/LineChartData.swift index 2ebd6b42a9..c813b12030 100644 --- a/Source/Charts/Data/Implementations/Standard/LineChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/LineChartData.swift @@ -14,7 +14,7 @@ import Foundation /// Data object that encapsulates all data associated with a LineChart. open class LineChartData: ChartData { - public override init() + public required init() { super.init() } diff --git a/Source/Charts/Data/Implementations/Standard/PieChartData.swift b/Source/Charts/Data/Implementations/Standard/PieChartData.swift index f2dc35a948..8e31f1523d 100644 --- a/Source/Charts/Data/Implementations/Standard/PieChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/PieChartData.swift @@ -13,7 +13,7 @@ import Foundation open class PieChartData: ChartData { - public override init() + public required init() { super.init() } diff --git a/Source/Charts/Data/Implementations/Standard/RadarChartData.swift b/Source/Charts/Data/Implementations/Standard/RadarChartData.swift index 31fd7d2ba0..87bcf56e7c 100644 --- a/Source/Charts/Data/Implementations/Standard/RadarChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/RadarChartData.swift @@ -29,7 +29,7 @@ open class RadarChartData: ChartData self.labels = labels } - public override init() + public required init() { super.init() } diff --git a/Source/Charts/Data/Implementations/Standard/ScatterChartData.swift b/Source/Charts/Data/Implementations/Standard/ScatterChartData.swift index ff8ccaf93e..58a63760b8 100644 --- a/Source/Charts/Data/Implementations/Standard/ScatterChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ScatterChartData.swift @@ -14,7 +14,7 @@ import CoreGraphics open class ScatterChartData: BarLineScatterCandleBubbleChartData { - public override init() + public required init() { super.init() } From cb32b0879f82da69c6e667277a769129ef9b72c3 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Fri, 1 Dec 2017 12:07:29 -0500 Subject: [PATCH 06/34] ChartData adopts ExressibleByArrayLiteral --- .../Data/Implementations/Standard/BarChartData.swift | 6 +++++- .../BarLineScatterCandleBubbleChartData.swift | 4 ++++ .../Implementations/Standard/BubbleChartData.swift | 6 +++++- .../Implementations/Standard/CandleChartData.swift | 4 ++++ .../Data/Implementations/Standard/ChartData.swift | 12 ++++++++++-- .../Implementations/Standard/CombinedChartData.swift | 4 ++++ .../Implementations/Standard/LineChartData.swift | 4 ++++ .../Data/Implementations/Standard/PieChartData.swift | 4 ++++ .../Implementations/Standard/RadarChartData.swift | 6 +++++- .../Implementations/Standard/ScatterChartData.swift | 4 ++++ 10 files changed, 49 insertions(+), 5 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/BarChartData.swift b/Source/Charts/Data/Implementations/Standard/BarChartData.swift index 050ea3a6fc..15673925eb 100644 --- a/Source/Charts/Data/Implementations/Standard/BarChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/BarChartData.swift @@ -23,7 +23,11 @@ open class BarChartData: BarLineScatterCandleBubbleChartData { super.init(dataSets: dataSets) } - + + public required init(arrayLiteral elements: IChartDataSet...) { + super.init(dataSets: elements) + } + /// The width of the bars on the x-axis, in values (not pixels) /// /// **default**: 0.85 diff --git a/Source/Charts/Data/Implementations/Standard/BarLineScatterCandleBubbleChartData.swift b/Source/Charts/Data/Implementations/Standard/BarLineScatterCandleBubbleChartData.swift index b5e0d6d421..0f4ed64595 100644 --- a/Source/Charts/Data/Implementations/Standard/BarLineScatterCandleBubbleChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/BarLineScatterCandleBubbleChartData.swift @@ -22,4 +22,8 @@ open class BarLineScatterCandleBubbleChartData: ChartData { super.init(dataSets: dataSets) } + + public required init(arrayLiteral elements: IChartDataSet...) { + super.init(dataSets: elements) + } } diff --git a/Source/Charts/Data/Implementations/Standard/BubbleChartData.swift b/Source/Charts/Data/Implementations/Standard/BubbleChartData.swift index 6cf729f152..c8255c4079 100644 --- a/Source/Charts/Data/Implementations/Standard/BubbleChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/BubbleChartData.swift @@ -23,7 +23,11 @@ open class BubbleChartData: BarLineScatterCandleBubbleChartData { super.init(dataSets: dataSets) } - + + public required init(arrayLiteral elements: IChartDataSet...) { + super.init(dataSets: elements) + } + /// Sets the width of the circle that surrounds the bubble when highlighted for all DataSet objects this data object contains @objc open func setHighlightCircleWidth(_ width: CGFloat) { diff --git a/Source/Charts/Data/Implementations/Standard/CandleChartData.swift b/Source/Charts/Data/Implementations/Standard/CandleChartData.swift index 401159e043..41f47d633c 100644 --- a/Source/Charts/Data/Implementations/Standard/CandleChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/CandleChartData.swift @@ -22,4 +22,8 @@ open class CandleChartData: BarLineScatterCandleBubbleChartData { super.init(dataSets: dataSets) } + + public required init(arrayLiteral elements: IChartDataSet...) { + super.init(dataSets: elements) + } } diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index 42cc4941a2..ffbeeff456 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -11,7 +11,7 @@ import Foundation -open class ChartData: NSObject +open class ChartData: NSObject, ExpressibleByArrayLiteral { @objc internal var _yMax: Double = -Double.greatestFiniteMagnitude @objc internal var _yMin: Double = Double.greatestFiniteMagnitude @@ -30,7 +30,15 @@ open class ChartData: NSObject _dataSets = [IChartDataSet]() } - + + public required init(arrayLiteral elements: IChartDataSet...) { + super.init() + + _dataSets = dataSets + + self.initialize(dataSets: _dataSets) + } + @objc public init(dataSets: [IChartDataSet]?) { super.init() diff --git a/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift b/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift index 523c729318..1b2cdcf9dc 100644 --- a/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift @@ -28,6 +28,10 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData { super.init(dataSets: dataSets) } + + public required init(arrayLiteral elements: IChartDataSet...) { + super.init(dataSets: elements) + } @objc open var lineData: LineChartData! { diff --git a/Source/Charts/Data/Implementations/Standard/LineChartData.swift b/Source/Charts/Data/Implementations/Standard/LineChartData.swift index c813b12030..69ef89d67d 100644 --- a/Source/Charts/Data/Implementations/Standard/LineChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/LineChartData.swift @@ -23,4 +23,8 @@ open class LineChartData: ChartData { super.init(dataSets: dataSets) } + + public required init(arrayLiteral elements: IChartDataSet...) { + super.init(dataSets: elements) + } } diff --git a/Source/Charts/Data/Implementations/Standard/PieChartData.swift b/Source/Charts/Data/Implementations/Standard/PieChartData.swift index 8e31f1523d..fcba8b8491 100644 --- a/Source/Charts/Data/Implementations/Standard/PieChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/PieChartData.swift @@ -23,6 +23,10 @@ open class PieChartData: ChartData super.init(dataSets: dataSets) } + public required init(arrayLiteral elements: IChartDataSet...) { + super.init(dataSets: elements) + } + @objc var dataSet: IPieChartDataSet? { get diff --git a/Source/Charts/Data/Implementations/Standard/RadarChartData.swift b/Source/Charts/Data/Implementations/Standard/RadarChartData.swift index 87bcf56e7c..334746c257 100644 --- a/Source/Charts/Data/Implementations/Standard/RadarChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/RadarChartData.swift @@ -38,7 +38,11 @@ open class RadarChartData: ChartData { super.init(dataSets: dataSets) } - + + public required init(arrayLiteral elements: IChartDataSet...) { + super.init(dataSets: elements) + } + open override func entryForHighlight(_ highlight: Highlight) -> ChartDataEntry? { return getDataSetByIndex(highlight.dataSetIndex)?.entryForIndex(Int(highlight.x)) diff --git a/Source/Charts/Data/Implementations/Standard/ScatterChartData.swift b/Source/Charts/Data/Implementations/Standard/ScatterChartData.swift index 58a63760b8..3c952ff509 100644 --- a/Source/Charts/Data/Implementations/Standard/ScatterChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ScatterChartData.swift @@ -23,6 +23,10 @@ open class ScatterChartData: BarLineScatterCandleBubbleChartData { super.init(dataSets: dataSets) } + + public required init(arrayLiteral elements: IChartDataSet...) { + super.init(dataSets: elements) + } /// - returns: The maximum shape-size across all DataSets. @objc open func getGreatestShapeSize() -> CGFloat From fcd9fa2c07665673fc4b8e429601381685e884f9 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Fri, 1 Dec 2017 12:22:09 -0500 Subject: [PATCH 07/34] Modified demos to take advantage of collection conformance. --- ChartsDemo/Swift/DemoBaseViewController.swift | 6 +++--- .../Swift/Demos/AnotherBarChartViewController.swift | 2 +- ChartsDemo/Swift/Demos/BarChartViewController.swift | 2 +- .../Swift/Demos/BubbleChartViewController.swift | 2 +- .../Swift/Demos/CandleStickChartViewController.swift | 2 +- .../Swift/Demos/CombinedChartViewController.swift | 6 +++--- .../Swift/Demos/CubicLineChartViewController.swift | 10 +++++----- .../Swift/Demos/LineChart1ViewController.swift | 10 +++++----- .../Swift/Demos/LineChart2ViewController.swift | 12 ++++++------ .../Swift/Demos/LineChartFilledViewController.swift | 2 +- .../Swift/Demos/LineChartTimeViewController.swift | 10 +++++----- .../Swift/Demos/MultipleBarChartViewController.swift | 2 +- .../Demos/MultipleLinesChartViewController.swift | 8 ++++---- .../Swift/Demos/RadarChartViewController.swift | 6 +++--- .../Swift/Demos/ScatterChartViewController.swift | 2 +- .../Data/Implementations/Standard/ChartData.swift | 3 ++- 16 files changed, 43 insertions(+), 42 deletions(-) diff --git a/ChartsDemo/Swift/DemoBaseViewController.swift b/ChartsDemo/Swift/DemoBaseViewController.swift index c19c174ee9..683dfb95eb 100644 --- a/ChartsDemo/Swift/DemoBaseViewController.swift +++ b/ChartsDemo/Swift/DemoBaseViewController.swift @@ -117,13 +117,13 @@ class DemoBaseViewController: UIViewController, ChartViewDelegate { func handleOption(_ option: Option, forChartView chartView: ChartViewBase) { switch option { case .toggleValues: - for set in chartView.data!.dataSets { + for set in chartView.data! { set.drawValuesEnabled = !set.drawValuesEnabled } chartView.setNeedsDisplay() case .toggleIcons: - for set in chartView.data!.dataSets { + for set in chartView.data! { set.drawIconsEnabled = !set.drawIconsEnabled } chartView.setNeedsDisplay() @@ -159,7 +159,7 @@ class DemoBaseViewController: UIViewController, ChartViewDelegate { updateChartData() case .toggleBarBorders: - for set in chartView.data!.dataSets { + for set in chartView.data! { if let set = set as? BarChartDataSet { set.barBorderWidth = set.barBorderWidth == 1.0 ? 0.0 : 1.0 } diff --git a/ChartsDemo/Swift/Demos/AnotherBarChartViewController.swift b/ChartsDemo/Swift/Demos/AnotherBarChartViewController.swift index abe86cd94a..1168f6b55d 100644 --- a/ChartsDemo/Swift/Demos/AnotherBarChartViewController.swift +++ b/ChartsDemo/Swift/Demos/AnotherBarChartViewController.swift @@ -66,7 +66,7 @@ class AnotherBarChartViewController: DemoBaseViewController { } var set1: BarChartDataSet! = nil - if let set = chartView.data?.dataSets.first as? BarChartDataSet { + if let set = chartView.data?.first as? BarChartDataSet { set1 = set set1?.values = yVals chartView.data?.notifyDataChanged() diff --git a/ChartsDemo/Swift/Demos/BarChartViewController.swift b/ChartsDemo/Swift/Demos/BarChartViewController.swift index a9a629120a..60be8856c1 100644 --- a/ChartsDemo/Swift/Demos/BarChartViewController.swift +++ b/ChartsDemo/Swift/Demos/BarChartViewController.swift @@ -119,7 +119,7 @@ class BarChartViewController: DemoBaseViewController { } var set1: BarChartDataSet! = nil - if let set = chartView.data?.dataSets.first as? BarChartDataSet { + if let set = chartView.data?.first as? BarChartDataSet { set1 = set set1.values = yVals chartView.data?.notifyDataChanged() diff --git a/ChartsDemo/Swift/Demos/BubbleChartViewController.swift b/ChartsDemo/Swift/Demos/BubbleChartViewController.swift index 61d944b735..4aa1645f73 100644 --- a/ChartsDemo/Swift/Demos/BubbleChartViewController.swift +++ b/ChartsDemo/Swift/Demos/BubbleChartViewController.swift @@ -104,7 +104,7 @@ class BubbleChartViewController: DemoBaseViewController { set3.setColor(ChartColorTemplates.colorful()[2], alpha: 0.5) set3.drawValuesEnabled = true - let data = BubbleChartData(dataSets: [set1, set2, set3]) + let data = [set1, set2, set3] as BubbleChartData data.setDrawValues(false) data.setValueFont(UIFont(name: "HelveticaNeue-Light", size: 7)!) data.setHighlightCircleWidth(1.5) diff --git a/ChartsDemo/Swift/Demos/CandleStickChartViewController.swift b/ChartsDemo/Swift/Demos/CandleStickChartViewController.swift index 082ba9d269..d6138b66fc 100644 --- a/ChartsDemo/Swift/Demos/CandleStickChartViewController.swift +++ b/ChartsDemo/Swift/Demos/CandleStickChartViewController.swift @@ -104,7 +104,7 @@ class CandleStickChartViewController: DemoBaseViewController { override func optionTapped(_ option: Option) { if .toggleShadowColorSameAsCandle ~= option { - for set in chartView.data!.dataSets as! [CandleChartDataSet] { + for set in chartView.data as! CandleChartData { set.shadowColorSameAsCandle = !set.shadowColorSameAsCandle } chartView.notifyDataSetChanged() diff --git a/ChartsDemo/Swift/Demos/CombinedChartViewController.swift b/ChartsDemo/Swift/Demos/CombinedChartViewController.swift index d3162293a7..9efd9079d7 100644 --- a/ChartsDemo/Swift/Demos/CombinedChartViewController.swift +++ b/ChartsDemo/Swift/Demos/CombinedChartViewController.swift @@ -94,7 +94,7 @@ class CombinedChartViewController: DemoBaseViewController { override func optionTapped(_ option: Option) { switch option { case .toggleLineValues: - for set in chartView.data!.dataSets { + for set in chartView.data! { if let set = set as? LineChartDataSet { set.drawValuesEnabled = !set .drawValuesEnabled @@ -103,7 +103,7 @@ class CombinedChartViewController: DemoBaseViewController { chartView.setNeedsDisplay() case .toggleBarValues: - for set in chartView.data!.dataSets { + for set in chartView.data! { if let set = set as? BarChartDataSet { set.drawValuesEnabled = !set .drawValuesEnabled } @@ -171,7 +171,7 @@ class CombinedChartViewController: DemoBaseViewController { let barWidth = 0.45 // x2 dataset // (0.45 + 0.02) * 2 + 0.06 = 1.00 -> interval per "group" - let data = BarChartData(dataSets: [set1, set2]) + let data: BarChartData = [set1, set2] data.barWidth = barWidth // make this BarData object grouped diff --git a/ChartsDemo/Swift/Demos/CubicLineChartViewController.swift b/ChartsDemo/Swift/Demos/CubicLineChartViewController.swift index 627d13d08d..e0e5db902b 100644 --- a/ChartsDemo/Swift/Demos/CubicLineChartViewController.swift +++ b/ChartsDemo/Swift/Demos/CubicLineChartViewController.swift @@ -111,31 +111,31 @@ class CubicLineChartViewController: DemoBaseViewController { override func optionTapped(_ option: Option) { switch option { case .toggleFilled: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.drawFilledEnabled = !set.drawFilledEnabled } chartView.setNeedsDisplay() case .toggleCircles: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.drawCirclesEnabled = !set.drawCirclesEnabled } chartView.setNeedsDisplay() case .toggleCubic: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.mode = (set.mode == .cubicBezier) ? .linear : .cubicBezier } chartView.setNeedsDisplay() case .toggleStepped: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.mode = (set.mode == .stepped) ? .linear : .stepped } chartView.setNeedsDisplay() case .toggleHorizontalCubic: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.mode = (set.mode == .cubicBezier) ? .horizontalBezier : .cubicBezier } chartView.setNeedsDisplay() diff --git a/ChartsDemo/Swift/Demos/LineChart1ViewController.swift b/ChartsDemo/Swift/Demos/LineChart1ViewController.swift index bf5eb7f67d..e97c05dcf5 100644 --- a/ChartsDemo/Swift/Demos/LineChart1ViewController.swift +++ b/ChartsDemo/Swift/Demos/LineChart1ViewController.swift @@ -144,31 +144,31 @@ class LineChart1ViewController: DemoBaseViewController { override func optionTapped(_ option: Option) { switch option { case .toggleFilled: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.drawFilledEnabled = !set.drawFilledEnabled } chartView.setNeedsDisplay() case .toggleCircles: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.drawCirclesEnabled = !set.drawCirclesEnabled } chartView.setNeedsDisplay() case .toggleCubic: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.mode = (set.mode == .cubicBezier) ? .linear : .cubicBezier } chartView.setNeedsDisplay() case .toggleStepped: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.mode = (set.mode == .stepped) ? .linear : .stepped } chartView.setNeedsDisplay() case .toggleHorizontalCubic: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.mode = (set.mode == .cubicBezier) ? .horizontalBezier : .cubicBezier } chartView.setNeedsDisplay() diff --git a/ChartsDemo/Swift/Demos/LineChart2ViewController.swift b/ChartsDemo/Swift/Demos/LineChart2ViewController.swift index 564d50f66d..dca194a61e 100644 --- a/ChartsDemo/Swift/Demos/LineChart2ViewController.swift +++ b/ChartsDemo/Swift/Demos/LineChart2ViewController.swift @@ -135,7 +135,7 @@ class LineChart2ViewController: DemoBaseViewController { set3.highlightColor = UIColor(red: 244/255, green: 117/255, blue: 117/255, alpha: 1) set3.drawCircleHoleEnabled = false - let data = LineChartData(dataSets: [set1, set2, set3]) + let data: LineChartData = [set1, set2, set3] data.setValueTextColor(.white) data.setValueFont(.systemFont(ofSize: 9)) @@ -145,31 +145,31 @@ class LineChart2ViewController: DemoBaseViewController { override func optionTapped(_ option: Option) { switch option { case .toggleFilled: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.drawFilledEnabled = !set.drawFilledEnabled } chartView.setNeedsDisplay() case .toggleCircles: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.drawCirclesEnabled = !set.drawCirclesEnabled } chartView.setNeedsDisplay() case .toggleCubic: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.mode = (set.mode == .cubicBezier) ? .linear : .cubicBezier } chartView.setNeedsDisplay() case .toggleStepped: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.mode = (set.mode == .stepped) ? .linear : .stepped } chartView.setNeedsDisplay() case .toggleHorizontalCubic: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.mode = (set.mode == .cubicBezier) ? .horizontalBezier : .cubicBezier } chartView.setNeedsDisplay() diff --git a/ChartsDemo/Swift/Demos/LineChartFilledViewController.swift b/ChartsDemo/Swift/Demos/LineChartFilledViewController.swift index 11726e4e4f..21f20b3839 100644 --- a/ChartsDemo/Swift/Demos/LineChartFilledViewController.swift +++ b/ChartsDemo/Swift/Demos/LineChartFilledViewController.swift @@ -102,7 +102,7 @@ class LineChartFilledViewController: DemoBaseViewController { return CGFloat(self.chartView.leftAxis.axisMaximum) } - let data = LineChartData(dataSets: [set1, set2]) + let data: LineChartData = [set1, set2] data.setDrawValues(false) chartView.data = data diff --git a/ChartsDemo/Swift/Demos/LineChartTimeViewController.swift b/ChartsDemo/Swift/Demos/LineChartTimeViewController.swift index 1b4c425ba6..664c0034bf 100644 --- a/ChartsDemo/Swift/Demos/LineChartTimeViewController.swift +++ b/ChartsDemo/Swift/Demos/LineChartTimeViewController.swift @@ -120,31 +120,31 @@ class LineChartTimeViewController: DemoBaseViewController { override func optionTapped(_ option: Option) { switch option { case .toggleFilled: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.drawFilledEnabled = !set.drawFilledEnabled } chartView.setNeedsDisplay() case .toggleCircles: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.drawCirclesEnabled = !set.drawCirclesEnabled } chartView.setNeedsDisplay() case .toggleCubic: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.mode = (set.mode == .cubicBezier) ? .linear : .cubicBezier } chartView.setNeedsDisplay() case .toggleStepped: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.mode = (set.mode == .stepped) ? .linear : .stepped } chartView.setNeedsDisplay() case .toggleHorizontalCubic: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.mode = (set.mode == .cubicBezier) ? .horizontalBezier : .cubicBezier } chartView.setNeedsDisplay() diff --git a/ChartsDemo/Swift/Demos/MultipleBarChartViewController.swift b/ChartsDemo/Swift/Demos/MultipleBarChartViewController.swift index 93c9f4654b..8f423312e3 100644 --- a/ChartsDemo/Swift/Demos/MultipleBarChartViewController.swift +++ b/ChartsDemo/Swift/Demos/MultipleBarChartViewController.swift @@ -119,7 +119,7 @@ class MultipleBarChartViewController: DemoBaseViewController { let set4 = BarChartDataSet(values: yVals4, label: "Company D") set4.setColor(UIColor(red: 255/255, green: 102/255, blue: 0/255, alpha: 1)) - let data = BarChartData(dataSets: [set1, set2, set3, set4]) + let data: BarChartData = [set1, set2, set3, set4] data.setValueFont(.systemFont(ofSize: 10, weight: .light)) data.setValueFormatter(LargeValueFormatter()) diff --git a/ChartsDemo/Swift/Demos/MultipleLinesChartViewController.swift b/ChartsDemo/Swift/Demos/MultipleLinesChartViewController.swift index 4b11f73d28..88823b5538 100644 --- a/ChartsDemo/Swift/Demos/MultipleLinesChartViewController.swift +++ b/ChartsDemo/Swift/Demos/MultipleLinesChartViewController.swift @@ -101,25 +101,25 @@ class MultipleLinesChartViewController: DemoBaseViewController { override func optionTapped(_ option: Option) { switch option { case .toggleFilled: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.drawFilledEnabled = !set.drawFilledEnabled } chartView.setNeedsDisplay() case .toggleCircles: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.drawCirclesEnabled = !set.drawCirclesEnabled } chartView.setNeedsDisplay() case .toggleCubic: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.mode = (set.mode == .cubicBezier) ? .linear : .cubicBezier } chartView.setNeedsDisplay() case .toggleStepped: - for set in chartView.data!.dataSets as! [LineChartDataSet] { + for set in chartView.data as! LineChartData { set.mode = (set.mode == .stepped) ? .linear : .stepped } chartView.setNeedsDisplay() diff --git a/ChartsDemo/Swift/Demos/RadarChartViewController.swift b/ChartsDemo/Swift/Demos/RadarChartViewController.swift index f90bb74c6c..48f5447d0e 100644 --- a/ChartsDemo/Swift/Demos/RadarChartViewController.swift +++ b/ChartsDemo/Swift/Demos/RadarChartViewController.swift @@ -142,7 +142,7 @@ class RadarChartViewController: DemoBaseViewController { set2.drawHighlightCircleEnabled = true set2.setDrawHighlightIndicators(false) - let data = RadarChartData(dataSets: [set1, set2]) + let data: RadarChartData = [set1, set2] data.setValueFont(.systemFont(ofSize: 8, weight: .light)) data.setDrawValues(false) data.setValueTextColor(.white) @@ -166,14 +166,14 @@ class RadarChartViewController: DemoBaseViewController { chartView.rotationEnabled = !chartView.rotationEnabled case .toggleFilled: - for set in chartView.data!.dataSets as! [RadarChartDataSet] { + for set in chartView.data as! RadarChartData { set.drawFilledEnabled = !set.drawFilledEnabled } chartView.setNeedsDisplay() case .toggleHighlightCircle: - for set in chartView.data!.dataSets as! [RadarChartDataSet] { + for set in chartView.data as! RadarChartData { set.drawHighlightCircleEnabled = !set.drawHighlightCircleEnabled } chartView.setNeedsDisplay() diff --git a/ChartsDemo/Swift/Demos/ScatterChartViewController.swift b/ChartsDemo/Swift/Demos/ScatterChartViewController.swift index 2dea8e3627..357c329d67 100644 --- a/ChartsDemo/Swift/Demos/ScatterChartViewController.swift +++ b/ChartsDemo/Swift/Demos/ScatterChartViewController.swift @@ -105,7 +105,7 @@ class ScatterChartViewController: DemoBaseViewController { set3.setColor(ChartColorTemplates.colorful()[2]) set3.scatterShapeSize = 8 - let data = ScatterChartData(dataSets: [set1, set2, set3]) + let data: ScatterChartData = [set1, set2, set3] data.setValueFont(.systemFont(ofSize: 7, weight: .light)) chartView.data = data diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index 131b338558..64d50e8618 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -677,7 +677,8 @@ extension ChartData { public func appendEntry(_ e: ChartDataEntry, toDataSet dataSetIndex: Index) { guard indices.contains(dataSetIndex) else { - return print("ChartData.addEntry() - Cannot add Entry because dataSetIndex too high or too low.", terminator: "\n") + print("ChartData.addEntry() - Cannot add Entry because dataSetIndex too high or too low.", terminator: "\n") + return } let set = self[dataSetIndex] From 01d0753fe57c7dc118ff99aec877b7255618f19b Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Mon, 11 Dec 2017 12:11:20 -0400 Subject: [PATCH 08/34] Pulled latest master --- .../Data/Implementations/Standard/CandleChartDataSet.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/CandleChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/CandleChartDataSet.swift index 6391a758d5..212f1166d3 100644 --- a/Source/Charts/Data/Implementations/Standard/CandleChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/CandleChartDataSet.swift @@ -56,8 +56,8 @@ open class CandleChartDataSet: LineScatterCandleRadarChartDataSet, ICandleChartD /// the space between the candle entries /// /// **default**: 0.1 (10%) - fileprivate 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 From 1d819d7b9ef6325725732012fab641a29770d140 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Wed, 13 Dec 2017 22:02:23 -0400 Subject: [PATCH 09/34] Pulled latest master --- .../Standard/ChartDataSet.swift | 77 +++++++++---------- 1 file changed, 36 insertions(+), 41 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift index c7ccb4e9de..955d25e697 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift @@ -26,24 +26,24 @@ open class ChartDataSet: ChartBaseDataSet { public required init() { - _values = [ChartDataEntry]() - + values = [] + super.init() } public override init(label: String) { - _values = [ChartDataEntry]() - + values = [] + super.init(label: label) } @objc public init(values: [ChartDataEntry], label: String) { - _values = values - + self.values = values + super.init(label: label) - + self.calcMinMax() } @@ -53,12 +53,16 @@ open class ChartDataSet: ChartBaseDataSet } // MARK: - Data functions and accessors - + /// * /// - note: Calls `notifyDataSetChanged()` after setting a new value. /// - returns: The array of y-values that this DataSet represents. /// the entries that this dataset represents / holds together - internal var _values: [ChartDataEntry] + @objc open var values: [ChartDataEntry] { + didSet { + notifyDataSetChanged() + } + } /// maximum y-value in the value array internal var _yMax: Double = -Double.greatestFiniteMagnitude @@ -74,20 +78,20 @@ open class ChartDataSet: ChartBaseDataSet open override func calcMinMax() { - guard !_values.isEmpty else { return } + guard !values.isEmpty else { return } _yMax = -Double.greatestFiniteMagnitude _yMin = Double.greatestFiniteMagnitude _xMax = -Double.greatestFiniteMagnitude _xMin = Double.greatestFiniteMagnitude - - _values.forEach { calcMinMax(entry: $0) } + + values.forEach { calcMinMax(entry: $0) } } open override func calcMinMaxY(fromX: Double, toX: Double) { - guard !_values.isEmpty else { return } - + guard !values.isEmpty else { return } + _yMax = -Double.greatestFiniteMagnitude _yMin = Double.greatestFiniteMagnitude @@ -95,7 +99,7 @@ open class ChartDataSet: ChartBaseDataSet let indexTo = entryIndex(x: toX, closestToY: Double.nan, rounding: .up) guard indexTo >= indexFrom else { return } - (indexFrom...indexTo).forEach { calcMinMaxY(entry: _values[$0]) } // only recalculate y + (indexFrom...indexTo).forEach { calcMinMaxY(entry: values[$0]) } // only recalculate y } @objc open func calcMinMaxX(entry e: ChartDataEntry) @@ -132,14 +136,14 @@ open class ChartDataSet: ChartBaseDataSet @objc open override var xMax: Double { return _xMax } /// - returns: The number of y-values this DataSet represents - @objc open override var entryCount: Int { return _values.count } + open override var entryCount: Int { return values.count } /// - returns: The entry object found at the given index (not x-value!) /// - throws: out of bounds /// if `i` is out of bounds, it may throw an out-of-bounds exception open override func entryForIndex(_ i: Int) -> ChartDataEntry? { - guard _values.indices.contains(i) else { + guard i >= values.startIndex, i < values.endIndex else { return nil } return values[i] @@ -293,12 +297,12 @@ open class ChartDataSet: ChartBaseDataSet { let closestXValue = values[closest].x - if rounding == .up, closestXValue < xValue, closest < _values.endIndex - 1 + 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... closest += 1 } - else if rounding == .down, closestXValue > xValue, closest > _values.startIndex + 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... closest -= 1 @@ -343,7 +347,7 @@ open class ChartDataSet: ChartBaseDataSet // TODO: Should be returning `nil` to follow Swift convention open override func entryIndex(entry e: ChartDataEntry) -> Int { - return _values.index { $0 === e } ?? -1 + return values.index { $0 === e } ?? -1 } /// Adds an Entry to the DataSet dynamically. @@ -395,11 +399,10 @@ open class ChartDataSet: ChartBaseDataSet // TODO: This should return the removed entry to follow Swift convention. open override func removeEntry(_ entry: ChartDataEntry) -> Bool { - guard let i = _values.index(where: { $0 === entry }) else { return false } - - _values.remove(at: i) - calcMinMax() - + guard let i = values.index(where: { $0 === entry }) else { return false } + + values.remove(at: i) + return true } @@ -409,33 +412,25 @@ open class ChartDataSet: ChartBaseDataSet // TODO: This should return the removed entry to follow Swift convention. open override func removeFirst() -> Bool { - guard !_values.isEmpty else { return false } - - _values.removeFirst() - calcMinMax() - - return true + let entry: ChartDataEntry? = values.isEmpty ? nil : values.removeFirst() + return entry != nil } /// Removes the last Entry (at index size-1) of this DataSet from the entries array. /// /// - returns: `true` if successful, `false` ifnot. // TODO: This should return the removed entry to follow Swift convention. - open override func removeLast() -> Bool + open override func removeLast() -> Bool { - guard !_values.isEmpty else { return false } - - _values.removeLast() - calcMinMax() - - return true + let entry: ChartDataEntry? = values.isEmpty ? nil : values.removeLast() + return entry != nil } /// Checks if this DataSet contains the specified Entry. /// - returns: `true` if contains the entry, `false` if not. open override func contains(_ e: ChartDataEntry) -> Bool { - return _values.contains(e) + return values.contains(e) } /// Removes all values from this DataSet and recalculates min and max value. @@ -445,7 +440,7 @@ open class ChartDataSet: ChartBaseDataSet } // MARK: - Data functions and accessors - + // MARK: - NSCopying open override func copyWithZone(_ zone: NSZone?) -> AnyObject @@ -455,7 +450,7 @@ open class ChartDataSet: ChartBaseDataSet copy.values = values copy._yMax = _yMax copy._yMin = _yMin - + return copy } } From ca5afad0bb15bdfb57ff0ea8ea0fbe0bc64ab9c6 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Wed, 10 Jan 2018 21:31:57 -0400 Subject: [PATCH 10/34] Updates for PR Also added remove subrange. --- .../Implementations/Standard/ChartData.swift | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index 59477f0695..c9609429d4 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -33,7 +33,7 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral { super.init() - _dataSets = dataSets + _dataSets = elements self.initialize(dataSets: _dataSets) } @@ -807,47 +807,47 @@ extension ChartData: RangeReplaceableCollection { public func append(_ newElement: Element) { - self._dataSets.append(newElement) + _dataSets.append(newElement) calcMinMax(dataSet: newElement) } public func remove(at position: Index) -> Element { - let element = self._dataSets.remove(at: position) + let element = _dataSets.remove(at: position) calcMinMax() return element } public func removeFirst() -> Element { - let element = self._dataSets.removeFirst() + let element = _dataSets.removeFirst() notifyDataChanged() return element } public func removeFirst(_ n: Int) { - self._dataSets.removeFirst(n) + _dataSets.removeFirst(n) notifyDataChanged() } public func removeLast() -> Element { - let element = self._dataSets.removeLast() + let element = _dataSets.removeLast() notifyDataChanged() return element } public func removeLast(_ n: Int) { - self._dataSets.removeLast(n) + _dataSets.removeLast(n) notifyDataChanged() } -// public func removeSubrange(_ bounds: Range) { -// self.dataSets.removeSubrange(bounds) -// notifyDataChanged() -// } + public func removeSubrange(_ bounds: R) where R : RangeExpression, ChartData.Index == R.Bound { + _dataSets.removeSubrange(bounds) + notifyDataChanged() + } } // MARK: Swift Accessors From fdae403eb0a97c87332083d61efa8f5b02289143 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Wed, 10 Jan 2018 23:50:42 -0400 Subject: [PATCH 11/34] Refactored ChartData Removed redundancy from min/max logic. Lots of naming changes. Cleaner implementations. --- Charts.xcodeproj/project.pbxproj | 88 ++-- .../Charts/Charts/BarLineChartViewBase.swift | 4 +- Source/Charts/Charts/ChartViewBase.swift | 8 +- Source/Charts/Charts/CombinedChartView.swift | 2 +- .../Standard/BarChartData.swift | 2 +- .../BarLineScatterCandleBubbleChartData.swift | 2 +- .../Standard/BubbleChartData.swift | 2 +- .../Standard/CandleChartData.swift | 2 +- .../Implementations/Standard/ChartData.swift | 429 +++++------------- .../Standard/CombinedChartData.swift | 84 ++-- .../Standard/LineChartData.swift | 2 +- .../Standard/PieChartData.swift | 26 +- .../Standard/RadarChartData.swift | 6 +- .../Standard/ScatterChartData.swift | 2 +- .../Interfaces/ChartDataSetProtocol.swift | 2 +- Source/Charts/Highlight/BarHighlighter.swift | 2 +- .../Highlight/CombinedHighlighter.swift | 2 +- .../Highlight/HorizontalBarHighlighter.swift | 2 +- .../Charts/Highlight/RadarHighlighter.swift | 2 +- .../Charts/Renderers/BarChartRenderer.swift | 4 +- .../Renderers/BubbleChartRenderer.swift | 2 +- .../Renderers/CandleStickChartRenderer.swift | 2 +- Source/Charts/Renderers/LegendRenderer.swift | 2 +- .../Charts/Renderers/LineChartRenderer.swift | 6 +- .../Charts/Renderers/PieChartRenderer.swift | 2 +- .../Charts/Renderers/RadarChartRenderer.swift | 4 +- .../Renderers/ScatterChartRenderer.swift | 4 +- 27 files changed, 244 insertions(+), 451 deletions(-) diff --git a/Charts.xcodeproj/project.pbxproj b/Charts.xcodeproj/project.pbxproj index 7bd02ef15b..5bf7a1a0ce 100644 --- a/Charts.xcodeproj/project.pbxproj +++ b/Charts.xcodeproj/project.pbxproj @@ -109,8 +109,8 @@ B0D28C68BB9A958DC56EB214 /* DefaultValueFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 107D8F8163EE54D6D9E916B0 /* DefaultValueFormatter.swift */; }; B13C74B4FF705D7B595D01EF /* AxisValueFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0BD9DF16AF59680A3BB49452 /* AxisValueFormatter.swift */; }; B539114951455C35BADAE3F3 /* PieChartDataSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4FB5E3761EF8B4D1E1E1014 /* PieChartDataSet.swift */; }; - B6C9F450D937B87224D29D5C /* FillFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 818AC6B12505B7C0A53D62F9 /* FillFormatter.swift */; }; B6BF9A561F91993A00E62A5D /* CombinedChartTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6BF9A551F91993A00E62A5D /* CombinedChartTests.swift */; }; + B6C9F450D937B87224D29D5C /* FillFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 818AC6B12505B7C0A53D62F9 /* FillFormatter.swift */; }; B6DCC229615EFE706F64A37D /* LineScatterCandleRadarRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 923206233CA89FD03565FF87 /* LineScatterCandleRadarRenderer.swift */; }; B85DEB06B4C1AFFC8A0E3295 /* CircleShapeRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECE1B1623D3AF69CECAE8562 /* CircleShapeRenderer.swift */; }; BEFD9518F3A74ACF8FA33308 /* Charts.h in Headers */ = {isa = PBXBuildFile; fileRef = 4F9922F0641F7955DC6CD324 /* Charts.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -356,7 +356,9 @@ 098621EDFBF928494B94BEA1 /* Data */ = { isa = PBXGroup; children = ( - 3B9DD76FCE8D873300A822C7 /* Implementations */, + 22C014222006FFE800D5B025 /* ChartData */, + 3B9DD76FCE8D873300A822C7 /* ChartDataSet */, + 22C014232006FFFA00D5B025 /* ChartEntry */, DB2D9648877455028EBEAA8F /* DataSet Protocols */, ); name = Data; @@ -405,6 +407,37 @@ name = ChartRenderers; sourceTree = ""; }; + 22C014222006FFE800D5B025 /* ChartData */ = { + isa = PBXGroup; + children = ( + 0108D5925E21A47DA36A66AA /* BarChartData.swift */, + 559DB735FEA17AB90676D6CA /* BarLineScatterCandleBubbleChartData.swift */, + 7EDA3AD550AEFC93C8D15B9C /* BubbleChartData.swift */, + F4785FEACAE4367F36FB8868 /* CandleChartData.swift */, + E120E76C6F1B5877D56126DD /* ChartData.swift */, + 0DDE409E9ECF54D2C146A6F0 /* CombinedChartData.swift */, + 4C978F31F23C7D21197DC2A1 /* LineChartData.swift */, + 6E03A4987F72414A02A0631B /* PieChartData.swift */, + 4BBB57D6FA41029B08F26D7B /* RadarChartData.swift */, + E7AD2FC320A16CA1EE0A52F4 /* ScatterChartData.swift */, + ); + name = ChartData; + sourceTree = ""; + }; + 22C014232006FFFA00D5B025 /* ChartEntry */ = { + isa = PBXGroup; + children = ( + E3F8BFF1CBC58D5B9DBFFB9B /* BarChartDataEntry.swift */, + DD8ED233775EEC31243A6919 /* BubbleChartDataEntry.swift */, + D2E698FF540029B70AC97AD7 /* CandleChartDataEntry.swift */, + F22750328058DEC2F019646F /* ChartDataEntry.swift */, + 12409C3EA15787C11AF0D2BC /* ChartDataEntryBase.swift */, + BD02157CF8CEE1189BF681DA /* PieChartDataEntry.swift */, + 91EEEDE2AB8F2DA3AFCF0733 /* RadarChartDataEntry.swift */, + ); + name = ChartEntry; + sourceTree = ""; + }; 2647844720BC6574A544A337 /* Charts */ = { isa = PBXGroup; children = ( @@ -423,13 +456,23 @@ name = Charts; sourceTree = ""; }; - 3B9DD76FCE8D873300A822C7 /* Implementations */ = { + 3B9DD76FCE8D873300A822C7 /* ChartDataSet */ = { isa = PBXGroup; children = ( C9FE42E868A225C116537368 /* ChartBaseDataSet.swift */, - 740017197A160047EBB8A9A0 /* Standard */, + C31AA65EA27776F8C653C7E8 /* BarChartDataSet.swift */, + 6D717F0808DE7EC8A4AE9C2A /* BarLineScatterCandleBubbleChartDataSet.swift */, + B44829AF0ADA583F1F0279B7 /* BubbleChartDataSet.swift */, + 274116834B1B0345D622E027 /* CandleChartDataSet.swift */, + 6CEC0C69C89CE9B99F3B4409 /* ChartDataSet.swift */, + A5A75AA73C5AA381DA517959 /* LineChartDataSet.swift */, + 45E31A4356CC6F283C29954B /* LineRadarChartDataSet.swift */, + C58BD7B14BEA440783ED8D2B /* LineScatterCandleRadarChartDataSet.swift */, + A4FB5E3761EF8B4D1E1E1014 /* PieChartDataSet.swift */, + B1BA6B21CBDF77A15848994F /* RadarChartDataSet.swift */, + CB1DD1A0F64266A10EE94194 /* ScatterChartDataSet.swift */, ); - name = Implementations; + name = ChartDataSet; sourceTree = ""; }; 42824E1F334B0C484AF4C594 /* Highlight */ = { @@ -467,41 +510,6 @@ name = Components; sourceTree = ""; }; - 740017197A160047EBB8A9A0 /* Standard */ = { - isa = PBXGroup; - children = ( - 0108D5925E21A47DA36A66AA /* BarChartData.swift */, - E3F8BFF1CBC58D5B9DBFFB9B /* BarChartDataEntry.swift */, - C31AA65EA27776F8C653C7E8 /* BarChartDataSet.swift */, - 559DB735FEA17AB90676D6CA /* BarLineScatterCandleBubbleChartData.swift */, - 6D717F0808DE7EC8A4AE9C2A /* BarLineScatterCandleBubbleChartDataSet.swift */, - 7EDA3AD550AEFC93C8D15B9C /* BubbleChartData.swift */, - DD8ED233775EEC31243A6919 /* BubbleChartDataEntry.swift */, - B44829AF0ADA583F1F0279B7 /* BubbleChartDataSet.swift */, - F4785FEACAE4367F36FB8868 /* CandleChartData.swift */, - D2E698FF540029B70AC97AD7 /* CandleChartDataEntry.swift */, - 274116834B1B0345D622E027 /* CandleChartDataSet.swift */, - E120E76C6F1B5877D56126DD /* ChartData.swift */, - F22750328058DEC2F019646F /* ChartDataEntry.swift */, - 12409C3EA15787C11AF0D2BC /* ChartDataEntryBase.swift */, - 6CEC0C69C89CE9B99F3B4409 /* ChartDataSet.swift */, - 0DDE409E9ECF54D2C146A6F0 /* CombinedChartData.swift */, - 4C978F31F23C7D21197DC2A1 /* LineChartData.swift */, - A5A75AA73C5AA381DA517959 /* LineChartDataSet.swift */, - 45E31A4356CC6F283C29954B /* LineRadarChartDataSet.swift */, - C58BD7B14BEA440783ED8D2B /* LineScatterCandleRadarChartDataSet.swift */, - 6E03A4987F72414A02A0631B /* PieChartData.swift */, - BD02157CF8CEE1189BF681DA /* PieChartDataEntry.swift */, - A4FB5E3761EF8B4D1E1E1014 /* PieChartDataSet.swift */, - 4BBB57D6FA41029B08F26D7B /* RadarChartData.swift */, - 91EEEDE2AB8F2DA3AFCF0733 /* RadarChartDataEntry.swift */, - B1BA6B21CBDF77A15848994F /* RadarChartDataSet.swift */, - E7AD2FC320A16CA1EE0A52F4 /* ScatterChartData.swift */, - CB1DD1A0F64266A10EE94194 /* ScatterChartDataSet.swift */, - ); - name = Standard; - sourceTree = ""; - }; 74A391010038924F637D6752 /* Formatters */ = { isa = PBXGroup; children = ( diff --git a/Source/Charts/Charts/BarLineChartViewBase.swift b/Source/Charts/Charts/BarLineChartViewBase.swift index efb5536d88..30da3e79b6 100644 --- a/Source/Charts/Charts/BarLineChartViewBase.swift +++ b/Source/Charts/Charts/BarLineChartViewBase.swift @@ -1675,7 +1675,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD { if let h = getHighlightByTouchPoint(pt) { - return data!.entryForHighlight(h) + return data!.entry(for: h) } return nil } @@ -1686,7 +1686,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD let h = getHighlightByTouchPoint(pt) if h !== nil { - return data?.getDataSetByIndex(h!.dataSetIndex) as! BarLineScatterCandleBubbleChartDataSetProtocol + return data?.dataSet(forIndex: h!.dataSetIndex) as! BarLineScatterCandleBubbleChartDataSetProtocol } return nil } diff --git a/Source/Charts/Charts/ChartViewBase.swift b/Source/Charts/Charts/ChartViewBase.swift index 59159c6b86..bb38ed554b 100644 --- a/Source/Charts/Charts/ChartViewBase.swift +++ b/Source/Charts/Charts/ChartViewBase.swift @@ -52,7 +52,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate guard let data = data else { return } // calculate how many digits are needed - setupDefaultFormatter(min: data.getYMin(), max: data.getYMax()) + setupDefaultFormatter(min: data.yMin, max: data.yMax) for set in data.dataSets { @@ -430,7 +430,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate var high = highlight guard let h = high, - let entry = data?.entryForHighlight(h) + let entry = data?.entry(for: h) else { high = nil @@ -487,8 +487,8 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate for highlight in highlighted { guard - let set = data?.getDataSetByIndex(highlight.dataSetIndex), - let e = data?.entryForHighlight(highlight) + let set = data?.dataSet(forIndex: highlight.dataSetIndex), + let e = data?.entry(for: highlight) else { continue } let entryIndex = set.entryIndex(entry: e) diff --git a/Source/Charts/Charts/CombinedChartView.swift b/Source/Charts/Charts/CombinedChartView.swift index 7a5af57ecc..4e2efde357 100644 --- a/Source/Charts/Charts/CombinedChartView.swift +++ b/Source/Charts/Charts/CombinedChartView.swift @@ -219,7 +219,7 @@ open class CombinedChartView: BarLineChartViewBase, CombinedChartDataProvider guard let set = combinedData?.getDataSetByHighlight(highlight), - let e = data?.entryForHighlight(highlight) + let e = data?.entry(for: highlight) else { continue } let entryIndex = set.entryIndex(entry: e) diff --git a/Source/Charts/Data/Implementations/Standard/BarChartData.swift b/Source/Charts/Data/Implementations/Standard/BarChartData.swift index 6516428275..04e1e6f8de 100644 --- a/Source/Charts/Data/Implementations/Standard/BarChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/BarChartData.swift @@ -19,7 +19,7 @@ open class BarChartData: BarLineScatterCandleBubbleChartData super.init() } - public override init(dataSets: [ChartDataSetProtocol]?) + public override init(dataSets: [ChartDataSetProtocol]) { super.init(dataSets: dataSets) } diff --git a/Source/Charts/Data/Implementations/Standard/BarLineScatterCandleBubbleChartData.swift b/Source/Charts/Data/Implementations/Standard/BarLineScatterCandleBubbleChartData.swift index 715709708b..75eb8a278e 100644 --- a/Source/Charts/Data/Implementations/Standard/BarLineScatterCandleBubbleChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/BarLineScatterCandleBubbleChartData.swift @@ -18,7 +18,7 @@ open class BarLineScatterCandleBubbleChartData: ChartData super.init() } - public override init(dataSets: [ChartDataSetProtocol]?) + public override init(dataSets: [ChartDataSetProtocol]) { super.init(dataSets: dataSets) } diff --git a/Source/Charts/Data/Implementations/Standard/BubbleChartData.swift b/Source/Charts/Data/Implementations/Standard/BubbleChartData.swift index 89fd484804..5a0a85563d 100644 --- a/Source/Charts/Data/Implementations/Standard/BubbleChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/BubbleChartData.swift @@ -19,7 +19,7 @@ open class BubbleChartData: BarLineScatterCandleBubbleChartData super.init() } - public override init(dataSets: [ChartDataSetProtocol]?) + public override init(dataSets: [ChartDataSetProtocol]) { super.init(dataSets: dataSets) } diff --git a/Source/Charts/Data/Implementations/Standard/CandleChartData.swift b/Source/Charts/Data/Implementations/Standard/CandleChartData.swift index 2c0d3bd443..3cfe5bac35 100644 --- a/Source/Charts/Data/Implementations/Standard/CandleChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/CandleChartData.swift @@ -18,7 +18,7 @@ open class CandleChartData: BarLineScatterCandleBubbleChartData super.init() } - public override init(dataSets: [ChartDataSetProtocol]?) + public override init(dataSets: [ChartDataSetProtocol]) { super.init(dataSets: dataSets) } diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index 2d02ea689b..398dc71ec8 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -13,50 +13,40 @@ import Foundation open class ChartData: NSObject, ExpressibleByArrayLiteral { - internal var _yMax: Double = -Double.greatestFiniteMagnitude - internal var _yMin: Double = Double.greatestFiniteMagnitude - internal var _xMax: Double = -Double.greatestFiniteMagnitude - internal var _xMin: Double = Double.greatestFiniteMagnitude - internal var _leftAxisMax: Double = -Double.greatestFiniteMagnitude - internal var _leftAxisMin: Double = Double.greatestFiniteMagnitude - internal var _rightAxisMax: Double = -Double.greatestFiniteMagnitude - internal var _rightAxisMin: Double = Double.greatestFiniteMagnitude + + @objc public internal(set) var xMax = -Double.greatestFiniteMagnitude + @objc public internal(set) var xMin = Double.greatestFiniteMagnitude + @objc public internal(set) var yMax = -Double.greatestFiniteMagnitude + @objc public internal(set) var yMin = Double.greatestFiniteMagnitude + var leftAxisMax = -Double.greatestFiniteMagnitude + var leftAxisMin = Double.greatestFiniteMagnitude + var rightAxisMax = -Double.greatestFiniteMagnitude + var rightAxisMin = Double.greatestFiniteMagnitude - internal var _dataSets = [ChartDataSetProtocol]() + var _dataSets = [Element]() public override required init() { super.init() } - public required init(arrayLiteral elements: ChartDataSetProtocol...) + public required init(arrayLiteral elements: Element...) { super.init() - - _dataSets = elements - - self.initialize(dataSets: _dataSets) + self.dataSets = elements } - @objc public init(dataSets: [ChartDataSetProtocol]?) + @objc public init(dataSets: [Element]) { super.init() - - _dataSets = dataSets ?? [ChartDataSetProtocol]() - - self.initialize(dataSets: _dataSets) + self.dataSets = dataSets } - @objc public convenience init(dataSet: ChartDataSetProtocol?) + @objc public convenience init(dataSet: Element) { - self.init(dataSets: dataSet === nil ? nil : [dataSet!]) - } - - internal func initialize(dataSets: [ChartDataSetProtocol]) - { - notifyDataChanged() + self.init(dataSets: [dataSet]) } - + /// Call this method to let the ChartData know that the underlying data has changed. /// Calling this performs all necessary recalculations needed when the contained data has changed. @objc open func notifyDataChanged() @@ -75,256 +65,117 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral /// calc minimum and maximum y value over all datasets @objc open func calcMinMax() { - _yMax = -Double.greatestFiniteMagnitude - _yMin = Double.greatestFiniteMagnitude - _xMax = -Double.greatestFiniteMagnitude - _xMin = Double.greatestFiniteMagnitude + leftAxisMax = -.greatestFiniteMagnitude + leftAxisMin = .greatestFiniteMagnitude + rightAxisMax = -.greatestFiniteMagnitude + rightAxisMin = .greatestFiniteMagnitude + xMax = -.greatestFiniteMagnitude + xMin = .greatestFiniteMagnitude forEach { calcMinMax(dataSet: $0) } - - _leftAxisMax = -Double.greatestFiniteMagnitude - _leftAxisMin = Double.greatestFiniteMagnitude - _rightAxisMax = -Double.greatestFiniteMagnitude - _rightAxisMin = Double.greatestFiniteMagnitude - - // left axis - let firstLeft = getFirstLeft(dataSets: dataSets) - - if firstLeft !== nil - { - _leftAxisMax = firstLeft!.yMax - _leftAxisMin = firstLeft!.yMin - - for dataSet in _dataSets where dataSet.axisDependency == .left - { - if dataSet.yMin < _leftAxisMin - { - _leftAxisMin = dataSet.yMin - } - - if dataSet.yMax > _leftAxisMax - { - _leftAxisMax = dataSet.yMax - } - } - } - - // right axis - let firstRight = getFirstRight(dataSets: dataSets) - - if firstRight !== nil - { - _rightAxisMax = firstRight!.yMax - _rightAxisMin = firstRight!.yMin - - for dataSet in _dataSets where dataSet.axisDependency == .right - { - if dataSet.yMin < _rightAxisMin - { - _rightAxisMin = dataSet.yMin - } - - if dataSet.yMax > _rightAxisMax - { - _rightAxisMax = dataSet.yMax - } - } - } } /// Adjusts the current minimum and maximum values based on the provided Entry object. @objc open func calcMinMax(entry e: ChartDataEntry, axis: YAxis.AxisDependency) { - if _yMax < e.y - { - _yMax = e.y - } - - if _yMin > e.y - { - _yMin = e.y - } - - if _xMax < e.x - { - _xMax = e.x - } - - if _xMin > e.x - { - _xMin = e.x - } - - if axis == .left - { - if _leftAxisMax < e.y - { - _leftAxisMax = e.y - } - - if _leftAxisMin > e.y - { - _leftAxisMin = e.y - } - } - else + xMax = Swift.max(xMax, e.x) + xMin = Swift.min(xMin, e.x) + yMax = Swift.max(yMax, e.y) + yMin = Swift.min(yMin, e.y) + + switch axis { - if _rightAxisMax < e.y - { - _rightAxisMax = e.y - } - - if _rightAxisMin > e.y - { - _rightAxisMin = e.y - } + case .left: + leftAxisMax = Swift.max(leftAxisMax, e.y) + leftAxisMin = Swift.min(leftAxisMin, e.y) + + case .right: + rightAxisMax = Swift.max(rightAxisMax, e.y) + rightAxisMin = Swift.min(rightAxisMin, e.y) } } /// Adjusts the minimum and maximum values based on the given DataSet. - @objc open func calcMinMax(dataSet d: ChartDataSetProtocol) + @objc open func calcMinMax(dataSet d: Element) { - if _yMax < d.yMax - { - _yMax = d.yMax - } - - if _yMin > d.yMin - { - _yMin = d.yMin - } - - if _xMax < d.xMax - { - _xMax = d.xMax - } - - if _xMin > d.xMin - { - _xMin = d.xMin - } - - if d.axisDependency == .left - { - if _leftAxisMax < d.yMax - { - _leftAxisMax = d.yMax - } - - if _leftAxisMin > d.yMin - { - _leftAxisMin = d.yMin - } - } - else + xMax = Swift.max(xMax, d.xMax) + xMin = Swift.min(xMin, d.xMin) + yMax = Swift.max(yMax, d.yMax) + yMin = Swift.min(yMin, d.yMin) + + switch d.axisDependency { - if _rightAxisMax < d.yMax - { - _rightAxisMax = d.yMax - } - - if _rightAxisMin > d.yMin - { - _rightAxisMin = d.yMin - } + case .left: + leftAxisMax = Swift.max(leftAxisMax, d.yMax) + leftAxisMin = Swift.min(leftAxisMin, d.yMin) + + case .right: + rightAxisMax = Swift.max(rightAxisMax, d.yMax) + rightAxisMin = Swift.min(rightAxisMin, d.yMin) } } /// - returns: The number of LineDataSets this object contains @objc open var dataSetCount: Int { - return _dataSets.count - } - - /// - returns: The smallest y-value the data object contains. - @objc open var yMin: Double - { - return _yMin - } - - @nonobjc - open func getYMin() -> Double - { - return _yMin + return dataSets.count } - + @objc open func getYMin(axis: YAxis.AxisDependency) -> Double { - if axis == .left + // TODO: Why does it make sense to return the other axisMin if there is none for the one requested? + switch axis { - if _leftAxisMin == Double.greatestFiniteMagnitude + case .left: + if leftAxisMin == .greatestFiniteMagnitude { - return _rightAxisMin + return rightAxisMin } else { - return _leftAxisMin + return leftAxisMin } - } - else - { - if _rightAxisMin == Double.greatestFiniteMagnitude + + case .right: + if rightAxisMin == .greatestFiniteMagnitude { - return _leftAxisMin + return leftAxisMin } else { - return _rightAxisMin + return rightAxisMin } } } - - /// - returns: The greatest y-value the data object contains. - @objc open var yMax: Double - { - return _yMax - } - - @nonobjc - open func getYMax() -> Double - { - return _yMax - } - + @objc open func getYMax(axis: YAxis.AxisDependency) -> Double { if axis == .left { - if _leftAxisMax == -Double.greatestFiniteMagnitude + if leftAxisMax == -.greatestFiniteMagnitude { - return _rightAxisMax + return rightAxisMax } else { - return _leftAxisMax + return leftAxisMax } } else { - if _rightAxisMax == -Double.greatestFiniteMagnitude + if rightAxisMax == -.greatestFiniteMagnitude { - return _leftAxisMax + return leftAxisMax } else { - return _rightAxisMax + return rightAxisMax } } } - - /// - returns: The minimum x-value the data object contains. - @objc open var xMin: Double - { - return _xMin - } - /// - returns: The maximum x-value the data object contains. - @objc open var xMax: Double - { - return _xMax - } - + /// - returns: All DataSet objects this ChartData object holds. - @objc open var dataSets: [ChartDataSetProtocol] + @objc open var dataSets: [Element] { get { @@ -336,33 +187,12 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral notifyDataChanged() } } - - /// Retrieve the index of a ChartDataSet with a specific label from the ChartData. Search can be case sensitive or not. - /// - /// **IMPORTANT: This method does calculations at runtime, do not over-use in performance critical situations.** - /// - /// - parameter dataSets: the DataSet array to search - /// - parameter type: - /// - parameter ignorecase: if true, the search is not case-sensitive - /// - returns: The index of the DataSet Object with the given label. Sensitive or not. - internal func getDataSetIndexByLabel(_ label: String, ignorecase: Bool) -> Int - { - return ignorecase - ? index { $0.label?.caseInsensitiveCompare(label) == .orderedSame } ?? -1 - : index { $0.label == label } ?? -1 - } - - /// - returns: The labels of all DataSets as a string array. - internal func dataSetLabels() -> [String] - { - return flatMap { $0.label } - } - + /// Get the Entry for a corresponding highlight object /// /// - parameter highlight: /// - returns: The entry that is highlighted - @objc open func entryForHighlight(_ highlight: Highlight) -> ChartDataEntry? + @objc open func entry(for highlight: Highlight) -> ChartDataEntry? { guard indices.contains(highlight.dataSetIndex) else { return nil } return self[highlight.dataSetIndex].entryForXValue(highlight.x, closestToY: highlight.y) @@ -373,60 +203,45 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral /// - parameter label: /// - parameter ignorecase: /// - returns: The DataSet Object with the given label. Sensitive or not. - @objc open func getDataSetByLabel(_ label: String, ignorecase: Bool) -> Element? + @objc open func dataSet(forLabel label: String, ignorecase: Bool) -> Element? { guard let index = index(forLabel: label, ignoreCase: ignorecase) else { return nil } return self[index] } - @objc open func getDataSetByIndex(_ index: Index) -> Element! + @objc open func dataSet(forIndex index: Index) -> Element? { - if index < 0 || index >= _dataSets.count - { - return nil - } - + guard dataSets.indices.contains(index) else { return nil } return self[index] } - @objc open func addDataSet(_ dataSet: Element!) + @objc open func addDataSet(_ dataSet: Element) { - calcMinMax(dataSet: dataSet) - - _dataSets.append(dataSet) + append(dataSet) } /// Removes the given DataSet from this data object. /// Also recalculates all minimum and maximum values. /// /// - returns: `true` if a DataSet was removed, `false` ifno DataSet could be removed. - @objc @discardableResult open func removeDataSet(_ dataSet: Element!) -> Bool + @objc @discardableResult open func removeDataSet(_ dataSet: Element) -> Element? { - guard - dataSet != nil, - let index = index(where: { $0 === dataSet }) - else { return false } - - _ = remove(at: index) - return true + guard let index = index(where: { $0 === dataSet }) else { return nil } + return remove(at: index) } /// Removes the DataSet at the given index in the DataSet array from the data object. /// Also recalculates all minimum and maximum values. /// /// - returns: `true` if a DataSet was removed, `false` ifno DataSet could be removed. - @objc @discardableResult open func removeDataSetByIndex(_ index: Int) -> Bool + @objc @discardableResult open func removeDataSet(at index: Index) -> Element? { - guard indices.contains(index) else { return false } - - _ = remove(at: index) - calcMinMax() - - return true + guard indices.contains(index) else { return nil } + return remove(at: index) } /// Adds an Entry to the DataSet at the specified index. Entries are added to the end of the list. - @objc open func addEntry(_ e: ChartDataEntry, dataSetIndex: Int) + @objc open func addEntry(_ e: ChartDataEntry, dataSetIndex: Index) { guard indices.contains(dataSetIndex) else { return print("ChartData.addEntry() - Cannot add Entry because dataSetIndex too high or too low.", terminator: "\n") @@ -438,7 +253,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: Int) -> Bool + @objc @discardableResult open func removeEntry(_ entry: ChartDataEntry, dataSetIndex: Index) -> Bool { guard indices.contains(dataSetIndex) else { return false } @@ -456,7 +271,7 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral /// Removes the Entry object closest to the given xIndex from the ChartDataSet at the /// specified index. /// - returns: `true` if an entry was removed, `false` ifno Entry was found that meets the specified requirements. - @objc @discardableResult open func removeEntry(xValue: Double, dataSetIndex: Int) -> Bool + @objc @discardableResult open func removeEntry(xValue: Double, dataSetIndex: Index) -> Bool { guard indices.contains(dataSetIndex), @@ -467,57 +282,51 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral } /// - returns: The DataSet that contains the provided Entry, or null, if no DataSet contains this entry. - @objc open func getDataSetForEntry(_ e: ChartDataEntry!) -> ChartDataSetProtocol? + @objc open func getDataSetForEntry(_ e: ChartDataEntry) -> Element? { - guard e != nil else { return nil } - return first { $0.entryForXValue(e.x, closestToY: e.y) === e } } /// - returns: The index of the provided DataSet in the DataSet array of this data object, or -1 if it does not exist. - @objc open func indexOfDataSet(_ dataSet: ChartDataSetProtocol) -> Int + @objc open func index(of dataSet: Element) -> Index { return index(where: { $0 === dataSet }) ?? -1 } /// - returns: The first DataSet from the datasets-array that has it's dependency on the left axis. Returns null if no DataSet with left dependency could be found. - @objc open func getFirstLeft(dataSets: [ChartDataSetProtocol]) -> ChartDataSetProtocol? + @objc open func getFirstLeft(dataSets: [Element]) -> Element? { return first { $0.axisDependency == .left } } /// - returns: The first DataSet from the datasets-array that has it's dependency on the right axis. Returns null if no DataSet with right dependency could be found. - @objc open func getFirstRight(dataSets: [ChartDataSetProtocol]) -> ChartDataSetProtocol? + @objc open func getFirstRight(dataSets: [Element]) -> Element? { return first { $0.axisDependency == .right } } /// - returns: All colors used across all DataSet objects this object represents. - // TODO: This should return a non-optional array - @objc open func getColors() -> [NSUIColor]? + @objc open func getColors() -> [NSUIColor] { return flatMap { $0.colors.map { $0 } } } /// Sets a custom ValueFormatter for all DataSets this data object contains. - @objc open func setValueFormatter(_ formatter: ValueFormatter?) + @objc open func setValueFormatter(_ formatter: ValueFormatter) { - guard let formatter = formatter - else { return } - forEach { $0.valueFormatter = formatter } } /// Sets the color of the value-text (color in which the value-labels are drawn) for all DataSets this data object contains. - @objc open func setValueTextColor(_ color: NSUIColor!) + @objc open func setValueTextColor(_ color: NSUIColor) { - forEach { $0.valueTextColor = color ?? $0.valueTextColor } + forEach { $0.valueTextColor = color } } /// Sets the font for all value-labels for all DataSets this data object contains. - @objc open func setValueFont(_ font: NSUIFont!) + @objc open func setValueFont(_ font: NSUIFont) { - forEach { $0.valueFont = font ?? $0.valueFont } + forEach { $0.valueFont = font } } /// Enables / disables drawing values (value-text) for all DataSets this data object contains. @@ -528,32 +337,22 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral /// Enables / disables highlighting values for all DataSets this data object contains. /// If set to true, this means that values can be highlighted programmatically or by touch gesture. - @objc open var highlightEnabled: Bool + @objc open var isHighlightEnabled: Bool { - get - { - return first { $0.highlightEnabled == false } == nil - } - set - { - forEach { $0.highlightEnabled = newValue } - } + get { return first { $0.highlightEnabled == false } == nil } + set { forEach { $0.highlightEnabled = newValue } } } - - /// if true, value highlightning is enabled - @objc open var isHighlightEnabled: Bool { return highlightEnabled } - + /// Clears this data object from all DataSets and removes all Entries. /// Don't forget to invalidate the chart after this. @objc open func clearValues() { - dataSets.removeAll(keepingCapacity: false) - notifyDataChanged() + removeAll(keepingCapacity: false) } /// Checks if this data object contains the specified DataSet. /// - returns: `true` if so, `false` ifnot. - @objc open func contains(dataSet: ChartDataSetProtocol) -> Bool + @objc open func contains(dataSet: Element) -> Bool { return contains { $0 === dataSet } } @@ -565,7 +364,7 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral } /// - returns: The DataSet object with the maximum number of entries or null if there are no DataSets. - @objc open var maxEntryCountSet: ChartDataSetProtocol? + @objc open var maxEntryCountSet: Element? { return self.max { $0.entryCount > $1.entryCount } } @@ -579,23 +378,23 @@ extension ChartData: MutableCollection public var startIndex: Index { - return _dataSets.startIndex + return dataSets.startIndex } public var endIndex: Index { - return _dataSets.endIndex + return dataSets.endIndex } public func index(after: Index) -> Index { - return _dataSets.index(after: after) + return dataSets.index(after: after) } public subscript(position: Index) -> Element { - get{ return _dataSets[position] } - set{ self._dataSets[position] = newValue } + get { return dataSets[position] } + set { self._dataSets[position] = newValue } } } @@ -604,7 +403,7 @@ extension ChartData: RandomAccessCollection { public func index(before: Index) -> Index { - return _dataSets.index(before: before) + return dataSets.index(before: before) } } @@ -654,6 +453,12 @@ extension ChartData: RangeReplaceableCollection _dataSets.removeSubrange(bounds) notifyDataChanged() } + + public func removeAll(keepingCapacity keepCapacity: Bool) + { + _dataSets.removeAll(keepingCapacity: keepCapacity) + notifyDataChanged() + } } // MARK: Swift Accessors diff --git a/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift b/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift index 2e28ab2421..994574c66f 100644 --- a/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift @@ -24,7 +24,7 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData super.init() } - public override init(dataSets: [ChartDataSetProtocol]?) + public override init(dataSets: [ChartDataSetProtocol]) { super.init(dataSets: dataSets) } @@ -103,15 +103,15 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData { _dataSets.removeAll() - _yMax = -Double.greatestFiniteMagnitude - _yMin = Double.greatestFiniteMagnitude - _xMax = -Double.greatestFiniteMagnitude - _xMin = Double.greatestFiniteMagnitude + yMax = -Double.greatestFiniteMagnitude + yMin = Double.greatestFiniteMagnitude + xMax = -Double.greatestFiniteMagnitude + xMin = Double.greatestFiniteMagnitude - _leftAxisMax = -Double.greatestFiniteMagnitude - _leftAxisMin = Double.greatestFiniteMagnitude - _rightAxisMax = -Double.greatestFiniteMagnitude - _rightAxisMin = Double.greatestFiniteMagnitude + leftAxisMax = -Double.greatestFiniteMagnitude + leftAxisMin = Double.greatestFiniteMagnitude + rightAxisMax = -Double.greatestFiniteMagnitude + rightAxisMin = Double.greatestFiniteMagnitude let allData = self.allData @@ -122,48 +122,48 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData let sets = data.dataSets _dataSets.append(contentsOf: sets) - if data.yMax > _yMax + if data.yMax > yMax { - _yMax = data.yMax + yMax = data.yMax } - if data.yMin < _yMin + if data.yMin < yMin { - _yMin = data.yMin + yMin = data.yMin } - if data.xMax > _xMax + if data.xMax > xMax { - _xMax = data.xMax + xMax = data.xMax } - if data.xMin < _xMin + if data.xMin < xMin { - _xMin = data.xMin + xMin = data.xMin } for dataset in sets { if dataset.axisDependency == .left { - if dataset.yMax > _leftAxisMax + if dataset.yMax > leftAxisMax { - _leftAxisMax = dataset.yMax + leftAxisMax = dataset.yMax } - if dataset.yMin < _leftAxisMin + if dataset.yMin < leftAxisMin { - _leftAxisMin = dataset.yMin + leftAxisMin = dataset.yMin } } else { - if dataset.yMax > _rightAxisMax + if dataset.yMax > rightAxisMax { - _rightAxisMax = dataset.yMax + rightAxisMax = dataset.yMax } - if dataset.yMin < _rightAxisMin + if dataset.yMin < rightAxisMin { - _rightAxisMin = dataset.yMin + rightAxisMin = dataset.yMin } } } @@ -209,29 +209,23 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData return allData.index(of: data) } - open override func removeDataSet(_ dataSet: ChartDataSetProtocol!) -> Bool + open override func removeDataSet(_ dataSet: ChartDataSetProtocol) -> Element? { - let datas = allData - - var success = false - - for data in datas + for data in allData { - success = data.removeDataSet(dataSet) - - if success + if let e = data.removeDataSet(dataSet) { - break + return e } } - return success + return nil } - open override func removeDataSetByIndex(_ index: Int) -> Bool + open override func removeDataSet(at index: Int) -> Element? { print("removeDataSet(index) not supported for CombinedData", terminator: "\n") - return false + return nil } open override func removeEntry(_ entry: ChartDataEntry, dataSetIndex: Int) -> Bool @@ -261,7 +255,7 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData /// /// - parameter highlight: /// - returns: The entry that is highlighted - open override func entryForHighlight(_ highlight: Highlight) -> ChartDataEntry? + @objc override open func entry(for highlight: Highlight) -> ChartDataEntry? { if highlight.dataIndex >= allData.count { @@ -276,15 +270,9 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData } // The value of the highlighted entry could be NaN - if we are not interested in highlighting a specific value. - let entries = data.getDataSetByIndex(highlight.dataSetIndex).entriesForXValue(highlight.x) - for e in entries - { - if e.y == highlight.y || highlight.y.isNaN - { - return e - } - } - return nil + return data.dataSet(forIndex: highlight.dataSetIndex)? + .entriesForXValue(highlight.x) + .first { $0.y == highlight.y || highlight.y.isNaN } } /// Get dataset for highlight diff --git a/Source/Charts/Data/Implementations/Standard/LineChartData.swift b/Source/Charts/Data/Implementations/Standard/LineChartData.swift index 468620ff49..fe3eb92862 100644 --- a/Source/Charts/Data/Implementations/Standard/LineChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/LineChartData.swift @@ -19,7 +19,7 @@ open class LineChartData: ChartData super.init() } - public override init(dataSets: [ChartDataSetProtocol]?) + public override init(dataSets: [ChartDataSetProtocol]) { super.init(dataSets: dataSets) } diff --git a/Source/Charts/Data/Implementations/Standard/PieChartData.swift b/Source/Charts/Data/Implementations/Standard/PieChartData.swift index 1ea1f2675a..0a98837581 100644 --- a/Source/Charts/Data/Implementations/Standard/PieChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/PieChartData.swift @@ -18,7 +18,7 @@ open class PieChartData: ChartData super.init() } - public override init(dataSets: [ChartDataSetProtocol]?) + public override init(dataSets: [ChartDataSetProtocol]) { super.init(dataSets: dataSets) } @@ -47,16 +47,13 @@ open class PieChartData: ChartData } } - open override func getDataSetByIndex(_ index: Int) -> ChartDataSetProtocol? + open override func dataSet(forIndex index: ChartData.Index) -> ChartData.Element? { - if index != 0 - { - return nil - } - return super.getDataSetByIndex(index) + guard index == 0 else { return nil } + return super.dataSet(forIndex: index) } - open override func getDataSetByLabel(_ label: String, ignorecase: Bool) -> ChartDataSetProtocol? + open override func dataSet(forLabel label: String, ignorecase: Bool) -> ChartDataSetProtocol? { if dataSets.count == 0 || dataSets[0].label == nil { @@ -80,12 +77,12 @@ open class PieChartData: ChartData return nil } - open override func entryForHighlight(_ highlight: Highlight) -> ChartDataEntry? + @objc override open func entry(for highlight: Highlight) -> ChartDataEntry? { return dataSet?.entryForIndex(Int(highlight.x)) } - open override func addDataSet(_ d: ChartDataSetProtocol!) + open override func addDataSet(_ d: ChartDataSetProtocol) { super.addDataSet(d) } @@ -94,14 +91,9 @@ open class PieChartData: ChartData /// Also recalculates all minimum and maximum values. /// /// - returns: `true` if a DataSet was removed, `false` ifno DataSet could be removed. - open override func removeDataSetByIndex(_ index: Int) -> Bool + open override func removeDataSet(at index: Int) -> Element? { - if index >= _dataSets.count || index < 0 - { - return false - } - - return false + return nil } /// - returns: The total y-value sum across all DataSet objects the this object represents. diff --git a/Source/Charts/Data/Implementations/Standard/RadarChartData.swift b/Source/Charts/Data/Implementations/Standard/RadarChartData.swift index 313ca65147..808fe90510 100644 --- a/Source/Charts/Data/Implementations/Standard/RadarChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/RadarChartData.swift @@ -34,7 +34,7 @@ open class RadarChartData: ChartData super.init() } - public override init(dataSets: [ChartDataSetProtocol]?) + public override init(dataSets: [ChartDataSetProtocol]) { super.init(dataSets: dataSets) } @@ -44,8 +44,8 @@ open class RadarChartData: ChartData super.init(dataSets: elements) } - open override func entryForHighlight(_ highlight: Highlight) -> ChartDataEntry? + @objc open override func entry(for highlight: Highlight) -> ChartDataEntry? { - return getDataSetByIndex(highlight.dataSetIndex)?.entryForIndex(Int(highlight.x)) + return dataSet(forIndex: highlight.dataSetIndex)?.entryForIndex(Int(highlight.x)) } } diff --git a/Source/Charts/Data/Implementations/Standard/ScatterChartData.swift b/Source/Charts/Data/Implementations/Standard/ScatterChartData.swift index b30304a1b2..6fbc7bbe3e 100644 --- a/Source/Charts/Data/Implementations/Standard/ScatterChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ScatterChartData.swift @@ -19,7 +19,7 @@ open class ScatterChartData: BarLineScatterCandleBubbleChartData super.init() } - public override init(dataSets: [ChartDataSetProtocol]?) + public override init(dataSets: [ChartDataSetProtocol]) { super.init(dataSets: dataSets) } diff --git a/Source/Charts/Data/Interfaces/ChartDataSetProtocol.swift b/Source/Charts/Data/Interfaces/ChartDataSetProtocol.swift index 77b1e6b0d2..67008baa48 100644 --- a/Source/Charts/Data/Interfaces/ChartDataSetProtocol.swift +++ b/Source/Charts/Data/Interfaces/ChartDataSetProtocol.swift @@ -20,7 +20,7 @@ public protocol ChartDataSetProtocol /// Use this method to tell the data set that the underlying data has changed func notifyDataSetChanged() - /// Calculates the minimum and maximum x and y values (_xMin, _xMax, _yMin, _yMax). + /// Calculates the minimum and maximum x and y values (_xMin, _xMax, _yMin, yMax). func calcMinMax() /// Calculates the min and max y-values from the Entry closest to the given fromX to the Entry closest to the given toX value. diff --git a/Source/Charts/Highlight/BarHighlighter.swift b/Source/Charts/Highlight/BarHighlighter.swift index e34e2bc47a..dbe6d46ea5 100644 --- a/Source/Charts/Highlight/BarHighlighter.swift +++ b/Source/Charts/Highlight/BarHighlighter.swift @@ -29,7 +29,7 @@ open class BarHighlighter: ChartHighlighter let pos = getValsForTouch(x: x, y: y) if - let set = barData.getDataSetByIndex(high!.dataSetIndex) as? BarChartDataSetProtocol, + let set = barData.dataSet(forIndex: high!.dataSetIndex) as? BarChartDataSetProtocol, set.isStacked { return getStackedHighlight(high: high!, diff --git a/Source/Charts/Highlight/CombinedHighlighter.swift b/Source/Charts/Highlight/CombinedHighlighter.swift index f7ca82f71c..1c1b2c9f17 100644 --- a/Source/Charts/Highlight/CombinedHighlighter.swift +++ b/Source/Charts/Highlight/CombinedHighlighter.swift @@ -52,7 +52,7 @@ open class CombinedHighlighter: ChartHighlighter { for j in 0.. Date: Fri, 12 Jan 2018 07:29:56 -0400 Subject: [PATCH 12/34] PR review fixes --- Source/Charts/Data/Implementations/Standard/ChartData.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index c9609429d4..11d28bbeae 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -844,7 +844,8 @@ extension ChartData: RangeReplaceableCollection notifyDataChanged() } - public func removeSubrange(_ bounds: R) where R : RangeExpression, ChartData.Index == R.Bound { + public func removeSubrange(_ bounds: R) where R : RangeExpression, ChartData.Index == R.Bound + { _dataSets.removeSubrange(bounds) notifyDataChanged() } @@ -853,7 +854,6 @@ extension ChartData: RangeReplaceableCollection // MARK: Swift Accessors extension ChartData { - //TODO: Reevaluate if warning is still true /// Retrieve the index of a ChartDataSet with a specific label from the ChartData. Search can be case sensitive or not. /// **IMPORTANT: This method does calculations at runtime, do not over-use in performance critical situations.** /// From f28d3d5c707b1813f2e73b780c0298321f3710ad Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Fri, 12 Jan 2018 07:31:59 -0400 Subject: [PATCH 13/34] Removed unnecessary `get` from subscripts. --- .../Implementations/Standard/ChartData.swift | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index 11d28bbeae..7eb29e213f 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -870,20 +870,14 @@ extension ChartData public subscript(label: String, ignoreCase: Bool) -> Element? { - get - { - guard let index = index(forLabel: label, ignoreCase: ignoreCase) else { return nil } - return self[index] - } + guard let index = index(forLabel: label, ignoreCase: ignoreCase) else { return nil } + return self[index] } - + public subscript(entry: ChartDataEntry) -> Element? { - get - { - guard let index = index(where: { $0.entryForXValue(entry.x, closestToY: entry.y) === entry }) else { return nil } - return self[index] - } + guard let index = index(where: { $0.entryForXValue(entry.x, closestToY: entry.y) === entry }) else { return nil } + return self[index] } public func appendEntry(_ e: ChartDataEntry, toDataSet dataSetIndex: Index) From 583dab67c2fe946937ec631aeab1069e22add7e6 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sun, 14 Jan 2018 22:30:35 -0400 Subject: [PATCH 14/34] Disabled `remove(at:)` for CombinedChartView --- Source/Charts/Data/Implementations/Standard/ChartData.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index 7eb29e213f..987bfda013 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -813,6 +813,10 @@ extension ChartData: RangeReplaceableCollection public func remove(at position: Index) -> Element { + guard !(self is CombinedChartData) else + { + fatalError("remove(at:) not supported for CombinedData") + } let element = _dataSets.remove(at: position) calcMinMax() return element From 47b6a1c602b5126d25de3232fa5f5f5ab2e6c5ef Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sun, 14 Jan 2018 22:59:19 -0400 Subject: [PATCH 15/34] Removed redundant methods --- .../Implementations/Standard/ChartData.swift | 25 +++---------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index f37ed9cd73..9101b7928f 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -388,14 +388,7 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral return self[index] } - - @objc open func addDataSet(_ dataSet: Element!) - { - calcMinMax(dataSet: dataSet) - - _dataSets.append(dataSet) - } - + /// Removes the given DataSet from this data object. /// Also recalculates all minimum and maximum values. /// @@ -410,21 +403,7 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral _ = remove(at: index) return true } - - /// Removes the DataSet at the given index in the DataSet array from the data object. - /// Also recalculates all minimum and maximum values. - /// - /// - returns: `true` if a DataSet was removed, `false` ifno DataSet could be removed. - @objc @discardableResult open func removeDataSetByIndex(_ index: Int) -> Bool - { - guard indices.contains(index) else { return false } - _ = remove(at: index) - calcMinMax() - - return true - } - /// Adds an Entry to the DataSet at the specified index. Entries are added to the end of the list. @objc open func addEntry(_ e: ChartDataEntry, dataSetIndex: Int) { @@ -611,12 +590,14 @@ extension ChartData: RandomAccessCollection // MARK: RangeReplaceableCollection extension ChartData: RangeReplaceableCollection { + @objc(addDataSet:) public func append(_ newElement: Element) { _dataSets.append(newElement) calcMinMax(dataSet: newElement) } + @objc(removeDataSetByIndex:) public func remove(at position: Index) -> Element { guard !(self is CombinedChartData) else From c0b7d65e21a20489848b6f770c0a9437a0fe46aa Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sun, 14 Jan 2018 23:01:26 -0400 Subject: [PATCH 16/34] Relocated `appendEntry(_:todataSet:)` --- .../Implementations/Standard/ChartData.swift | 33 +++++-------------- 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index 987bfda013..ac5bc1ef38 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -498,22 +498,20 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral } /// Adds an Entry to the DataSet at the specified index. Entries are added to the end of the list. - @objc open func addEntry(_ e: ChartDataEntry, dataSetIndex: Int) + @objc(addEntry:dataSetIndex:) + open func appendEntry(_ e: ChartDataEntry, toDataSet dataSetIndex: Index) { - if _dataSets.count > dataSetIndex && dataSetIndex >= 0 - { - let set = _dataSets[dataSetIndex] - - if !set.addEntry(e) { return } - - calcMinMax(entry: e, axis: set.axisDependency) - } - else + guard indices.contains(dataSetIndex) else { print("ChartData.addEntry() - Cannot add Entry because dataSetIndex too high or too low.", terminator: "\n") + return } + + let set = self[dataSetIndex] + if !set.addEntry(e) { return } + calcMinMax(entry: e, axis: set.axisDependency) } - + /// Removes the given Entry object from the DataSet at the specified index. @objc @discardableResult open func removeEntry(_ entry: ChartDataEntry, dataSetIndex: Int) -> Bool { @@ -883,17 +881,4 @@ extension ChartData guard let index = index(where: { $0.entryForXValue(entry.x, closestToY: entry.y) === entry }) else { return nil } return self[index] } - - public func appendEntry(_ e: ChartDataEntry, toDataSet dataSetIndex: Index) - { - guard indices.contains(dataSetIndex) else - { - print("ChartData.addEntry() - Cannot add Entry because dataSetIndex too high or too low.", terminator: "\n") - return - } - - let set = self[dataSetIndex] - if !set.addEntry(e) { return } - calcMinMax(entry: e, axis: set.axisDependency) - } } From c76ed4638b47894cc5be317f2e61cfb818faebea Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sun, 14 Jan 2018 23:08:18 -0400 Subject: [PATCH 17/34] pulled latest 4.0.0 --- .../Implementations/Standard/ChartData.swift | 22 ++++--------------- 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index bff4018c33..4437a494cd 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -214,12 +214,7 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral guard dataSets.indices.contains(index) else { return nil } return self[index] } - - @objc open func addDataSet(_ dataSet: Element) - { - append(dataSet) - } - + /// Removes the given DataSet from this data object. /// Also recalculates all minimum and maximum values. /// @@ -229,19 +224,10 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral guard let index = index(where: { $0 === dataSet }) else { return nil } return remove(at: index) } - - /// Removes the DataSet at the given index in the DataSet array from the data object. - /// Also recalculates all minimum and maximum values. - /// - /// - returns: `true` if a DataSet was removed, `false` ifno DataSet could be removed. - @objc @discardableResult open func removeDataSet(at index: Index) -> Element? - { - guard indices.contains(index) else { return nil } - return remove(at: index) - } - + /// Adds an Entry to the DataSet at the specified index. Entries are added to the end of the list. - @objc open func addEntry(_ e: ChartDataEntry, dataSetIndex: Index) + @objc(addEntry:dataSetIndex:) + open func appendEntry(_ e: ChartDataEntry, toDataSet dataSetIndex: Index) { guard indices.contains(dataSetIndex) else { return print("ChartData.addEntry() - Cannot add Entry because dataSetIndex too high or too low.", terminator: "\n") From 09d27a703a31789a78d8a187aaabf7d14a812b51 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sun, 14 Jan 2018 23:34:08 -0400 Subject: [PATCH 18/34] Disabled Collection support for CombinedChartData --- .../Implementations/Standard/ChartData.swift | 41 +++++++++++++++++++ .../Standard/CombinedChartData.swift | 8 +--- .../Standard/PieChartData.swift | 14 ------- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index 4437a494cd..817379f1c4 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -399,6 +399,11 @@ extension ChartData: RangeReplaceableCollection @objc(addDataSet:) public func append(_ newElement: Element) { + guard !(self is CombinedChartData) else + { + fatalError("append(_:) not supported for CombinedData") + } + _dataSets.append(newElement) calcMinMax(dataSet: newElement) } @@ -410,6 +415,7 @@ extension ChartData: RangeReplaceableCollection { fatalError("remove(at:) not supported for CombinedData") } + let element = _dataSets.remove(at: position) calcMinMax() return element @@ -417,6 +423,11 @@ extension ChartData: RangeReplaceableCollection public func removeFirst() -> Element { + guard !(self is CombinedChartData) else + { + fatalError("removeFirst() not supported for CombinedData") + } + let element = _dataSets.removeFirst() notifyDataChanged() return element @@ -424,12 +435,22 @@ extension ChartData: RangeReplaceableCollection public func removeFirst(_ n: Int) { + guard !(self is CombinedChartData) else + { + fatalError("removeFirst(_:) not supported for CombinedData") + } + _dataSets.removeFirst(n) notifyDataChanged() } public func removeLast() -> Element { + guard !(self is CombinedChartData) else + { + fatalError("removeLast() not supported for CombinedData") + } + let element = _dataSets.removeLast() notifyDataChanged() return element @@ -437,18 +458,33 @@ extension ChartData: RangeReplaceableCollection public func removeLast(_ n: Int) { + guard !(self is CombinedChartData) else + { + fatalError("removeLast(_:) not supported for CombinedData") + } + _dataSets.removeLast(n) notifyDataChanged() } public func removeSubrange(_ bounds: R) where R : RangeExpression, ChartData.Index == R.Bound { + guard !(self is CombinedChartData) else + { + fatalError("removeSubrange(_:) not supported for CombinedData") + } + _dataSets.removeSubrange(bounds) notifyDataChanged() } public func removeAll(keepingCapacity keepCapacity: Bool) { + guard !(self is CombinedChartData) else + { + fatalError("removeAll(keepingCapacity:) not supported for CombinedData") + } + _dataSets.removeAll(keepingCapacity: keepCapacity) notifyDataChanged() } @@ -479,6 +515,11 @@ extension ChartData public subscript(entry: ChartDataEntry) -> Element? { + guard !(self is CombinedChartData) else + { + fatalError("subscript(entry:) not supported for CombinedData") + } + guard let index = index(where: { $0.entryForXValue(entry.x, closestToY: entry.y) === entry }) else { return nil } return self[index] } diff --git a/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift b/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift index 994574c66f..1c83738597 100644 --- a/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift @@ -221,13 +221,7 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData return nil } - - open override func removeDataSet(at index: Int) -> Element? - { - print("removeDataSet(index) not supported for CombinedData", terminator: "\n") - return nil - } - + open override func removeEntry(_ entry: ChartDataEntry, dataSetIndex: Int) -> Bool { print("removeEntry(entry, dataSetIndex) not supported for CombinedData", terminator: "\n") diff --git a/Source/Charts/Data/Implementations/Standard/PieChartData.swift b/Source/Charts/Data/Implementations/Standard/PieChartData.swift index 0a98837581..1b6a8adf94 100644 --- a/Source/Charts/Data/Implementations/Standard/PieChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/PieChartData.swift @@ -82,20 +82,6 @@ open class PieChartData: ChartData return dataSet?.entryForIndex(Int(highlight.x)) } - open override func addDataSet(_ d: ChartDataSetProtocol) - { - super.addDataSet(d) - } - - /// Removes the DataSet at the given index in the DataSet array from the data object. - /// Also recalculates all minimum and maximum values. - /// - /// - returns: `true` if a DataSet was removed, `false` ifno DataSet could be removed. - open override func removeDataSet(at index: Int) -> Element? - { - return nil - } - /// - returns: The total y-value sum across all DataSet objects the this object represents. @objc open var yValueSum: Double { From a71f87cfb39c22bb32636ac61c71a89867589af2 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sun, 14 Jan 2018 23:38:55 -0400 Subject: [PATCH 19/34] Removed methods from CombinedChartData --- .../Implementations/Standard/ChartData.swift | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index ac5bc1ef38..5acb2ea926 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -805,6 +805,11 @@ extension ChartData: RangeReplaceableCollection { public func append(_ newElement: Element) { + guard !(self is CombinedChartData) else + { + fatalError("append(_:) not supported for CombinedData") + } + _dataSets.append(newElement) calcMinMax(dataSet: newElement) } @@ -815,6 +820,7 @@ extension ChartData: RangeReplaceableCollection { fatalError("remove(at:) not supported for CombinedData") } + let element = _dataSets.remove(at: position) calcMinMax() return element @@ -822,6 +828,11 @@ extension ChartData: RangeReplaceableCollection public func removeFirst() -> Element { + guard !(self is CombinedChartData) else + { + fatalError("removeFirst() not supported for CombinedData") + } + let element = _dataSets.removeFirst() notifyDataChanged() return element @@ -829,12 +840,22 @@ extension ChartData: RangeReplaceableCollection public func removeFirst(_ n: Int) { + guard !(self is CombinedChartData) else + { + fatalError("removeFirst(_:) not supported for CombinedData") + } + _dataSets.removeFirst(n) notifyDataChanged() } public func removeLast() -> Element { + guard !(self is CombinedChartData) else + { + fatalError("removeLast() not supported for CombinedData") + } + let element = _dataSets.removeLast() notifyDataChanged() return element @@ -842,15 +863,36 @@ extension ChartData: RangeReplaceableCollection public func removeLast(_ n: Int) { + guard !(self is CombinedChartData) else + { + fatalError("removeLast(_:) not supported for CombinedData") + } + _dataSets.removeLast(n) notifyDataChanged() } public func removeSubrange(_ bounds: R) where R : RangeExpression, ChartData.Index == R.Bound { + guard !(self is CombinedChartData) else + { + fatalError("removeSubrange(_:) not supported for CombinedData") + } + _dataSets.removeSubrange(bounds) notifyDataChanged() } + + public func removeAll(keepingCapacity keepCapacity: Bool) + { + guard !(self is CombinedChartData) else + { + fatalError("removeAll(keepingCapacity:) not supported for CombinedData") + } + + _dataSets.removeAll(keepingCapacity: keepCapacity) + notifyDataChanged() + } } // MARK: Swift Accessors From e6eb970aac0bb372c13d50fabda29dd5f0aa0751 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Mon, 15 Jan 2018 20:03:05 -0400 Subject: [PATCH 20/34] Pulled latest 4.0 --- .../Highlight/HorizontalBarHighlighter.swift | 19 ++++--------------- .../Charts/Highlight/RadarHighlighter.swift | 7 +++---- .../Charts/Renderers/BarChartRenderer.swift | 12 ++---------- 3 files changed, 9 insertions(+), 29 deletions(-) diff --git a/Source/Charts/Highlight/HorizontalBarHighlighter.swift b/Source/Charts/Highlight/HorizontalBarHighlighter.swift index 0546c05629..7223ef1dc0 100644 --- a/Source/Charts/Highlight/HorizontalBarHighlighter.swift +++ b/Source/Charts/Highlight/HorizontalBarHighlighter.swift @@ -25,21 +25,10 @@ open class HorizontalBarHighlighter: BarHighlighter if let set = barData.getDataSetByIndex(high.dataSetIndex) as? BarChartDataSetProtocol, set.isStacked { - let pos = getValsForTouch(x: y, y: x) - - guard let high = getHighlight(xValue: Double(pos.y), x: y, y: x) - else { return nil } - - if let set = barData.dataSet(forIndex: high.dataSetIndex) as? BarChartDataSetProtocol, - set.isStacked - { - return getStackedHighlight(high: high, - set: set, - xValue: Double(pos.y), - yValue: Double(pos.x)) - } - - return high + return getStackedHighlight(high: high, + set: set, + xValue: Double(pos.y), + yValue: Double(pos.x)) } return high diff --git a/Source/Charts/Highlight/RadarHighlighter.swift b/Source/Charts/Highlight/RadarHighlighter.swift index ccde6c0af8..d54193c2f8 100644 --- a/Source/Charts/Highlight/RadarHighlighter.swift +++ b/Source/Charts/Highlight/RadarHighlighter.swift @@ -59,10 +59,9 @@ open class RadarHighlighter: PieRadarHighlighter for i in chartData.dataSets.indices { - guard let dataSet = chart.data?.dataSet(forIndex: i) - else { continue } - - guard let entry = dataSet.entryForIndex(index) + guard + let dataSet = chartData.getDataSetByIndex(i), + let entry = dataSet.entryForIndex(index) else { continue } let y = (entry.y - chart.chartYMin) diff --git a/Source/Charts/Renderers/BarChartRenderer.swift b/Source/Charts/Renderers/BarChartRenderer.swift index fe394c4604..63696e0e5d 100644 --- a/Source/Charts/Renderers/BarChartRenderer.swift +++ b/Source/Charts/Renderers/BarChartRenderer.swift @@ -166,16 +166,8 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer for i in 0 ..< barData.dataSetCount { - guard let set = barData.dataSet(forIndex: i) else { continue } - - if set.isVisible - { - if !(set is BarChartDataSetProtocol) - { - fatalError("Datasets for BarChartRenderer must conform to BarChartDataSetProtocol") - } - - drawDataSet(context: context, dataSet: set as! BarChartDataSetProtocol, index: i) + guard let set = barData.getDataSetByIndex(i) as? BarChartDataSetProtocol else { + fatalError("Datasets for BarChartRenderer must conform to IBarChartDataset") } guard set.isVisible else { continue } From 29d3e91ec17543f45e835390b149a498a3dd53cb Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Mon, 15 Jan 2018 20:13:54 -0400 Subject: [PATCH 21/34] Fixes after merge --- Source/Charts/Highlight/BarHighlighter.swift | 14 +-------- .../Highlight/CombinedHighlighter.swift | 30 ++----------------- .../Highlight/HorizontalBarHighlighter.swift | 2 +- .../Charts/Highlight/RadarHighlighter.swift | 7 ++--- .../Charts/Renderers/BarChartRenderer.swift | 4 +-- 5 files changed, 8 insertions(+), 49 deletions(-) diff --git a/Source/Charts/Highlight/BarHighlighter.swift b/Source/Charts/Highlight/BarHighlighter.swift index c578fbe9a0..21e670d6c7 100644 --- a/Source/Charts/Highlight/BarHighlighter.swift +++ b/Source/Charts/Highlight/BarHighlighter.swift @@ -24,7 +24,7 @@ open class BarHighlighter: ChartHighlighter let pos = getValsForTouch(x: x, y: y) - if let set = barData.getDataSetByIndex(high.dataSetIndex) as? BarChartDataSetProtocol, + if let set = barData[high.dataSetIndex] as? BarChartDataSetProtocol, set.isStacked { return getStackedHighlight(high: high, @@ -34,18 +34,6 @@ open class BarHighlighter: ChartHighlighter } else { - let pos = getValsForTouch(x: x, y: y) - - if - let set = barData.dataSet(forIndex: high!.dataSetIndex) as? BarChartDataSetProtocol, - set.isStacked - { - return getStackedHighlight(high: high!, - set: set, - xValue: Double(pos.x), - yValue: Double(pos.y)) - } - return high } } diff --git a/Source/Charts/Highlight/CombinedHighlighter.swift b/Source/Charts/Highlight/CombinedHighlighter.swift index 020684fed2..c292ec5515 100644 --- a/Source/Charts/Highlight/CombinedHighlighter.swift +++ b/Source/Charts/Highlight/CombinedHighlighter.swift @@ -48,13 +48,9 @@ open class CombinedHighlighter: ChartHighlighter } else { - for j in 0.. Date: Mon, 15 Jan 2018 20:33:37 -0400 Subject: [PATCH 22/34] Removed used of dataSet(forIndex:) --- .../Charts/Charts/BarLineChartViewBase.swift | 2 +- Source/Charts/Charts/ChartViewBase.swift | 10 ++++----- Source/Charts/Charts/PieChartView.swift | 5 +---- .../Implementations/Standard/ChartData.swift | 1 + .../Standard/CombinedChartData.swift | 6 +++--- .../Standard/PieChartData.swift | 2 +- .../Standard/RadarChartData.swift | 2 +- .../Charts/Renderers/BarChartRenderer.swift | 8 +++---- .../Renderers/BubbleChartRenderer.swift | 2 +- .../Renderers/CandleStickChartRenderer.swift | 2 +- .../HorizontalBarChartRenderer.swift | 10 ++++----- Source/Charts/Renderers/LegendRenderer.swift | 6 ++---- .../Charts/Renderers/LineChartRenderer.swift | 21 ++++++++----------- .../Charts/Renderers/PieChartRenderer.swift | 2 +- .../Charts/Renderers/RadarChartRenderer.swift | 6 +++--- .../Renderers/ScatterChartRenderer.swift | 21 ++++++++----------- 16 files changed, 48 insertions(+), 58 deletions(-) diff --git a/Source/Charts/Charts/BarLineChartViewBase.swift b/Source/Charts/Charts/BarLineChartViewBase.swift index 195ad58f3f..2a43fba8e1 100644 --- a/Source/Charts/Charts/BarLineChartViewBase.swift +++ b/Source/Charts/Charts/BarLineChartViewBase.swift @@ -1686,7 +1686,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD let h = getHighlightByTouchPoint(pt) if h !== nil { - return data?.dataSet(forIndex: h!.dataSetIndex) as! BarLineScatterCandleBubbleChartDataSetProtocol + return data?[h!.dataSetIndex] as! BarLineScatterCandleBubbleChartDataSetProtocol } return nil } diff --git a/Source/Charts/Charts/ChartViewBase.swift b/Source/Charts/Charts/ChartViewBase.swift index cca5ba26a3..2c61a17b93 100644 --- a/Source/Charts/Charts/ChartViewBase.swift +++ b/Source/Charts/Charts/ChartViewBase.swift @@ -398,14 +398,14 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate Swift.print("Value not highlighted because data is nil") return } - - if dataSetIndex < 0 || dataSetIndex >= data.dataSetCount + + if data.indices.contains(dataSetIndex) { - highlightValue(nil, callDelegate: callDelegate) + highlightValue(Highlight(x: x, y: y, dataSetIndex: dataSetIndex), callDelegate: callDelegate) } else { - highlightValue(Highlight(x: x, y: y, dataSetIndex: dataSetIndex), callDelegate: callDelegate) + highlightValue(nil, callDelegate: callDelegate) } } @@ -480,7 +480,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate for highlight in highlighted { guard - let set = data?.dataSet(forIndex: highlight.dataSetIndex), + let set = data?[highlight.dataSetIndex], let e = data?.entry(for: highlight) else { continue } diff --git a/Source/Charts/Charts/PieChartView.swift b/Source/Charts/Charts/PieChartView.swift index b4bfbd6b7f..8ab6dd7b5b 100644 --- a/Source/Charts/Charts/PieChartView.swift +++ b/Source/Charts/Charts/PieChartView.swift @@ -194,14 +194,11 @@ 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 { - let set = dataSets[i] let entryCount = set.entryCount for j in 0 ..< entryCount diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index 817379f1c4..32fbe478c8 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -116,6 +116,7 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral } /// - returns: The number of LineDataSets this object contains + // exists only for objc compatibility @objc open var dataSetCount: Int { return dataSets.count diff --git a/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift b/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift index 1c83738597..2c8f8de1c7 100644 --- a/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift @@ -258,13 +258,13 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData let data = dataByIndex(highlight.dataIndex) - if highlight.dataSetIndex >= data.dataSetCount + 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.dataSet(forIndex: highlight.dataSetIndex)? + return data[highlight.dataSetIndex] .entriesForXValue(highlight.x) .first { $0.y == highlight.y || highlight.y.isNaN } } @@ -282,7 +282,7 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData let data = dataByIndex(highlight.dataIndex) - if highlight.dataSetIndex >= data.dataSetCount + if highlight.dataSetIndex >= data.endIndex { return nil } diff --git a/Source/Charts/Data/Implementations/Standard/PieChartData.swift b/Source/Charts/Data/Implementations/Standard/PieChartData.swift index 1b6a8adf94..a141a7b56b 100644 --- a/Source/Charts/Data/Implementations/Standard/PieChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/PieChartData.swift @@ -50,7 +50,7 @@ open class PieChartData: ChartData open override func dataSet(forIndex index: ChartData.Index) -> ChartData.Element? { guard index == 0 else { return nil } - return super.dataSet(forIndex: index) + return self[index] } open override func dataSet(forLabel label: String, ignorecase: Bool) -> ChartDataSetProtocol? diff --git a/Source/Charts/Data/Implementations/Standard/RadarChartData.swift b/Source/Charts/Data/Implementations/Standard/RadarChartData.swift index 808fe90510..2f8a2db7ae 100644 --- a/Source/Charts/Data/Implementations/Standard/RadarChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/RadarChartData.swift @@ -46,6 +46,6 @@ open class RadarChartData: ChartData @objc open override func entry(for highlight: Highlight) -> ChartDataEntry? { - return dataSet(forIndex: highlight.dataSetIndex)?.entryForIndex(Int(highlight.x)) + return self[highlight.dataSetIndex].entryForIndex(Int(highlight.x)) } } diff --git a/Source/Charts/Renderers/BarChartRenderer.swift b/Source/Charts/Renderers/BarChartRenderer.swift index 0923475195..5e740a59df 100644 --- a/Source/Charts/Renderers/BarChartRenderer.swift +++ b/Source/Charts/Renderers/BarChartRenderer.swift @@ -37,13 +37,13 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer guard let barData = dataProvider?.barData else { return _buffers.removeAll() } // Matche buffers count to dataset count - if _buffers.count != barData.dataSetCount + if _buffers.count != barData.count { - while _buffers.count < barData.dataSetCount + while _buffers.count < barData.count { _buffers.append(Buffer()) } - while _buffers.count > barData.dataSetCount + while _buffers.count > barData.count { _buffers.removeLast() } @@ -551,7 +551,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer for high in indices { guard - let set = barData.dataSet(forIndex: high.dataSetIndex) as? BarChartDataSetProtocol, + let set = barData[high.dataSetIndex] as? BarChartDataSetProtocol, set.isHighlightEnabled else { continue } diff --git a/Source/Charts/Renderers/BubbleChartRenderer.swift b/Source/Charts/Renderers/BubbleChartRenderer.swift index 0bb51b091e..5d8723a6cf 100644 --- a/Source/Charts/Renderers/BubbleChartRenderer.swift +++ b/Source/Charts/Renderers/BubbleChartRenderer.swift @@ -219,7 +219,7 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer for high in indices { guard - let dataSet = bubbleData.dataSet(forIndex: high.dataSetIndex) as? BubbleChartDataSetProtocol, + let dataSet = bubbleData[high.dataSetIndex] as? BubbleChartDataSetProtocol, dataSet.isHighlightEnabled, let entry = dataSet.entryForXValue(high.x, closestToY: high.y) as? BubbleChartDataEntry, isInBoundsX(entry: entry, dataSet: dataSet) diff --git a/Source/Charts/Renderers/CandleStickChartRenderer.swift b/Source/Charts/Renderers/CandleStickChartRenderer.swift index 4c0e4d223c..ef6f4532e0 100644 --- a/Source/Charts/Renderers/CandleStickChartRenderer.swift +++ b/Source/Charts/Renderers/CandleStickChartRenderer.swift @@ -331,7 +331,7 @@ open class CandleStickChartRenderer: LineScatterCandleRadarRenderer for high in indices { guard - let set = candleData.dataSet(forIndex: high.dataSetIndex) as? CandleChartDataSetProtocol, + let set = candleData[high.dataSetIndex] as? CandleChartDataSetProtocol, set.isHighlightEnabled else { continue } diff --git a/Source/Charts/Renderers/HorizontalBarChartRenderer.swift b/Source/Charts/Renderers/HorizontalBarChartRenderer.swift index 768e79c307..c88547c41c 100644 --- a/Source/Charts/Renderers/HorizontalBarChartRenderer.swift +++ b/Source/Charts/Renderers/HorizontalBarChartRenderer.swift @@ -37,19 +37,19 @@ open class HorizontalBarChartRenderer: BarChartRenderer if let barData = dataProvider?.barData { // Matche buffers count to dataset count - if _buffers.count != barData.dataSetCount + if _buffers.count != barData.count { - while _buffers.count < barData.dataSetCount + while _buffers.count < barData.count { _buffers.append(Buffer()) } - while _buffers.count > barData.dataSetCount + while _buffers.count > barData.count { _buffers.removeLast() } } - for i in stride(from: 0, to: barData.dataSetCount, by: 1) + for i in barData.indices { let set = barData.dataSets[i] as! BarChartDataSetProtocol let size = set.entryCount * (set.isStacked ? set.stackSize : 1) @@ -314,7 +314,7 @@ open class HorizontalBarChartRenderer: BarChartRenderer var negOffset: CGFloat let drawValueAboveBar = dataProvider.isDrawValueAboveBarEnabled - for dataSetIndex in 0 ..< barData.dataSetCount + for dataSetIndex in barData.indices { guard let dataSet = dataSets[dataSetIndex] as? BarChartDataSetProtocol else { continue } diff --git a/Source/Charts/Renderers/LegendRenderer.swift b/Source/Charts/Renderers/LegendRenderer.swift index 6526cd996d..66d3194281 100755 --- a/Source/Charts/Renderers/LegendRenderer.swift +++ b/Source/Charts/Renderers/LegendRenderer.swift @@ -42,10 +42,8 @@ open class LegendRenderer: NSObject, Renderer var entries: [LegendEntry] = [] // loop for building up the colors and labels used in the legend - for i in 0.. Date: Sun, 21 Jan 2018 17:56:51 -0400 Subject: [PATCH 23/34] Fixed merge conflicts --- .../Implementations/Standard/ChartData.swift | 171 +----------------- .../Standard/CombinedChartData.swift | 18 +- .../Standard/PieChartData.swift | 19 -- 3 files changed, 16 insertions(+), 192 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index e149fd3a85..b4b7dfbedd 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -594,11 +594,6 @@ extension ChartData: RangeReplaceableCollection @objc(addDataSet:) public func append(_ newElement: Element) { - guard !(self is CombinedChartData) else - { - fatalError("append(_:) not supported for CombinedData") - } - _dataSets.append(newElement) calcMinMax(dataSet: newElement) } @@ -606,11 +601,6 @@ extension ChartData: RangeReplaceableCollection @objc(removeDataSetByIndex:) public func remove(at position: Index) -> Element { - guard !(self is CombinedChartData) else - { - fatalError("remove(at:) not supported for CombinedData") - } - let element = _dataSets.remove(at: position) calcMinMax() return element @@ -662,7 +652,7 @@ extension ChartData: RangeReplaceableCollection notifyDataChanged() } - public func removeSubrange(_ bounds: R) where R : RangeExpression, ChartData.Index == R.Bound + public func removeSubrange(_ bounds: R) where R : RangeExpression, Index == R.Bound { guard !(self is CombinedChartData) else { @@ -683,167 +673,16 @@ extension ChartData: RangeReplaceableCollection _dataSets.removeAll(keepingCapacity: keepCapacity) notifyDataChanged() } -} - -// MARK: Swift Accessors -extension ChartData -{ - /// Retrieve the index of a ChartDataSet with a specific label from the ChartData. Search can be case sensitive or not. - /// **IMPORTANT: This method does calculations at runtime, do not over-use in performance critical situations.** - /// - /// - Parameters: - /// - label: The label to search for - /// - ignoreCase: if true, the search is not case-sensitive - /// - Returns: The index of the DataSet Object with the given label. `nil` if not found - public func index(forLabel label: String, ignoreCase: Bool) -> Index? - { - return ignoreCase - ? index { $0.label?.caseInsensitiveCompare(label) == .orderedSame } - : index { $0.label == label } - } - - public subscript(label: String, ignoreCase: Bool) -> Element? - { - guard let index = index(forLabel: label, ignoreCase: ignoreCase) else { return nil } - return self[index] - } - - public subscript(entry: ChartDataEntry) -> Element? - { - guard let index = index(where: { $0.entryForXValue(entry.x, closestToY: entry.y) === entry }) else { return nil } - return self[index] - } -} - -// MARK: MutableCollection -extension ChartData: MutableCollection -{ - public typealias Index = Int - public typealias Element = ChartDataSetProtocol - - public var startIndex: Index - { - return _dataSets.startIndex - } - - public var endIndex: Index - { - return _dataSets.endIndex - } - - public func index(after: Index) -> Index - { - return _dataSets.index(after: after) - } - - public subscript(position: Index) -> Element - { - get{ return _dataSets[position] } - set{ self._dataSets[position] = newValue } - } -} - -// MARK: RandomAccessCollection -extension ChartData: RandomAccessCollection -{ - public func index(before: Index) -> Index - { - return _dataSets.index(before: before) - } -} - -// MARK: RangeReplaceableCollection -extension ChartData: RangeReplaceableCollection -{ - public func append(_ newElement: Element) - { - guard !(self is CombinedChartData) else - { - fatalError("append(_:) not supported for CombinedData") - } - - _dataSets.append(newElement) - calcMinMax(dataSet: newElement) - } - - public func remove(at position: Index) -> Element - { - guard !(self is CombinedChartData) else - { - fatalError("remove(at:) not supported for CombinedData") - } - - let element = _dataSets.remove(at: position) - calcMinMax() - return element - } - - public func removeFirst() -> Element - { - guard !(self is CombinedChartData) else - { - fatalError("removeFirst() not supported for CombinedData") - } - - let element = _dataSets.removeFirst() - notifyDataChanged() - return element - } - - public func removeFirst(_ n: Int) - { - guard !(self is CombinedChartData) else - { - fatalError("removeFirst(_:) not supported for CombinedData") - } - - _dataSets.removeFirst(n) - notifyDataChanged() - } - - public func removeLast() -> Element - { - guard !(self is CombinedChartData) else - { - fatalError("removeLast() not supported for CombinedData") - } - - let element = _dataSets.removeLast() - notifyDataChanged() - return element - } - - public func removeLast(_ n: Int) - { - guard !(self is CombinedChartData) else - { - fatalError("removeLast(_:) not supported for CombinedData") - } - - _dataSets.removeLast(n) - notifyDataChanged() - } - - public func removeSubrange(_ bounds: R) where R : RangeExpression, ChartData.Index == R.Bound - { - guard !(self is CombinedChartData) else - { - fatalError("removeSubrange(_:) not supported for CombinedData") - } - - _dataSets.removeSubrange(bounds) - notifyDataChanged() - } - public func removeAll(keepingCapacity keepCapacity: Bool) + public func replaceSubrange(_ subrange: Swift.Range, with newElements: C) where C : Collection, Element == C.Element { guard !(self is CombinedChartData) else { - fatalError("removeAll(keepingCapacity:) not supported for CombinedData") + fatalError("replaceSubrange(_:) not supported for CombinedData") } - _dataSets.removeAll(keepingCapacity: keepCapacity) - notifyDataChanged() + _dataSets.replaceSubrange(subrange, with: newElements) + newElements.forEach { self.calcMinMax(dataSet: $0) } } } diff --git a/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift b/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift index 2e28ab2421..23e6992ebe 100644 --- a/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift @@ -227,13 +227,7 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData return success } - - open override func removeDataSetByIndex(_ index: Int) -> Bool - { - print("removeDataSet(index) not supported for CombinedData", terminator: "\n") - return false - } - + open override func removeEntry(_ entry: ChartDataEntry, dataSetIndex: Int) -> Bool { print("removeEntry(entry, dataSetIndex) not supported for CombinedData", terminator: "\n") @@ -307,4 +301,14 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData return data.dataSets[highlight.dataSetIndex] } + + // MARK: Unsupported Collection Methods + + public override func append(_ newElement: ChartData.Element) { + fatalError("append(_:) not supported for CombinedData") + } + + public override func remove(at i: Int) -> ChartDataSetProtocol { + fatalError("remove(at:) not supported for CombinedData") + } } diff --git a/Source/Charts/Data/Implementations/Standard/PieChartData.swift b/Source/Charts/Data/Implementations/Standard/PieChartData.swift index 1ea1f2675a..5ee2f4347e 100644 --- a/Source/Charts/Data/Implementations/Standard/PieChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/PieChartData.swift @@ -85,25 +85,6 @@ open class PieChartData: ChartData return dataSet?.entryForIndex(Int(highlight.x)) } - open override func addDataSet(_ d: ChartDataSetProtocol!) - { - super.addDataSet(d) - } - - /// Removes the DataSet at the given index in the DataSet array from the data object. - /// Also recalculates all minimum and maximum values. - /// - /// - returns: `true` if a DataSet was removed, `false` ifno DataSet could be removed. - open override func removeDataSetByIndex(_ index: Int) -> Bool - { - if index >= _dataSets.count || index < 0 - { - return false - } - - return false - } - /// - returns: The total y-value sum across all DataSet objects the this object represents. @objc open var yValueSum: Double { From 31a76eb0b7fa99ba05af6cc97e2144cf2bb9f2d1 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Tue, 23 Jan 2018 06:25:36 -0400 Subject: [PATCH 24/34] Fixed merge conflicts --- Source/Charts/Charts/ChartViewBase.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Charts/Charts/ChartViewBase.swift b/Source/Charts/Charts/ChartViewBase.swift index 5f0c2b5843..abca440d61 100644 --- a/Source/Charts/Charts/ChartViewBase.swift +++ b/Source/Charts/Charts/ChartViewBase.swift @@ -250,7 +250,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate } - if let formatter = _defaultValueFormatter as? DefaultValueFormatter + if let formatter = defaultValueFormatter as? DefaultValueFormatter { // setup the formatter with a new number of digits let digits = reference.decimalPlaces From b7e6f93449d5d4f23ad3373d7fd051416af41f9b Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Fri, 26 Jan 2018 07:03:33 -0400 Subject: [PATCH 25/34] updated demos --- .../Swift/Demos/CandleStickChartViewController.swift | 2 +- .../Swift/Demos/CubicLineChartViewController.swift | 12 +++++++----- .../Swift/Demos/LineChart1ViewController.swift | 12 +++++++----- .../Swift/Demos/LineChart2ViewController.swift | 12 +++++++----- .../Swift/Demos/LineChartTimeViewController.swift | 12 +++++++----- .../Demos/MultipleLinesChartViewController.swift | 10 ++++++---- .../Swift/Demos/RadarChartViewController.swift | 6 ++++-- 7 files changed, 39 insertions(+), 27 deletions(-) diff --git a/ChartsDemo/Swift/Demos/CandleStickChartViewController.swift b/ChartsDemo/Swift/Demos/CandleStickChartViewController.swift index d6a3b8caff..e3b7418fef 100644 --- a/ChartsDemo/Swift/Demos/CandleStickChartViewController.swift +++ b/ChartsDemo/Swift/Demos/CandleStickChartViewController.swift @@ -104,7 +104,7 @@ class CandleStickChartViewController: DemoBaseViewController { override func optionTapped(_ option: Option) { if .toggleShadowColorSameAsCandle ~= option { - for set in chartView.data as! CandleChartData { + for case let set as CandleChartDataSet in chartView.data! { set.shadowColorSameAsCandle = !set.shadowColorSameAsCandle } chartView.notifyDataSetChanged() diff --git a/ChartsDemo/Swift/Demos/CubicLineChartViewController.swift b/ChartsDemo/Swift/Demos/CubicLineChartViewController.swift index 0ff8135902..320b02e2c0 100644 --- a/ChartsDemo/Swift/Demos/CubicLineChartViewController.swift +++ b/ChartsDemo/Swift/Demos/CubicLineChartViewController.swift @@ -109,33 +109,35 @@ class CubicLineChartViewController: DemoBaseViewController { } override func optionTapped(_ option: Option) { + guard let data = chartView.data else { return } + switch option { case .toggleFilled: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.drawFilledEnabled = !set.drawFilledEnabled } chartView.setNeedsDisplay() case .toggleCircles: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.drawCirclesEnabled = !set.drawCirclesEnabled } chartView.setNeedsDisplay() case .toggleCubic: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.mode = (set.mode == .cubicBezier) ? .linear : .cubicBezier } chartView.setNeedsDisplay() case .toggleStepped: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.mode = (set.mode == .stepped) ? .linear : .stepped } chartView.setNeedsDisplay() case .toggleHorizontalCubic: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.mode = (set.mode == .cubicBezier) ? .horizontalBezier : .cubicBezier } chartView.setNeedsDisplay() diff --git a/ChartsDemo/Swift/Demos/LineChart1ViewController.swift b/ChartsDemo/Swift/Demos/LineChart1ViewController.swift index 153cf6b234..43c345d4c9 100644 --- a/ChartsDemo/Swift/Demos/LineChart1ViewController.swift +++ b/ChartsDemo/Swift/Demos/LineChart1ViewController.swift @@ -142,33 +142,35 @@ class LineChart1ViewController: DemoBaseViewController { } override func optionTapped(_ option: Option) { + guard let data = chartView.data else { return } + switch option { case .toggleFilled: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.drawFilledEnabled = !set.drawFilledEnabled } chartView.setNeedsDisplay() case .toggleCircles: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.drawCirclesEnabled = !set.drawCirclesEnabled } chartView.setNeedsDisplay() case .toggleCubic: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.mode = (set.mode == .cubicBezier) ? .linear : .cubicBezier } chartView.setNeedsDisplay() case .toggleStepped: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.mode = (set.mode == .stepped) ? .linear : .stepped } chartView.setNeedsDisplay() case .toggleHorizontalCubic: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.mode = (set.mode == .cubicBezier) ? .horizontalBezier : .cubicBezier } chartView.setNeedsDisplay() diff --git a/ChartsDemo/Swift/Demos/LineChart2ViewController.swift b/ChartsDemo/Swift/Demos/LineChart2ViewController.swift index cc3899f15b..3132199f91 100644 --- a/ChartsDemo/Swift/Demos/LineChart2ViewController.swift +++ b/ChartsDemo/Swift/Demos/LineChart2ViewController.swift @@ -143,33 +143,35 @@ class LineChart2ViewController: DemoBaseViewController { } override func optionTapped(_ option: Option) { + guard let data = chartView.data else { return } + switch option { case .toggleFilled: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.drawFilledEnabled = !set.drawFilledEnabled } chartView.setNeedsDisplay() case .toggleCircles: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.drawCirclesEnabled = !set.drawCirclesEnabled } chartView.setNeedsDisplay() case .toggleCubic: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.mode = (set.mode == .cubicBezier) ? .linear : .cubicBezier } chartView.setNeedsDisplay() case .toggleStepped: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.mode = (set.mode == .stepped) ? .linear : .stepped } chartView.setNeedsDisplay() case .toggleHorizontalCubic: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.mode = (set.mode == .cubicBezier) ? .horizontalBezier : .cubicBezier } chartView.setNeedsDisplay() diff --git a/ChartsDemo/Swift/Demos/LineChartTimeViewController.swift b/ChartsDemo/Swift/Demos/LineChartTimeViewController.swift index 801fc494af..554e6354f0 100644 --- a/ChartsDemo/Swift/Demos/LineChartTimeViewController.swift +++ b/ChartsDemo/Swift/Demos/LineChartTimeViewController.swift @@ -118,33 +118,35 @@ class LineChartTimeViewController: DemoBaseViewController { } override func optionTapped(_ option: Option) { + guard let data = chartView.data else { return } + switch option { case .toggleFilled: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.drawFilledEnabled = !set.drawFilledEnabled } chartView.setNeedsDisplay() case .toggleCircles: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.drawCirclesEnabled = !set.drawCirclesEnabled } chartView.setNeedsDisplay() case .toggleCubic: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.mode = (set.mode == .cubicBezier) ? .linear : .cubicBezier } chartView.setNeedsDisplay() case .toggleStepped: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.mode = (set.mode == .stepped) ? .linear : .stepped } chartView.setNeedsDisplay() case .toggleHorizontalCubic: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.mode = (set.mode == .cubicBezier) ? .horizontalBezier : .cubicBezier } chartView.setNeedsDisplay() diff --git a/ChartsDemo/Swift/Demos/MultipleLinesChartViewController.swift b/ChartsDemo/Swift/Demos/MultipleLinesChartViewController.swift index 9ac283d02a..c358e8b3a2 100644 --- a/ChartsDemo/Swift/Demos/MultipleLinesChartViewController.swift +++ b/ChartsDemo/Swift/Demos/MultipleLinesChartViewController.swift @@ -99,27 +99,29 @@ class MultipleLinesChartViewController: DemoBaseViewController { } override func optionTapped(_ option: Option) { + guard let data = chartView.data else { return } + switch option { case .toggleFilled: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.drawFilledEnabled = !set.drawFilledEnabled } chartView.setNeedsDisplay() case .toggleCircles: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.drawCirclesEnabled = !set.drawCirclesEnabled } chartView.setNeedsDisplay() case .toggleCubic: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.mode = (set.mode == .cubicBezier) ? .linear : .cubicBezier } chartView.setNeedsDisplay() case .toggleStepped: - for set in chartView.data as! LineChartData { + for case let set as LineChartDataSet in data { set.mode = (set.mode == .stepped) ? .linear : .stepped } chartView.setNeedsDisplay() diff --git a/ChartsDemo/Swift/Demos/RadarChartViewController.swift b/ChartsDemo/Swift/Demos/RadarChartViewController.swift index 512cf57da5..1d680293b3 100644 --- a/ChartsDemo/Swift/Demos/RadarChartViewController.swift +++ b/ChartsDemo/Swift/Demos/RadarChartViewController.swift @@ -151,6 +151,8 @@ class RadarChartViewController: DemoBaseViewController { } override func optionTapped(_ option: Option) { + guard let data = chartView.data else { return } + switch option { case .toggleXLabels: chartView.xAxis.drawLabelsEnabled = !chartView.xAxis.drawLabelsEnabled @@ -166,14 +168,14 @@ class RadarChartViewController: DemoBaseViewController { chartView.rotationEnabled = !chartView.rotationEnabled case .toggleFilled: - for set in chartView.data as! RadarChartData { + for case let set as RadarChartDataSet in data { set.drawFilledEnabled = !set.drawFilledEnabled } chartView.setNeedsDisplay() case .toggleHighlightCircle: - for set in chartView.data as! RadarChartData { + for case let set as RadarChartDataSet in data { set.drawHighlightCircleEnabled = !set.drawHighlightCircleEnabled } chartView.setNeedsDisplay() From 9f5a7f9b952fce53fc7b07ad1d89ddf868809d9d Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Fri, 26 Jan 2018 17:25:43 -0400 Subject: [PATCH 26/34] Pulled latest 4.0.0 --- Source/Charts/Data/Implementations/Standard/ChartData.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index b78575792e..82785197e8 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -71,10 +71,10 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral rightAxisMin = .greatestFiniteMagnitude xMax = -.greatestFiniteMagnitude xMin = .greatestFiniteMagnitude - + forEach { calcMinMax(dataSet: $0) } } - + /// Adjusts the current minimum and maximum values based on the provided Entry object. @objc open func calcMinMax(entry e: ChartDataEntry, axis: YAxis.AxisDependency) { From b73ac7e5bab57a7c173bc988b0ef180e2c5bb1f0 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Fri, 26 Jan 2018 17:54:51 -0400 Subject: [PATCH 27/34] Fixed demos --- ChartsDemo/Swift/DemoBaseViewController.swift | 2 +- ChartsDemo/Swift/Demos/ColoredLineChartViewController.swift | 2 +- ChartsDemo/Swift/Demos/CombinedChartViewController.swift | 2 +- ChartsDemo/Swift/Demos/LineChart2ViewController.swift | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ChartsDemo/Swift/DemoBaseViewController.swift b/ChartsDemo/Swift/DemoBaseViewController.swift index 82265eb594..b643420520 100644 --- a/ChartsDemo/Swift/DemoBaseViewController.swift +++ b/ChartsDemo/Swift/DemoBaseViewController.swift @@ -129,7 +129,7 @@ class DemoBaseViewController: UIViewController, ChartViewDelegate { chartView.setNeedsDisplay() case .toggleHighlight: - chartView.data!.highlightEnabled = !chartView.data!.isHighlightEnabled + chartView.data!.isHighlightEnabled = !chartView.data!.isHighlightEnabled chartView.setNeedsDisplay() case .animateX: diff --git a/ChartsDemo/Swift/Demos/ColoredLineChartViewController.swift b/ChartsDemo/Swift/Demos/ColoredLineChartViewController.swift index 7a8e443502..dfc3a2464c 100644 --- a/ChartsDemo/Swift/Demos/ColoredLineChartViewController.swift +++ b/ChartsDemo/Swift/Demos/ColoredLineChartViewController.swift @@ -31,7 +31,7 @@ class ColoredLineChartViewController: DemoBaseViewController { } func setupChart(_ chart: LineChartView, data: LineChartData, color: UIColor) { - (data.getDataSetByIndex(0) as! LineChartDataSet).circleHoleColor = color + (data[0] as! LineChartDataSet).circleHoleColor = color chart.delegate = self chart.backgroundColor = color diff --git a/ChartsDemo/Swift/Demos/CombinedChartViewController.swift b/ChartsDemo/Swift/Demos/CombinedChartViewController.swift index 82d26e6523..7e779f1a3f 100644 --- a/ChartsDemo/Swift/Demos/CombinedChartViewController.swift +++ b/ChartsDemo/Swift/Demos/CombinedChartViewController.swift @@ -112,7 +112,7 @@ class CombinedChartViewController: DemoBaseViewController { case .removeDataSet: let rnd = Int(arc4random_uniform(UInt32(chartView.data!.dataSetCount))) - chartView.data?.removeDataSet(chartView.data!.getDataSetByIndex(rnd)) + chartView.data?.removeDataSet(chartView.data![rnd]) chartView.data?.notifyDataChanged() chartView.notifyDataSetChanged() diff --git a/ChartsDemo/Swift/Demos/LineChart2ViewController.swift b/ChartsDemo/Swift/Demos/LineChart2ViewController.swift index 3132199f91..f7d8e5a1fb 100644 --- a/ChartsDemo/Swift/Demos/LineChart2ViewController.swift +++ b/ChartsDemo/Swift/Demos/LineChart2ViewController.swift @@ -194,7 +194,7 @@ class LineChart2ViewController: DemoBaseViewController { super.chartValueSelected(chartView, entry: entry, highlight: highlight) self.chartView.centerViewToAnimated(xValue: entry.x, yValue: entry.y, - axis: self.chartView.data!.getDataSetByIndex(highlight.dataSetIndex).axisDependency, + axis: self.chartView.data![highlight.dataSetIndex].axisDependency, duration: 1) //[_chartView moveViewToAnimatedWithXValue:entry.x yValue:entry.y axis:[_chartView.data getDataSetByIndex:dataSetIndex].axisDependency duration:1.0]; //[_chartView zoomAndCenterViewAnimatedWithScaleX:1.8 scaleY:1.8 xValue:entry.x yValue:entry.y axis:[_chartView.data getDataSetByIndex:dataSetIndex].axisDependency duration:1.0]; From 33ed6b54611d5c359cf12607f870733813cbaa1d Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Fri, 26 Jan 2018 17:55:44 -0400 Subject: [PATCH 28/34] Fixed objective c demos --- ChartsDemo/Objective-C/DemoBaseViewController.m | 2 +- ChartsDemo/Objective-C/Demos/ColoredLineChartViewController.m | 2 +- ChartsDemo/Objective-C/Demos/CombinedChartViewController.m | 2 +- ChartsDemo/Objective-C/Demos/LineChart2ViewController.m | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ChartsDemo/Objective-C/DemoBaseViewController.m b/ChartsDemo/Objective-C/DemoBaseViewController.m index 26cc0c7369..02dd5ff060 100644 --- a/ChartsDemo/Objective-C/DemoBaseViewController.m +++ b/ChartsDemo/Objective-C/DemoBaseViewController.m @@ -95,7 +95,7 @@ - (void)handleOption:(NSString *)key forChartView:(ChartViewBase *)chartView if ([key isEqualToString:@"toggleHighlight"]) { - chartView.data.highlightEnabled = !chartView.data.isHighlightEnabled; + chartView.data.isHighlightEnabled = !chartView.data.isHighlightEnabled; [chartView setNeedsDisplay]; } diff --git a/ChartsDemo/Objective-C/Demos/ColoredLineChartViewController.m b/ChartsDemo/Objective-C/Demos/ColoredLineChartViewController.m index ee4fed4768..86b0885afb 100644 --- a/ChartsDemo/Objective-C/Demos/ColoredLineChartViewController.m +++ b/ChartsDemo/Objective-C/Demos/ColoredLineChartViewController.m @@ -44,7 +44,7 @@ - (void)viewDidLoad - (void)setupChart:(LineChartView *)chart data:(LineChartData *)data color:(UIColor *)color { - [(LineChartDataSet *)[data getDataSetByIndex:0] setCircleHoleColor:color]; + [(LineChartDataSet *)[data dataSetForIndex:0] setCircleHoleColor:color]; chart.delegate = self; chart.backgroundColor = color; diff --git a/ChartsDemo/Objective-C/Demos/CombinedChartViewController.m b/ChartsDemo/Objective-C/Demos/CombinedChartViewController.m index 4605c0e7fa..f7abc28f08 100644 --- a/ChartsDemo/Objective-C/Demos/CombinedChartViewController.m +++ b/ChartsDemo/Objective-C/Demos/CombinedChartViewController.m @@ -151,7 +151,7 @@ - (void)optionTapped:(NSString *)key if ([key isEqualToString:@"removeDataSet"]) { int rnd = (int)arc4random_uniform((float)_chartView.data.dataSetCount); - [_chartView.data removeDataSet:[_chartView.data getDataSetByIndex:rnd]]; + [_chartView.data removeDataSet:[_chartView.data dataSetForIndex:rnd]]; [_chartView.data notifyDataChanged]; [_chartView notifyDataSetChanged]; } diff --git a/ChartsDemo/Objective-C/Demos/LineChart2ViewController.m b/ChartsDemo/Objective-C/Demos/LineChart2ViewController.m index 38f1bc0777..30e9ee9beb 100644 --- a/ChartsDemo/Objective-C/Demos/LineChart2ViewController.m +++ b/ChartsDemo/Objective-C/Demos/LineChart2ViewController.m @@ -282,7 +282,7 @@ - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartData { NSLog(@"chartValueSelected"); - [_chartView centerViewToAnimatedWithXValue:entry.x yValue:entry.y axis:[_chartView.data getDataSetByIndex:highlight.dataSetIndex].axisDependency duration:1.0]; + [_chartView centerViewToAnimatedWithXValue:entry.x yValue:entry.y axis:[_chartView.data dataSetForIndex:highlight.dataSetIndex].axisDependency duration:1.0]; //[_chartView moveViewToAnimatedWithXValue:entry.x yValue:entry.y axis:[_chartView.data getDataSetByIndex:dataSetIndex].axisDependency duration:1.0]; //[_chartView zoomAndCenterViewAnimatedWithScaleX:1.8 scaleY:1.8 xValue:entry.x yValue:entry.y axis:[_chartView.data getDataSetByIndex:dataSetIndex].axisDependency duration:1.0]; From b1ca0c55d97315aa2e8fad0303bda58709d9e18a Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Tue, 13 Mar 2018 20:54:03 -0300 Subject: [PATCH 29/34] Moved travis to Xcode 9.3 beta temporarily --- .travis.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6d6fe4d549..0740950980 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,12 @@ language: objective-c -osx_image: xcode9 +osx_image: xcode9.3beta matrix: include: - - osx_image: xcode9 + - osx_image: xcode9.3beta env: PLATFORM="iOS" - - osx_image: xcode9 + - osx_image: xcode9.3beta env: PLATFORM="tvOS" - - osx_image: xcode9 + - osx_image: xcode9.3beta env: PLATFORM="macOS" env: global: From 251f50144a4bf66f590a662593d3574362c4790e Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Tue, 13 Mar 2018 22:32:43 -0300 Subject: [PATCH 30/34] Fixed macOS demo info.plist and tv demo device name --- ChartsDemo-macOS/ChartsDemo-macOS.xcodeproj/project.pbxproj | 4 ++-- Rakefile | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ChartsDemo-macOS/ChartsDemo-macOS.xcodeproj/project.pbxproj b/ChartsDemo-macOS/ChartsDemo-macOS.xcodeproj/project.pbxproj index 2d45668a87..922588b12b 100644 --- a/ChartsDemo-macOS/ChartsDemo-macOS.xcodeproj/project.pbxproj +++ b/ChartsDemo-macOS/ChartsDemo-macOS.xcodeproj/project.pbxproj @@ -371,7 +371,7 @@ CODE_SIGN_STYLE = Manual; COMBINE_HIDPI_IMAGES = YES; DEVELOPMENT_TEAM = ""; - INFOPLIST_FILE = "ChartsDemo-OSX/Info.plist"; + INFOPLIST_FILE = "$(SRCROOT)/ChartsDemo-macOS/Info.plist"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.dcg.ChartsDemo-OSX"; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -389,7 +389,7 @@ CODE_SIGN_STYLE = Manual; COMBINE_HIDPI_IMAGES = YES; DEVELOPMENT_TEAM = ""; - INFOPLIST_FILE = "ChartsDemo-OSX/Info.plist"; + INFOPLIST_FILE = "$(SRCROOT)/ChartsDemo-macOS/Info.plist"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.dcg.ChartsDemo-OSX"; PRODUCT_NAME = "$(TARGET_NAME)"; diff --git a/Rakefile b/Rakefile index 3a8868f3c5..ba4f9ab777 100644 --- a/Rakefile +++ b/Rakefile @@ -66,8 +66,8 @@ def devices }, tvOS: { sdk: 'appletvsimulator', - device: "name='Apple TV 1080p'", - name: 'Apple TV 1080p' + device: "name='Apple TV'", + name: 'Apple TV' } } end From 8031da728292ce2cd56fce3b56c85518350e1625 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Tue, 27 Mar 2018 18:49:21 -0300 Subject: [PATCH 31/34] PR Fixes --- .../Objective-C/Demos/ColoredLineChartViewController.m | 2 +- .../Objective-C/Demos/CombinedChartViewController.m | 2 +- ChartsDemo-iOS/Objective-C/Demos/LineChart2ViewController.m | 2 +- Source/Charts/Charts/ChartViewBase.swift | 2 +- Source/Charts/Data/Implementations/Standard/ChartData.swift | 6 +++--- .../Charts/Data/Implementations/Standard/PieChartData.swift | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ChartsDemo-iOS/Objective-C/Demos/ColoredLineChartViewController.m b/ChartsDemo-iOS/Objective-C/Demos/ColoredLineChartViewController.m index 98bad8cbc5..2c5f882d99 100644 --- a/ChartsDemo-iOS/Objective-C/Demos/ColoredLineChartViewController.m +++ b/ChartsDemo-iOS/Objective-C/Demos/ColoredLineChartViewController.m @@ -44,7 +44,7 @@ - (void)viewDidLoad - (void)setupChart:(LineChartView *)chart data:(LineChartData *)data color:(UIColor *)color { - [(LineChartDataSet *)[data dataSetForIndex:0] setCircleHoleColor:color]; + [(LineChartDataSet *)[data dataSetAt:0] setCircleHoleColor:color]; chart.delegate = self; chart.backgroundColor = color; diff --git a/ChartsDemo-iOS/Objective-C/Demos/CombinedChartViewController.m b/ChartsDemo-iOS/Objective-C/Demos/CombinedChartViewController.m index 27f13e51a4..dfadfaccf3 100644 --- a/ChartsDemo-iOS/Objective-C/Demos/CombinedChartViewController.m +++ b/ChartsDemo-iOS/Objective-C/Demos/CombinedChartViewController.m @@ -151,7 +151,7 @@ - (void)optionTapped:(NSString *)key if ([key isEqualToString:@"removeDataSet"]) { int rnd = (int)arc4random_uniform((float)_chartView.data.dataSetCount); - [_chartView.data removeDataSet:[_chartView.data dataSetForIndex:rnd]]; + [_chartView.data removeDataSet:[_chartView.data dataSetAt:rnd]]; [_chartView.data notifyDataChanged]; [_chartView notifyDataSetChanged]; } diff --git a/ChartsDemo-iOS/Objective-C/Demos/LineChart2ViewController.m b/ChartsDemo-iOS/Objective-C/Demos/LineChart2ViewController.m index f7007e43eb..cf47e86efc 100644 --- a/ChartsDemo-iOS/Objective-C/Demos/LineChart2ViewController.m +++ b/ChartsDemo-iOS/Objective-C/Demos/LineChart2ViewController.m @@ -282,7 +282,7 @@ - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartData { NSLog(@"chartValueSelected"); - [_chartView centerViewToAnimatedWithXValue:entry.x yValue:entry.y axis:[_chartView.data dataSetForIndex:highlight.dataSetIndex].axisDependency duration:1.0]; + [_chartView centerViewToAnimatedWithXValue:entry.x yValue:entry.y axis:[_chartView.data dataSetAt:highlight.dataSetIndex].axisDependency duration:1.0]; //[_chartView moveViewToAnimatedWithXValue:entry.x yValue:entry.y axis:[_chartView.data getDataSetByIndex:dataSetIndex].axisDependency duration:1.0]; //[_chartView zoomAndCenterViewAnimatedWithScaleX:1.8 scaleY:1.8 xValue:entry.x yValue:entry.y axis:[_chartView.data getDataSetByIndex:dataSetIndex].axisDependency duration:1.0]; diff --git a/Source/Charts/Charts/ChartViewBase.swift b/Source/Charts/Charts/ChartViewBase.swift index 67ef299bf4..0f21234ad1 100644 --- a/Source/Charts/Charts/ChartViewBase.swift +++ b/Source/Charts/Charts/ChartViewBase.swift @@ -414,7 +414,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate if data.indices.contains(dataSetIndex) { - highlightValue(Highlight(x: x, y: y, dataSetIndex: dataSetIndex), callDelegate: callDelegate) + highlightValue(Highlight(x: x, y: y, dataSetIndex: dataSetIndex, dataIndex: dataIndex), callDelegate: callDelegate) } else { diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index da144d73fc..dfa1d122f0 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -210,7 +210,7 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral return self[index] } - @objc open func dataSet(forIndex index: Index) -> Element? + @objc open func dataSet(at index: Index) -> Element? { guard dataSets.indices.contains(index) else { return nil } return self[index] @@ -510,13 +510,13 @@ extension ChartData : index { $0.label == label } } - public subscript(label: String, ignoreCase: Bool) -> Element? + public subscript(label label: String, ignoreCase ignoreCase: Bool) -> Element? { guard let index = index(forLabel: label, ignoreCase: ignoreCase) else { return nil } return self[index] } - public subscript(entry: ChartDataEntry) -> Element? + public subscript(entry entry: ChartDataEntry) -> Element? { guard !(self is CombinedChartData) else { diff --git a/Source/Charts/Data/Implementations/Standard/PieChartData.swift b/Source/Charts/Data/Implementations/Standard/PieChartData.swift index a141a7b56b..53c1764eed 100644 --- a/Source/Charts/Data/Implementations/Standard/PieChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/PieChartData.swift @@ -47,7 +47,7 @@ open class PieChartData: ChartData } } - open override func dataSet(forIndex index: ChartData.Index) -> ChartData.Element? + open override func dataSet(at index: ChartData.Index) -> ChartData.Element? { guard index == 0 else { return nil } return self[index] From d371fa8b6c44a60fd9c2f7a54a737738bf9508f5 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Tue, 27 Mar 2018 18:51:24 -0300 Subject: [PATCH 32/34] Fixed objective-c naming --- .../Objective-C/Demos/ColoredLineChartViewController.m | 2 +- ChartsDemo-iOS/Objective-C/Demos/CombinedChartViewController.m | 2 +- ChartsDemo-iOS/Objective-C/Demos/LineChart2ViewController.m | 2 +- Source/Charts/Data/Implementations/Standard/ChartData.swift | 3 ++- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/ChartsDemo-iOS/Objective-C/Demos/ColoredLineChartViewController.m b/ChartsDemo-iOS/Objective-C/Demos/ColoredLineChartViewController.m index 2c5f882d99..867b8627dc 100644 --- a/ChartsDemo-iOS/Objective-C/Demos/ColoredLineChartViewController.m +++ b/ChartsDemo-iOS/Objective-C/Demos/ColoredLineChartViewController.m @@ -44,7 +44,7 @@ - (void)viewDidLoad - (void)setupChart:(LineChartView *)chart data:(LineChartData *)data color:(UIColor *)color { - [(LineChartDataSet *)[data dataSetAt:0] setCircleHoleColor:color]; + [(LineChartDataSet *)[data dataSetAtIndex:0] setCircleHoleColor:color]; chart.delegate = self; chart.backgroundColor = color; diff --git a/ChartsDemo-iOS/Objective-C/Demos/CombinedChartViewController.m b/ChartsDemo-iOS/Objective-C/Demos/CombinedChartViewController.m index dfadfaccf3..68b6abd969 100644 --- a/ChartsDemo-iOS/Objective-C/Demos/CombinedChartViewController.m +++ b/ChartsDemo-iOS/Objective-C/Demos/CombinedChartViewController.m @@ -151,7 +151,7 @@ - (void)optionTapped:(NSString *)key if ([key isEqualToString:@"removeDataSet"]) { int rnd = (int)arc4random_uniform((float)_chartView.data.dataSetCount); - [_chartView.data removeDataSet:[_chartView.data dataSetAt:rnd]]; + [_chartView.data removeDataSet:[_chartView.data dataSetAtIndex:rnd]]; [_chartView.data notifyDataChanged]; [_chartView notifyDataSetChanged]; } diff --git a/ChartsDemo-iOS/Objective-C/Demos/LineChart2ViewController.m b/ChartsDemo-iOS/Objective-C/Demos/LineChart2ViewController.m index cf47e86efc..c192f4a9b3 100644 --- a/ChartsDemo-iOS/Objective-C/Demos/LineChart2ViewController.m +++ b/ChartsDemo-iOS/Objective-C/Demos/LineChart2ViewController.m @@ -282,7 +282,7 @@ - (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartData { NSLog(@"chartValueSelected"); - [_chartView centerViewToAnimatedWithXValue:entry.x yValue:entry.y axis:[_chartView.data dataSetAt:highlight.dataSetIndex].axisDependency duration:1.0]; + [_chartView centerViewToAnimatedWithXValue:entry.x yValue:entry.y axis:[_chartView.data dataSetAtIndex:highlight.dataSetIndex].axisDependency duration:1.0]; //[_chartView moveViewToAnimatedWithXValue:entry.x yValue:entry.y axis:[_chartView.data getDataSetByIndex:dataSetIndex].axisDependency duration:1.0]; //[_chartView zoomAndCenterViewAnimatedWithScaleX:1.8 scaleY:1.8 xValue:entry.x yValue:entry.y axis:[_chartView.data getDataSetByIndex:dataSetIndex].axisDependency duration:1.0]; diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index dfa1d122f0..b65c3e82c2 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -210,7 +210,8 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral return self[index] } - @objc open func dataSet(at index: Index) -> Element? + @objc(dataSetAtIndex:) + open func dataSet(at index: Index) -> Element? { guard dataSets.indices.contains(index) else { return nil } return self[index] From 7d642737668748f130d9cc96a7cb0c6f390bf328 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Thu, 29 Mar 2018 07:16:33 -0400 Subject: [PATCH 33/34] PR Fixes --- Source/Charts/Data/Implementations/Standard/ChartData.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index b65c3e82c2..69568fc725 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -69,6 +69,8 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral leftAxisMin = .greatestFiniteMagnitude rightAxisMax = -.greatestFiniteMagnitude rightAxisMin = .greatestFiniteMagnitude + yMax = -.greatestFiniteMagnitude + yMin = .greatestFiniteMagnitude xMax = -.greatestFiniteMagnitude xMin = .greatestFiniteMagnitude @@ -294,9 +296,9 @@ 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 flatMap { $0.colors.map { $0 } } + return reduce(into: []) { $0 += $1.colors } } /// Sets a custom ValueFormatter for all DataSets this data object contains. From 8f74fe02462f729f7104fc94009d39ab0e027cb6 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sun, 29 Apr 2018 22:40:42 -0300 Subject: [PATCH 34/34] PR Fixes --- .../Data/Implementations/Standard/BarChartDataSet.swift | 7 ++----- .../Data/Implementations/Standard/ChartDataSet.swift | 4 ++-- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift index 03b3785cc7..3ff93b004b 100644 --- a/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift @@ -56,12 +56,9 @@ open class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, BarChartData /// calculates the maximum stacksize that occurs in the Entries array of this DataSet private func calcStackSize(entries: [BarChartDataEntry]) { - for e in entries + for e in entries where (e.yValues?.count ?? 0) > _stackSize { - guard let vals = e.yValues, vals.count > _stackSize else { - continue - } - _stackSize = vals.count + _stackSize = e.yValues!.count } } diff --git a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift index 5c3d2a16b9..8abbf3fe71 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift @@ -421,7 +421,7 @@ open class ChartDataSet: ChartBaseDataSet /// Removes the first Entry (at index 0) of this DataSet from the entries array. /// - /// - returns: `true` if successful, `false` ifnot. + /// - returns: `true` if successful, `false` if not. // TODO: This should return the removed entry to follow Swift convention. open override func removeFirst() -> Bool { @@ -431,7 +431,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` ifnot. + /// - returns: `true` if successful, `false` if not. // TODO: This should return the removed entry to follow Swift convention. open override func removeLast() -> Bool {