Iphone畫餅圖工具類詳解

來源:互聯網
上載者:User

項目中需要畫餅圖,在此將工具類添出來:
h檔案:
[cpp]

#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; 
 
@end 

m檔案:
[cpp]
#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; 
             
             
            //繪製厚度 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); 
         
         
 
 
             
         
 
    } 
     
     
     
    //整體漸層 
    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)
[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: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]; 


摘自 RiverAM的專欄

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.