Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MP Pie Chart how to remove text and values from Pie chart

Actual image

How can i achieve this using MPAndroidChart ?

Using version : com.github.PhilJay:MPAndroidChart:v3.1.0-alpha Adding code for Legend and piechart margin:

private void setPieChartData() {
        //https://stackoverflow.com/a/50551488/1427037
        pieChart.setUsePercentValues(true);
        pieChart.getDescription().setEnabled(false);
        pieChart.setExtraOffsets(5,5,10,5);
        pieChart.setDragDecelerationFrictionCoef(0.9f);
        pieChart.setDrawCenterText(false);
        pieChart.setDrawHoleEnabled(false);
        //pieChart.animateY(1000, Easing.EasingOption.EaseInOutCubic);
        ArrayList<PieEntry> yValues = new ArrayList<>();
        yValues.add(new PieEntry(34f,"Ilala"));
        yValues.add(new PieEntry(56f,"Temeke"));
        yValues.add(new PieEntry(66f,"Kinondoni"));
        yValues.add(new PieEntry(45f,"Kigamboni"));
        yValues.add(new PieEntry(45f,"Kigamboni2"));
        pieChart.setDrawEntryLabels(false);

        PieDataSet dataSet = new PieDataSet(yValues, "Desease Per Regions");
        dataSet.setSliceSpace(0.1f);
        dataSet.setDrawValues(false);
        dataSet.setSelectionShift(5f);
        dataSet.setColors(ColorTemplate.VORDIPLOM_COLORS);

        PieData pieData = new PieData((dataSet));
        pieData.setValueTextSize(10f);
        pieData.setValueTextColor(Color.BLACK);
        pieChart.setData(pieData);
        pieChart.setDrawSliceText(false);
        setLegend();
    }

    private void setLegend() {
        Legend l = pieChart.getLegend();
        l.setFormSize(10f); // set the size of the legend forms/shapes
        l.setForm(Legend.LegendForm.SQUARE); // set what type of form/shape should be used
        l.setYOffset(5f);
        //l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); // set vertical alignment for legend
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); // set horizontal alignment for legend
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setTextSize(12f);
        l.setTextColor(Color.BLACK);
        //l.setXEntrySpace(5f); // set the space between the legend entries on the x-axis
        l.setYEntrySpace(1f); // set the space between the legend entries on the y-axis
        // set custom labels and colors
        List<LegendEntry> entries = new ArrayList<>();
        ArrayList<PieEntry> yValues = new ArrayList<>();
        yValues.add(new PieEntry(34f,"Ilala"));
        yValues.add(new PieEntry(56f,"Temeke"));
        yValues.add(new PieEntry(66f,"Kinondoni"));
        yValues.add(new PieEntry(45f,"Kigamboni"));
        yValues.add(new PieEntry(45f,"Kigamboni2"));
        for (int i = 0; i < 3; i++) {
            LegendEntry entry = new LegendEntry();
            entry.formColor = ColorTemplate.VORDIPLOM_COLORS[i];
            entry.label = yValues.get(i).getLabel() ;
            entries.add(entry);
        }
        l.setCustom(entries);
    }
   [![code result][2]][2]

How to set position of legend same as actual image i am getting problem to set its layout.please correct code and share any link for help Please share details to resolve this problem

code result

How to resolve this UI issue .. legend label should be on right side same as Required mock enter image description here

like image 559
Erum Avatar asked Dec 12 '18 11:12

Erum


2 Answers

For removing entry value & labels use method :

myPieChart.setDrawSliceText(false); // To remove slice text
myPieChart.setDrawMarkers(false); // To remove markers when click
myPieChart.setDrawEntryLabels(false); // To remove labels from piece of pie
myPieChart.getDescription().setEnabled(false); // To remove description of pie

For showing legends beside your chart try below methods :

Legend l = myPieChart.getLegend(); // get legend of pie
l.setVerticalAlignment(Legend.LegendVerticalAlignment.CENTER); // set vertical alignment for legend
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); // set horizontal alignment for legend
l.setOrientation(Legend.LegendOrientation.VERTICAL); // set orientation for legend
l.setDrawInside(false); // set if legend should be drawn inside or not

Check out more from here & from main doc page of MPAndroidChart.

like image 121
Jeel Vankhede Avatar answered Nov 06 '22 14:11

Jeel Vankhede


I hope it will help!

I did it,

    pieChart.setDrawCenterText(true);
    pieChart.setDrawEntryLabels(true);
    pieChart.setDrawMarkers(false);
    pieChart.getDescription().setEnabled(false);

    pieChart.getLegend().setEnabled(false);

just change to false all and you shouldn't see any text(s)! [ sorry if i had poor English ]

like image 42
NyirWeb.hu Avatar answered Nov 06 '22 15:11

NyirWeb.hu