Skip to content

Commit

Permalink
Merge pull request #4721 from kcome/4719-highlighter-fix
Browse files Browse the repository at this point in the history
Highlighter fix
  • Loading branch information
pmairoldi authored May 25, 2022
2 parents 9f334a4 + 30538b3 commit eab0e40
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions Source/Charts/Charts/ChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
{
delegate?.chartValueNothingSelected?(self)
}
setNeedsDisplay()
return
}

Expand Down
25 changes: 18 additions & 7 deletions Source/Charts/Data/Implementations/Standard/ChartDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ open class ChartDataSet: ChartBaseDataSet

guard !isEmpty else { return }

let indexFrom = entryIndex(x: fromX, closestToY: .nan, rounding: .down)
let indexTo = entryIndex(x: toX, closestToY: .nan, rounding: .up)
let indexFrom = entryIndex(x: fromX, closestToY: .nan, rounding: .closest)
var indexTo = entryIndex(x: toX, closestToY: .nan, rounding: .up)
if indexTo == -1 { indexTo = entryIndex(x: toX, closestToY: .nan, rounding: .closest) }

guard indexTo >= indexFrom else { return }
// only recalculate y
Expand Down Expand Up @@ -197,9 +198,11 @@ open class ChartDataSet: ChartBaseDataSet
open override func entriesForXValue(_ xValue: Double) -> [ChartDataEntry]
{
let match: (ChartDataEntry) -> Bool = { $0.x == xValue }
let i = partitioningIndex(where: match)
var partitioned = self.entries
_ = partitioned.partition(by: match)
let i = partitioned.partitioningIndex(where: match)
guard i < endIndex else { return [] }
return self[i...].prefix(while: match)
return partitioned[i...].prefix(while: match)
}

/// - Parameters:
Expand All @@ -214,9 +217,9 @@ open class ChartDataSet: ChartBaseDataSet
rounding: ChartDataSetRounding) -> Int
{
var closest = partitioningIndex { $0.x >= xValue }
guard closest < endIndex else { return -1 }
guard closest < endIndex else { return rounding == .closest ? (endIndex-1) : -1 }

let closestXValue = self[closest].x
var closestXValue = self[closest].x

switch rounding {
case .up:
Expand All @@ -234,7 +237,15 @@ open class ChartDataSet: ChartBaseDataSet
}

case .closest:
break
// The closest value in the beginning of this function
// `var closest = partitioningIndex { $0.x >= xValue }`
// doesn't guarantee closest rounding method
if closest > 0 {
let distanceAfter = abs(self[closest].x - xValue)
let distanceBefore = abs(self[closest-1].x - xValue)
distanceBefore < distanceAfter ? closest -= 1 : ()
closestXValue = self[closest].x
}
}

// Search by closest to y-value
Expand Down

0 comments on commit eab0e40

Please sign in to comment.