androidplot Get Y Value in Series From Touch

194 views Asked by At

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?

1

There are 1 answers

0
StuStirling On BEST ANSWER

Ok solved it.

I use the original Number[] that I used to populate my SimpleXySeries. This has X and Y values interleaved so I'm going to need someway of finding the index of the X value in this Number[] 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.

double xValue = mSpeedPlot.getXVal(click).doubleValue(); // gives me the value of x
int index = Double.valueOf(Math.floor(xValue / distanceSegments)).intValue()*2;
Log.d("HOF","Speed Index: "+index+" Y Value: "+speedValues[index+1]);

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.