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
- <com.github.mikephil.charting.charts.piechart
- android:id="@+id/chart"
- Android:layout_width="Match_parent"
- android:layout_height="Match_parent"/>
Java code:
1. Initialize pie chart
[Java]View Plain Copy
- private void Initchart () {
- Mchart = (piechart) Findviewbyid (R.id.chart);
- Mchart.setusepercentvalues (true);
- Mchart.setdescription ("");
- Mchart.setextraoffsets (5, 5, 5);
- Mchart.setdrawslicetext (false);//Set hide text on pie chart, show only percentages
- Mchart.setdrawholeenabled (true);
- Mchart.setholecolortransparent (true);
- Mchart.settransparentcirclecolor (Getresources (). GetColor (R.COLOR.BUTTOMBAR_BG));
- Mchart.settransparentcirclealpha (110);
- Mchart.setonchartvalueselectedlistener (this);
- Mchart.setholeradius (45f); //Radius
- //mchart.setholeradius (0)//Solid Circle
- Mchart.settransparentcircleradius (48f); //Translucent ring
- Mchart.setdrawcentertext (true); You can add text in the middle of a pie chart
- //If there is no data, it will show this, similar to the ListView Emptyview
- Mchart.setnodatatext (Getresources (). getString (R.string.no_data));
- Mchart.setusepercentvalues (true); Set display proportional
- SimpleDateFormat format = new SimpleDateFormat ("yyyy");
- String year = Format.format (since_at*1000);
- Mchart.setcentertext (Generatecenterspannabletext (year));
- Mchart.setrotationangle (0); //Initial rotation angle
- //Enable rotation of the chart by touch
- Mchart.setrotationenabled (true); //Can be rotated manually
- Mchart.sethighlightpertapenabled (true);
- Mchart.animatey (Easing.EasingOption.EaseInOutQuad); //Set animation
- Legend mlegend = Mchart.getlegend (); //Set Scale chart
- Mlegend.setposition (Legend.LegendPosition.BELOW_CHART_LEFT); //Left bottom display
- Mlegend.setformsize (12f); //Proportional block font size
- Mlegend.setxentryspace (2f); //Set distance from pie chart to prevent overlap with pie chart
- Mlegend.setyentryspace (2f);
- //Set proportional block wrap ...
- Mlegend.setwordwrapenabled (true);
- Mlegend.setdirection (Legend.LegendDirection.LEFT_TO_RIGHT);
- Mlegend.settextcolor (Getresources (). GetColor (r.color.alpha_80));
- Mlegend.setform (Legend.LegendForm.SQUARE); //Set scale block shape, default to block
- Mlegend.setenabled (false);//Set Disable scale block
- }
2. Set up pie chart data
[Java]View Plain Copy
- /**
- * Set data for pie chart
- * @param the scale name shown on the names pie chart
- * @param counts Percentage
- */
- private void SetData (arraylist<string> names,arraylist<entry> counts) {
- Piedataset DataSet = new Piedataset (counts, "");
- Dataset.setslicespace (2f);
- Dataset.setselectionshift (5f);
- arraylist<integer> colors = new arraylist<integer> ();
- For (int c:colortemplate.joyful_colors)
- Colors.add (c);
- //
- For (int c:colortemplate.colorful_colors)
- Colors.add (c);
- For (int c:colortemplate.liberty_colors)
- Colors.add (c);
- for (int c:colortemplate.pastel_colors)
- Colors.add (c);
- Colors.add (Colortemplate.getholoblue ());
- Colors.add (Getresources (). GetColor (R.color.stastic_team));
- Dataset.setcolors (colors);
- //dataset.setselectionshift (0f);
- Piedata data = new Piedata (names, dataSet);
- Data.setvalueformatter (new Percentformatter ());
- Data.setvaluetextsize (12f);
- Data.setvaluetextcolor (Getresources (). GetColor (R.color.whrite));
- Mchart.setdata (data);
- //Undo All highlights
- Mchart.highlightvalues (null);
- Mchart.invalidate ();
- }
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
- Set the scale block wrapping ...
- Mlegend.setwordwrapenabled (true);
[Java]View Plain Copy
- 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
- Mlegend.setxentryspace (2f); //Set distance from pie chart to prevent overlap with pie chart
- Mlegend.setyentryspace (2f);
Mpandroidchart pie chart properties and related settings