Skip to content

Commit

Permalink
Fix highlight on chart values
Browse files Browse the repository at this point in the history
The fix is provided by the community and applied based on the following pull request:
ChartsOrg#4721
  • Loading branch information
Alexandru Zaharia committed Mar 7, 2022
1 parent 2703afc commit 4e613d4
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions Source/Charts/Data/Implementations/Standard/ChartDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ 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 match: (ChartDataEntry) -> Bool = { $0.x >= xValue }
let i = partitioningIndex(where: match)
guard i < endIndex else { return [] }
return self[i...].prefix(while: match)
guard i < endIndex, self[i].x == xValue else { return [] }
return self[i...].prefix(while: { $0.x == xValue })
}

/// - Parameters:
Expand Down Expand Up @@ -234,7 +234,16 @@ open class ChartDataSet: ChartBaseDataSet
}

case .closest:
break
if closest > startIndex {
let closestXIndex = closest
formIndex(before: &closest)
let value = self[closest]

// If the x value is closer to the original x index revert closest otherwise fall through
if abs(value.x - xValue) > abs(closestXValue - xValue) {
closest = closestXIndex
}
}
}

// Search by closest to y-value
Expand Down

0 comments on commit 4e613d4

Please sign in to comment.