问题
First off I am using this library in my app http://www.jjoe64.com/p/graphview-library.html to create the graphs. I then use this code (which I found in the comments) to get the x location of the touch event.
//intercepts touch events on the graphview
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
//when you touch the screen
case MotionEvent.ACTION_DOWN:
// gets the location of the touch on the graphview
float x = event.getX(event.getActionIndex());
// gets the boundries of what you are viewing from the function you just added
// vp is set to vp[0] = 0 & vp[1] = numDaysOil, numDaysOil could be anywhere from 2 to 30
double[] vp = graphOilView.getViewPort();
// gets the width of the graphview
int width = graphOilView.getWidth();
//gets the x-Value of the graph where you touched
@SuppressWarnings("unused")
double xValue = vp[0] + (x / width) * vp[1];
// trying a different ways to get different x vaules
double xVal = (x / width) * numDaysOil;
//add a method to lookup a value and do anything you want based on xValue.
break;
} // end switch
return super.dispatchTouchEvent(event);
} // end dispatchTouch Event
depending on where you touch (or click on the emulator) I will get the right x value but sometimes its to small or to large of an x value.
anyone have any insight into this?
EDIT: sometimes when I click on a location in the graph xValue will return the right x value and sometimes it will return a xValue that is lower than or greater than the x value that corresponds to y value. example x value used to create point on graph is 12 and y value is 30, but xValue calculated with the above code could return 9. so if i then try to lookup x = 9 I will get the wrong y value. data being graphed: x: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 y: 5, 16, 8, 11, 1, 30, 20, 11, 18, 29, 27, 30, 8, 10, 19
回答1:
I solved it like this:
graphView = new LineGraphView(context,"Title");
graphView = initGraphView(graphView);
graphView.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
int size = series1.size();
float screenX = event.getX();
float screenY = event.getY();
float width_x = v.getWidth();
float viewX = screenX - v.getLeft();
float viewY = screenY - v.getTop();
float percent_x = (viewX/width_x);
int pos = (int) (size*percent_x);
System.out.println("X: " + viewX + " Y: " + viewY +" Percent = " +percent_x);
System.out.println("YVal = " +series1.getY(pos));
tvNum.setText(series1.getY(pos)+"");
return true;
}
return false;
}
});
Which looks about the same as you are but remember to setgraphView.setScrollable(false)
and graphView.setScalable(false)
whenever you want to get the numbers from the view. I think this is some weird quirk of the package. I ended up having to stop data collection and 'freeze' the graph in order to get the x and y values from the series.
Does anyone know of a better way?
来源:https://stackoverflow.com/questions/14452498/android-graphview-getting-touch-location