Mohammad ARAFA
Mohammad ARAFA

Reputation: 31

How to onClickListen MP Android line Chart Circles?

I am trying to listen the click on the circles at the Line Chart .. but I could not. So please help me.

chart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
    @Override
    public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {

    }

    @Override
    public void onNothingSelected() {

    }
});

I tried this but it did not work!.

Upvotes: 3

Views: 3170

Answers (2)

dmkrush
dmkrush

Reputation: 31

Implement OnChartValueSelectedListener this interface and override these two methods as below

 @Override
    public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
        Toast.makeText(getContext(),entries.indexOf(e)+"",Toast.LENGTH_LONG).show();
    }

    @Override
    public void onNothingSelected() {
    }

and add listener on lineChart lineChart.setOnChartValueSelectedListener(this); Note: Entries is list of Entry objects. Toast will show clicked circle index value.

Upvotes: 1

Cory Charlton
Cory Charlton

Reputation: 8938

It's hard to tell from the code you provided but I ran into this issue when I called chart.setHighlightPerTapEnabled(false)

From the names "highlight" versus "selected" if wasn't clear at first sight that the setHighlightPerTapEnabled method also disables the OnChartValueSelectedListener

My solution was to replace the OnChartValueSelectedListener with a OnChartGestureListener. Here's a snippet of what I implemented:

private class BarChartGestureListener implements OnChartGestureListener {
    private int _lastTappedIndex = -1;

    /* ... */

    @Override
    public void onChartSingleTapped(MotionEvent me) {
        final Entry entry = _barChart.getEntryByTouchPoint(me.getX(), me.getY());
        if (entry != null && _lastTappedIndex != entry.getXIndex()) {
            final Object data = entry.getData();

            // TODO: Insert your magic here...
        }
    }

    /* ... */
}

In the onChartSingleTapped you can get the Entry that was tapped and programmatically highlight it or whatever else you want.

Upvotes: 3

Related Questions