Palakp
Palakp

Reputation: 181

MPAndroidChart click listener on chart

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

Answers (6)

Roopak Varghese
Roopak Varghese

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

Krishna
Krishna

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

kartik_g
kartik_g

Reputation: 21

  1. Implement interface OnChartValueSelectedListener
  2. Implemented methods
@Override
public void onValueSelected(Entry e, Highlight h) {
   // enter your code here
}

@Override
public void onNothingSelected() {
    // do nothing
}
  1. Add this code to your chart display method
chart.setOnChartValueSelectedListener(this);

Upvotes: 1

Rana Umer
Rana Umer

Reputation: 581

  1. Implement OnChartValueSelectedListener
  2. Register listener in activity/fragment
  3. override methods
  4. 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

Hossein Hajizadeh
Hossein Hajizadeh

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

Philipp Jahoda
Philipp Jahoda

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

Related Questions