So far I can get the Y value of a touch on the graph like so.
mSpeedPlot.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
PointF click = new PointF(event.getX(),event.getY());
if ( mSpeedPlot.getGraphWidget().containsPoint( click )) {
Log.d("HOF","Plot X: "+DistanceValue.fromMetres(mSpeedPlot.getXVal(click).doubleValue()).km()+"km "+" Y: "+mSpeedPlot.getYVal(click)+"km/h");
}
return false;
}
});
What I want to do is instead of just return the y value of where the touch was, I want the Y value of the series that is plotted on the graph that corresponds to the x value.
Any way to do this?
Ok solved it.
I use the original
Number[]
that I used to populate mySimpleXySeries
. This has X and Y values interleaved so I'm going to need someway of finding the index of the X value in thisNumber[]
I have retrieved using the code in my question.So I process my data before I add it to this Number[] by splitting the data into segments of distance mainly due to smoothing and aesthetic reasons. So to find the index of the value returned by the touch I do the following.
I divide the xValue by distanceSegments because that essentially gives how many distance segments were used to get to this x value. I then times it by 2 because in my
Number[]
the x y values are interleaved so multiplying by 2 avoids the y values.Now I have the index of the x value I can easily find the y value by adding 1 to the index because the values are interleaved so I know the value after x is the corresponding y.