获得AndroidPlot点的正确位置 [英] Get the correct position of a point in AndroidPlot

查看:139
本文介绍了获得AndroidPlot点的正确位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的Andr​​oidPlot一个值的像素位置,但我不能得到它的工作。我们的想法是将光标在精确的点上表现出的情节,在特定的条件。难道任你遇到/解决类似的问题?

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?

问候
加斯帕

推荐答案

我不知道是否有一个更好的内置解决方案,但这里有一个手动的方法。结果
下面code放置光标置于X坐标,用户触摸屏幕和第一个数据系列的情节的Y坐标对应的。

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;
    }
});

这篇关于获得AndroidPlot点的正确位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆