私はiphoneアプリで円グラフを作成しています。配列から4つの値を取得し、円グラフを作成します。円グラフは別々の色で塗りつぶされています。また、1が15%、15%、35%のように、各部分のパーセントを表示する必要があります。 35%このように、私は円グラフを作成するために次のコードを使用しています。
以下はPieクラスの実装です
- (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];
}