Skip to content
Open
Show file tree
Hide file tree
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: 15 additions & 2 deletions src/main/java/com/jjoe64/graphview/GraphView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/jjoe64/graphview/series/BaseSeries.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,34 @@ 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) {
float shortestDistance = Float.NaN;
E shortest = null;
for (Map.Entry<PointF, E> 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;
}

/**
* called by the tap detector in order to trigger
* the on tap on datapoint event.
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/jjoe64/graphview/series/Series.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ public interface Series<E extends DataPointInterface> {
*/
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.
Expand Down