如何修复饼图中的错误在iPhone应用程序中显示空白空间切片

时间:2012-06-01 07:16:02

标签: iphone xcode

我正在使用以下代码创建饼图:

Pie Class

 @interface PieClass : UIView {

NSArray* itemArray;
NSArray* myColorArray; 
int radius;


 }

 @property(nonatomic,retain)NSArray* itemArray;
 @property(nonatomic,retain)NSArray* myColorArray;
 @property(nonatomic,assign) int radius;


@end

Pie Class Implementation

 - (id)initWithFrame:(CGRect)frame
 {
self = [super initWithFrame:frame];
if (self) {
    // Initialization code

}
return self;
}



- (void)drawRect:(CGRect)rect
{


int c=[itemArray count];

CGFloat angleArray[c];
CGFloat offset;
int sum=0;

CGContextRef context = UIGraphicsGetCurrentContext();


CGContextSetAllowsAntialiasing(context, false);
CGContextSetShouldAntialias(context, false);



for(int i=0;i<[itemArray count];i++)

{


    sum+=[[itemArray objectAtIndex:i] intValue];


}


for(int i=0;i<[itemArray count];i++)
{

    angleArray[i]=(float)(([[itemArray objectAtIndex:i] intValue])/(float)sum)*(2*3.14); // in radians
    CGContextMoveToPoint(context, radius, radius);
    if(i==0)
        CGContextAddArc(context, radius, radius, radius, 0,angleArray[i], 0);
    else
        CGContextAddArc(context, radius, radius, radius,offset,offset+angleArray[i], 0);
    offset+=angleArray[i];


    CGContextSetFillColorWithColor(context, ((UIColor *)[myColorArray objectAtIndex:i]).CGColor);
    CGContextClosePath(context); 
    CGContextFillPath(context);

    }
       }

   - (void)dealloc
     {

[super dealloc];

    }

  @end




-(void)createGraph{
    PieClass *myPieClass=[[PieClass alloc]initWithFrame:CGRectMake(25,460,230,200)];
    myPieClass.backgroundColor=[UIColor clearColor];

    int one=[valueOne  intValue];
    int two=[valueTwo intValue];
    int three=[valueThree intValue];
    int four=[valueFour intValue]; 

    int total=one+two+three+four;

    float oneValue=(float)one/(float)total*100;
    float twoValue=(float)two/(float)total*100;
    float threeValue=(float)three/(float)total*100;
    float fourValue=(float)four/(float)total*100;
    float floatTotal=oneValue+twoValue+threeValue+fourValue;

    NSString *value1=[NSString stringWithFormat:@"%f",oneValue];
    NSString *value2=[NSString stringWithFormat:@"%f",twoValue];
    NSString *value3=[NSString stringWithFormat:@"%f",threeValue];
    NSString *value4=[NSString stringWithFormat:@"%f",fourValue];

    myPieClass.itemArray=[[NSArray alloc]initWithObjects:valueOne,valueTwo,valueThree,valueFour, nil];
    myPieClass.myColorArray=[[NSArray alloc]initWithObjects:[UIColor colorWithRed:(128/255.0) green:(100/255.0) blue:(162/255.0) alpha:1],[UIColor colorWithRed:(155/255.0) green:(187/255.0) blue:(89/255.0) alpha:1],[UIColor colorWithRed:(192/255.0) green:(80/255.0) blue:(77/255.0) alpha:1],[UIColor colorWithRed:(79/255.0) green:(129/255.0) blue:(189/255.0) alpha:1], nil];
    myPieClass.radius=100;

    [self.view addSubview:myPieClass];
}

它工作正常,但有时候每个切片部分显示空的灰色空间。我想删除它。下面是显示饼图的附图。

Image showing pie chart

0 个答案:

没有答案