How To Get Selected Bar X-axis Name(strings Added To X-axis) Using Mp Chart?
I know this is a duplicate to the linkquestion I have followed the same but I am getting the following error : @Override public void onValueSelected(Entry e, Highlight h) {
Solution 1:
In MPAndroidChart 3.0.0+ this should work:
chart.setOnChartValueSelectedListener(newOnChartValueSelectedListener() {
@OverridepublicvoidonValueSelected(Entry e, Highlight h) {
chart.getXAxis().getValueFormatter().getFormattedValue(e.getX(), chart.getXAxis());
}
@OverridepublicvoidonNothingSelected() {
}
});
Solution 2:
If you are using your model class for drawing chart and when you click on bar, wants to get more values instead of just Y axis points or x axis name then below is the solution.
first Your activity or fragment should implements OnChartValueSelectedListener. then implement onValueSelected()
@OverridepublicvoidonValueSelected(Entry e, int dataSetIndex, Highlight h) {
StringxAxisValue= chart.getData().getXVals().get(e.getXIndex());
//yourList (model class or pojo class type), first get the full listfor (int i=0; i<yourList.size(); i++){
YourModelClasscym= yourList.get(i);
Stringyear=cym.getYear();
// compare the xAxisValue with one of your Model class attribute. which is written on x-axis of the graphif (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 loopbreak;
}
}
}
@OverridepublicvoidonNothingSelected() {
}
Solution 3:
In Kotlin :-
barChart.setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
overridefunonNothingSelected() {
}
overridefunonValueSelected(e: Entry?, h: Highlight?) {
val xAxisLabel = e?.x?.let { barChart.xAxis?.valueFormatter?.getAxisLabel(it, barChart.xAxis) }
}
})
Post a Comment for "How To Get Selected Bar X-axis Name(strings Added To X-axis) Using Mp Chart?"