7

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

Erum
  • 790
  • 3
  • 14
  • 36
  • @JeelVankhede do u have any idea how to set Legend same as picture attached l.setForm(Legend.LegendForm.SQUARE); // set what type of form/shape should be used //l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART); l.setVerticalAlignment(Legend.LegendVerticalAlignment.CENTER); l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); but its overlapping my piechart ? – Erum Dec 12 '18 at 11:27
  • @JeelVankhede setDrawEntryLabels its only removing text lable but still showing values inside piechart how to remove all label and value ? – Erum Dec 12 '18 at 11:32
  • @JeelVankhede pls check my edit ... and image how to overcome ? – Erum Dec 12 '18 at 12:41
  • @JeelVankhede i tried but its still same when trying to write this l.setVerticalAlignment(Legend.LegendVerticalAlignment.CENTRE); its layout is fine but showing only 3 legends and two are not there setting padding bottom as well but no response – Erum Dec 12 '18 at 13:15
  • @JeelVankhede any idea? – Erum Dec 13 '18 at 06:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/185185/discussion-between-erum-and-jeel-vankhede). – Erum Dec 13 '18 at 06:52
  • https://pastebin.com/uU00NZRJ pls check this & join chat room – Erum Dec 13 '18 at 06:54
  • @JeelVankhede my legend label text is too large and due to this my pie chart is extreme small in size .... is there any way to dynamically generate label using any layout and append dynamically ? pls share way of handling – Erum Dec 13 '18 at 12:58
  • @JeelVankhede pls check update UI issues – Erum Dec 14 '18 at 06:42
  • And what is the issue right now? – Jeel Vankhede Dec 15 '18 at 06:19
  • @Jeel How to resolve this UI issue .. legend label should be on right side same as Required mock pls check last ui snapshot how to resolve? – Erum Dec 16 '18 at 07:50
  • Have you tried this property `l.setDrawInside(false);`? – Jeel Vankhede Dec 17 '18 at 05:55
  • yes its already set – Erum Dec 17 '18 at 07:11

2 Answers2

11

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.

Jeel Vankhede
  • 11,592
  • 2
  • 28
  • 58
  • `setDrawSliceText()` is deprecated. and `setDrawEntryLabels` does the same thing what `setDrawSliceText` do. To remove drawing the value of the entries, use `setDrawValues(false)` on PieData object. – Samrat Mar 08 '23 at 04:59
  • @Samrat This answer was mainly intended for OP's given version of library as "v3.1.0-alpha". If this API is changed now then you can request your edit on the answer which I can update after the verification. Thank you :) – Jeel Vankhede Mar 13 '23 at 12:35
5

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 ]

NyirWeb.hu
  • 51
  • 1
  • 1