Let's take a look at the effect chart.
First analysis of the composition of pie chart, it is obvious that pie chart is one after another fan-shaped, each sector has a different color, corresponding to the name, data and percentages.
The above information can be derived from the pie chart of the most basic data should include: first-name data value percentage of the corresponding angle color.
User-cared data: name data value percent
Data to be calculated by program: percent-corresponding angle
Where the color of this item can be defined by the user
public class Piedata {
private String name; Name
private float value; Value
Private float percentage//percent
private int color = 0; Color
private float angle = 0;//Angle Public
piedata (@NonNull String name, @NonNull float value) {
this.name = name;
This.value = value;
}
}
Custom View:
First comb through the custom view process (determine what each step should do):
Steps |
Key words |
Role |
1 |
Constructors |
Initialization (initialization of brush paint) |
2 |
Onmeasure |
Measure the size of the view (don't care for the moment) |
3 |
Onsizechanged |
Determine the view size (record the width of the current view) |
4 |
Nlayout |
Determine the child view layout (No child view, no care) |
5 |
OnDraw |
Actually draw the content (draw a pie chart) |
6 |
Provide interface |
Provides an interface (provides an interface for setting data)
|
The code is as follows:
public class Pieview extends View {//color table private int[] Mcolors = {0xffccff00, 0xff6495ed, 0xffe32636, 0xff800000,
0xff808000, 0xffff8c69, 0xff808080, 0xffe6b800, 0xff7cfc00};
Pie chart Initial drawing angle private float mstartangle = 0;
Data private arraylist<piedata> Mdata;
Wide high private int mwidth, mheight;
Brush private Paint Mpaint = new Paint ();
Public Pieview {This (context, NULL);
Public Pieview (context, AttributeSet attrs) {Super (context, attrs);
Mpaint.setstyle (Paint.Style.FILL);
Mpaint.setantialias (TRUE); @Override protected void onsizechanged (int w, int h, int oldw, int oldh) {super.onsizechanged (W, H, OLDW, OLDH
);
Mwidth = W;
Mheight = h;
} @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas);
if (null = = Mdata) return; float currentstartangle = Mstartangle; Current Starting angle canvas.translate (MWIDTH/2, MHEIGHT/2); Move the canvas coordinate origin to the center position float R =(float) (Math.min (Mwidth, Mheight)/2 * 0.8); Pie chart radius RECTF rect = new RECTF (-R, R, R., R);
Pie plot area for (int i = 0; i < mdata.size (); i++) {Piedata pie = mdata.get (i);
Mpaint.setcolor (Pie.getcolor ());
Canvas.drawarc (Rect, Currentstartangle, Pie.getangle (), True, Mpaint);
Currentstartangle + + pie.getangle ();
}//Set starting angle public void setstartangle (int mstartangle) {this.mstartangle = Mstartangle; Invalidate ();
Refresh}//Set data public void SetData (arraylist<piedata> mdata) {this.mdata = Mdata;
Initdate (Mdata); Invalidate (); Refresh}//Initialize data private void Initdate (arraylist<piedata> mdata) {if (null = Mdata | | mdata.size () = 0
///data problem directly return;
float Sumvalue = 0;
for (int i = 0; i < mdata.size (); i++) {Piedata pie = mdata.get (i); Sumvalue + + pie.getvalue (); Calculates the value and int j = i% Mcolors.length;
Set color Pie.setcolor (Mcolors[j]); } float SumangLe = 0;
for (int i = 0; i < mdata.size (); i++) {Piedata pie = mdata.get (i); float percentage = Pie.getvalue ()/sumvalue; Percent float angle = percentage * 360; The corresponding angle pie.setpercentage (percentage); Record percentage pie.setangle (angle);
Record angle size sumangle + = angle;
LOG.I ("Angle", "" "+ Pie.getangle ()); }
}
Note: When you change the data, you need to redraw the interface to invoke the invalidate () function to redraw.
These are small series for everyone to tidy up the Android (Android) custom pie chart production ideas, the use of instance code to achieve pie chart is very simple, I hope this article for everyone to develop Android (Android) help.