1

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) { // handle the click event on the chart

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

Now i want to get position of this point or position of touch to display a bubble to show detail point, any idea for help for examp

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

Anh Vu
  • 219
  • 3
  • 10

3 Answers3

3

Have you tried this?

SeriesSelection seriesSelection = 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.

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;
  }
});
Community
  • 1
  • 1
Luca Sepe
  • 2,435
  • 1
  • 20
  • 26
  • i just reference your link handle onClick. i also try view.toRealPoint(0) but seem it return wrong value because it return different value each touch with same point. – Anh Vu Mar 14 '14 at 01:54
  • @AnhVu You are welcome! if this resolve your problem, please, would you be so kind to accept the answer (also for the others that will search for the same feature) thanks! :-) – Luca Sepe Mar 14 '14 at 14:25
  • Glad to hear that! :-).. yes I would check on more series too. – Luca Sepe Mar 14 '14 at 14:56
  • I found this as I've got similar problems obtaining the graphical view's getCurrentSeriesAndPoint, within an onTouch listener. I was joyful when I discovered your solution, but it's throwing npe on the call to chart.toScreenPoint, even though both my chart and series are valid. Any ideas? I'm getting desperate ;-) – wkhatch May 16 '14 at 17:48
  • Hi @wkhatch, take a look here ( http://stackoverflow.com/questions/17721044/achartengine-toscreenpointdouble-always-returns-nullpointerexception ) – Luca Sepe May 16 '14 at 18:12
  • Thanks, LuS, but my problem is that the call crashes with an NPE, so it never returns anything. Also, I updated to nightly build 1.2.0, and, my chart is definitely on screen, or I wouldn't be able to get the click at all; unless I'm not understanding something here. Thanks for the reply – wkhatch May 16 '14 at 18:30
  • I'm sorry didn't help. Try to debug step by step Achartengine code (for example XYChart if you are using it), to find out the bug...eventually, create a question, post your code. Good luck! – Luca Sepe May 16 '14 at 19:01
  • I figured out my issue, LuS; and now it makes sense. I needed to construct my graphical view using the LineChart instance, such that it was rendered and displayed so it had valid screen point data when calling toScreenPoint on it. Duh... ;-) Thanks for the help, and the great answer here. – wkhatch May 19 '14 at 18:43
  • Hi @wkhatch happy to read that. And yes make sense! thanks you and have a great day. – Luca Sepe May 19 '14 at 19:14
1

Thank Lus, i also solved my problem:

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

});

Luca Sepe
  • 2,435
  • 1
  • 20
  • 26
Anh Vu
  • 219
  • 3
  • 10
-1

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 = 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.
Pang
  • 9,564
  • 146
  • 81
  • 122
Jaykishan Sewak
  • 822
  • 6
  • 13