javaandroidmpandroidchart

How to get X index in MP Android Pie Chart?


I'm making a function to help people identify each pie chart slice with it's value via click listener. The problem is that it ONLY returns the first company value regardless if the second company has a value or not.

Furthermore the logs reveal the following "I/DEPRECATED: Pie entries do not have x values"

The "entry.getX()" is how I identified slices in the past. This code will no doubt work if it was a bar chart,but that's not what doing. All it does on Pie charts is return the first entry value nothing more.

The version of MP android Chart I'm using is Version 3.1.0.

This is my code below

//Chart click listener that displays the percentage when clicked
piechart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {

    @Override
    public void onValueSelected(Entry entry, Highlight highlight) {
        try {

            String company = null;
            Double formattedvalue = 0.0;
            float getval = entry.getY();
            testformat = new DoublePercentConverter();
            if (entry.getX() == 0) {
                company = “Company 1” + " ";

            } else {
                company = “Company 2” + " ";
            }
            formattedvalue = testformat.convertvalue(getval);


            AlertDialog.Builder a_builder545 = new AlertDialog.Builder(activity1.this);
            a_builder545.setMessage(company + Double.toString(formattedvalue) + "%").setCancelable(false)
                    .setPositiveButton(ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            piechart.highlightValues(null);
                            piechart.invalidate();
                        }
                    });

            AlertDialog alert743 = a_builder545.create();
            alert743.setTitle(“Company info”);
            alert743.show();

        } catch (Exception eg) {
            eg.printStackTrace();
        }
    }

    @Override
    public void onNothingSelected() {

    }
});

Solution

  • I had a fail moment and made an obvious mistake

    highlight.getX() is how you get the selected label.