From b22e54feea6e0661c1eb7fdbf6750d3ac1b435ed Mon Sep 17 00:00:00 2001 From: Andrew Benson Date: Wed, 6 Oct 2021 15:26:12 -0500 Subject: [PATCH] Resolves Issue 4668 "entriesForXValue() not finding point with matching xValue when it should" (https://github.com/danielgindi/Charts/issues/4668). Resolves Issue 4645 "Markers not appearing as expected" (https://github.com/danielgindi/Charts/issues/4645). The entriesForXValue function was incorrectly partiioning on ==, and also sometimes incorrectly returning an array of values that don't match at all. The entryIndex function's switch statement assumes that the closest X value had already been found, which would often not be the case, since we are partitioning on >=. --- .../Implementations/Standard/ChartDataSet.swift | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift index 703c0abe72..7aa236ffec 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift @@ -196,10 +196,9 @@ open class ChartDataSet: ChartBaseDataSet /// An empty array if no Entry object at that index. open override func entriesForXValue(_ xValue: Double) -> [ChartDataEntry] { - let match: (ChartDataEntry) -> Bool = { $0.x == xValue } - let i = partitioningIndex(where: match) - guard i < endIndex else { return [] } - return self[i...].prefix(while: match) + let i = partitioningIndex(where: { $0.x >= xValue }) + guard i < endIndex, self[i].x == xValue else { return [] } + return self[i...].prefix { $0.x == xValue } } /// - Parameters: @@ -216,7 +215,12 @@ open class ChartDataSet: ChartBaseDataSet var closest = partitioningIndex { $0.x >= xValue } guard closest < endIndex else { return -1 } - let closestXValue = self[closest].x + // because our partition is >=, our closest match may be behind us + var closestXValue = self[closest].x + if closest > startIndex && abs(closestXValue - xValue) > abs(xValue - self[index(before: closest)].x) { + formIndex(before: &closest) + closestXValue = self[closest].x + } switch rounding { case .up: