For IOS, the simple code for creating a pie chart is annotated. for ios

Source: Internet
Author: User

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 !!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.