From 846b1ef387272d200f4aadabdca4e440f88a2421 Mon Sep 17 00:00:00 2001 From: Georg Hieronimus Date: Fri, 31 Mar 2017 16:48:37 +0200 Subject: [PATCH 1/2] calling only the nearest point on tapping, even if more then one series have points near to it --- .../java/com/jjoe64/graphview/GraphView.java | 17 +++++++++++++++-- .../com/jjoe64/graphview/series/BaseSeries.java | 15 +++++++++++++++ .../com/jjoe64/graphview/series/Series.java | 8 ++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/jjoe64/graphview/GraphView.java b/src/main/java/com/jjoe64/graphview/GraphView.java index 5283b761b..37b71b2c6 100644 --- a/src/main/java/com/jjoe64/graphview/GraphView.java +++ b/src/main/java/com/jjoe64/graphview/GraphView.java @@ -450,14 +450,27 @@ public boolean onTouchEvent(MotionEvent event) { // is it a click? if (mTapDetector.onTouchEvent(event)) { + Series nearestSeries = null; + float minDistance = Float.MAX_VALUE; for (Series s : mSeries) { - s.onTap(event.getX(), event.getY()); + float distance = s.distanceToNearestPoint(event.getX(), event.getY()); + if (distance < minDistance) { + nearestSeries = s; + minDistance = distance; + } } if (mSecondScale != null) { for (Series s : mSecondScale.getSeries()) { - s.onTap(event.getX(), event.getY()); + float distance = s.distanceToNearestPoint(event.getX(), event.getY()); + if (distance < minDistance) { + nearestSeries = s; + minDistance = distance; + } } } + if (minDistance != Float.MAX_VALUE) { + nearestSeries.onTap(event.getX(), event.getY()); + } } return b || a; diff --git a/src/main/java/com/jjoe64/graphview/series/BaseSeries.java b/src/main/java/com/jjoe64/graphview/series/BaseSeries.java index 141b7497d..a44441e44 100644 --- a/src/main/java/com/jjoe64/graphview/series/BaseSeries.java +++ b/src/main/java/com/jjoe64/graphview/series/BaseSeries.java @@ -294,6 +294,21 @@ public void setOnDataPointTapListener(OnDataPointTapListener l) { this.mOnDataPointTapListener = l; } + /** + * @param x + * @param y + * @return the distance between the given x,y and the nearest point of the series + */ + public float distanceToNearestPoint(float x, float y) { + E p = findDataPoint(x, y); + if (p == null) return Float.MAX_VALUE; + else { + double x2 = p.getX(); + double y2 = p.getY(); + return (float) Math.sqrt((x-x2)*(x-x2) + (y-y2)*(y-y2)); + } + } + /** * called by the tap detector in order to trigger * the on tap on datapoint event. diff --git a/src/main/java/com/jjoe64/graphview/series/Series.java b/src/main/java/com/jjoe64/graphview/series/Series.java index 95e82a7e1..b0533e0df 100644 --- a/src/main/java/com/jjoe64/graphview/series/Series.java +++ b/src/main/java/com/jjoe64/graphview/series/Series.java @@ -99,6 +99,14 @@ public interface Series { */ public void setOnDataPointTapListener(OnDataPointTapListener l); + + /** + * @param x + * @param y + * @return the distance between the given x,y and the nearest point of the series + */ + float distanceToNearestPoint(float x, float y); + /** * called by the tap detector in order to trigger * the on tap on datapoint event. From 01f89647f08efb846cfbad5bd5b0909077ccc4ec Mon Sep 17 00:00:00 2001 From: Georg Hieronimus Date: Fri, 31 Mar 2017 17:15:30 +0200 Subject: [PATCH 2/2] now working --- .../jjoe64/graphview/series/BaseSeries.java | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/jjoe64/graphview/series/BaseSeries.java b/src/main/java/com/jjoe64/graphview/series/BaseSeries.java index a44441e44..93e88b602 100644 --- a/src/main/java/com/jjoe64/graphview/series/BaseSeries.java +++ b/src/main/java/com/jjoe64/graphview/series/BaseSeries.java @@ -300,13 +300,26 @@ public void setOnDataPointTapListener(OnDataPointTapListener l) { * @return the distance between the given x,y and the nearest point of the series */ public float distanceToNearestPoint(float x, float y) { - E p = findDataPoint(x, y); - if (p == null) return Float.MAX_VALUE; - else { - double x2 = p.getX(); - double y2 = p.getY(); - return (float) Math.sqrt((x-x2)*(x-x2) + (y-y2)*(y-y2)); + float shortestDistance = Float.NaN; + E shortest = null; + for (Map.Entry entry : mDataPoints.entrySet()) { + float x1 = entry.getKey().x; + float y1 = entry.getKey().y; + float x2 = x; + float y2 = y; + + float distance = (float) Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)); + if (shortest == null || distance < shortestDistance) { + shortestDistance = distance; + shortest = entry.getValue(); + } + } + if (shortest != null) { + if (shortestDistance < 120) { + return shortestDistance; + } } + return Float.MAX_VALUE; } /**