Mpandroidchart pie chart properties and related settings

Source: Internet
Author: User
Tags getcolor

The company recently in the statistical function, so used to the pie chart, on-line search some information finally decided to use Mpandroidchart, very convenient to use, there are some problems through a variety of search, finally solved ... Don't say much nonsense, first look at:

Layout file:

[Java]View Plain Copy  
    1. <com.github.mikephil.charting.charts.piechart
    2. android:id="@+id/chart"
    3. Android:layout_width="Match_parent"
    4. android:layout_height="Match_parent"/>

Java code:

1. Initialize pie chart

[Java]View Plain Copy  
  1. private void Initchart () {
  2. Mchart = (piechart) Findviewbyid (R.id.chart);
  3. Mchart.setusepercentvalues (true);
  4. Mchart.setdescription ("");
  5. Mchart.setextraoffsets (5, 5, 5);
  6. Mchart.setdrawslicetext (false);//Set hide text on pie chart, show only percentages
  7. Mchart.setdrawholeenabled (true);
  8. Mchart.setholecolortransparent (true);
  9. Mchart.settransparentcirclecolor (Getresources (). GetColor (R.COLOR.BUTTOMBAR_BG));
  10. Mchart.settransparentcirclealpha (110);
  11. Mchart.setonchartvalueselectedlistener (this);
  12. Mchart.setholeradius (45f); //Radius
  13. //mchart.setholeradius (0)//Solid Circle
  14. Mchart.settransparentcircleradius (48f); //Translucent ring
  15. Mchart.setdrawcentertext (true); You can add text in the middle of a pie chart
  16. //If there is no data, it will show this, similar to the ListView Emptyview
  17. Mchart.setnodatatext (Getresources (). getString (R.string.no_data));
  18. Mchart.setusepercentvalues (true); Set display proportional
  19. SimpleDateFormat format = new SimpleDateFormat ("yyyy");
  20. String year = Format.format (since_at*1000);
  21. Mchart.setcentertext (Generatecenterspannabletext (year));
  22. Mchart.setrotationangle (0); //Initial rotation angle
  23. //Enable rotation of the chart by touch
  24. Mchart.setrotationenabled (true); //Can be rotated manually
  25. Mchart.sethighlightpertapenabled (true);
  26. Mchart.animatey (Easing.EasingOption.EaseInOutQuad); //Set animation
  27. Legend mlegend = Mchart.getlegend (); //Set Scale chart
  28. Mlegend.setposition (Legend.LegendPosition.BELOW_CHART_LEFT); //Left bottom display
  29. Mlegend.setformsize (12f); //Proportional block font size
  30. Mlegend.setxentryspace (2f); //Set distance from pie chart to prevent overlap with pie chart
  31. Mlegend.setyentryspace (2f);
  32. //Set proportional block wrap ...
  33. Mlegend.setwordwrapenabled (true);
  34. Mlegend.setdirection (Legend.LegendDirection.LEFT_TO_RIGHT);
  35. Mlegend.settextcolor (Getresources (). GetColor (r.color.alpha_80));
  36. Mlegend.setform (Legend.LegendForm.SQUARE); //Set scale block shape, default to block
  37. Mlegend.setenabled (false);//Set Disable scale block
  38. }

2. Set up pie chart data

[Java]View Plain Copy  
  1. /** 
  2. * Set data for pie chart
  3. * @param the scale name shown on the names pie chart
  4. * @param counts Percentage
  5. */
  6. private void SetData (arraylist<string> names,arraylist<entry> counts) {
  7. Piedataset DataSet = new Piedataset (counts, "");
  8. Dataset.setslicespace (2f);
  9. Dataset.setselectionshift (5f);
  10. arraylist<integer> colors = new arraylist<integer> ();
  11. For (int c:colortemplate.joyful_colors)
  12. Colors.add (c);
  13. //
  14. For (int c:colortemplate.colorful_colors)
  15. Colors.add (c);
  16. For (int c:colortemplate.liberty_colors)
  17. Colors.add (c);
  18. for (int c:colortemplate.pastel_colors)
  19. Colors.add (c);
  20. Colors.add (Colortemplate.getholoblue ());
  21. Colors.add (Getresources (). GetColor (R.color.stastic_team));
  22. Dataset.setcolors (colors);
  23. //dataset.setselectionshift (0f);
  24. Piedata data = new Piedata (names, dataSet);
  25. Data.setvalueformatter (new Percentformatter ());
  26. Data.setvaluetextsize (12f);
  27. Data.setvaluetextcolor (Getresources (). GetColor (R.color.whrite));
  28. Mchart.setdata (data);
  29. //Undo All highlights
  30. Mchart.highlightvalues (null);
  31. Mchart.invalidate ();
  32. }


There are several problems encountered in this process:

1, the bottom of the proportional block display only one row, if more data, will be outside the screen, so you need to wrap the scale block, set as follows:

[Java]View Plain Copy  
    1. Set the scale block wrapping ...
    2. Mlegend.setwordwrapenabled (true);
[Java]View Plain Copy  
    1. At the same time need to put the proportional block below or put on top <span style="font-family:arial, Helvetica, Sans-serif;"  > mlegend.setposition (Legend.LegendPosition.BELOW_CHART_LEFT); //Left bottom display </span>
2.How to show the percentages on the pie chart, look up a lot of information no one mentioned this, and finally found the answer on the StackOverflow.

Http://stackoverflow.com/questions/31154706/mpandroidchart-piechart-remove-percents

Piechart.setdrawslicetext (False)

3. If the proportional block is placed on the left or right, if the font is too long, it will be stacked with the pie chart and the following processing can prevent overlay.

[Java]View Plain Copy  
    1. Mlegend.setxentryspace (2f); //Set distance from pie chart to prevent overlap with pie chart
    2. Mlegend.setyentryspace (2f);

Mpandroidchart pie chart properties and related settings

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.