Skip to content

Instantly share code, notes, and snippets.

@kleneway
Created August 7, 2013 06:14
Show Gist options
  • Save kleneway/6171650 to your computer and use it in GitHub Desktop.
Save kleneway/6171650 to your computer and use it in GitHub Desktop.
Example of drawing a pie chart slice from Haiku Deck using Core Graphics
- (UIBezierPath*)slicePathForDataPoint:(DataPoint*)dataPoint forCompleted:(float)completed forTotal:(float)total {
if(!total) {
return nil;
}
float percentOfTotal = (completed+fabsf([dataPoint.number floatValue]))/total;
float percentCompleted = completed/total;
percentOfTotal *= self.animationScaleFactor;
percentCompleted *= self.animationScaleFactor;
UIBezierPath *slicePath = [UIBezierPath bezierPathWithArcCenter:self.centerPoint
radius:self.radius
startAngle:self.startingAngle+DEGREES_TO_RADIANS(percentCompleted*360)
endAngle:self.startingAngle+DEGREES_TO_RADIANS(percentOfTotal*360)
clockwise:YES];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:self.centerPoint];
[path appendPath:slicePath];
[path addLineToPoint:self.centerPoint];
return path;
}
- (void)drawSliceWithDataPoint:(DataPoint*)dataPoint forCompleted:(float)completed forTotal:(float)total {
if(!total)
return;
int colorIdx = [dataPoint.position intValue]%self.colors.count;
UIColor *color;
if(colorIdx<self.colors.count) {
if(self.highlightedIdx > -1 && self.highlightedIdx != colorIdx) {
color = [UIColor grayColor];
} else {
color = [self.colors objectAtIndex:colorIdx];
}
}
[color setFill];
UIBezierPath *path = [self slicePathForDataPoint:dataPoint forCompleted:completed forTotal:total];
[path fillWithBlendMode: kCGBlendModeNormal alpha:1.0f];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment