ios 用程序绘制饼图的demo(4)
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)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;
相关新闻>>
- 发表评论
-
- 最新评论 更多>>