The Android implementation will rotate the pie chart instance code
Recently in a project, due to the need for statistical needs, and then made the following pie-like chart.
The following illustration is an effect chart:
The general idea is:
About the introduction here do not do a detailed introduction, if you want to go deep please click on open source Project Mpandroidchart
Here's how it's implemented:
The first is to add Mpandroidchart dependencies:
Compile ' com.github.philjay:mpandroidchart:v3.0.1 '
Mainactivity
Package com.example.geekp.myapplication;
Import Android.graphics.Color;
Import Android.graphics.Typeface;
Import android.support.annotation.Nullable;
Import Android.support.design.widget.TabLayout;
Import Android.support.design.widget.FloatingActionButton;
Import Android.support.design.widget.Snackbar;
Import android.support.v7.app.AppCompatActivity;
Import Android.support.v7.widget.Toolbar;
Import android.support.v4.app.Fragment;
Import Android.support.v4.app.FragmentManager;
Import Android.support.v4.app.FragmentPagerAdapter;
Import Android.support.v4.view.ViewPager;
Import Android.os.Bundle;
Import android.text.SpannableString;
Import Android.text.style.RelativeSizeSpan;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.Window;
Import Android.view.WindowManager;
Import Android.widget.TextView;
Import com.github.mikephil.charting.animation.Easing;
Import Com.github.mikephil.charting.charts.PieChart; Import Com.githuB.mikephil.charting.components.legend;
Import Com.github.mikephil.charting.data.PieData;
Import Com.github.mikephil.charting.data.PieDataSet;
Import Com.github.mikephil.charting.data.PieEntry;
Import Com.github.mikephil.charting.formatter.PercentFormatter;
Import Com.github.mikephil.charting.utils.ColorTemplate;
Import java.util.ArrayList; Import Butterknife.
BindView; Import Butterknife.
Butterknife;
public class Mainactivity extends Appcompatactivity {private Sectionspageradapter msectionspageradapter;
Private Viewpager Mviewpager;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Set Full-screen GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSC
Reen);
Setcontentview (R.layout.activity_main);
Toolbar Toolbar = (Toolbar) Findviewbyid (R.id.toolbar);
Setsupportactionbar (toolbar); Create the adapter that would return a fragment for each of the THree//Primary sections of the activity.
Msectionspageradapter = new Sectionspageradapter (Getsupportfragmentmanager ());
Set up the Viewpager with the sections adapter.
Mviewpager = (Viewpager) Findviewbyid (R.id.container);
Mviewpager.setadapter (Msectionspageradapter);
Getsupportactionbar (). Settitle ("pie chart");
Tablayout tablayout = (tablayout) Findviewbyid (r.id.tabs);
Tablayout.setupwithviewpager (Mviewpager); }//fragment public static class Placeholderfragment extends Fragment {@BindView (R.id.chart1) Piechart
T
@BindView (R.id.tvxmax) TextView Tvxmax;
@BindView (R.id.tvymax) TextView Tvymax;
Protected string[] mparties = new string[]{"Completed", "incomplete"};
protected typeface mtfregular;
protected typeface mtflight;
private static final String Arg_section_number = "Section_number"; Public placeholderfragment () {} public static placeholderfragment newinstance (int sectionnumber){Placeholderfragment fragment = new Placeholderfragment ();
Bundle args = new Bundle ();
Args.putint (Arg_section_number, Sectionnumber);
Fragment.setarguments (args);
return fragment; @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle Savedi
Nstancestate) {View Rootview = inflater.inflate (R.layout.fragment_main, container, false);
Butterknife.bind (this, rootview);
int index = Getarguments (). GetInt (Arg_section_number);
Mtfregular = Typeface.createfromasset (GetContext (). Getassets (), "Opensans-regular.ttf");
Mtflight = Typeface.createfromasset (GetContext (). Getassets (), "Opensans-light.ttf");
Mchart.setusepercentvalues (TRUE);
Mchart.getdescription (). setenabled (false);
Mchart.setextraoffsets (5, 10, 5, 5);
Mchart.setdragdecelerationfrictioncoef (0.95f);
Mchart.setcentertexttypeface (Mtflight); Mchart.setcentertext (GeneratecenterspannAbletext (index));
Mchart.setdrawholeenabled (TRUE);
Mchart.setholecolor (Color.White);
Mchart.settransparentcirclecolor (Color.White);
Mchart.settransparentcirclealpha (110);
Mchart.setholeradius (58f);
Mchart.settransparentcircleradius (61f);
Mchart.setdrawcentertext (TRUE);
Mchart.setrotationangle (0);
Enable rotation of the chart by Touch mchart.setrotationenabled (true);
Mchart.sethighlightpertapenabled (TRUE);
SetData (index);
Mchart.animatey (1400, Easing.EasingOption.EaseInOutQuad);
Mchart.spin (2000, 0, 360);
Legend L = mchart.getlegend ();
L.setverticalalignment (Legend.LegendVerticalAlignment.TOP);
L.sethorizontalalignment (Legend.LegendHorizontalAlignment.RIGHT);
L.setorientation (Legend.LegendOrientation.VERTICAL);
L.setdrawinside (FALSE);
L.setxentryspace (7f);
L.setyentryspace (0f);
L.setyoffset (0f); Entry Label Styling Mchart.seteNtrylabelcolor (Color.White);
Mchart.setentrylabeltypeface (Mtfregular);
Mchart.setentrylabeltextsize (12f);
return rootview;
}//Pie chart in the middle of the content to display private spannablestring generatecenterspannabletext (int index) {String sectionname = "";
Switch (index) {case 1:sectionname = "Account One";
Break
Case 2:sectionname = "Subject two";
Break
Case 3:sectionname = "Subject three";
Break
Case 4:sectionname = "Subject four";
Break
} spannablestring s = new spannablestring (sectionname);
S.setspan (New Relativesizespan (1.7f), 0, Sectionname.length (), 0);
return s; } private void SetData (int fragmentindex) {arraylist<pieentry> entries = new arraylist<pieentry> (
);
Piedataset DataSet = new Piedataset (entries, "correct rate:" + 25 + "%");
Dataset.setslicespace (3f);
Dataset.setselectionshift (5f); Arraylist<integer> colors = new arraylist<integer> (); if (Fragmentindex = = 1) {//is written here is part of pie chart, as I write is the first part is accounted for 75%, the second part is accounted for 25% Entries.Add (The new Pieentry (m
Parties[0]));
Entries.Add (New Pieentry, mparties[1]);
for (int c:colortemplate.vordiplom_colors) Colors.add (c);
else if (Fragmentindex = = 2) {Entries.Add (new Pieentry (mparties[0)));
Entries.Add (New Pieentry, mparties[1]);
Colors.add (Getresources (). GetColor (R.COLOR.PIECOLOR8));
Colors.add (Getresources (). GetColor (R.color.piecolor2));
else if (Fragmentindex = = 3) {Entries.Add (new Pieentry (mparties[0)));
Entries.Add (New Pieentry, mparties[1]);
Colors.add (Getresources (). GetColor (R.color.piecolor3));
Colors.add (Getresources (). GetColor (R.color.piecolor4));
else {entries.add (new pieentry, mparties[0]);
Entries.Add (New Pieentry, mparties[1]); Colors.add (gEtresources (). GetColor (R.color.piecolor5));
Colors.add (Getresources (). GetColor (R.COLOR.PIECOLOR6));
} colors.add (Colortemplate.getholoblue ());
Dataset.setcolors (colors);
Dataset.setselectionshift (0f);
Piedata data = new Piedata (DataSet);
Data.setvalueformatter (New Percentformatter ());
Data.setvaluetextsize (11f);
Data.setvaluetextcolor (Color.Black);
Data.setvaluetypeface (Mtflight);
Mchart.setdata (data);
Undo all Highlights Mchart.highlightvalues (null);
Mchart.invalidate (); }//Adapter public class Sectionspageradapter extends Fragmentpageradapter {public Sectionspageradapter (fragmen
Tmanager FM) {super (FM);
@Override public Fragment getitem (int position) {return placeholderfragment.newinstance (position + 1);
@Override public int GetCount () {return 4; }//This method is used to display the title @Override public charsequence getpagetitle (intPosition) {switch (position) {case 0:return "account One";
Case 1:return "Subject two";
Case 2:return "Subject three";
Case 3:return "Subject four";
return null;
}
}
}
Activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"?> <android.support.design.widget.coordinatorlayout xmlns:android= " Http://schemas.android.com/apk/res/android "xmlns:app=" Http://schemas.android.com/apk/res-auto "xmlns:tools=" Http://schemas.android.com/tools "android:id=" @+id/main_content "android:layout_width=" Match_parent "Android: layout_height= "Match_parent" android:fitssystemwindows= "true" tools:context= " Com.example.geekp.myapplication.MainActivity "> <android.support.design.widget.appbarlayout android:id=" @+ Id/appbar "android:layout_width=" match_parent "android:layout_height=" wrap_content "android:paddingTop=" @dimen
/appbar_padding_top "Android:theme=" @style/apptheme.appbaroverlay "> <android.support.v7.widget.toolbar
Android:id= "@+id/toolbar" android:layout_width= "match_parent" android:layout_height= "Attr/actionBarSize"
Android:background= "Attr/colorprimary" app:layout_scrollflags= "Scroll|enteralways" App:popuptheme= "@style/apptheme.popupoverlay" > </android.support.v7.widget.Toolbar> <android.s Upport.design.widget.TabLayout android:id= "@+id/tabs" android:layout_width= "Match_parent" android:layout _height= "Wrap_content"/> </android.support.design.widget.AppBarLayout> < Android.support.v4.view.ViewPager android:id= "@+id/container" android:layout_width= "Match_parent" Android:layo ut_height= "Match_parent" app:layout_behavior= "@string/appbar_scrolling_view_behavior"/> </
Android.support.design.widget.coordinatorlayout>
Fragment.xml
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "> <com.github.mikephil.cha Rting.charts.PieChart android:id= "@+id/chart1" android:layout_margintop= "100DP" android:layout_width= "Match_pa" Rent "android:layout_height=" match_parent "/> <textview android:id=" @+id/tvxmax "Android:layout_widt H= "50DP" android:layout_height= "Wrap_content" android:layout_alignparentright= "true" Android:layout_marginbott Om= "15DP" android:layout_marginright= "10DP" android:gravity= "right" android:textappearance= "? Android:attr/text Appearancemedium "/> <textview android:id=" @+id/tvymax "android:layout_width=" 50DP "android:layout_he ight= "Wrap_content" android:layout_alignparentright= "true" android:layout_marginbottom= "15DP" android:layout_m arginright= "10DP" android:grAvity= "Right" android:textappearance= "Android:attr/textappearancemedium"/> </RelativeLayout>
SOURCE Gate: Http://xiazai.jb51.net/201612/yuanma/piechart-master (jb51.net). rar
Thank you for reading, I hope to help you, thank you for your support for this site!