Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

android - Get the correct position of a point in AndroidPlot

I am trying to get the pixel position of a values in my AndroidPlot, but I can't get it to work. The idea is to place the cursor at the exact point showed on the plot, at specific terms. Did any of you encounter/solve a similar problem?

Regards Jesper

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't know if there's a better built-in solution, but here's a manual approach.
The following code places the cursor at the X coordinate where the user touched the screen and the corresponding Y coordinate of the first data series of the plot.

//NOTE about XYPlotZoomPan: when an OnTouchListener is set, zooming is disabled. Subclass to avoid it.
mPlot.setOnTouchListener(new View.OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent me) {
        float touchX = me.getX();
        float touchY = me.getY();
        XYGraphWidget widget = mPlot.getGraphWidget();
        RectF gridRect = widget.getGridRect();
        if(gridRect.contains(touchX, touchY)){ //Check the touch event is in the grid
            XYSeries xyData = mPlot.getSeriesSet().iterator().next();
            long targetValX = Math.round(widget.getXVal(touchX));
            Log.d(TAG, "Touched at " + touchX + ", " + touchY + ". Target val X: " + targetValX);
            Long targetValY = null; 
            Long prevValX = null;
            if(mPlot.getSeriesSet().size() > 1){
                Log.w(TAG, "More than one series in plot. Using only the first one");
            }
            for(int i = 0; i < xyData.size(); ++i){
                long currValX = xyData.getX(i).longValue();
                long currValY = xyData.getY(i).longValue();
                //Calculate the range value of the closest domain value (assumes xyData is sorted in ascending X order)
                if(currValX >= targetValX){
                    long currDiff = currValX - targetValX; 
                    if(prevValX != null && (targetValX - prevValX) < currDiff){
                        targetValY = xyData.getY(i-1).longValue();
                    }else{
                        targetValY = currValY;
                    }
                    break;
                }
                prevValX = currValX;
            }
            if(targetValY != null){
                long maxValY = mPlot.getCalculatedMaxY().longValue();
                long minValY = mPlot.getCalculatedMinY().longValue();
                float pixelPosY = gridRect.top + ValPixConverter.valToPix(
                        (double)targetValY, (double)minValY, (double)maxValY, (float)gridRect.height(), true);
                widget.setRangeCursorPosition(pixelPosY);
                widget.setDomainCursorPosition(touchX);
                Log.d(TAG, String.format("Domain cursor set at Y %.2f, val %.2f = %d, min-maxValY (%d, %d)",
                        pixelPosY,
                        widget.getRangeCursorVal(), targetValY,
                        minValY, maxValY));
            }else{
                Log.w(TAG, "Couldn't find the closest range to the selected domain coordinate");
            }
            mPlot.invalidate();
        }else{
            Log.d(TAG, "Touched outside the plot grid");
        }

        return false;
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...