Skip to content

Commit 7ffa9ca

Browse files
committed
Fixed entry searching algorithm to handle sequential same values
1 parent b53c8cd commit 7ffa9ca

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

  • MPChartLib/src/main/java/com/github/mikephil/charting/data

MPChartLib/src/main/java/com/github/mikephil/charting/data/DataSet.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,28 @@ public int getEntryIndex(float xValue, Rounding rounding) {
298298
while (low < high) {
299299
int m = (low + high) / 2;
300300

301-
float d1 = Math.abs(mValues.get(m).getX() - xValue);
302-
float d2 = Math.abs(mValues.get(m + 1).getX() - xValue);
301+
final float d1 = mValues.get(m).getX() - xValue,
302+
d2 = mValues.get(m + 1).getX() - xValue,
303+
ad1 = Math.abs(d1), ad2 = Math.abs(d2);
303304

304-
if (d2 <= d1) {
305+
if (ad2 < ad1) {
306+
// [m + 1] is closer to xValue
307+
// Search in an higher place
305308
low = m + 1;
306-
} else {
309+
} else if (ad1 < ad2) {
310+
// [m] is closer to xValue
311+
// Search in a lower place
307312
high = m;
313+
} else {
314+
// We have multiple sequential x-value with same distance
315+
316+
if (d1 >= 0.0) {
317+
// Search in a lower place
318+
high = m;
319+
} else if (d1 < 0.0) {
320+
// Search in an higher place
321+
low = m + 1;
322+
}
308323
}
309324
}
310325

0 commit comments

Comments
 (0)