For a simple pie chart, even with the above animations, Core Plot is not required. If you only need a Core Plot solution, please ignore it. However, here is a simple solution that does not require external packages or even any frameworks.
Create a UIView called PieChartView. In PieChartView.h:
Then implementation in PieChartView.m:
#import "PieChartView.h" @implementation PieChartView @synthesize zeroAngle; @synthesize valueArray, colorArray; - (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { } return self; } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); // DETERMINE NUMBER OF WEDGES OF PIE int wedges = [valueArray count]; if (wedges > [colorArray count]) { NSLog(@"INSUFFICENT COLORS FOR PIE CHART: Please add %d additional colors.",wedges-[colorArray count]); for (int i=[colorArray count] ; i<wedges ; ++i) { [colorArray addObject:[UIColor whiteColor]]; } } // DETERMINE TOTAL VOLUME OF PIE float sum = 0.0; for (int i=0; i<wedges ; ++i) { sum += [[valueArray objectAtIndex:i] floatValue]; } float frac = 2.0*M_PI/sum; // DETERMINE CENTER AND MAXIMUM RADIUS OF INSCRIBED CIRCLE int center_x = rect.size.width/2.0; int center_y = rect.size.height/2.0; int radius = (center_x > center_y ? center_x : center_y); // DRAW WEDGES CLOCKWISE FROM POSITIVE X-AXIS float startAngle=zeroAngle; float endAngle=zeroAngle; for (int i=0; i<wedges; ++i) { startAngle = endAngle; endAngle += [[valueArray objectAtIndex:i] floatValue]*frac; [[colorArray objectAtIndex:i] setFill]; CGContextMoveToPoint(context, center_x, center_y); CGContextAddArc(context, center_x, center_y, radius, startAngle, endAngle, 0); CGContextClosePath(context); CGContextFillPath(context); } } - (void)dealloc { [valueArray release]; [colorArray release]; [super dealloc]; } @end
Now you can bind the float zeroAngle to the accelerometer in any way that you want to achieve the desired animation.
Pengone
source share