Mpandroidchart Open Source Chart Library pie chart
To introduce an icon open Source Library Mpandroidchart, it can not only on the Android device to draw a variety of statistical charts, but also can drag and zoom the chart operation, very flexible. Mpandroidchart also has a common chart type: line, pie, histogram, and scatter charts.
Mpandroidchartlibrary.jar Package Download Address:
Https://github.com/PhilJay/MPAndroidChart/releases
The following are mainly implemented as a pie chart:
1. Download the latest Mpandroidchartlibrary-2-0-8.jar package from the address above and copy it to the Libs of the project
2. Defining XML files
3. The main Java logic code is as follows.
Importjava.util.ArrayList;
Importcom.github.mikephil.charting.charts.PieChart;
Importcom.github.mikephil.charting.components.Legend;
Importcom.github.mikephil.charting.components.Legend.LegendPosition;
Importcom.github.mikephil.charting.data.Entry;
Importcom.github.mikephil.charting.data.PieData;
Importcom.github.mikephil.charting.data.PieDataSet;
Import android.support.v7.app.ActionBarActivity;
Importandroid.graphics.Color;
Importandroid.os.Bundle;
Importandroid.util.DisplayMetrics;
public class Mainactivity extends actionbaractivity {privatepiechartmchart;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Mchart = (piechart) Findviewbyid (R.id.spread_pie_chart);
Piedatampiedata = Getpiedata (4, 100);
Showchart (Mchart, mpiedata);
} private void Showchart (Piechartpiechart, Piedatapiedata) {piechart.setholecolortransparent (true); Piechart.setholeradius (60f); Radius piechart.setTransparentcircleradius (64f);
Translucent ring//piechart.setholeradius (0)//solid round piechart.setdescription ("Test pie chart");
Mchart.setdrawyvalues (TRUE); Piechart.setdrawcentertext (TRUE);
The middle of a pie chart can add text piechart.setdrawholeenabled (true); Piechart.setrotationangle (90);
Initial rotation angle//draws the corresponding description value into the slice//mchart.setdrawxvalues (TRUE); Enable rotation of the chart by Touch piechart.setrotationenabled (true); You can manually rotate//display percentage values Piechart.setusepercentvalues (TRUE);
Show as percent//mchart.setunit ("?");
Mchart.setdrawunitsinchart (TRUE);
Add a selection listener//Mchart.setonchartvalueselectedlistener (this);
Mchart.settouchenabled (FALSE);
Mchart.setonanimationlistener (this); Piechart.setcentertext ("Quarterly Revenue");
Pie chart in the middle of the text//settings Data piechart.setdata (Piedata);
Undo all Highlights//piechart.highlightvalues (NULL);
Piechart.invalidate (); Legend mlegend = Piechart.getlegend (); Set scale graph Mlegend.setposition (legendposition.Right_of_chart); Rightmost display//Mlegend.setform (Legendform.line);
Set the shape of the scale diagram, the default is square mlegend.setxentryspace (7f);
Mlegend.setyentryspace (5f); Piechart.animatexy (1000, 1000);
Set animation//Mchart.spin (2000, 0, 360); /** * * @param count into several separate * @param range/privatepiedatagetpiedata (int count, float range) {Arraylist<strin G>xvalues = new arraylist<string> (); Xvals is used to represent the content on each pie block for (inti = 0; i< count; i++) {Xvalues.add ("quarterly" + (i + 1)); , Quarterly3, Quarterly4} arraylist<entry>yvalues = new arraylist<entry> ();
Yvals is used to represent the actual data//pie chart data that encapsulates each piece of pie/** * Divides a pie chart into four parts and a four-part value of 14:14:34:38 * So the percentage of 14 is 14%/float quarterly1 = 14;
float quarterly2 = 14;
float Quarterly3 = 34;
float quarterly4 = 38;
Yvalues.add (New Entry (quarterly1, 0));
Yvalues.add (New Entry (Quarterly2, 1));
Yvalues.add (New Entry (Quarterly3, 2));
Yvalues.add (New Entry (Quarterly4, 3)); The y-axis set piedatasetpiedataset = new Piedataset (yvalues, "QuarTerly Revenue 2014 "/* shown on the scale chart * *); Piedataset.setslicespace (0f);
Sets the distance between a pie chart arraylist<integer> colors = new arraylist<integer> ();
Pie chart Color Colors.add (COLOR.RGB (205, 205, 205));
Colors.add (Color.rgb (114, 188, 223));
Colors.add (Color.rgb (255, 123, 124));
Colors.add (Color.rgb (57, 135, 200));
Piedataset.setcolors (colors);
Displaymetrics metrics = getresources (). Getdisplaymetrics ();
FLOATPX = 5 * (metrics.densitydpi/160f); Piedataset.setselectionshift (px);
The length of the selected Piedatapiedata = new Piedata (xValues, Piedataset);
Returnpiedata; }
}
The effect chart is as follows:
Mpandroidchart Open Source Chart Gallery Line chart
1. Copy the Mpandroidchartlibrary-2-0-8.jar package to the Libs of the project
2. Defining XML files
3. The main Java logic code is as follows.
Packagecom.example.mpandroidlinechart;
Importjava.util.ArrayList;
Importcom.github.mikephil.charting.charts.LineChart;
Importcom.github.mikephil.charting.components.Legend;
Importcom.github.mikephil.charting.components.Legend.LegendForm;
Importcom.github.mikephil.charting.data.Entry;
Importcom.github.mikephil.charting.data.LineData;
Importcom.github.mikephil.charting.data.LineDataSet;
Import android.support.v7.app.ActionBarActivity;
Importandroid.graphics.Color;
Importandroid.os.Bundle;
public class Mainactivity extends actionbaractivity {privatelinechartmlinechart;
Private typeface mTf;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Mlinechart = (Linechart) Findviewbyid (R.id.spread_line_chart);
MTf = Typeface.createfromasset (Getassets (), "Opensans-bold.ttf");
Linedatamlinedata = Getlinedata (36, 100);
Showchart (Mlinechart, Mlinedata, Color.rgb (114, 188, 223)); }
// Sets the style to display private void Showchart (Linechartlinechart, linedatalinedata, int color) {linechart.setdrawborders (false); Adds a border//No description text Linechart.setdescription ("") to a line chart;//Data Description///If there is no data, this will be displayed, similar to the ListView Emtpyview Linechart
. setnodatatextdescription ("You need to provide data for the chart."); enable/disable Grid background Linechart.setdrawgridbackground (false); Whether to display the table color Linechart.setgridbackgroundcolor (color.white& 0x70ffffff); The color of the table, here is to give the color a transparency//enable Touch gestures linechart.settouchenabled (true); Sets whether you can touch//enable scaling and dragging linechart.setdragenabled (true);//Whether you can drag linechart.setscaleenabled (true); No zoom//If disabled, scaling can do on X-and y-axis separately linechart.setpinchzoom (false);//Linechart.setback Groundcolor (color),//setting background//Add Data Linechart.setdata (Linedata); Set data//Get the legend (only possible after setting data) legend Mlegend = Linechart.getlegend (); Set the scale chart, which is the value of the group y//Modify THe legend ...//mlegend.setposition (Legendposition.left_of_chart); Mlegend.setform (legendform.circle);//Style mlegend.setformsize (6f);//font Mlegend.settextcolor (color.white);//color// Mlegend.settypeface (mTf);//font Linechart.animatex (2500); Immediately executed animation, x-axis}/** * Generate a data * @param count indicates how many coordinate points in the chart * @param range is used to generate random numbers within range * @return * * privatelinedatage
Tlinedata (int count, float range) {arraylist<string>xvalues = new arraylist<string> ();
for (inti = 0; i< count; i++) {//X axis display data, where the default use of digital subscript display Xvalues.add ("" + i);
}//y-axis data arraylist<entry>yvalues = new arraylist<entry> ();
for (inti = 0; i< count; i++) {Float value = (float) (Math.random () * range) + 3;
Yvalues.add (New Entry (value, i)); }//Create a dataset and give it a type//Y axis data Set linedatasetlinedataset = new Linedataset (yvalues, "Test Line Chart"/* is displayed on the scale chart *
/);
Mlinedataset.setfillalpha (110);
Mlinedataset.setfillcolor (color.red); Set the parameter Linedataset.setlinewidth (1.75f) with a set of Y axes; LineWidth linedataset.setcirclesize (3f);//Display Circle size Linedataset.setcolor (color.white);//Display Color Linedataset.setcirclecolor ( Color.White);//Round color linedataset.sethighlightcolor (color.white);
The color of the highlighted line arraylist<linedataset>linedatasets = new arraylist<linedataset> (); Linedatasets.add (Linedataset);
Add the Datasets//Create a data object with the Datasets Linedatalinedata = new Linedata (xValues, linedatasets);
Returnlinedata; }
}
The effect chart is as follows:
Mpandroidchart Open Source Chart Library column chart
1. Copy the Mpandroidchartlibrary-2-0-8.jar package to the Libs of the project
2. Defining XML files
3. The main Java logic code is as follows.
Packagecom.jackie.mpandoidbarchart;
Importjava.util.ArrayList;
Importcom.github.mikephil.charting.charts.BarChart;
Importcom.github.mikephil.charting.charts.LineChart;
Importcom.github.mikephil.charting.components.Legend;
Importcom.github.mikephil.charting.components.Legend.LegendForm;
Importcom.github.mikephil.charting.components.XAxis;
Importcom.github.mikephil.charting.components.XAxis.XAxisPosition;
Importcom.github.mikephil.charting.data.BarData;
Importcom.github.mikephil.charting.data.BarDataSet;
Importcom.github.mikephil.charting.data.BarEntry;
Import android.support.v7.app.ActionBarActivity;
Importandroid.graphics.Color;
Importandroid.os.Bundle;
public class Mainactivity extends actionbaractivity {privatebarchartmbarchart; privatebardatambardata; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);
Mbarchart = (barchart) Findviewbyid (R.id.spread_bar_chart);
Mbardata = Getbardata (4, 100); ShoWbarchart (Mbarchart, mbardata);
} private void Showbarchart (Barchartbarchart, Bardatabardata) {barchart.setdrawborders (false);////whether to add a border on a line chart Barchart.setdescription ("")///Data Description///If there is no data, this will be shown, similar to the ListView Emptyview barchart.setnodatatextdescription ("You
Need to provide data for the chart. "); Barchart.setdrawgridbackground (FALSE); Whether to display the table color Barchart.setgridbackgroundcolor (color.white& 0x70ffffff); The color of the table, here is to set a transparency barchart.settouchenabled (true) for the color; Sets whether you can touch barchart.setdragenabled (true);/Whether you can drag barchart.setscaleenabled (true), or whether you can scale barchart.setpinchzoom (
FALSE);///Barchart.setbackgroundcolor ();//Set Background Barchart.setdrawbarshadow (true); Barchart.setdata (BarData); Set data Legend mlegend = Barchart.getlegend (); Set scale chart to mark Mlegend.setform (legendform.circle);//Style mlegend.setformsize (6f);//font Mlegend.settextcolor (Color.Black)
;//color/x-axis set//Xaxisxaxis = Barchart.getxaxis ();
Xaxis.setposition (Xaxisposition.bottom); Barchart.animatex (2500); Execute the animation immediately, X axis} priVatebardatagetbardata (int count, float range) {arraylist<string>xvalues = new arraylist<string> (); for ( inti = 0; i< count;
i++) {Xvalues.add ("first" + (i + 1) + "quarter");}
Arraylist<barentry>yvalues = new arraylist<barentry> (); for (inti = 0; i< count; i++) {Float value = (float) (random number within math.random () * range/*100 * *) + 3; Yvalues.add (New Barent
Ry (value, i));
The data set of the//Y axis Bardatasetbardataset = new Bardataset (yvalues, "Test pie chart");
Bardataset.setcolor (Color.rgb (114, 188, 223));
Arraylist<bardataset>bardatasets = new arraylist<bardataset> (); Bardatasets.add (Bardataset);
Add the Datasets Bardatabardata = new BarData (xValues, bardatasets);
Returnbardata;
}} Packagecom.jackie.mpandoidbarchart;
Importjava.util.ArrayList;
Importcom.github.mikephil.charting.charts.BarChart;
Importcom.github.mikephil.charting.charts.LineChart;
Importcom.github.mikephil.charting.components.Legend;
Importcom.github.mikephil.charting.components.Legend.LegendForm; ImportCom.github.mikephil.charting.components.XAxis;
Importcom.github.mikephil.charting.components.XAxis.XAxisPosition;
Importcom.github.mikephil.charting.data.BarData;
Importcom.github.mikephil.charting.data.BarDataSet;
Importcom.github.mikephil.charting.data.BarEntry;
Import android.support.v7.app.ActionBarActivity;
Importandroid.graphics.Color;
Importandroid.os.Bundle;
public class Mainactivity extends actionbaractivity {privatebarchartmbarchart; privatebardatambardata; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);
Mbarchart = (barchart) Findviewbyid (R.id.spread_bar_chart);
Mbardata = Getbardata (4, 100);
Showbarchart (Mbarchart, mbardata);
} private void Showbarchart (Barchartbarchart, Bardatabardata) {barchart.setdrawborders (false);////whether to add a border on a line chart Barchart.setdescription ("")///Data Description///If there is no data, this will be shown, similar to the ListView Emptyview barchart.setnodatatextdescription ("You Need to provide data for the chart."); Barchart.setdrawgridbackground (FALSE); Whether to display the table color Barchart.setgridbackgroundcolor (color.white& 0x70ffffff); The color of the table, here is to set a transparency barchart.settouchenabled (true) for the color; Sets whether you can touch barchart.setdragenabled (true);/Whether you can drag barchart.setscaleenabled (true), or whether you can scale barchart.setpinchzoom (
FALSE);///Barchart.setbackgroundcolor ();//Set Background Barchart.setdrawbarshadow (true); Barchart.setdata (BarData); Set data Legend mlegend = Barchart.getlegend (); Set scale chart to mark Mlegend.setform (legendform.circle);//Style mlegend.setformsize (6f);//font Mlegend.settextcolor (Color.Black)
;//color/x-axis set//Xaxisxaxis = Barchart.getxaxis ();
Xaxis.setposition (Xaxisposition.bottom); Barchart.animatex (2500); Animation immediately executed, x-axis} privatebardatagetbardata (int count, float range) {arraylist<string>xvalues = new Arraylist<stri
Ng> ();
for (inti = 0; i< count; i++) {Xvalues.add ("first" + (i + 1) + "quarter");}
Arraylist<barentry>yvalues = new arraylist<barentry> (); for (inti = 0; i< count; i++) {floatValue = (float) (random number within math.random () * range/*100) + 3;
Yvalues.add (New Barentry (value, i));
The data set of the//Y axis Bardatasetbardataset = new Bardataset (yvalues, "Test pie chart");
Bardataset.setcolor (Color.rgb (114, 188, 223));
Arraylist<bardataset>bardatasets = new arraylist<bardataset> (); Bardatasets.add (Bardataset);
Add the Datasets Bardatabardata = new BarData (xValues, bardatasets);
Returnbardata; }
}
The effect chart is as follows:
The above is a small set of Mpandroidchart open source Chart library using the introduction of the pie chart, line chart and column chart of the relevant knowledge, hope to help.