Pregunta

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

¿Fue útil?

Solución 2

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

});

Otros consejos

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

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.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top