首页 文章

AchartEngine获得触摸点的位置

提问于
浏览
1

我用achartengine绘制折线图 . 我使用此代码获取当前点选择`view.setOnClickListener(new View.OnClickListener(){public void onClick(View v){//处理图表上的click事件

quickAction.show(v);
            SeriesSelection seriesSelection = view.getCurrentSeriesAndPoint();
            if (seriesSelection != null) {
                if(mToast == null){
                    Toast.makeText( mContext  , "" , Toast.LENGTH_SHORT );
                }
                mToast.setText( "" + seriesSelection.getValue() + "mg/dL");
                mToast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
                mToast.show();
            } else {
                Log.i(this.toString(), "OnClickListener" + v.getX() + "y:" + v.getY());
            }
        }
    });`

现在我想得到这个点或触摸位置的位置来显示一个泡泡来显示细节点,任何想法帮助的例子

https://dl.dropboxusercontent.com/u/5860710/Untitled.jpg

3 回答

  • -1

    你试过这个吗?

    SeriesSelection seriesSelection = view.getCurrentSeriesAndPoint();
    double[] xy = view.toRealPoint(0); 
    Log.i(this.toString(), "OnClickListener" + xy[0] + "y:" + xy[1]);
    

    或者更好看this示例行:167-172

    编辑

    好的,试试这个,对于数据集中的每个点:

    • 将实际点转换为屏幕点

    • 计算触摸事件的距离

    • 如果距离小于指定数量(在此示例中为2 * pointSize),则获取值并显示弹出窗口

    我在这里不喜欢的是,在最坏的情况下,你要迭代所有的点...但我希望这会给你一些提示 .

    final XYChart chart = new LineChart(mDataset, mRenderer);
    mChartView = new GraphicalView(this, chart);
    mChartView.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
         XYSeries series = mDataset.getSeriesAt(0);
         for(int i = 0; i < series.getItemCount(); i++) {
           double[] xy = chart.toScreenPoint(new double[] { series.getX(i), series.getY(i) }, 0);
    
           double dx = (xy[0] - event.getX());
           double dy = (xy[1] - event.getY());
           double distance = Math.sqrt(dx*dx + dy*dy);
           if (distance <= 2*pointSize) {  //.pointSize that you've specified in your renderer
              SeriesSelection sel = 
                chart.getSeriesAndPointForScreenCoordinate(new Point((float)xy[0], (float)xy[1]));
              if (sel != null) {
                 Toast.makeText(XYChartBuilder.this, "Touched: " + sel.getValue(), Toast.LENGTH_SHORT).show();
              }
              break;
           }
           Log.i("LuS", "dist: " + distance);
         }
    
         return true;
      }
    });
    
  • 2

    感谢Lus,我也解决了我的问题:

    final LineChart chart = new LineChart(buildDataset(mTitles, data), mRenderer);
    final GraphicalView view = new GraphicalView(mContext, chart);
    view.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
         double[] xy = chart.toScreenPoint(view.toRealPoint(0));
         int[] location = new int[] {(int) xy[0], (int) xy[1]};
         SeriesSelection seriesSelection = view.getCurrentSeriesAndPoint();
         if (seriesSelection != null) {
             final Data d = mModel.getDiaryAt(seriesSelection.getSeriesIndex(), 
             seriesSelection.getPointIndex());
             //show popup at xy[0] xy[1]
         }
      }
    

    });

  • 1

    这是我的代码,用于获取在折线图上单击的图的位置 . 它对我有用 . 我在点击点的位置显示文本 .

    final XYChart chart = new LineChart(mDataset, mRenderer);
    mChartView = new GraphicalView(this, chart);
    SeriesSelection ss=mChartView.getCurrentSeriesAndPoint();
    double x=ss.getPointIndex()// index of point in chart ex: for first point its 1,for 2nd its 2.
    double y=ss.getValue(); //value of y axis
    double[] xy = chart.toScreenPoint(new double[] {x, y });
    // so now xy[0] is yout x location on screen and xy[1] is y location on screen.
    

相关问题