Reputation: 181
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.
Upvotes: 5
Views: 26239
Reputation: 51
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...
Upvotes: 5
Reputation: 1634
For PieChart :-
mPieChart.setOnChartValueSelectedListener(object :
OnChartValueSelectedListener {
override fun onNothingSelected() {
}
override fun onValueSelected(e: Entry?, h: Highlight?) {
val pieEntry = e as PieEntry
val label: String = pieEntry.label
}
})
Upvotes: 3
Reputation: 21
OnChartValueSelectedListener
@Override
public void onValueSelected(Entry e, Highlight h) {
// enter your code here
}
@Override
public void onNothingSelected() {
// do nothing
}
chart.setOnChartValueSelectedListener(this);
Upvotes: 1
Reputation: 581
OnChartValueSelectedListener
This is in Kotlin
chart.setOnChartValueSelectedListener(this)
override fun onNothingSelected() {
Log.i("Entry selected", "Nothing selected.")
}
override fun onValueSelected(e: Entry?, h: Highlight?) {
Log.i("Entry selected", e.toString())
val x:Float =e!!.x
val y:Float =e!!.y
chart.highlightValue(h)
}
Upvotes: 2
Reputation: 1485
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()
{
}
});
Upvotes: 12
Reputation: 51411
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.
Upvotes: 14