123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- //
- // PieView.m
- // 饼状图
- //
- // Created by mac on 14-3-31.
- // Copyright (c) 2014年 mac. All rights reserved.
- //
- #import "PieView.h"
- @implementation PieView
- @synthesize delegate;
- @synthesize datasource;
- //初始化
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- self.backgroundColor = [UIColor clearColor];
- self.layer.shadowColor = [[UIColor grayColor] CGColor];
- self.layer.shadowOffset = CGSizeMake(3, 3);
- self.layer.shadowRadius = 1.5;
- self.layer.shadowOpacity = 0.9;
- }
- return self;
- }
- //加载视图
- -(void)reloadData
- {
- [self setNeedsDisplay];
- }
- //绘制
- - (void)drawRect:(CGRect)rect
- {
- //prepare
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGFloat theHalf = rect.size.width/2;
- CGFloat lineWidth = theHalf;
- if ([self.delegate respondsToSelector:@selector(centerCircleRadius)])
- {
- lineWidth -= [self.delegate centerCircleRadius];
- NSAssert(lineWidth <= theHalf, @"wrong circle radius");
- }
- CGFloat radius = theHalf-lineWidth/2;
-
- CGFloat centerX = theHalf;
- CGFloat centerY = rect.size.height/2;
-
- //drawing
-
- double sum = 0.0f;
- int slicesCount = [self.datasource numberOfSlicesInPieChartView:self];
-
- for (int i = 0; i < slicesCount; i++)
- {
- sum += [self.datasource pieChartView:self valueForSliceAtIndex:i];
- }
-
- float startAngle = - M_PI_2;
- float endAngle = 0.0f;
-
- for (int i = 0; i < slicesCount; i++)
- {
- double value = [self.datasource pieChartView:self valueForSliceAtIndex:i];
-
- endAngle = startAngle + M_PI*2*value/sum;
- CGContextAddArc(context, centerX, centerY, radius, startAngle, endAngle, false);
-
- UIColor *drawColor = [self.datasource pieChartView:self colorForSliceAtIndex:i];
-
- CGContextSetStrokeColorWithColor(context, drawColor.CGColor);
- CGContextSetLineWidth(context, lineWidth);
- CGContextStrokePath(context);
- startAngle += M_PI*2*value/sum;
- }
- }
- @end
|