Skip to content Skip to sidebar Skip to footer

Achartengine Get Position Of Touched Point

I use achartengine to draw a line chart. I use this code to get current point sellected `view.setOnClickListener(new View.OnClickListener() { public void onClick(View v

Solution 1:

Have you tried this?

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

or much better look at this example rows: 167-172

EDIT

Ok, try this, for each point in your dataset:

  1. Transform the real point to screen point
  2. Compute the distance from the touch event
  3. If the distance is less then a specified amount (2*pointSize in this example) then grab the value and show your popup

What I don't like here is that, in the worst case, you've to iterate all the points...but i hope that this will give you some hints.

finalXYChartchart=newLineChart(mDataset, mRenderer);
mChartView = newGraphicalView(this, chart);
mChartView.setOnTouchListener(newView.OnTouchListener() {
  @OverridepublicbooleanonTouch(View v, MotionEvent event) {
     XYSeriesseries= mDataset.getSeriesAt(0);
     for(inti=0; i < series.getItemCount(); i++) {
       double[] xy = chart.toScreenPoint(newdouble[] { series.getX(i), series.getY(i) }, 0);
       
       doubledx= (xy[0] - event.getX());
       doubledy= (xy[1] - event.getY());
       doubledistance= Math.sqrt(dx*dx + dy*dy);
       if (distance <= 2*pointSize) {  //.pointSize that you've specified in your rendererSeriesSelectionsel= 
            chart.getSeriesAndPointForScreenCoordinate(newPoint((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);
     }
        
     returntrue;
  }
});

Solution 2:

Thank Lus, i also solved my problem:

finalLineChartchart=newLineChart(buildDataset(mTitles, data), mRenderer);
finalGraphicalViewview=newGraphicalView(mContext, chart);
view.setOnClickListener(newView.OnClickListener() {
  publicvoidonClick(View v) {
     double[] xy = chart.toScreenPoint(view.toRealPoint(0));
     int[] location = newint[] {(int) xy[0], (int) xy[1]};
     SeriesSelectionseriesSelection= view.getCurrentSeriesAndPoint();
     if (seriesSelection != null) {
         finalDatad= mModel.getDiaryAt(seriesSelection.getSeriesIndex(), 
         seriesSelection.getPointIndex());
         //show popup at xy[0] xy[1]
     }
  }

});

Solution 3:

Here's my code to get the position of the plot that is clicked on the line chart. It's working for me. I am displaying text on the location where the point is clicked.

final XYChart chart = newLineChart(mDataset, mRenderer);
mChartView = newGraphicalView(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 axisdouble[] xy = chart.toScreenPoint(newdouble[] {x, y });
// so now xy[0] is yout x location on screen and xy[1] is y location on screen.

Post a Comment for "Achartengine Get Position Of Touched Point"