From 6e80eb0f2ef58a2d6b28123d6a42ddf2ea0024ad Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sat, 18 Nov 2017 14:25:17 -0800 Subject: [PATCH 01/16] 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 02/16] [#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 03/16] 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 04/16] 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 05/16] 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 ca5afad0bb15bdfb57ff0ea8ea0fbe0bc64ab9c6 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Wed, 10 Jan 2018 21:31:57 -0400 Subject: [PATCH 06/16] 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 07/16] 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 08/16] 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 09/16] 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 10/16] 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 11/16] 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 12/16] 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 329e00c351115961a2cb4deebac90aa84c311c2a Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sun, 21 Jan 2018 17:56:51 -0400 Subject: [PATCH 13/16] 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 14/16] 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 15/16] 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 eb8e031b86fc3555a84ce920affb0b4cadd15838 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Fri, 26 Jan 2018 08:11:08 -0400 Subject: [PATCH 16/16] Updated macOS demos --- .../ChartsDemo-OSX/Demos/BarDemoViewController.swift | 4 ++-- .../ChartsDemo-OSX/Demos/LineDemoViewController.swift | 4 ++-- .../ChartsDemo-OSX/Demos/PieDemoViewController.swift | 2 +- .../ChartsDemo-OSX/Demos/RadarDemoViewController.swift | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ChartsDemo-OSX/ChartsDemo-OSX/Demos/BarDemoViewController.swift b/ChartsDemo-OSX/ChartsDemo-OSX/Demos/BarDemoViewController.swift index 88d3b91087..10e9101426 100644 --- a/ChartsDemo-OSX/ChartsDemo-OSX/Demos/BarDemoViewController.swift +++ b/ChartsDemo-OSX/ChartsDemo-OSX/Demos/BarDemoViewController.swift @@ -31,11 +31,11 @@ open class BarDemoViewController: NSViewController let data = BarChartData() let ds1 = BarChartDataSet(values: yse1, label: "Hello") ds1.colors = [NSUIColor.red] - data.addDataSet(ds1) + data.append(ds1) let ds2 = BarChartDataSet(values: yse2, label: "World") ds2.colors = [NSUIColor.blue] - data.addDataSet(ds2) + data.append(ds2) let barWidth = 0.4 let barSpace = 0.05 diff --git a/ChartsDemo-OSX/ChartsDemo-OSX/Demos/LineDemoViewController.swift b/ChartsDemo-OSX/ChartsDemo-OSX/Demos/LineDemoViewController.swift index ea78112cc6..91384c9a19 100644 --- a/ChartsDemo-OSX/ChartsDemo-OSX/Demos/LineDemoViewController.swift +++ b/ChartsDemo-OSX/ChartsDemo-OSX/Demos/LineDemoViewController.swift @@ -30,11 +30,11 @@ open class LineDemoViewController: NSViewController let data = LineChartData() let ds1 = LineChartDataSet(values: yse1, label: "Hello") ds1.colors = [NSUIColor.red] - data.addDataSet(ds1) + data.append(ds1) let ds2 = LineChartDataSet(values: yse2, label: "World") ds2.colors = [NSUIColor.blue] - data.addDataSet(ds2) + data.append(ds2) self.lineChartView.data = data self.lineChartView.gridBackgroundColor = NSUIColor.white diff --git a/ChartsDemo-OSX/ChartsDemo-OSX/Demos/PieDemoViewController.swift b/ChartsDemo-OSX/ChartsDemo-OSX/Demos/PieDemoViewController.swift index 5ace6f32da..de35306ea0 100644 --- a/ChartsDemo-OSX/ChartsDemo-OSX/Demos/PieDemoViewController.swift +++ b/ChartsDemo-OSX/ChartsDemo-OSX/Demos/PieDemoViewController.swift @@ -30,7 +30,7 @@ open class PieDemoViewController: NSViewController ds1.colors = ChartColorTemplates.vordiplom() - data.addDataSet(ds1) + data.append(ds1) let paragraphStyle: NSMutableParagraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle paragraphStyle.lineBreakMode = .byTruncatingTail diff --git a/ChartsDemo-OSX/ChartsDemo-OSX/Demos/RadarDemoViewController.swift b/ChartsDemo-OSX/ChartsDemo-OSX/Demos/RadarDemoViewController.swift index 8f60f97629..e4a83c8fb9 100644 --- a/ChartsDemo-OSX/ChartsDemo-OSX/Demos/RadarDemoViewController.swift +++ b/ChartsDemo-OSX/ChartsDemo-OSX/Demos/RadarDemoViewController.swift @@ -30,11 +30,11 @@ open class RadarDemoViewController: NSViewController let data = RadarChartData() let ds1 = RadarChartDataSet(values: yse1, label: "Hello") ds1.colors = [NSUIColor.red] - data.addDataSet(ds1) + data.append(ds1) let ds2 = RadarChartDataSet(values: yse2, label: "World") ds2.colors = [NSUIColor.blue] - data.addDataSet(ds2) + data.append(ds2) self.radarChartView.data = data self.radarChartView.chartDescription.text = "Radarchart Demo"