Brian Iglesias
Brian Iglesias

Reputation: 13

AndroidPlot: Detecting touch event on point

I'm writing an application using AndroidPlot in which the user needs to be able to touch a point on a scatter plot and bring up information about that specific point. In other words, the application needs to identify the closest point to the location touched or otherwise recognize that a point has been touched, and be able to return the specific identity of the point. All of the points in this scatter plot will always be of one series, so identifying between series isn't an issue, but I don't know how to implement finding or identifying the touched point.

I can get as far as:

plot.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        PointF click = new PointF(motionEvent.getX(), motionEvent.getY());
        if(plot.getGraphWidget().containsPoint(click)) {
            AlertDialog.Builder builder = new AlertDialog.Builder(GraphView.this);
            builder.setTitle("Point: ");
            builder.setMessage("Description: ");
            AlertDialog dialog = builder.create();
            dialog.show();
        }
        return false;
    }
    });
}

Which creates the AlertDialog whenever the graph is touched.

Upvotes: 0

Views: 847

Answers (1)

Nick
Nick

Reputation: 8317

The DemoApp's BarPlotExampleActivity has this functionality implemented in it's onPlotClicked(...) method. It could certainly be improved but should give you a good starting point.

The basic steps are:

  • Filter for clicks within the plot's graph area. (You've got this piece already)
  • Convert the screen coords to domain/range values using XYPlot.getXVal(screenX) & XYPlot.getYVal(screenY).
  • Find the closest point(s) in your model for the above domain/range values.

Upvotes: 2

Related Questions