AchartEngine获得触摸点的位置

时间:2014-03-13 17:10:46

标签: java android achartengine

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

            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 个答案:

答案 0 :(得分:2)

你试过这个吗?

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

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

修改

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

  1. 将真实点转换为屏幕点
  2. 计算与触摸事件的距离
  3. 如果距离小于指定的数量 (在此示例中为2 * pointSize)然后获取值并显示弹出窗口
  4. 我不喜欢的是,在最坏的情况下,你要迭代所有的点...但我希望这会给你一些提示。

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

答案 1 :(得分:1)

感谢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]
     }
  }

});

答案 2 :(得分:-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.