Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Safe index subscript logic for ChartData #5200

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Source/Charts/Data/Implementations/Standard/ChartData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,10 @@ extension ChartData: MutableCollection
get { return dataSets[position] }
set { self._dataSets[position] = newValue }
}

public subscript(safe index: Index) -> Element? {
return indices.contains(index) ? self[index] : nil
}
}

// MARK: RandomAccessCollection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ open class RadarChartData: ChartData

@objc open override func entry(for highlight: Highlight) -> ChartDataEntry?
{
return self[highlight.dataSetIndex].entryForIndex(Int(highlight.x))
return self[safe: highlight.dataSetIndex]?.entryForIndex(Int(highlight.x))
}
}
2 changes: 1 addition & 1 deletion Source/Charts/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
for high in indices
{
guard
let set = barData[high.dataSetIndex] as? BarChartDataSetProtocol,
let set = barData[safe: high.dataSetIndex] as? BarChartDataSetProtocol,
set.isHighlightEnabled
else { continue }

Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Renderers/BubbleChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer
for high in indices
{
guard
let dataSet = bubbleData[high.dataSetIndex] as? BubbleChartDataSetProtocol,
let dataSet = bubbleData[safe: high.dataSetIndex] as? BubbleChartDataSetProtocol,
dataSet.isHighlightEnabled,
let entry = dataSet.entryForXValue(high.x, closestToY: high.y) as? BubbleChartDataEntry,
isInBoundsX(entry: entry, dataSet: dataSet)
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Renderers/CandleStickChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ open class CandleStickChartRenderer: LineScatterCandleRadarRenderer
for high in indices
{
guard
let set = candleData[high.dataSetIndex] as? CandleChartDataSetProtocol,
let set = candleData[safe: high.dataSetIndex] as? CandleChartDataSetProtocol,
set.isHighlightEnabled
else { continue }

Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Renderers/LineChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ open class LineChartRenderer: LineRadarRenderer

for high in indices
{
guard let set = lineData[high.dataSetIndex] as? LineChartDataSetProtocol,
guard let set = lineData[safe: high.dataSetIndex] as? LineChartDataSetProtocol,
set.isHighlightEnabled
else { continue }

Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Renderers/PieChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ open class PieChartRenderer: NSObject, DataRenderer
// get the index to highlight
let index = Int(hightlight.x)
guard index < drawAngles.count,
let set = data[hightlight.dataSetIndex] as? PieChartDataSetProtocol,
let set = data[safe: hightlight.dataSetIndex] as? PieChartDataSetProtocol,
set.isHighlightEnabled
else
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Renderers/RadarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ open class RadarChartRenderer: LineRadarRenderer
for high in indices
{
guard
let set = chart.data?[high.dataSetIndex] as? RadarChartDataSetProtocol,
let set = chart.data?[safe: high.dataSetIndex] as? RadarChartDataSetProtocol,
set.isHighlightEnabled
else { continue }

Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Renderers/ScatterChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ open class ScatterChartRenderer: LineScatterCandleRadarRenderer
for high in indices
{
guard
let set = scatterData[high.dataSetIndex] as? ScatterChartDataSetProtocol,
let set = scatterData[safe: high.dataSetIndex] as? ScatterChartDataSetProtocol,
set.isHighlightEnabled
else { continue }

Expand Down