AchartEngine得到触摸点的位置(AchartEngine get position of

2019-10-19 13:48发布

我用achartengine绘制折线图。 我用这个代码来获得sellected`view.setOnClickListener当前点(新View.OnClickListener(){公共无效的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());
            }
        }
    });`

现在,我想这点或触摸的位置的位置显示一个气泡显示细节点,任何想法为exa​​mp帮助

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

Answer 1:

您是否尝试过这个?

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

或者更好的看看这个例子行:167-172

编辑

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

  1. 变换真正点到屏幕点
  2. 计算从触摸事件的距离
  3. 如果距离小于规定量(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;
  }
});


Answer 2:

龚延明,谢谢,我也解决我的问题:

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

});



Answer 3:

这里是我的代码来获取点击线形图上的情节的位置。 它为我工作。 我在哪里点击了点位置显示文本。

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.


文章来源: AchartEngine get position of touched point