How to get the selected x-axis name (rows added to x-axis) using MP Chart?

I know this is a duplicate link question

I followed the same, but I get the following error:

 @Override
    public void onValueSelected(Entry e, Highlight h) {

    String xAxisValue = mChart.getData().getXVals().get(e.getXIndex());

}

ERROR: cannot resolve getXVals () and cannot resolve getXIndex ()

Please help: I ​​have the following x-axis values: India, Australia, USA, China

When clicking a panel in barchart I need to get one of the lines above

+1
source share
2 answers

In MPAndroidChart 3.0.0+, this should work:

chart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
    @Override
    public void onValueSelected(Entry e, Highlight h) {
        chart.getXAxis().getValueFormatter().getFormattedValue(e.getX(), chart.getXAxis());
    }

    @Override
    public void onNothingSelected() {

    }
});
+2
source

, , , Y x, .

OnChartValueSelectedListener. onValueSelected()

@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {


    String xAxisValue = chart.getData().getXVals().get(e.getXIndex());
    //yourList (model class or  pojo class type), first get the full list
    for (int i=0; i<yourList.size(); i++){
        YourModelClass cym = yourList.get(i);
        String year =cym.getYear();
        // compare the xAxisValue with one of your Model class attribute. which is written on x-axis of the graph
        if (year.equals(xAxisValue)){
            // now you got the index number of your list which is matched with xAxis Bar value. 
            //so do your stuff here. Something like setting text or showing toast etc etc
           // if done everything then break the loop
            break;
        }

    }

}

@Override
public void onNothingSelected() {
}
0

Source: https://habr.com/ru/post/1624723/


All Articles