Recently in the work of the need to use the function of the pie chart, online check the Mpandroidchart Open Source Chart Library is a good thing, and downloaded the Mpandroidchart project run. So I wrote a simple example, using the Piechart (pie chart) method is as follows:
Source code Download Address: Https://github.com/PhilJay/MPAndroidChart
(i) Import it into the project (more methods are described on GitHub)
Here I am using Android Studio, just add the following code to the Build.grade file in module
Repositories {
maven {URL "Https://jitpack.io"}
}
dependencies {
compile ' Com.github.PhilJay: mpandroidchart:v2.2.5 '
}
After you add it, the compilation succeeds.
(ii) Add Piechart to the layout file you want to use
<com.github.mikephil.charting.charts.piechart
android:id= "@+id/piechart"
android:layout_width= " Match_parent "
android:layout_height=" match_parent ">
</ Com.github.mikephil.charting.charts.piechart>
(iii) Use of Piechart in code
private void Initchart () {//sets whether the pie chart receives a click event, and the default is True piechart.settouchenabled (true);
Sets whether the pie chart uses percent Piechart.setusepercentvalues (true);
Set the text description piechart.setdescription ("Test") in the lower right corner of the pie chart;
Sets the text size in the lower-right corner of the pie chart piechart.setdescriptiontextsize (16);
Whether to display the disc intermediate text, the default display Piechart.setdrawcentertext (TRUE);
Set the disc intermediate text Piechart.setcentertext ("I am in the Middle");
Sets the size of the disc intermediate text piechart.setcentertextsize (20);
Sets the color Piechart.setcentertextcolor (color.white) of the middle text of the disc;
Sets the font piechart.setcentertexttypeface (typeface.default) for the middle text of the disc;
Sets the color Piechart.setholecolor (color.green) of the intermediate disc;
Sets the radius of the intermediate disk, the value of which is the percentage of the pie chart Piechart.setholeradius (20);
Sets the radius of the middle transparent circle, and the value is the percentage of the pie chart Piechart.settransparentcircleradius (40);
Whether to display an empty space in the middle of the pie chart, piechart.setdrawholeenabled (true) is displayed by default;
Sets whether the disk rotates, the default rotation piechart.setrotationenabled (true); //Set initial rotation angle piechart.setrotationangle (0);
Set scale graph Legend mlegend = Piechart.getlegend ();
Sets the scale graph to show where in the pie chart mlegend.setposition (Legend.LegendPosition.RIGHT_OF_CHART);
Set the shape of the scale chart, which is square by default, and can be square, round, linear mlegend.setform (Legend.LegendForm.CIRCLE);
Mlegend.setxentryspace (7f);
Mlegend.setyentryspace (5f);
Set X-axis animation Piechart.animatex (1800);
Set y-axis animation//Piechart.animatey (1800);
Set the XY axis together with the animation//piechart.animatexy (1800, 1800);
Binding Data Binddata (3);
Set a selected area to listen Piechart.setonchartvalueselectedlistener (new Onchartvalueselectedlistener () {@Override public void onvalueselected (Entry e, int datasetindex, Highlight h) {Toast.maketext (mainactiv
ity.this,datasetindex+ "" +e.tostring (), Toast.length_short). Show (); } @Override public void onnothingselected () {}}); }/** * * @param count is divided into several parts */private void Binddata (int count) {/** * namelist Used to represent the text on each piece of pie. * For example: Part One, Part two, part three */arraylist<string> namelist = new arraylist<string>
();
for (int i = 0; i < count; i++) {Namelist.add ("part" + (i + 1)); }/** * ValueList divides a pie chart into three parts, the value of the percentage of each region * Entry constructor * The first value represents the percentage, the second value represents the region bit * (can have a third parameter, which represents the data carrying the object) is useless here. */arraylist<entry> valueList = new ARRAYLIST<ENTRY&G
t; ();
Valuelist.add (New Entry (20, 0));
Valuelist.add (New Entry (30, 1));
Valuelist.add (New Entry (50, 2));
Display on scale chart Piedataset dataSet = new Piedataset (valueList, "meaning of different colors");
Sets the distance between a pie chart dataset.setslicespace (3f);
Some areas are selected when the extra length of the Dataset.setselectionshift (5f); Set the color of each area of the pie chart arraylist<integer> colors = newArraylist<integer> ();
Colors.add (color.red);
Colors.add (Color.green);
Colors.add (Color.Blue);
Dataset.setcolors (colors);
Piedata data = new Piedata (NameList, DataSet);
Set to show Data.setvalueformatter (New Percentformatter ()) as a percentage;
The size of the area text data.setvaluetextsize (11f);
Sets the color of the area text Data.setvaluetextcolor (color.white);
Sets the font data.setvaluetypeface (Typeface.default) for the area text;
Piechart.setdata (data);
Sets whether to display area text content Piechart.setdrawslicetext (piechart.isdrawslicetextenabled ()); Sets whether to display the value of the zone percentage for (idataset<?> set:pieChart.getData (). Getdatasets ()) {set.setdrawvalues (!set
. isdrawvaluesenabled ());
}//Undo all highlights Piechart.highlightvalues (null);
Piechart.invalidate (); }
Column chart using:
private void Initbarchart () {//sets whether the rectangle shadow displays Barchart.setdrawbarshadow (false);
Sets whether the value displays Barchart.setdrawvalueabovebar (true) above the rectangle;
Set the lower right corner to describe the barchart.setdescription ("test");
Show Barchart.setnodatatext when no data is used ("no data"); If more than entries is displayed in the chart, no values would be//drawn Barchart.setmaxvisibleva
Luecount (60);
Sets whether barchart.settouchenabled can be touched (true);
Whether you can drag barchart.setdragenabled (true);
Whether barchart.setscaleenabled can be scaled (true);
Set two-finger scaling barchart.setpinchzoom (false);
Sets whether the table color is displayed and the gap between the rectangles is barchart.setdrawgridbackground (false);
Set the color of the table, the gap between the rectangles color barchart.setgridbackgroundcolor (color.gray);
Set the scale display Legend L = barchart.getlegend ();
Sets the scale at which position l.setposition (Legend.LegendPosition.BELOW_CHART_LEFT) is displayed in the column chart; Set scale display shape, square, round, linear l.sEtform (Legend.LegendForm.SQUARE);
Sets the size of the scale display shape l.setformsize (15f);
Sets the size of the scale display text l.settextsize (15f);
L.setxentryspace (4f);
Set properties on X-axis Xaxis Xaxis = Barchart.getxaxis ();
The setting label is displayed above or below the column chart xaxis.setposition (XAxis.XAxisPosition.TOP);
Xaxis.settypeface (Typeface.default);
Sets whether to draw table Xaxis.setdrawgridlines (false);
Set the X-label Gap xaxis.setspacebetweenlabels (2);
Sets the property on the left y-axis of the column chart YAxis leftaxis = Barchart.getaxisleft ();
Leftaxis.settypeface (Typeface.default);
Sets the number of labels on the y-axis, and a Boolean value of TRUE indicates that 8 Leftaxis.setlabelcount (8, false) must be required;
Leftaxis.setvalueformatter (new Defaultyaxisvalueformatter (0));
Sets the position of the label in the column chart leftaxis.setposition (YAxis.YAxisLabelPosition.OUTSIDE_CHART);
Sets the spacing between the y-axis labels leftaxis.setspacetop (15f); Leftaxis.setaxisminvalue (0f); This replaces Setstartatzero (TRUE)//sets the genus on the right y-axis of the column chartThe same as above yaxis Rightaxis = Barchart.getaxisright ();
Rightaxis.setdrawgridlines (FALSE);
Rightaxis.settypeface (Typeface.default);
Rightaxis.setlabelcount (5, true);
Rightaxis.setvalueformatter (new Defaultyaxisvalueformatter (0));
Rightaxis.setspacetop (15f); Rightaxis.setaxisminvalue (0f);
This replaces Setstartatzero (true)//hides the right axis//Barchart.getaxisright (). setenabled (false);
Hide the Left axis (IBID.)//Barchart.getaxisleft (). setenabled (false);
SetData (15); }
/** * Bind data * @param number of labels on the count x axis */private void SetData (int count) {//Set label data on x-axis direction a
rraylist<string> xvals = new arraylist<string> ();
for (int i = 0; i < count; i++) {Xvals.add (i+ "");
}//sets the value of each rectangle on the y-axis arraylist<barentry> yVals1 = new arraylist<barentry> ();
for (int i = 0; i < count; i++) {Yvals1.add (new Barentry (20*i, i));
}//The first parameter is a collection of values for each rectangle in the y-axis direction, and the second parameter is proportional to the text description bardataset Set1 = new Bardataset (YVALS1, "different colors represent a different value");
Set the spacing between rectangles, the parameter is a percentage, you can control the width of the rectangle set1.setbarspacepercent (10f);
Sets the color of the rectangle int colors[]={0xffff0000,0xff00ff00,0xff0000ff};
Set1.setcolors (colors);
arraylist<ibardataset> dataSets = new arraylist<ibardataset> ();
Datasets.add (SET1);
Set the data for the column chart bardata to new BarData (Xvals, dataSets);
Data.setvaluetextsize (10f); Data.setvaLuetypeface (Typeface.default);
Barchart.setdata (data); }
Column chart More property settings view address http://www.ithao123.cn/content-10519924.html