Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 429
Palakp Avatar asked Feb 08 '16 11:02

Palakp


3 Answers

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.

like image 83
Philipp Jahoda Avatar answered Oct 17 '22 00:10

Philipp Jahoda


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()
        {

        }
    });
like image 11
Hossein Hajizadeh Avatar answered Oct 17 '22 02:10

Hossein Hajizadeh


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...

like image 5
Roopak Varghese Avatar answered Oct 17 '22 02:10

Roopak Varghese