Iphone画饼图工具类
项目中需要画饼图,在此将工具类添出来:
h文件:
#import <UIKit/UIKit.h> #import <QuartzCore/QuartzCore.h> @interface CLMView : UIView { float spaceHeight; //高度 float scaleY ; NSArray *titleArr ; //文字 NSArray *valueArr; //值 NSArray *colorArr; //颜色 } @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; @endm文件:
#import "CLMView.h" #define K_PI 3.1415 #define KDGREED(x) ((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; scaleY = 0.4; } return self; } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); //cgcontextsets //抗锯齿 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; //饼图 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); //绘制上面的扇形 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); //绘制侧面 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) { //绘制厚度 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); } //只有弧度《 3.14 的才会画前面的厚度 else if(startAngle < K_PI) { endAngle = K_PI; endx = 10; endy1 = 230+spaceHeight; //绘制厚度 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); } //整体渐变 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); //绘制文字 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调用:(主要是设置cv的titleArr,valueArr,colorArr)
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:235.0f/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:140.0f/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 = bilv*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];
本文由用户 fmms 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!