Iphone pie chart tools

Source: Internet
Author: User

You need to draw a pie chart for the project, and add the tool class here:
H file:
[Cpp]

# Import <UIKit/UIKit. h>
# Import <QuartzCore/QuartzCore. h>
 
 
@ Interface CLMView: UIView
{
 
Float spaceHeight; // height
Float scaleY;
NSArray * titleArr; // text
NSArray * valueArr; // Value
NSArray * colorArr; // color
}
 
@ Property (nonatomic, assign) float spaceHeight;
@ Property (nonatomic, assign) float scaleY;
@ Property (nonatomic, retain) NSArray * titleArr;
@ Property (nonatomic, retain) NSArray * valueArr;
@ Property (nonatomic, retain) NSArray * colorArr;
 
@ End

M file:
[Cpp]
# Import "CLMView. h"
 
 
 
# Deprecision K_PI 3.1415
# Define KDGREED (x) * K_PI * 2)
 
 
@ Implementation CLMView
@ Synthesize spaceHeight, scaleY;
@ Synthesize titleArr, valueArr, colorArr;
 
 
-(Id) initWithFrame :( CGRect) frame {
Self = [super initWithFrame: frame];
If (self ){
// Initialization code
Self. backgroundColor = [UIColor colorWithRed: 240.0f/255.0f green: 1 blue: 1 alpha: 1.0];

SpaceHeight = 40;
Scale= 0.4;


}
Return self;
}
 
 
-(Void) drawRect :( CGRect) rect
{
CGContextRef context = UIGraphicsGetCurrentContext ();
// Cgcontextsets

// Anti-aliasing
CGContextSetAllowsAntialiasing (context, TRUE );

// Int arr [5] = {20, 15, 35, 85, 60 };

Float sum = 0;


For (int j = 0; j <[valueArr count]; j ++)
{
Sum + = [[valueArr objectAtIndex: j] floatValue];
}


CGContextMoveToPoint (context, 160,230 );

Float currentangel = 0;

// Pie chart
CGContextSaveGState (context );
CGContextScaleCTM (context, 1.0, scaleY );
 
Currentangel = 0;
For (int I = 0; I <[valueArr count]; I ++)
{
 
Float startAngle = KDGREED (currentangel );

Currentangel + = [[valueArr objectAtIndex: I] floatValue]/sum;
Float endAngle = KDGREED (currentangel );
// Draw the slice above
CGContextMoveToPoint (context, 160,230 );

[[ColorArr objectAtIndex: I % [valueArr count] setFill];

[[UIColor colorWithWhite: 1.0 alpha: 0.8] setStroke];

CGContextAddArc (context, 160,230,150, startAngle, endAngle, 0 );

CGContextClosePath (context );
CGContextDrawPath (context, kCGPathFill );



// Draw the side
Float starx = cos (startAngle) × 150 + 160;
Float stary = sin (startAngle) × 150 + 230;

Float endx = cos (endAngle) * 150 + 160;
Float endy = sin (endAngle) * 150 + 230;

// Float starty1 = stary + spaceHeight;
Float endy1 = endy + spaceHeight;


If (endAngle <K_PI)
{

// Draw the thickness
CGMutablePathRef path = CGPathCreateMutable ();
CGPathMoveToPoint (path, nil, starx, stary );
CGPathAddArc (path, nil, 160,230,150, startAngle, endAngle, 0 );
CGPathAddLineToPoint (path, nil, endx, endy1 );

CGPathAddArc (path, nil, 160,230 + spaceHeight, 150, endAngle, startAngle, 1 );
CGContextAddPath (context, path );

[[ColorArr objectAtIndex: I % [valueArr count] setFill];
[[UIColor colorWithWhite: 0.9 alpha: 1.0] setStroke];

CGContextDrawPath (context, kCGPathFill );

[[UIColor colorWithWhite: 0.1 alpha: 0.4] setFill];
CGContextAddPath (context, path );
CGContextDrawPath (context, kCGPathFill );
}

// Only a radian of 3.14 can draw the thickness of the front.
Else if (startAngle <K_PI)
{
EndAngle = K_PI;
Endx = 10;
Endy1 = 230 + spaceHeight;


// Draw thickness www.2cto.com
CGMutablePathRef path = CGPathCreateMutable ();
CGPathMoveToPoint (path, nil, starx, stary );
CGPathAddArc (path, nil, 160,230,150, startAngle, endAngle, 0 );
CGPathAddLineToPoint (path, nil, endx, endy1 );

CGPathAddArc (path, nil, 160,230 + spaceHeight, 150, endAngle, startAngle, 1 );
CGContextAddPath (context, path );

[[ColorArr objectAtIndex: I % [valueArr count] setFill];
[[UIColor colorWithWhite: 0.9 alpha: 1.0] setStroke];

CGContextDrawPath (context, kCGPathFill );

[[UIColor colorWithWhite: 0.1 alpha: 0.4] setFill];
CGContextAddPath (context, path );
CGContextDrawPath (context, kCGPathFill );
}

Else
// Break
;


// CGContextSetBlendMode (context, kCGBlendModeMultiply );


 
 


 
}



// Overall gradient
CGFloat componets [] = {0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.1 };

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB ();

CGGradientRef gradient = CGGradientCreateWithColorComponents (colorspace, componets, nil, 2 );

CGContextDrawRadialGradient (context, gradient, CGPointMake (160,230), 0, CGPointMake (160,230), 150, 0 );

CFRelease (colorspace );
CGGradientRelease (gradient );


CGContextRestoreGState (context );

// Draw text

For (int I = 0; I <[valueArr count]; I ++)
{
Float origionx = 50;
Float origiony = I * 30 + 200;

[[ColorArr objectAtIndex: I % [valueArr count] setFill];

CGContextFillRect (context, CGRectMake (origionx, origiony, 20, 20 ));
CGContextDrawPath (context, kCGPathFill );


If (I <[titleArr count])
{
NSString * title = [titleArr objectAtIndex: I];
[Title drawAtPoint: CGPointMake (origionx + 50, origiony) withFont: [UIFont systemFontOfSize: 16];
}

}

}
 
 
-(Void) dealloc {
[TitleArr release];
[ValueArr release];
[ColorArr release];
[Super dealloc];
}
 
 
@ End

Call: (mainly set titleArr, valueArr, colorArr)
[Cpp]
NSMutableArray * title = [[NSMutableArray alloc] init];
NSMutableArray * value = [[NSMutableArray alloc] init];
NSMutableArray * color = [[NSMutableArray alloc] init];
NSArray * Allcolor = [NSArray arrayWithObjects: [UIColor yellowColor], [UIColor blueColor], [UIColor redColor], [UIColor brownColor], [UIColor purpleColor], [UIColor orangeColor], [UIColor greenColor], [UIColor grayColor],
[UIColor colorWithRed: 135.0f/255.0f green: 206.0f/255.0f blue: 235366f/255.0f alpha: 1],
[UIColor colorWithRed: 240.0f/255.0f green: 1 blue: 1 alpha: 1],
[UIColor colorWithRed: 1 green: 0 blue: 1 alpha: 1],
[UIColor colorWithRed: 0 green: 199.0f/255.0f blue: 140366f/255.0f alpha: 1],
[UIColor colorWithRed: 160.0f/255.0f green: 32.0f/255.0f blue: 240.0f/255.0f alpha: 1],
[UIColor colorWithRed: 124.0f/255.0f green: 252.0f/255.0f blue: 0 alpha: 1],
Nil];

For (int I = 0; I <[self. onetableDatacostchat count]; I ++ ){
Float bilv = [[self. onetableDatacostchat objectAtIndex: I] floatValue]/[self. zongcost floatValue];
Float lastbilv = billv * 100;

[Title addObject: [NSString stringWithFormat: @ "% @ % f % @", [self. onetableDatawenzichat objectAtIndex: I], lastbilv, @ "%"];


[Value addObject: [NSNumber numberWithInt: lastbilv];
[Color addObject: [Allcolor objectAtIndex: I];
}

CLMView * cv = [[CLMView alloc] initWithFrame: CGRectMake (0, 0,320,440)];

Cv. titleArr = title;
Cv. valueArr = value;
Cv. colorArr = color;

[Self. view addSubview: cv];
[Cv release];
[Title release];
[Value release];
[Color release];


From RiverAM's column

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.