我在iphone应用程序中创建一个饼图,它从数组中获取4个值,然后创建饼图,它显示填充了单独颜色的饼图我还希望它必须显示每个部分的百分比,如果1是15%然后15%像这样35%和35%我使用以下代码创建饼图。
下面是Pie Class实现
- (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)createGraph{
PieClass *myPieClass=[[PieClass alloc]initWithFrame:CGRectMake(10,440,230,200)];
myPieClass.backgroundColor=[UIColor clearColor];
myPieClass.itemArray=[[NSArray alloc]initWithObjects:valueOne,valueTwo,valueThree,valueFour, nil];
myPieClass.myColorArray=[[NSArray alloc]initWithObjects:[UIColor blueColor],[UIColor redColor],[UIColor greenColor],[UIColor brownColor], nil];
myPieClass.radius=100;
[self.view addSubview:myPieClass];
}