How does Android implement a chart? Open Source Library Mpandroidchart tutorial [photos]

Source: Internet
Author: User

Mpandroidchart Effect Chart

How to use Mpandroidchart

Here we draw an example of a pie chart with the following steps:

XML Layout Code

The code is as follows Copy Code

<com.github.mikephil.charting.charts.piechart
Android:id= "@+id/spread_pie_chart"
Android:layout_width= "Match_parent"
android:layout_height= "320dip"/>

Initialize pie chart

The code is as follows Copy Code

Colortemplate mCt;
MCt = new Colortemplate ();
Mct.adddatasetcolors (colortemplate.pastel_colors, this);
Mchart = (piechart) Headview.findviewbyid (R.id.spread_pie_chart);
Mchart.setcolortemplate (mCt);
Mchart.setdescription ("");
Mchart.setholeradius (30f);
Mchart.settransparentcircleradius (0f);
Mchart.setcentertextsize (18f);
Mchart.setdrawxvalues (TRUE);
Mchart.setusepercentvalues (TRUE);
Mchart.setdrawlegend (FALSE);
Space between slices
Mchart.setslicespace (1f);
Mchart.setdrawholeenabled (FALSE);
Mchart.settouchenabled (FALSE);

Binding chart Data

  code is as follows copy code
 ArrayList yvals = new ArrayList (); 
ArrayList xvals = new ArrayList ();
for (int i = 0; i < listdatas.size (); i++) {
Yvals.add (new Entry (float) listdatas.get (i). Getprovincecount (), i));
Xvals.add (Listdatas.get (i). Getprovincename ());
}
DataSet Set1 = new DataSet (Yvals, "Content");
ArrayList datasets = new ArrayList ();
Datasets.add (SET1);
ChartData data = new ChartData (xvals, datasets);
Mchart.setdata (data);

Summary: simple Android Chart requirements, we can use mpandroidchart very good solution. Of course, Mpandroidchart can also be very friendly to achieve complex Android graphics requirements, you can be the next official Chinese manual in-depth study.

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.