Skip to content

Commit 019d979

Browse files
authored
Merge pull request #5200 from tring-bhagya/release/5.0.0_updated
Added Safe index subscript logic for ChartData
2 parents 2bfd37f + a930d59 commit 019d979

File tree

9 files changed

+12
-14
lines changed

9 files changed

+12
-14
lines changed

Source/Charts/Data/Implementations/Standard/ChartData.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,10 @@ extension ChartData: MutableCollection
452452
get { return dataSets[position] }
453453
set { self._dataSets[position] = newValue }
454454
}
455+
456+
public subscript(safe index: Index) -> Element? {
457+
return indices.contains(index) ? self[index] : nil
458+
}
455459
}
456460

457461
// MARK: RandomAccessCollection

Source/Charts/Data/Implementations/Standard/RadarChartData.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ open class RadarChartData: ChartData
4646

4747
@objc open override func entry(for highlight: Highlight) -> ChartDataEntry?
4848
{
49-
return self[highlight.dataSetIndex].entryForIndex(Int(highlight.x))
49+
return self[safe: highlight.dataSetIndex]?.entryForIndex(Int(highlight.x))
5050
}
5151
}

Source/Charts/Renderers/BarChartRenderer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
701701
for high in indices
702702
{
703703
guard
704-
let set = barData[high.dataSetIndex] as? BarChartDataSetProtocol,
704+
let set = barData[safe: high.dataSetIndex] as? BarChartDataSetProtocol,
705705
set.isHighlightEnabled
706706
else { continue }
707707

Source/Charts/Renderers/BubbleChartRenderer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer
252252
for high in indices
253253
{
254254
guard
255-
let dataSet = bubbleData[high.dataSetIndex] as? BubbleChartDataSetProtocol,
255+
let dataSet = bubbleData[safe: high.dataSetIndex] as? BubbleChartDataSetProtocol,
256256
dataSet.isHighlightEnabled,
257257
let entry = dataSet.entryForXValue(high.x, closestToY: high.y) as? BubbleChartDataEntry,
258258
isInBoundsX(entry: entry, dataSet: dataSet)

Source/Charts/Renderers/CandleStickChartRenderer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ open class CandleStickChartRenderer: LineScatterCandleRadarRenderer
362362
for high in indices
363363
{
364364
guard
365-
let set = candleData[high.dataSetIndex] as? CandleChartDataSetProtocol,
365+
let set = candleData[safe: high.dataSetIndex] as? CandleChartDataSetProtocol,
366366
set.isHighlightEnabled
367367
else { continue }
368368

Source/Charts/Renderers/LineChartRenderer.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -760,13 +760,7 @@ open class LineChartRenderer: LineRadarRenderer
760760

761761
for high in indices
762762
{
763-
// When updating the data set for the chart and then animating it, sometimes highlights are generated for the
764-
// removed sets. This guard prevents OOB in the case where the new set has less data than the old set
765-
if high.dataSetIndex >= lineData._dataSets.count - 1 {
766-
continue
767-
}
768-
769-
guard let set = lineData[high.dataSetIndex] as? LineChartDataSetProtocol,
763+
guard let set = lineData[safe: high.dataSetIndex] as? LineChartDataSetProtocol,
770764
set.isHighlightEnabled
771765
else { continue }
772766

Source/Charts/Renderers/PieChartRenderer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ open class PieChartRenderer: NSObject, DataRenderer
714714
// get the index to highlight
715715
let index = Int(hightlight.x)
716716
guard index < drawAngles.count,
717-
let set = data[hightlight.dataSetIndex] as? PieChartDataSetProtocol,
717+
let set = data[safe: hightlight.dataSetIndex] as? PieChartDataSetProtocol,
718718
set.isHighlightEnabled
719719
else
720720
{

Source/Charts/Renderers/RadarChartRenderer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ open class RadarChartRenderer: LineRadarRenderer
354354
for high in indices
355355
{
356356
guard
357-
let set = chart.data?[high.dataSetIndex] as? RadarChartDataSetProtocol,
357+
let set = chart.data?[safe: high.dataSetIndex] as? RadarChartDataSetProtocol,
358358
set.isHighlightEnabled
359359
else { continue }
360360

Source/Charts/Renderers/ScatterChartRenderer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ open class ScatterChartRenderer: LineScatterCandleRadarRenderer
203203
for high in indices
204204
{
205205
guard
206-
let set = scatterData[high.dataSetIndex] as? ScatterChartDataSetProtocol,
206+
let set = scatterData[safe: high.dataSetIndex] as? ScatterChartDataSetProtocol,
207207
set.isHighlightEnabled
208208
else { continue }
209209

0 commit comments

Comments
 (0)