For IOS, the simple code for creating a pie chart is annotated. for ios
Today we bring you the implementation of the pie chart in IOS graphics
. H file
# Import <UIKit/UIKit. h>
@ Interface ZXCircle: UIView
@ End
. M file
# Import "ZXCircle. h"
@ Implementation ZXCircle
-(Void) drawRect :( CGRect) rect {
CGFloat w = self. bounds. size. width;
CGFloat h = self. bounds. size. height;
// Data array
NSArray * array = @ [@ 1, @ 2, @ 3, @ 4, @ 5, @ 6, @ 7, @ 8, @ 9, @ 10];
// Color array
NSArray * colorArray = @ [[UIColor redColor], [UIColor greenColor], [UIColor yellowColor], [UIColor cyanColor], [UIColor blueColor], [UIColor lightGrayColor], [UIColor grayColor], [UIColor darkGrayColor], [UIColor magentaColor], [UIColor orangeColor];
// The following loop uses the color Algorithm in the case of only several colors but a large amount of data.
// New color array
NSMutableArray * colorArr = [NSMutableArray new];
// This loop shows only the first three colors in the color array. % The number following is based on the number of colors in your color array.
For (int I = 0; I <array. count; I ++ ){
[ColorArr addObject: colorArray [I % 10];
}
CGContextRef ctx = UIGraphicsGetCurrentContext ();
// Center point
CGPoint center = CGPointMake (w * 0.5, h * 0.5 );
// Radius
CGFloat radius = w * 0.3-5;
// Start point
CGFloat startA = 0;
// End point angle
CGFloat endA = M_PI;
// Scan the angle range
CGFloat angle = 0;
For (int I = 0; I <array. count; I ++ ){
// This sentence is used to draw one of them, and the second start point is the first end point. M_PI indicates that the value 180 degrees 55 is the sum of the data sources.
StartA = endA;
Angle = [array [I] integerValue]/55 * M_PI * 2;
EndA = startA + angle;
// Arc path
// Clockwise: whether it is rotated in the clock direction (clockwise or not)
UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter: center radius: radius startAngle: startA endAngle: endA clockwise: YES];
// Connection center, forming a fan
[Path addLineToPoint: center];
// Fill color
[(UIColor *) colorArr [I] set];
CGContextAddPath (ctx, path. CGPath );
// Render the context to the view
CGContextFillPath (ctx );
}
}
@ End
The following is a simple pie chart. I hope it will be helpful for your future development !! Thank you !!