How to get the selected x axis value using MPAndroidChart?

I use the MPAndroidChart library in my Android graphics application, and I need to display a dialog with a heading containing the selected value bar along the x axis.

I referenced this wiki entry for click events in bars on a bar chart. But now I need to get the selected x-axis value as a header. Can someone tell me how to achieve it?

+4
source share
3 answers

Use OnChartValueSelectedListener:

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

   final String x = chart.getXAxis().getValueFormatter().getFormattedValue(e.getX(), chart.getXAxis());
}

Highlight , dataSetIndex, x- y- , ( ),...

.

+8

: MPAndroidChart 3.x.x :

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

    @Override
    public void onNothingSelected() {

    }
});
+1

onValueSelected:

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

   int position =  e.getXIndex();
   Log.d("positin", position );

   //XValue
   final String selectedValue=barchart.getXAxis().getValues().get(position);
   Log.d("selctdX", selectedValue);

   //YValue
   final String selectedYValue = String.valueOf(e.getVal());
   Log.d("selctdY", selectedValue);

}

, , .

0

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


All Articles