add PCpieChart

This commit is contained in:
2014-08-15 09:25:42 +08:00
parent e03ff53116
commit 79db7250f2
5 changed files with 642 additions and 31 deletions
+26
View File
@@ -8,6 +8,8 @@
/* Begin PBXBuildFile section */
04C541E9AB294DF5BE18D1BB /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A671F2F14AEE4277BE5F0824 /* libPods.a */; };
A90BEBF5199D978100230242 /* PCPieChart.m in Sources */ = {isa = PBXBuildFile; fileRef = A90BEBF4199D978100230242 /* PCPieChart.m */; };
A90BEBF8199D983600230242 /* sample_piechart_data.plist in Resources */ = {isa = PBXBuildFile; fileRef = A90BEBF7199D983600230242 /* sample_piechart_data.plist */; };
A9543E3F1963ACB2006CC216 /* MessageViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A9543E3E1963ACB2006CC216 /* MessageViewController.m */; };
A958AF9B196E42690029EE41 /* fish_ku@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = A958AF9A196E42690029EE41 /* fish_ku@2x.png */; };
A958AFC0196E5F9C0029EE41 /* fish_heng.gif in Resources */ = {isa = PBXBuildFile; fileRef = A958AFBF196E5F9C0029EE41 /* fish_heng.gif */; };
@@ -49,6 +51,9 @@
/* Begin PBXFileReference section */
A66ECFF2A9974F1F991AD71C /* Pods.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.xcconfig; path = Pods/Pods.xcconfig; sourceTree = "<group>"; };
A671F2F14AEE4277BE5F0824 /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; };
A90BEBF3199D978100230242 /* PCPieChart.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PCPieChart.h; sourceTree = "<group>"; };
A90BEBF4199D978100230242 /* PCPieChart.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PCPieChart.m; sourceTree = "<group>"; };
A90BEBF7199D983600230242 /* sample_piechart_data.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = sample_piechart_data.plist; sourceTree = "<group>"; };
A9543E3D1963ACB2006CC216 /* MessageViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MessageViewController.h; sourceTree = "<group>"; };
A9543E3E1963ACB2006CC216 /* MessageViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MessageViewController.m; sourceTree = "<group>"; };
A958AF9A196E42690029EE41 /* fish_ku@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "fish_ku@2x.png"; sourceTree = "<group>"; };
@@ -112,9 +117,28 @@
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
A90BEBF2199D977200230242 /* Common */ = {
isa = PBXGroup;
children = (
A90BEBF3199D978100230242 /* PCPieChart.h */,
A90BEBF4199D978100230242 /* PCPieChart.m */,
);
name = Common;
sourceTree = "<group>";
};
A90BEBF6199D982E00230242 /* Resource */ = {
isa = PBXGroup;
children = (
A90BEBF7199D983600230242 /* sample_piechart_data.plist */,
);
path = Resource;
sourceTree = "<group>";
};
A9971A9B19629A070028FB1E = {
isa = PBXGroup;
children = (
A90BEBF6199D982E00230242 /* Resource */,
A90BEBF2199D977200230242 /* Common */,
A9ECA8E7198238A60035277D /* DoImagePicker */,
A9971AAD19629A070028FB1E /* Ash_First */,
A9971AC619629A070028FB1E /* Ash_FirstTests */,
@@ -297,6 +321,7 @@
A958AF9B196E42690029EE41 /* fish_ku@2x.png in Resources */,
A9ECA8F7198238A60035277D /* DoImagePickerController.xib in Resources */,
A98624F41963D54000416F38 /* test.gif in Resources */,
A90BEBF8199D983600230242 /* sample_piechart_data.plist in Resources */,
A958AFC0196E5F9C0029EE41 /* fish_heng.gif in Resources */,
A9971ABA19629A070028FB1E /* Images.xcassets in Resources */,
);
@@ -354,6 +379,7 @@
A9ECA8F3198238A60035277D /* AssetHelper.m in Sources */,
A9ECA8F4198238A60035277D /* DoAlbumCell.m in Sources */,
A9CEB7051962A6BA00C54CB0 /* MainViewController.m in Sources */,
A90BEBF5199D978100230242 /* PCPieChart.m in Sources */,
A9543E3F1963ACB2006CC216 /* MessageViewController.m in Sources */,
A9ECA8F8198238A60035277D /* DoPhotoCell.m in Sources */,
A9ECA8F6198238A60035277D /* DoImagePickerController.m in Sources */,
+47 -31
View File
@@ -10,6 +10,7 @@
#import "MessageViewController.h"
#import <DTCoreText.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import "PCPieChart.h"
@interface MainViewController ()
@end
@@ -31,39 +32,54 @@
// Do any additional setup after loading the view from its nib.
self.title = @"123";
self.navigationItem.title = @"woshishui";
// self.navigationController.navigationBarHidden = YES;
NSString* htmlStr = @"<h1>Header</h1><h2>Subheader</h2><p>Some <em>text</em></p><img src=\"test.gif\"/><a href=\"http://bbs.xmfish.com/read.php?tid=10031229\" target=\"_blank\">查看主题</a><span style=\"font-size:15px;\"><img width='32' height='32' src=\"fish_heng.gif\" /></span> ";
htmlStr = @"<span style=\"font-size:15px;\"><img width='32' height='32' src=\"fish_heng.gif\" /></span>";
NSData *data = [htmlStr dataUsingEncoding:NSUTF8StringEncoding];
NSAttributedString *string = [[NSAttributedString alloc] initWithHTMLData:data documentAttributes:NULL];
int height = [self.view bounds].size.width/3*2.; // 220;
int width = [self.view bounds].size.width; //320;
PCPieChart *pieChart = [[PCPieChart alloc] initWithFrame:CGRectMake(([self.view bounds].size.width-width)/2,([self.view bounds].size.height-height)/2,width,height)];
[pieChart setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin];
[pieChart setDiameter:width/2];
[pieChart setSameColorLabel:YES];
[self.view addSubview:pieChart];
if ([[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPad)
{
pieChart.titleFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:30];
pieChart.percentageFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:50];
}
NSString *sampleFile = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"sample_piechart_data.plist"];
NSDictionary *sampleInfo = [NSDictionary dictionaryWithContentsOfFile:sampleFile];
NSMutableArray *components = [NSMutableArray array];
for (int i=0; i<[[sampleInfo objectForKey:@"data"] count]; i++)
{
NSDictionary *item = [[sampleInfo objectForKey:@"data"] objectAtIndex:i];
PCPieComponent *component = [PCPieComponent pieComponentWithTitle:[item objectForKey:@"title"] value:[[item objectForKey:@"value"] floatValue]];
[components addObject:component];
if (i==0)
{
[component setColour:PCColorYellow];
}
else if (i==1)
{
[component setColour:PCColorGreen];
}
else if (i==2)
{
[component setColour:PCColorOrange];
}
else if (i==3)
{
[component setColour:PCColorRed];
}
else if (i==4)
{
[component setColour:PCColorBlue];
}
}
[pieChart setComponents:components];
DTAttributedTextContentView* _attributedTextContextView;
// _attributedTextContextView.delegate = self;
_attributedTextContextView = nil;
_attributedTextContextView = [[DTAttributedTextContentView alloc] init];
// _attributedTextContextView.edgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
_attributedTextContextView.shouldDrawImages = NO;
_attributedTextContextView.shouldDrawLinks = YES;
_attributedTextContextView.delegate = self;
_attributedTextContextView.backgroundColor = [UIColor clearColor];
_attributedTextContextView.attributedString = string;
_attributedTextContextView.frame = CGRectMake(15, 150, 200, 500);
[self.view addSubview:_attributedTextContextView];
UIEdgeInsets insets = UIEdgeInsetsMake(5, 5, 5, 5);
UIImage *redImage = [[UIImage imageNamed:@"button.9.png"]resizableImageWithCapInsets:insets];
UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 200, 200)];
[imageView setImage:redImage];
// [self.view addSubview:imageView];
DTAttributedLabel* attributedLabel = [[DTAttributedLabel alloc] initWithFrame:CGRectMake(0, 300, 300, 80)];
NSString* htmlStr1 = @"<span style=\"font-size:40px; color:red;\">33</span>%";
NSData *data1 = [htmlStr1 dataUsingEncoding:NSUTF8StringEncoding];
NSAttributedString *string1 = [[NSAttributedString alloc] initWithHTMLData:data1 documentAttributes:NULL];
attributedLabel.backgroundColor = [UIColor clearColor];
attributedLabel.attributedString = string1;
[self.view addSubview:attributedLabel];
}
Executable
+71
View File
@@ -0,0 +1,71 @@
/**
* Copyright (c) 2011 Muh Hon Cheng
* Created by honcheng on 28/4/11.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT
* WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR
* IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @author Muh Hon Cheng <honcheng@gmail.com>
* @copyright 2011 Muh Hon Cheng
* @version
*
*/
#import <UIKit/UIKit.h>
#import "PCPieChart.h"
@interface PCPieComponent : NSObject
{
float value, startDeg, endDeg;
NSString *title;
UIColor *colour;
}
@property (nonatomic, assign) float value, startDeg, endDeg;
@property (nonatomic, retain) UIColor *colour;
@property (nonatomic, retain) NSString *title;
- (id)initWithTitle:(NSString*)_title value:(float)_value;
+ (id)pieComponentWithTitle:(NSString*)_title value:(float)_value;
@end
#define PCColorBlue [UIColor colorWithRed:0.0 green:153/255.0 blue:204/255.0 alpha:1.0]
#define PCColorGreen [UIColor colorWithRed:153/255.0 green:204/255.0 blue:51/255.0 alpha:1.0]
#define PCColorOrange [UIColor colorWithRed:1.0 green:153/255.0 blue:51/255.0 alpha:1.0]
#define PCColorRed [UIColor colorWithRed:1.0 green:51/255.0 blue:51/255.0 alpha:1.0]
#define PCColorYellow [UIColor colorWithRed:1.0 green:220/255.0 blue:0.0 alpha:1.0]
#define PCColorDefault [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0]
@interface PCPieChart : UIView {
NSMutableArray *components;
int diameter;
UIFont *titleFont, *percentageFont;
BOOL showArrow, sameColorLabel;
}
@property (nonatomic, assign) int diameter;
@property (nonatomic, retain) NSMutableArray *components;
@property (nonatomic, retain) UIFont *titleFont, *percentageFont;
@property (nonatomic, assign) BOOL showArrow, sameColorLabel;
@end
Executable
+459
View File
@@ -0,0 +1,459 @@
/**
* Copyright (c) 2011 Muh Hon Cheng
* Created by honcheng on 28/4/11.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT
* WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR
* IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @author Muh Hon Cheng <honcheng@gmail.com>
* @copyright 2011 Muh Hon Cheng
* @version
*
*/
#import "PCPieChart.h"
@implementation PCPieComponent
@synthesize value, title, colour, startDeg, endDeg;
- (id)initWithTitle:(NSString*)_title value:(float)_value
{
self = [super init];
if (self)
{
self.title = _title;
self.value = _value;
self.colour = PCColorDefault;
}
return self;
}
+ (id)pieComponentWithTitle:(NSString*)_title value:(float)_value
{
return [[super alloc] initWithTitle:_title value:_value];
}
- (NSString*)description
{
NSMutableString *text = [NSMutableString string];
[text appendFormat:@"title: %@\n", self.title];
[text appendFormat:@"value: %f\n", self.value];
return text;
}
- (void)dealloc
{
colour = nil;
title = nil;
}
@end
@implementation PCPieChart
@synthesize components;
@synthesize diameter;
@synthesize titleFont, percentageFont;
@synthesize showArrow, sameColorLabel;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setBackgroundColor:[UIColor clearColor]];
self.titleFont = [UIFont fontWithName:@"GeezaPro" size:10];//[UIFont boldSystemFontOfSize:20];
self.percentageFont = [UIFont fontWithName:@"HiraKakuProN-W6" size:20];//[UIFont boldSystemFontOfSize:14];
self.showArrow = YES;
self.sameColorLabel = NO;
}
return self;
}
#define LABEL_TOP_MARGIN 15
#define ARROW_HEAD_LENGTH 6
#define ARROW_HEAD_WIDTH 4
- (void)drawRect:(CGRect)rect
{
// Drawing code
float margin = 15;
if (self.diameter==0)
{
diameter = MIN(rect.size.width, rect.size.height) - 2*margin;
}
float x = (rect.size.width - diameter)/2;
float y = (rect.size.height - diameter)/2;
float gap = 1;
float inner_radius = diameter/2;
float origin_x = x + diameter/2;
float origin_y = y + diameter/2;
// label stuff
float left_label_y = LABEL_TOP_MARGIN;
float right_label_y = LABEL_TOP_MARGIN;
if ([components count]>0)
{
float total = 0;
for (PCPieComponent *component in components)
{
total += component.value;
}
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(ctx);
CGContextSetRGBFillColor(ctx, 1.0f, 1.0f, 1.0f, 1.0f); // white color
CGContextSetShadow(ctx, CGSizeMake(0.0f, 0.0f), margin);
CGContextFillEllipseInRect(ctx, CGRectMake(x, y, diameter, diameter)); // a white filled circle with a diameter of 100 pixels, centered in (60, 60)
UIGraphicsPopContext();
CGContextSetShadow(ctx, CGSizeMake(0.0f, 0.0f), 0);
float nextStartDeg = 0;
float endDeg = 0;
NSMutableArray *tmpComponents = [NSMutableArray array];
int last_insert = -1;
for (int i=0; i<[components count]; i++)
{
PCPieComponent *component = [components objectAtIndex:i];
float perc = [component value]/total;
endDeg = nextStartDeg+perc*360;
CGContextSetFillColorWithColor(ctx, [component.colour CGColor]);
CGContextMoveToPoint(ctx, origin_x, origin_y);
CGContextAddArc(ctx, origin_x, origin_y, inner_radius, (nextStartDeg-90)*M_PI/180.0, (endDeg-90)*M_PI/180.0, 0);
CGContextClosePath(ctx);
CGContextFillPath(ctx);
CGContextSetRGBStrokeColor(ctx, 1, 1, 1, 1);
CGContextSetLineWidth(ctx, gap);
CGContextMoveToPoint(ctx, origin_x, origin_y);
CGContextAddArc(ctx, origin_x, origin_y, inner_radius, (nextStartDeg-90)*M_PI/180.0, (endDeg-90)*M_PI/180.0, 0);
CGContextClosePath(ctx);
CGContextStrokePath(ctx);
[component setStartDeg:nextStartDeg];
[component setEndDeg:endDeg];
if (nextStartDeg<180)
{
[tmpComponents addObject:component];
}
else
{
if (last_insert==-1)
{
last_insert = i;
[tmpComponents addObject:component];
}
else
{
[tmpComponents insertObject:component atIndex:last_insert];
}
}
nextStartDeg = endDeg;
}
nextStartDeg = 0;
endDeg = 0;
float max_text_width = x - 10;
for (int i=0; i<[tmpComponents count]; i++)
{
PCPieComponent *component = [tmpComponents objectAtIndex:i];
nextStartDeg = component.startDeg;
endDeg = component.endDeg;
if (nextStartDeg > 180 || (nextStartDeg < 180 && endDeg> 270) )
{
// left
// display percentage label
if (self.sameColorLabel)
{
CGContextSetFillColorWithColor(ctx, [component.colour CGColor]);
}
else
{
CGContextSetRGBFillColor(ctx, 0.1f, 0.1f, 0.1f, 1.0f);
}
//CGContextSetRGBStrokeColor(ctx, 1.0f, 1.0f, 1.0f, 1.0f);
//CGContextSetRGBFillColor(ctx, 1.0f, 1.0f, 1.0f, 1.0f);
CGContextSetShadow(ctx, CGSizeMake(0.0f, 0.0f), 3);
//float text_x = x + 10;
NSString *percentageText = [NSString stringWithFormat:@"%.1f%%", component.value/total*100];
CGSize optimumSize = [percentageText sizeWithFont:self.percentageFont constrainedToSize:CGSizeMake(max_text_width,100)];
CGRect percFrame = CGRectMake(5, left_label_y, max_text_width, optimumSize.height);
[percentageText drawInRect:percFrame withFont:self.percentageFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentRight];
if (self.showArrow)
{
// draw line to point to chart
CGContextSetRGBStrokeColor(ctx, 0.2f, 0.2f, 0.2f, 1);
CGContextSetRGBFillColor(ctx, 0.0f, 0.0f, 0.0f, 1.0f);
//CGContextSetRGBStrokeColor(ctx, 1.0f, 1.0f, 1.0f, 1.0f);
//CGContextSetRGBFillColor(ctx, 1.0f, 1.0f, 1.0f, 1.0f);
//CGContextSetShadow(ctx, CGSizeMake(0.0f, 0.0f), 5);
int x1 = inner_radius/4*3*cos((nextStartDeg+component.value/total*360/2-90)*M_PI/180.0)+origin_x;
int y1 = inner_radius/4*3*sin((nextStartDeg+component.value/total*360/2-90)*M_PI/180.0)+origin_y;
CGContextSetLineWidth(ctx, 1);
if (left_label_y + optimumSize.height/2 < y)//(left_label_y==LABEL_TOP_MARGIN)
{
CGContextMoveToPoint(ctx, 5 + max_text_width, left_label_y + optimumSize.height/2);
CGContextAddLineToPoint(ctx, x1, left_label_y + optimumSize.height/2);
CGContextAddLineToPoint(ctx, x1, y1);
CGContextStrokePath(ctx);
//CGContextSetRGBFillColor(ctx, 0.0f, 0.0f, 0.0f, 1.0f);
CGContextMoveToPoint(ctx, x1-ARROW_HEAD_WIDTH/2, y1);
CGContextAddLineToPoint(ctx, x1, y1+ARROW_HEAD_LENGTH);
CGContextAddLineToPoint(ctx, x1+ARROW_HEAD_WIDTH/2, y1);
CGContextClosePath(ctx);
CGContextFillPath(ctx);
}
else
{
CGContextMoveToPoint(ctx, 5 + max_text_width, left_label_y + optimumSize.height/2);
if (left_label_y + optimumSize.height/2 > y + diameter)
{
CGContextAddLineToPoint(ctx, x1, left_label_y + optimumSize.height/2);
CGContextAddLineToPoint(ctx, x1, y1);
CGContextStrokePath(ctx);
//CGContextSetRGBFillColor(ctx, 0.0f, 0.0f, 0.0f, 1.0f);
CGContextMoveToPoint(ctx, x1-ARROW_HEAD_WIDTH/2, y1);
CGContextAddLineToPoint(ctx, x1, y1-ARROW_HEAD_LENGTH);
CGContextAddLineToPoint(ctx, x1+ARROW_HEAD_WIDTH/2, y1);
CGContextClosePath(ctx);
CGContextFillPath(ctx);
}
else
{
float y_diff = y1 - (left_label_y + optimumSize.height/2);
if ( (y_diff < 2*ARROW_HEAD_LENGTH && y_diff>0) || (-1*y_diff < 2*ARROW_HEAD_LENGTH && y_diff<0))
{
// straight arrow
y1 = left_label_y + optimumSize.height/2;
CGContextAddLineToPoint(ctx, x1, y1);
CGContextStrokePath(ctx);
//CGContextSetRGBFillColor(ctx, 0.0f, 0.0f, 0.0f, 1.0f);
CGContextMoveToPoint(ctx, x1, y1-ARROW_HEAD_WIDTH/2);
CGContextAddLineToPoint(ctx, x1+ARROW_HEAD_LENGTH, y1);
CGContextAddLineToPoint(ctx, x1, y1+ARROW_HEAD_WIDTH/2);
CGContextClosePath(ctx);
CGContextFillPath(ctx);
}
else if (left_label_y + optimumSize.height/2<y1)
{
// arrow point down
y1 -= ARROW_HEAD_LENGTH;
CGContextAddLineToPoint(ctx, x1, left_label_y + optimumSize.height/2);
CGContextAddLineToPoint(ctx, x1, y1);
CGContextStrokePath(ctx);
//CGContextSetRGBFillColor(ctx, 0.0f, 0.0f, 0.0f, 1.0f);
CGContextMoveToPoint(ctx, x1-ARROW_HEAD_WIDTH/2, y1);
CGContextAddLineToPoint(ctx, x1, y1+ARROW_HEAD_LENGTH);
CGContextAddLineToPoint(ctx, x1+ARROW_HEAD_WIDTH/2, y1);
CGContextClosePath(ctx);
CGContextFillPath(ctx);
}
else
{
// arrow point up
y1 += ARROW_HEAD_LENGTH;
CGContextAddLineToPoint(ctx, x1, left_label_y + optimumSize.height/2);
CGContextAddLineToPoint(ctx, x1, y1);
CGContextStrokePath(ctx);
//CGContextSetRGBFillColor(ctx, 0.0f, 0.0f, 0.0f, 1.0f);
CGContextMoveToPoint(ctx, x1-ARROW_HEAD_WIDTH/2, y1);
CGContextAddLineToPoint(ctx, x1, y1-ARROW_HEAD_LENGTH);
CGContextAddLineToPoint(ctx, x1+ARROW_HEAD_WIDTH/2, y1);
CGContextClosePath(ctx);
CGContextFillPath(ctx);
}
}
}
}
// display title on the left
CGContextSetRGBFillColor(ctx, 0.4f, 0.4f, 0.4f, 1.0f);
left_label_y += optimumSize.height - 4;
optimumSize = [component.title sizeWithFont:self.titleFont constrainedToSize:CGSizeMake(max_text_width,100)];
CGRect titleFrame = CGRectMake(5, left_label_y, max_text_width, optimumSize.height);
[component.title drawInRect:titleFrame withFont:self.titleFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentRight];
left_label_y += optimumSize.height + 10;
}
else
{
// right
// display percentage label
if (self.sameColorLabel)
{
CGContextSetFillColorWithColor(ctx, [component.colour CGColor]);
//CGContextSetRGBStrokeColor(ctx, 1.0f, 1.0f, 1.0f, 0.5);
//CGContextSetTextDrawingMode(ctx, kCGTextFillStroke);
}
else
{
CGContextSetRGBFillColor(ctx, 0.1f, 0.1f, 0.1f, 1.0f);
}
//CGContextSetRGBStrokeColor(ctx, 1.0f, 1.0f, 1.0f, 1.0f);
//CGContextSetRGBFillColor(ctx, 1.0f, 1.0f, 1.0f, 1.0f);
CGContextSetShadow(ctx, CGSizeMake(0.0f, 0.0f), 2);
float text_x = x + diameter + 10;
NSString *percentageText = [NSString stringWithFormat:@"%.1f%%", component.value/total*100];
CGSize optimumSize = [percentageText sizeWithFont:self.percentageFont constrainedToSize:CGSizeMake(max_text_width,100)];
CGRect percFrame = CGRectMake(text_x, right_label_y, optimumSize.width, optimumSize.height);
[percentageText drawInRect:percFrame withFont:self.percentageFont];
if (self.showArrow)
{
// draw line to point to chart
CGContextSetRGBStrokeColor(ctx, 0.2f, 0.2f, 0.2f, 1);
CGContextSetRGBFillColor(ctx, 0.0f, 0.0f, 0.0f, 1.0f);
//CGContextSetRGBStrokeColor(ctx, 1.0f, 1.0f, 1.0f, 1.0f);
//CGContextSetRGBFillColor(ctx, 1.0f, 1.0f, 1.0f, 1.0f);
//CGContextSetShadow(ctx, CGSizeMake(0.0f, 0.0f), 5);
CGContextSetLineWidth(ctx, 1);
int x1 = inner_radius/4*3*cos((nextStartDeg+component.value/total*360/2-90)*M_PI/180.0)+origin_x;
int y1 = inner_radius/4*3*sin((nextStartDeg+component.value/total*360/2-90)*M_PI/180.0)+origin_y;
if (right_label_y + optimumSize.height/2 < y)//(right_label_y==LABEL_TOP_MARGIN)
{
CGContextMoveToPoint(ctx, text_x - 3, right_label_y + optimumSize.height/2);
CGContextAddLineToPoint(ctx, x1, right_label_y + optimumSize.height/2);
CGContextAddLineToPoint(ctx, x1, y1);
CGContextStrokePath(ctx);
//CGContextSetRGBFillColor(ctx, 0.0f, 0.0f, 0.0f, 1.0f);
CGContextMoveToPoint(ctx, x1-ARROW_HEAD_WIDTH/2, y1);
CGContextAddLineToPoint(ctx, x1, y1+ARROW_HEAD_LENGTH);
CGContextAddLineToPoint(ctx, x1+ARROW_HEAD_WIDTH/2, y1);
CGContextClosePath(ctx);
CGContextFillPath(ctx);
}
else
{
float y_diff = y1 - (right_label_y + optimumSize.height/2);
if ( (y_diff < 2*ARROW_HEAD_LENGTH && y_diff>0) || (-1*y_diff < 2*ARROW_HEAD_LENGTH && y_diff<0))
{
// straight arrow
y1 = right_label_y + optimumSize.height/2;
CGContextMoveToPoint(ctx, text_x, right_label_y + optimumSize.height/2);
CGContextAddLineToPoint(ctx, x1, y1);
CGContextStrokePath(ctx);
//CGContextSetRGBFillColor(ctx, 0.0f, 0.0f, 0.0f, 1.0f);
CGContextMoveToPoint(ctx, x1, y1-ARROW_HEAD_WIDTH/2);
CGContextAddLineToPoint(ctx, x1-ARROW_HEAD_LENGTH, y1);
CGContextAddLineToPoint(ctx, x1, y1+ARROW_HEAD_WIDTH/2);
CGContextClosePath(ctx);
CGContextFillPath(ctx);
}
else if (right_label_y + optimumSize.height/2<y1)
{
// arrow point down
y1 -= ARROW_HEAD_LENGTH;
CGContextMoveToPoint(ctx, text_x, right_label_y + optimumSize.height/2);
CGContextAddLineToPoint(ctx, x1, right_label_y + optimumSize.height/2);
//CGContextAddLineToPoint(ctx, x1+5, y1);
CGContextAddLineToPoint(ctx, x1, y1);
CGContextStrokePath(ctx);
//CGContextSetRGBFillColor(ctx, 0.0f, 0.0f, 0.0f, 1.0f);
CGContextMoveToPoint(ctx, x1+ARROW_HEAD_WIDTH/2, y1);
CGContextAddLineToPoint(ctx, x1, y1+ARROW_HEAD_LENGTH);
CGContextAddLineToPoint(ctx, x1-ARROW_HEAD_WIDTH/2, y1);
CGContextClosePath(ctx);
CGContextFillPath(ctx);
}
else //if (nextStartDeg<180 && endDeg>180)
{
// arrow point up
y1 += ARROW_HEAD_LENGTH;
CGContextMoveToPoint(ctx, text_x, right_label_y + optimumSize.height/2);
CGContextAddLineToPoint(ctx, x1, right_label_y + optimumSize.height/2);
CGContextAddLineToPoint(ctx, x1, y1);
CGContextStrokePath(ctx);
//CGContextSetRGBFillColor(ctx, 0.0f, 0.0f, 0.0f, 1.0f);
CGContextMoveToPoint(ctx, x1+ARROW_HEAD_WIDTH/2, y1);
CGContextAddLineToPoint(ctx, x1, y1-ARROW_HEAD_LENGTH);
CGContextAddLineToPoint(ctx, x1-ARROW_HEAD_WIDTH/2, y1);
CGContextClosePath(ctx);
CGContextFillPath(ctx);
}
}
}
// display title on the left
CGContextSetRGBFillColor(ctx, 0.4f, 0.4f, 0.4f, 1.0f);
right_label_y += optimumSize.height - 4;
optimumSize = [component.title sizeWithFont:self.titleFont constrainedToSize:CGSizeMake(max_text_width,100)];
CGRect titleFrame = CGRectMake(text_x, right_label_y, optimumSize.width, optimumSize.height);
[component.title drawInRect:titleFrame withFont:self.titleFont];
right_label_y += optimumSize.height + 10;
}
nextStartDeg = endDeg;
}
}
}
- (void)dealloc
{
self.titleFont = nil;
self.percentageFont = nil;
self.components = nil;
}
@end
+39
View File
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>data</key>
<array>
<dict>
<key>title</key>
<string>AAA</string>
<key>value</key>
<integer>12</integer>
</dict>
<dict>
<key>title</key>
<string>BBB</string>
<key>value</key>
<integer>30</integer>
</dict>
<dict>
<key>title</key>
<string>CCC</string>
<key>value</key>
<integer>10</integer>
</dict>
<dict>
<key>title</key>
<string>DDD</string>
<key>value</key>
<integer>30</integer>
</dict>
<dict>
<key>title</key>
<string>EEE</string>
<key>value</key>
<integer>40</integer>
</dict>
</array>
</dict>
</plist>