Mpandroidchart Open Source Chart library (i) pie chart

Source: Internet
Author: User
Tags float range

Mpandroidchart is an open source graphics library based on Android, Mpandroidchart can not only draw a variety of statistical charts on Android devices, but also can drag and zoom the chart operation, the application is very flexible. Mpandroidchart also has common chart types: line charts, pie charts, histogram charts, and scatter plots.

GitHub Address:

Https://github.com/PhilJay/MPAndroidChart

The following main implementation of the pie chart:

1. Download the latest Mpandroidchartlibrary-2-0-8.jar package from the above address and copy it to the project's Libs

2. Defining an XML file


3. The main Java logic code is as follows, and the comments have been added.

Package Com.jackie.mpandroidchart;import Java.util.arraylist;import Com.github.mikephil.charting.charts.PieChart; Import Com.github.mikephil.charting.components.legend;import Com.github.mikephil.charting.components.legend.legendposition;import Com.github.mikephil.charting.data.Entry; Import Com.github.mikephil.charting.data.piedata;import Com.github.mikephil.charting.data.piedataset;import Android.support.v7.app.actionbaractivity;import Android.graphics.color;import Android.os.Bundle;import Android.util.displaymetrics;public class Mainactivity extends Actionbaractivity {private Piechart mchart;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Mchart = (Piechart) Findviewbyid (R.id.spread_pie_chart); Showchart ();}  private void Showchart () {mchart.setholecolortransparent (true); Mchart.setholeradius (60f); Radius Mchart.settransparentcircleradius (64f); Translucent Circle Mchart.setdescription ("Test pie chart");//Mchart.setdrawyvalues(true); Mchart.setdrawcentertext (true); In the middle of the pie chart you can add text mchart.setdrawholeenabled (true); Mchart.setrotationangle (90); Initial rotation angle//draws the corresponding description value into the slice//mchart.setdrawxvalues (TRUE);//enable rotation of T He chart by touchmchart.setrotationenabled (true);  Can be manually rotated//display percentage valuesmchart.setusepercentvalues (TRUE); Displayed as a percentage//Mchart.setunit ("€");//Mchart.setdrawunitsinchart (TRUE);//Add a selection listener// Mchart.setonchartvalueselectedlistener (this);//mchart.settouchenabled (FALSE);//mchart.setonanimationlistener (  this); Mchart.setcentertext ("Quarterly Revenue"); The text in the middle of the pie chart//Fill the pie chart with Data SetData (4, 100);  Mchart.animatexy (1000, 1000); Set animation//Mchart.spin (2000, 0, 360);  Legend mlegend = Mchart.getlegend ();  Set the scale chart mlegend.setposition (Legendposition.right_of_chart);  The rightmost display//mlegend.setform (legendform.line); Sets the shape of the scale chart by default to square mlegend.setxentryspace (7f); Mlegend.setyentryspace (5f);} /** * * @param count is divided into several parts * @param range */private void setData (int count, float range) {arraylist<entry> yVals1 = new arraylist<entry> (); Yvals is used to represent the actual data//PIE data that encapsulates each pie block/** * Divides a pie chart into four parts, four parts of the numerical scale is 14:14:34:38 * So 14 represents a percentage of 14% */float quarterley1 = 14;float qu Arterley2 = 14;float Quarterley3 = 34;float Quarterley4 = 38;yvals1.add (New Entry (quarterley1, 0)); Yvals1.add (New Entry (q Uarterley2, 1)); Yvals1.add (New Entry (Quarterley3, 2)); Yvals1.add (New Entry (Quarterley4, 3));  arraylist<string> xvals = new arraylist<string> ();   Xvals is used to represent the contents of each pie block for (int i = 1; I <= count; i++) Xvals.add ("quarterly" + i); The pie blocks appear as Quarterly1, Quarterly2, Quarterly3, quarterly4piedataset mpiedataset = new Piedataset (YVALS1, "Quarterly Revenue 2014 "/* shown on the scale chart */); Mpiedataset.setslicespace (0f); Set the distance between pie charts arraylist<integer> colors = new arraylist<integer> ();//Pie chart Color Colors.add (COLOR.RGB (205, 205, 205) Colors.add (COLOR.RGB (188, 223)), Colors.add (Color.rgb (255, 123, 124)), Colors.add (Color.rgb (57, 135, 200)); Mpiedataset.setcolors (colors);Displaymetrics metrics = getresources (). Getdisplaymetrics (); float px = 5 * (metrics.densitydpi/160f); Mpiedataset.setselectionshift (px); The length of the selected state piedata mpiedata = new Piedata (xvals, Mpiedataset); Mchart.setdata (mpiedata);//Undo All Highlightsmchart.highlightvalues (null); Mchart.invalidate ();}}

As follows:



Mpandroidchart Open Source Chart library (i) pie chart

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.