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

Fix highlight on chart values #3

Merged
1 commit merged into from
Mar 8, 2022
Merged
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
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