diff --git a/Source/Charts/Charts/ChartViewBase.swift b/Source/Charts/Charts/ChartViewBase.swift index ad145f180b..dd77407fbf 100644 --- a/Source/Charts/Charts/ChartViewBase.swift +++ b/Source/Charts/Charts/ChartViewBase.swift @@ -62,7 +62,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate // calculate how many digits are needed setupDefaultFormatter(min: data.yMin, max: data.yMax) - for set in data.dataSets + for set in data { if set.valueFormatter is DefaultValueFormatter { diff --git a/Source/Charts/Charts/PieChartView.swift b/Source/Charts/Charts/PieChartView.swift index ed2b898687..edf00bb5a8 100644 --- a/Source/Charts/Charts/PieChartView.swift +++ b/Source/Charts/Charts/PieChartView.swift @@ -320,7 +320,7 @@ open class PieChartView: PieRadarChartViewBase @objc open func dataSetIndexForIndex(_ xValue: Double) -> Int { // TODO: Return nil instead of -1 - return data?.dataSets.firstIndex { + return data?.firstIndex { $0.entryForXValue(xValue, closestToY: .nan) != nil } ?? -1 } diff --git a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift index 7d4ab9f709..1d80da1632 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift @@ -375,7 +375,7 @@ open class ChartDataSet: ChartBaseDataSet /// - e: the entry to add /// - Returns: True // TODO: This should return `Void` to follow Swift convention - @available(*, deprecated, message: "Use `append(_:)` instead") + @available(*, deprecated, message: "Use `append(_:)` instead", renamed: "append(_:)") open override func addEntry(_ e: ChartDataEntry) -> Bool { append(e) diff --git a/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift b/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift index 7ffcb13d3e..f5fd4ef2a0 100644 --- a/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/CombinedChartData.swift @@ -119,8 +119,7 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData { data.calcMinMax() - let sets = data.dataSets - _dataSets.append(contentsOf: sets) + _dataSets.append(contentsOf: data) if data.yMax > yMax { @@ -142,28 +141,28 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData xMin = data.xMin } - for dataset in sets + for set in data { - if dataset.axisDependency == .left + if set.axisDependency == .left { - if dataset.yMax > leftAxisMax + if set.yMax > leftAxisMax { - leftAxisMax = dataset.yMax + leftAxisMax = set.yMax } - if dataset.yMin < leftAxisMin + if set.yMin < leftAxisMin { - leftAxisMin = dataset.yMin + leftAxisMin = set.yMin } } else { - if dataset.yMax > rightAxisMax + if set.yMax > rightAxisMax { - rightAxisMax = dataset.yMax + rightAxisMax = set.yMax } - if dataset.yMin < rightAxisMin + if set.yMin < rightAxisMin { - rightAxisMin = dataset.yMin + rightAxisMin = set.yMin } } } @@ -289,7 +288,7 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData return nil } - return data.dataSets[highlight.dataSetIndex] + return data[highlight.dataSetIndex] } // MARK: Unsupported Collection Methods diff --git a/Source/Charts/Highlight/PieHighlighter.swift b/Source/Charts/Highlight/PieHighlighter.swift index 54bb3d7b83..42b30a2b83 100644 --- a/Source/Charts/Highlight/PieHighlighter.swift +++ b/Source/Charts/Highlight/PieHighlighter.swift @@ -18,7 +18,7 @@ open class PieHighlighter: PieRadarHighlighter open override func closestHighlight(index: Int, x: CGFloat, y: CGFloat) -> Highlight? { guard - let set = chart?.data?.dataSets[0], + let set = chart?.data?[0], let entry = set.entryForIndex(index) else { return nil } diff --git a/Source/Charts/Renderers/BarChartRenderer.swift b/Source/Charts/Renderers/BarChartRenderer.swift index 72f1120860..6ea6f2432d 100644 --- a/Source/Charts/Renderers/BarChartRenderer.swift +++ b/Source/Charts/Renderers/BarChartRenderer.swift @@ -71,7 +71,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer } } - _buffers = zip(_buffers, barData.dataSets).map { buffer, set -> Buffer in + _buffers = zip(_buffers, barData).map { buffer, set -> Buffer in let set = set as! BarChartDataSetProtocol let size = set.entryCount * (set.isStacked ? set.stackSize : 1) return buffer.count == size @@ -436,17 +436,15 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer let barData = dataProvider.barData else { return } - let dataSets = barData.dataSets - let valueOffsetPlus: CGFloat = 4.5 var posOffset: CGFloat var negOffset: CGFloat let drawValueAboveBar = dataProvider.isDrawValueAboveBarEnabled - for dataSetIndex in barData.dataSets.indices + for dataSetIndex in barData.indices { guard - let dataSet = dataSets[dataSetIndex] as? BarChartDataSetProtocol, + let dataSet = barData[dataSetIndex] as? BarChartDataSetProtocol, shouldDrawValues(forDataSet: dataSet) else { continue } diff --git a/Source/Charts/Renderers/BubbleChartRenderer.swift b/Source/Charts/Renderers/BubbleChartRenderer.swift index 1c06d93e7a..386ac46695 100644 --- a/Source/Charts/Renderers/BubbleChartRenderer.swift +++ b/Source/Charts/Renderers/BubbleChartRenderer.swift @@ -45,7 +45,7 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer accessibleChartElements.append(element) } - for (i, set) in (bubbleData.dataSets as! [BubbleChartDataSetProtocol]).enumerated() where set.isVisible + for case let (i, set) as (Int, BubbleChartDataSetProtocol) in bubbleData.enumerated() where set.isVisible { drawDataSet(context: context, dataSet: set, dataSetIndex: i) } @@ -153,8 +153,7 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer guard let dataProvider = dataProvider, let bubbleData = dataProvider.bubbleData, - isDrawingValuesAllowed(dataProvider: dataProvider), - let dataSets = bubbleData.dataSets as? [BubbleChartDataSetProtocol] + isDrawingValuesAllowed(dataProvider: dataProvider) else { return } let phaseX = max(0.0, min(1.0, animator.phaseX)) @@ -162,9 +161,14 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer var pt = CGPoint() - for i in 0.. 1 + let doesContainMultipleDataSets = (dataProvider.candleData?.count ?? 1) > 1 var accessibilityMovementDescription = "neutral" var accessibilityRect = CGRect(x: CGFloat(xPos) + 0.5 - barSpace, y: CGFloat(low * phaseY), @@ -275,16 +275,14 @@ open class CandleStickChartRenderer: LineScatterCandleRadarRenderer // if values are drawn if isDrawingValuesAllowed(dataProvider: dataProvider) { - let dataSets = candleData.dataSets - let phaseY = animator.phaseY var pt = CGPoint() - for i in 0 ..< dataSets.count + for i in candleData.indices { guard let - dataSet = dataSets[i] as? BarLineScatterCandleBubbleChartDataSetProtocol, + dataSet = candleData[i] as? BarLineScatterCandleBubbleChartDataSetProtocol, shouldDrawValues(forDataSet: dataSet) else { continue } diff --git a/Source/Charts/Renderers/DataRenderer.swift b/Source/Charts/Renderers/DataRenderer.swift index 9ed27b5b01..8340de3e8d 100644 --- a/Source/Charts/Renderers/DataRenderer.swift +++ b/Source/Charts/Renderers/DataRenderer.swift @@ -62,9 +62,9 @@ internal struct AccessibleHeader { withDefaultDescription defaultDescription: String = "Chart") -> NSUIAccessibilityElement { let chartDescriptionText = chart.chartDescription.text ?? defaultDescription - let dataSetDescriptions = data.dataSets.map { $0.label ?? "" } + let dataSetDescriptions = data.map { $0.label ?? "" } let dataSetDescriptionText = dataSetDescriptions.joined(separator: ", ") - let dataSetCount = data.dataSets.count + let dataSetCount = data.count let element = NSUIAccessibilityElement(accessibilityContainer: chart) diff --git a/Source/Charts/Renderers/HorizontalBarChartRenderer.swift b/Source/Charts/Renderers/HorizontalBarChartRenderer.swift index bef0b60cf5..edf070f8b9 100644 --- a/Source/Charts/Renderers/HorizontalBarChartRenderer.swift +++ b/Source/Charts/Renderers/HorizontalBarChartRenderer.swift @@ -51,7 +51,7 @@ open class HorizontalBarChartRenderer: BarChartRenderer for i in barData.indices { - let set = barData.dataSets[i] as! BarChartDataSetProtocol + let set = barData[i] as! BarChartDataSetProtocol let size = set.entryCount * (set.isStacked ? set.stackSize : 1) if _buffers[i].rects.count != size { @@ -323,9 +323,7 @@ open class HorizontalBarChartRenderer: BarChartRenderer let dataProvider = dataProvider, let barData = dataProvider.barData else { return } - - let dataSets = barData.dataSets - + let textAlign = NSTextAlignment.left let valueOffsetPlus: CGFloat = 5.0 @@ -336,7 +334,7 @@ open class HorizontalBarChartRenderer: BarChartRenderer for dataSetIndex in barData.indices { guard let - dataSet = dataSets[dataSetIndex] as? BarChartDataSetProtocol, + dataSet = barData[dataSetIndex] as? BarChartDataSetProtocol, shouldDrawValues(forDataSet: dataSet) else { continue } diff --git a/Source/Charts/Renderers/LineChartRenderer.swift b/Source/Charts/Renderers/LineChartRenderer.swift index 1b7f134168..8c986dcbe7 100644 --- a/Source/Charts/Renderers/LineChartRenderer.swift +++ b/Source/Charts/Renderers/LineChartRenderer.swift @@ -530,16 +530,14 @@ open class LineChartRenderer: LineRadarRenderer if isDrawingValuesAllowed(dataProvider: dataProvider) { - let dataSets = lineData.dataSets - let phaseY = animator.phaseY var pt = CGPoint() - for i in 0 ..< dataSets.count + for i in lineData.indices { guard let - dataSet = dataSets[i] as? LineChartDataSetProtocol, + dataSet = lineData[i] as? LineChartDataSetProtocol, shouldDrawValues(forDataSet: dataSet) else { continue } @@ -621,8 +619,6 @@ open class LineChartRenderer: LineRadarRenderer else { return } let phaseY = animator.phaseY - - let dataSets = lineData.dataSets var pt = CGPoint() var rect = CGRect() @@ -641,7 +637,7 @@ open class LineChartRenderer: LineRadarRenderer context.saveGState() - for i in 0 ..< dataSets.count + for i in lineData.indices { guard let dataSet = lineData[i] as? LineChartDataSetProtocol else { continue } diff --git a/Source/Charts/Renderers/PieChartRenderer.swift b/Source/Charts/Renderers/PieChartRenderer.swift index c6a93f1c8e..51d568e7b3 100644 --- a/Source/Charts/Renderers/PieChartRenderer.swift +++ b/Source/Charts/Renderers/PieChartRenderer.swift @@ -38,20 +38,15 @@ open class PieChartRenderer: NSObject, DataRenderer open func drawData(context: CGContext) { - guard let chart = chart else { return } + guard let chart = chart, let pieData = chart.data else { return } - let pieData = chart.data + // If we redraw the data, remove and repopulate accessible elements to update label values and frames + accessibleChartElements.removeAll() - if pieData != nil + for case let set as PieChartDataSetProtocol in pieData where + set.isVisible && set.entryCount > 0 { - // If we redraw the data, remove and repopulate accessible elements to update label values and frames - accessibleChartElements.removeAll() - - for set in pieData!.dataSets as! [PieChartDataSetProtocol] - where set.isVisible && set.entryCount > 0 - { - drawDataSet(context: context, dataSet: set) - } + drawDataSet(context: context, dataSet: set) } } @@ -326,8 +321,6 @@ open class PieChartRenderer: NSObject, DataRenderer let labelRadius = radius - labelRadiusOffset - let dataSets = data.dataSets - let yValueSum = (data as! PieChartData).yValueSum let drawEntryLabels = chart.isDrawEntryLabelsEnabled @@ -340,9 +333,9 @@ open class PieChartRenderer: NSObject, DataRenderer context.saveGState() defer { context.restoreGState() } - for i in 0 ..< dataSets.count + for i in data.indices { - guard let dataSet = dataSets[i] as? PieChartDataSetProtocol else { continue } + guard let dataSet = data[i] as? PieChartDataSetProtocol else { continue } let drawValues = dataSet.isDrawValuesEnabled diff --git a/Source/Charts/Renderers/RadarChartRenderer.swift b/Source/Charts/Renderers/RadarChartRenderer.swift index c3d778a6bc..26da1e8560 100644 --- a/Source/Charts/Renderers/RadarChartRenderer.swift +++ b/Source/Charts/Renderers/RadarChartRenderer.swift @@ -35,29 +35,26 @@ open class RadarChartRenderer: LineRadarRenderer open override func drawData(context: CGContext) { - guard let chart = chart else { return } - - let radarData = chart.data - - if radarData != nil + guard let chart = chart, + let radarData = chart.data as? RadarChartData else { - let mostEntries = radarData?.maxEntryCountSet?.entryCount ?? 0 + return + } + + let mostEntries = radarData.maxEntryCountSet?.entryCount ?? 0 - // If we redraw the data, remove and repopulate accessible elements to update label values and frames - self.accessibleChartElements.removeAll() + // If we redraw the data, remove and repopulate accessible elements to update label values and frames + self.accessibleChartElements.removeAll() - // Make the chart header the first element in the accessible elements array - if let accessibilityHeaderData = radarData as? RadarChartData { - let element = createAccessibleHeader(usingChart: chart, - andData: accessibilityHeaderData, - withDefaultDescription: "Radar Chart") - self.accessibleChartElements.append(element) - } + // Make the chart header the first element in the accessible elements array + let element = createAccessibleHeader(usingChart: chart, + andData: radarData, + withDefaultDescription: "Radar Chart") + self.accessibleChartElements.append(element) - for set in radarData!.dataSets as! [RadarChartDataSetProtocol] where set.isVisible - { - drawDataSet(context: context, dataSet: set, mostEntries: mostEntries) - } + for case let set as RadarChartDataSetProtocol in radarData where set.isVisible + { + drawDataSet(context: context, dataSet: set, mostEntries: mostEntries) } } diff --git a/Source/Charts/Renderers/ScatterChartRenderer.swift b/Source/Charts/Renderers/ScatterChartRenderer.swift index 0bf420b26a..d80e572964 100644 --- a/Source/Charts/Renderers/ScatterChartRenderer.swift +++ b/Source/Charts/Renderers/ScatterChartRenderer.swift @@ -113,18 +113,15 @@ open class ScatterChartRenderer: LineScatterCandleRadarRenderer // if values are drawn if isDrawingValuesAllowed(dataProvider: dataProvider) - { - guard let dataSets = scatterData.dataSets as? [ScatterChartDataSetProtocol] else { return } - + { let phaseY = animator.phaseY var pt = CGPoint() for i in scatterData.indices { - let dataSet = dataSets[i] - - guard shouldDrawValues(forDataSet: dataSet) + guard let dataSet = scatterData[i] as? ScatterChartDataSetProtocol, + shouldDrawValues(forDataSet: dataSet) else { continue } let valueFont = dataSet.valueFont