From 6e80eb0f2ef58a2d6b28123d6a42ddf2ea0024ad Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sat, 18 Nov 2017 14:25:17 -0800 Subject: [PATCH 1/9] 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 36ca566d70732772bf4225222b28575710a3a78e Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sun, 19 Nov 2017 21:10:47 -0500 Subject: [PATCH 2/9] 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 3/9] 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 ca5afad0bb15bdfb57ff0ea8ea0fbe0bc64ab9c6 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Wed, 10 Jan 2018 21:31:57 -0400 Subject: [PATCH 4/9] 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 8976b95e80cd7678a596fcd5140c13bdb8e66c1e Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Fri, 12 Jan 2018 07:29:56 -0400 Subject: [PATCH 5/9] 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 6/9] 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 7/9] 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 c0b7d65e21a20489848b6f770c0a9437a0fe46aa Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sun, 14 Jan 2018 23:01:26 -0400 Subject: [PATCH 8/9] 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 a71f87cfb39c22bb32636ac61c71a89867589af2 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sun, 14 Jan 2018 23:38:55 -0400 Subject: [PATCH 9/9] 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