@@ -83,8 +83,8 @@ public void calcMinMaxY(float fromX, float toX) {
8383 mYMax = -Float .MAX_VALUE ;
8484 mYMin = Float .MAX_VALUE ;
8585
86- int indexFrom = getEntryIndex (fromX , Rounding .DOWN );
87- int indexTo = getEntryIndex (toX , Rounding .UP );
86+ int indexFrom = getEntryIndex (fromX , Float . NaN , Rounding .DOWN );
87+ int indexTo = getEntryIndex (toX , Float . NaN , Rounding .UP );
8888
8989 for (int i = indexFrom ; i <= indexTo ; i ++) {
9090
@@ -213,7 +213,7 @@ public void addEntryOrdered(T e) {
213213 calcMinMax (e );
214214
215215 if (mValues .size () > 0 && mValues .get (mValues .size () - 1 ).getX () > e .getX ()) {
216- int closestIndex = getEntryIndex (e .getX (), Rounding .UP );
216+ int closestIndex = getEntryIndex (e .getX (), e . getY (), Rounding .UP );
217217 mValues .add (closestIndex , e );
218218 } else {
219219 mValues .add (e );
@@ -268,17 +268,17 @@ public int getEntryIndex(Entry e) {
268268 }
269269
270270 @ Override
271- public T getEntryForXValue (float xValue , Rounding rounding ) {
271+ public T getEntryForXValue (float xValue , float closestToY , Rounding rounding ) {
272272
273- int index = getEntryIndex (xValue , rounding );
273+ int index = getEntryIndex (xValue , closestToY , rounding );
274274 if (index > -1 )
275275 return mValues .get (index );
276276 return null ;
277277 }
278278
279279 @ Override
280- public T getEntryForXValue (float xValue ) {
281- return getEntryForXValue (xValue , Rounding .CLOSEST );
280+ public T getEntryForXValue (float xValue , float closestToY ) {
281+ return getEntryForXValue (xValue , closestToY , Rounding .CLOSEST );
282282 }
283283
284284 @ Override
@@ -287,13 +287,14 @@ public T getEntryForIndex(int index) {
287287 }
288288
289289 @ Override
290- public int getEntryIndex (float xValue , Rounding rounding ) {
290+ public int getEntryIndex (float xValue , float closestToY , Rounding rounding ) {
291291
292292 if (mValues == null || mValues .isEmpty ())
293293 return -1 ;
294294
295295 int low = 0 ;
296296 int high = mValues .size () - 1 ;
297+ int closest = high ;
297298
298299 while (low < high ) {
299300 int m = (low + high ) / 2 ;
@@ -321,22 +322,53 @@ public int getEntryIndex(float xValue, Rounding rounding) {
321322 low = m + 1 ;
322323 }
323324 }
325+
326+ closest = high ;
324327 }
325328
326- if (high != -1 ) {
327- float closestXValue = mValues .get (high ).getX ();
329+ if (closest != -1 ) {
330+ float closestXValue = mValues .get (closest ).getX ();
328331 if (rounding == Rounding .UP ) {
329- if (closestXValue < xValue && high < mValues .size () - 1 ) {
330- ++high ;
332+ // If rounding up, and found x-value is lower than specified x, and we can go upper...
333+ if (closestXValue < xValue && closest < mValues .size () - 1 ) {
334+ ++closest ;
331335 }
332336 } else if (rounding == Rounding .DOWN ) {
333- if (closestXValue > xValue && high > 0 ) {
334- --high ;
337+ // If rounding down, and found x-value is upper than specified x, and we can go lower...
338+ if (closestXValue > xValue && closest > 0 ) {
339+ --closest ;
335340 }
336341 }
342+
343+ // Search by closest to y-value
344+ if (!Float .isNaN (closestToY )) {
345+ while (closest > 0 && mValues .get (closest - 1 ).getX () == closestXValue )
346+ closest -= 1 ;
347+
348+ float closestYValue = mValues .get (closest ).getY ();
349+ int closestYIndex = closest ;
350+
351+ while (true ) {
352+ closest += 1 ;
353+ if (closest >= mValues .size ())
354+ break ;
355+
356+ final Entry value = mValues .get (closest );
357+
358+ if (value .getX () != closestXValue )
359+ break ;
360+
361+ if (Math .abs (value .getY () - closestToY ) < Math .abs (closestYValue - closestToY )) {
362+ closestYValue = closestToY ;
363+ closestYIndex = closest ;
364+ }
365+ }
366+
367+ closest = closestYIndex ;
368+ }
337369 }
338370
339- return high ;
371+ return closest ;
340372 }
341373
342374 @ Override
@@ -382,7 +414,7 @@ public List<T> getEntriesForXValue(float xValue) {
382414
383415 /**
384416 * Determines how to round DataSet index values for
385- * {@link DataSet#getEntryIndex(float, Rounding)} DataSet.getEntryIndex()}
417+ * {@link DataSet#getEntryIndex(float, float, Rounding)} DataSet.getEntryIndex()}
386418 * when an exact x-index is not found.
387419 */
388420 public enum Rounding {
0 commit comments