/
RipPieChartView.m
98 lines (70 loc) · 2.01 KB
/
RipPieChartView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// RipPieChartView.m
// RipPieChart
//
// Created by Slava Karpenko on 11/23/08.
// Copyright 2008 Ripdev. All rights reserved.
//
#import "RipPieChartView.h"
@implementation RipPieChartSlice
@synthesize percent;
@synthesize color;
+ (RipPieChartSlice*)sliceWithPercent:(double)p color:(UIColor*)c
{
RipPieChartSlice* slice = [[RipPieChartSlice alloc] init];
slice.percent = p;
slice.color = c;
return [slice autorelease];
}
@end
@implementation RipPieChartView
@synthesize slices;
- (void)awakeFromNib
{
if (!self.slices)
self.slices = [NSMutableArray arrayWithCapacity:0];
}
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
self.slices = [NSMutableArray arrayWithCapacity:0];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
// init vars
CGPoint center;
CGFloat currentDegrees = 0.0;
center.x = rect.origin.x + (rect.size.width / 2.);
center.y = rect.origin.y + (rect.size.height / 2.);
for (RipPieChartSlice* slice in self.slices)
{
CGMutablePathRef path = CGPathCreateMutable();
CGFloat sliceDegrees = (M_PI * 2) - (slice.percent * (M_PI * 2));
CGPathAddArc(path, NULL, center.x, center.y, rect.size.height/2, currentDegrees, (currentDegrees + sliceDegrees), YES);
CGPathAddLineToPoint(path, NULL, center.x, center.y);
CGPathCloseSubpath(path);
currentDegrees += sliceDegrees;
CGContextSaveGState(ctx);
CGContextAddPath(ctx, path);
CGContextSetFillColorWithColor(ctx, slice.color.CGColor);
CGContextFillPath(ctx);
CGContextRestoreGState(ctx);
CGPathRelease(path);
}
// Draw a nice adornment
if (!overlayImage)
overlayImage = [[UIImage imageWithContentsOfFile:[[NSBundle bundleForClass:[self class]] pathForResource:@"RipPieChartOverlay" ofType:@"png"]] retain];
if (overlayImage)
{
CGContextDrawImage(ctx, rect, overlayImage.CGImage);
}
}
- (void)dealloc {
self.slices = nil;
[overlayImage release];
[super dealloc];
}
@end