柱状图 博客分类: iOS iPhoneIOS柱状图
程序员文章站
2024-03-20 16:00:46
...
#import <CoreGraphics/CoreGraphics.h> #define kMaxLine 240 #define kXStartPoint 40 - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetRGBFillColor(context, 0.5, 0.21, 0.52, 1.0); CGContextFillRect(context, CGRectMake(0, 0, 320, 480)); CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); CGContextMoveToPoint(context, kXStartPoint, 120); CGContextAddLineToPoint(context, kXStartPoint, 360); CGContextAddLineToPoint(context, 280, 360); CGContextSetLineWidth(context, 5); CGContextStrokePath(context); NSArray *voteNum = [[NSArray alloc] initWithObjects:@"850000", @"526000", @"750800", @"360500", nil]; NSArray *voteItemName = [[NSArray alloc] initWithObjects:@"奥迪", @"别克", @"凯迪拉克", @"大众", nil]; NSMutableArray *voteNumberStrArry = [[NSMutableArray alloc] init]; NSInteger voteNumBiggest = 0; NSInteger Max = 0; NSInteger numOfItem = [voteNum count]; NSInteger vote_num; NSString *voteNumStr; NSInteger temp = 0; NSInteger c = 0; for (int i = 0; i < [voteNum count]; i++) { NSInteger a = [[voteNum objectAtIndex:i] integerValue]; if (c != 0) { if (c >= a) { temp = c; a = temp; c = a; } else { temp = a; } } c = a; voteNumBiggest = temp; } for (int i = 0; i < [voteNum count]; i++) { vote_num = [[voteNum objectAtIndex:i] integerValue]; float oneVoteNum = 0.0; if (voteNumBiggest > 0 && voteNumBiggest <= 10) { oneVoteNum = vote_num * 24.0; Max = 10; } else if (voteNumBiggest > 10 && voteNumBiggest <= 100) { oneVoteNum = vote_num * 2.4; Max = 100; } else if(voteNumBiggest > 100 && voteNumBiggest <= 500) { oneVoteNum = vote_num * 0.48; Max = 500; } else if(voteNumBiggest > 500 && voteNumBiggest <= 1000) { oneVoteNum = vote_num * 0.24; Max = 1000; } else if(voteNumBiggest > 1000 &&voteNumBiggest <= 5000) { oneVoteNum = vote_num * 0.048; Max = 5000; } else if(voteNumBiggest >5000 && voteNumBiggest <= 10000) { oneVoteNum = vote_num * 0.024; Max = 10000; } else if(voteNumBiggest > 10000 && voteNumBiggest <= 100000) { oneVoteNum = vote_num * 0.0024; Max = 100000; } else if(voteNumBiggest > 100000 && voteNumBiggest <= 1000000) { oneVoteNum = vote_num * 0.00024; Max = 1000000; } voteNumStr = [NSString stringWithFormat:@"%f", oneVoteNum]; [voteNumberStrArry addObject:voteNumStr]; } for (int i = 0; i < numOfItem; i++) { CGContextSetLineWidth(context, 3); CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); float legPointY = 0.0; float q1 = numOfItem + 1; float q2 = i + 1; float q = q2 / q1 ; legPointY = 120 + kMaxLine * q; float a = [[voteNumberStrArry objectAtIndex:i] floatValue]; UIImage *myImage = [[UIImage imageNamed:@"1.png"] stretchableImageWithLeftCapWidth:0.5 topCapHeight:10 ]; CGRect imageRect = CGRectMake(kXStartPoint, legPointY, a, 20); [myImage drawInRect:imageRect]; CGRect rectangle = CGRectMake(kXStartPoint, legPointY, a, 20); CGContextAddRect(context, rectangle); CGContextStrokePath(context); [[UIColor greenColor] set]; NSString *n = [voteItemName objectAtIndex:i]; CGSize mSize = [n sizeWithFont:[UIFont systemFontOfSize:12.0] forWidth:100 lineBreakMode:UILineBreakModeTailTruncation]; [n drawInRect:CGRectMake(50, rectangle.origin.y - 20, mSize.width, mSize.height) withFont:[UIFont systemFontOfSize:12.0]]; [[voteNum objectAtIndex:i] drawInRect:CGRectMake(rectangle.size.width + 50, rectangle.origin.y, 70, 20) withFont:[UIFont systemFontOfSize:12.0]]; } NSString *zeroPointStr = @"0"; [zeroPointStr drawInRect:CGRectMake(kXStartPoint, 366, 52, 20) withFont:[UIFont systemFontOfSize:12.0]]; NSString *halfPointStr = [NSString stringWithFormat:@"%d", Max / 2]; [halfPointStr drawInRect:CGRectMake(140, 366, 52, 20) withFont:[UIFont systemFontOfSize:12.0]]; NSString *rightPointStr = [NSString stringWithFormat:@"%d", Max]; [rightPointStr drawInRect:CGRectMake(270, 366, 52, 20) withFont:[UIFont systemFontOfSize:12.0]]; [voteNum release]; [voteItemName release]; [voteNumberStrArry release]; }
效果图:
推荐阅读
-
柱状图 博客分类: iOS iPhoneIOS柱状图
-
JfreeChart柱状图饼图 博客分类: struts2 jfreechart
-
JFreeChart展示柱状图和折线图的组合 博客分类: 应用 jfreechartajaxJavaScript
-
Pentaho CDE 教程(五)走进CDE 之 柱状图高级设置 博客分类: CDEpentaho pentahocdebarchart
-
jfreechar 柱状图 博客分类: java语言 jfreechart柱状图
-
进度条之ADVProgressBar 博客分类: iOS iPhoneiOS进度条ADVProgressBar
-
iOS 静态库 博客分类: iOS开发
-
集成Reveal 博客分类: 常用命令iOS开发
-
CocoaPods使用 博客分类: 常用命令iOS开发
-
Git版本管理 博客分类: iOS开发常用命令