I am using MPAndroidChart and would like to know how to perform click event on following graphs and get the related callback: Pie chart: click on particular reason open its detail. Bar chart: click on any bar open its detail. Stacked Bar chart: click on any bar open its detail.
I need to get notified when the chart is clicked.
Use the OnChartValueSelectedListener. You can find the documentation on how to implement it here.
This listener allows you to react on click gestures performed on the charts.
for LineChart
chart.setOnChartValueSelectedListener(new OnChartValueSelectedListener()
{
@Override
public void onValueSelected(Entry e, Highlight h)
{
float x=e.getX();
float y=e.getY();
}
@Override
public void onNothingSelected()
{
}
});
for LineChart if you are using displayed non float value in Kotlin use below code
lineChart.setOnChartValueSelectedListener(object : OnChartValueSelectedListener
{
override fun onValueSelected(e: Entry, h: Highlight?) {
val x = e.x.toString()
val y = e.y
val selectedXAxisCount = x.substringBefore(".") //this value is float so use substringbefore method
// another method shown below
val nonFloat=lineChart.getXAxis().getValueFormatter().getFormattedValue(e.x)
//if you are display any string in x axis you will get this
}
override fun onNothingSelected() {}
})
Happy coding...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With