diff --git a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift index 04b418bf38..344f0f658e 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift @@ -217,7 +217,7 @@ open class ChartDataSet: ChartBaseDataSet rounding: ChartDataSetRounding) -> Int { var closest = partitioningIndex { $0.x >= xValue } - guard closest < endIndex else { return rounding == .closest ? (endIndex-1) : -1 } + guard closest < endIndex else { return index(before: endIndex) } var closestXValue = self[closest].x @@ -240,10 +240,13 @@ open class ChartDataSet: ChartBaseDataSet // The closest value in the beginning of this function // `var closest = partitioningIndex { $0.x >= xValue }` // doesn't guarantee closest rounding method - if closest > 0 { + if closest > startIndex { let distanceAfter = abs(self[closest].x - xValue) - let distanceBefore = abs(self[closest-1].x - xValue) - distanceBefore < distanceAfter ? closest -= 1 : () + let distanceBefore = abs(self[index(before: closest)].x - xValue) + if distanceBefore < distanceAfter + { + closest = index(before: closest) + } closestXValue = self[closest].x } }