Skip to content

Commit

Permalink
Remove internal use of ChartData.dataSets
Browse files Browse the repository at this point in the history
  • Loading branch information
jjatie committed Aug 28, 2020
1 parent 1c3891e commit 3801e5a
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 87 deletions.
2 changes: 1 addition & 1 deletion Source/Charts/Charts/ChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Charts/PieChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,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
{
calcMinMax(entry: e)
Expand Down
25 changes: 12 additions & 13 deletions Source/Charts/Data/Implementations/Standard/CombinedChartData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
}
}
}
Expand Down Expand Up @@ -289,7 +288,7 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData
return nil
}

return data.dataSets[highlight.dataSetIndex]
return data[highlight.dataSetIndex]
}

// MARK: Unsupported Collection Methods
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Highlight/PieHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
8 changes: 3 additions & 5 deletions Source/Charts/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }

Expand Down
14 changes: 9 additions & 5 deletions Source/Charts/Renderers/BubbleChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -153,18 +153,22 @@ 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))
let phaseY = animator.phaseY

var pt = CGPoint()

for i in 0..<dataSets.count
for i in bubbleData.indices
{
let dataSet = dataSets[i]
guard let dataSet = bubbleData[i] as? BubbleChartDataSetProtocol,
shouldDrawValues(forDataSet: dataSet)
else
{
continue
}

guard shouldDrawValues(forDataSet: dataSet) else { continue }

Expand Down
10 changes: 4 additions & 6 deletions Source/Charts/Renderers/CandleStickChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ open class CandleStickChartRenderer: LineScatterCandleRadarRenderer
accessibleChartElements.append(element)
}

for set in candleData.dataSets as! [CandleChartDataSetProtocol] where set.isVisible
for case let set as CandleChartDataSetProtocol in candleData where set.isVisible
{
drawDataSet(context: context, dataSet: set)
}
Expand Down Expand Up @@ -81,7 +81,7 @@ open class CandleStickChartRenderer: LineScatterCandleRadarRenderer
let high = e.high
let low = e.low

let doesContainMultipleDataSets = (dataProvider.candleData?.dataSets.count ?? 1) > 1
let doesContainMultipleDataSets = (dataProvider.candleData?.count ?? 1) > 1
var accessibilityMovementDescription = "neutral"
var accessibilityRect = CGRect(x: CGFloat(xPos) + 0.5 - barSpace,
y: CGFloat(low * phaseY),
Expand Down Expand Up @@ -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 }

Expand Down
4 changes: 2 additions & 2 deletions Source/Charts/Renderers/DataRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions Source/Charts/Renderers/HorizontalBarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand All @@ -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 }

Expand Down
10 changes: 3 additions & 7 deletions Source/Charts/Renderers/LineChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -621,8 +619,6 @@ open class LineChartRenderer: LineRadarRenderer
else { return }

let phaseY = animator.phaseY

let dataSets = lineData.dataSets

var pt = CGPoint()
var rect = CGRect()
Expand All @@ -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 }

Expand Down
23 changes: 8 additions & 15 deletions Source/Charts/Renderers/PieChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
35 changes: 16 additions & 19 deletions Source/Charts/Renderers/RadarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
9 changes: 3 additions & 6 deletions Source/Charts/Renderers/ScatterChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3801e5a

Please sign in to comment.