android graphview getting touch location

吃可爱长大的小学妹 提交于 2019-12-04 21:37:57

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!