问题
How to get label for corresponding bar when I swipe the barchart? I have another textview outside of barchart. When I swipe mpandroidchart graph, I need to get x axis label of corresponding position and display in textview. How to do that?
I tried with this.
barChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
Log.i("Entry", String.valueOf(e));
}
@Override
public void onNothingSelected() {
}
});
or OnChartGestureListener too, I tried but I don't know how to get the label of the corresponding bar.
回答1:
In case of OnChartValueSelectedListener
, you can do
barChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
String value = barChart.getBarData().getXVals().get(e.getXIndex());
}
@Override
public void onNothingSelected() {
}
});
In the similar way you can achieve this for other listeners too.
回答2:
Implement mChart.setOnChartGestureListener
method, and you will see onChartTranslate
Override
method.
mChart.getXValue(mChart.getHighestVisibleXIndex());
This will return you the value of the highest visible index after swipe. Hope this answer will help you to solve your problem.
来源:https://stackoverflow.com/questions/37321426/how-to-get-label-for-corresponding-swipe-in-barchart-of-mpandroidchart