iphone上如何绘制柱状图(转载,整理)

曾经在cocoachina上看到过绘制的立体的柱状图,效果非常不错,下面是链接,

http://www.cocoachina.com/bbs/read.php?tid-9462-toread-1.html

 

NTChartView.h

#import <Foundation/Foundation.h>


@interface NTChartView : UIView {
	
	//组数据,只实现一个组
	NSArray *groupData;
	
	//最大值,最小值, 列宽度, 
	float maxValue,minValue,columnWidth,sideWidth,maxScaleValue,maxScaleHeight;
	
	
}

@property(retain,nonatomic) NSArray *groupData;
@end

 

NTChartView.m

#import "NTChartView.h"

static int MARGIN_LEFT = 50;
static int MARGIN_BOTTOM = 30;
static int MARGIN_TOP = 20;
static int SHOW_SCALE_NUM = 5; 


@interface NTChartView(private)
-(void)drawColumn:(CGContextRef)context rect:(CGRect)_rect;
-(void)drawScale:(CGContextRef)context rect:(CGRect)_rect;
-(void)calcScales:(CGRect)_rect;
@end

@implementation NTChartView
@synthesize groupData;


- (void) dealloc
{
	[groupData release];
	[super dealloc];
}


-(void)drawRect:(CGRect)_rect{
	
	CGContextRef context = UIGraphicsGetCurrentContext();
	
	CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
    CGFloat colors[] =
    {     250/255.0,  175/255.0, 64/255.0, 10,
        240.0 / 255.0, 90.0 / 255.0, 40.0 / 255.0, 1.0};
    CGGradientRef gradient = CGGradientCreateWithColorComponents(rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
     CGContextDrawLinearGradient(context, gradient, CGPointMake(100,20), CGPointMake(100,200), (CGGradientDrawingOptions)NULL);
	CGGradientRelease(gradient);
	CGColorSpaceRelease(rgb);
	//CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
//	CGContextFillRect(context, _rect);
//	
//	//计算刻度
//	[self calcScales:_rect];
//	
//	//画刻度
//	[self drawScale:context rect:_rect];
//	
//	//画柱
//	[self drawColumn:context rect:_rect];
		
}

-(void)drawScale:(CGContextRef)context rect:(CGRect)_rect{
	CGPoint points[3];
	points[0] = CGPointMake(MARGIN_LEFT - 10, MARGIN_TOP);
	points[1] = CGPointMake(MARGIN_LEFT -10, _rect.size.height - MARGIN_BOTTOM + 1);
	points[2] = CGPointMake(_rect.size.width - 10, _rect.size.height - MARGIN_BOTTOM + 1);
	CGContextSetAllowsAntialiasing(context, NO);
	CGContextAddLines(context, points, 3);
	
	
	CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor); 
	
	for(int i=0;i<SHOW_SCALE_NUM + 1; i++){
		maxScaleHeight = (_rect.size.height - MARGIN_BOTTOM) * ( i ) / (SHOW_SCALE_NUM + 1);
		int vScal = ceil(1.0 * maxScaleValue / (SHOW_SCALE_NUM ) * (i ));
		float y = (_rect.size.height - MARGIN_BOTTOM) -
			maxScaleHeight;
		
		NSString *scaleStr = [NSString stringWithFormat:@"%d",vScal];
		[scaleStr
			drawAtPoint:CGPointMake(MARGIN_LEFT - 20 - [scaleStr sizeWithFont:[UIFont systemFontOfSize:12]].width, y - 10) withFont:[UIFont systemFontOfSize:12]];
		points[0] = CGPointMake(MARGIN_LEFT - 10, y);
		points[1] = CGPointMake(MARGIN_LEFT - 13, y);
		CGContextSetLineDash(context, 0, NULL, 0);
		CGContextAddLines(context, points, 2);
		CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);

		CGContextDrawPath(context, kCGPathStroke);

		
		points[0] = CGPointMake(MARGIN_LEFT - 10, y);
		points[1] = CGPointMake(_rect.size.width - 10 , y);
		float partren[] = {2,3};
		CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:.7 green:.7 blue:.7 alpha:1].CGColor);

		CGContextSetLineDash(context, 0,partren , 2);
		CGContextAddLines(context, points, 2);
		CGContextDrawPath(context, kCGPathStroke);
		
	}
	
	CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);

	CGContextDrawPath(context, kCGPathStroke);
	CGContextSetAllowsAntialiasing(context, YES);

	
}

-(void)drawColumn:(CGContextRef)context rect:(CGRect)_rect{
	int gNumber = 0, vNumber = 0;
	int baseGroundY = _rect.size.height - MARGIN_BOTTOM, baseGroundX = MARGIN_LEFT;
	CGPoint points[4];
	
	
	
	
	UIColor *columnColor = [UIColor redColor];
	
	CGContextSetFillColorWithColor(context, columnColor.CGColor);
	CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
	
	for(NSArray *g in groupData){
		vNumber = 0;
		for(NSNumber *v in g){

			float columnHeight = [v floatValue] / maxScaleValue * maxScaleHeight ;
			
			//画正面
			CGContextSetFillColorWithColor(context, columnColor.CGColor);
			CGContextAddRect(context, CGRectMake(vNumber * 20 + baseGroundX + columnWidth * vNumber
												 , baseGroundY - columnHeight 
												 , columnWidth
												 , columnHeight));
			CGContextDrawPath(context, kCGPathFill);
			NSLog(@"columnHeight:%f, (_rect.size.height - MARGIN_TOP - MARGIN_BOTTOM ):%f",columnHeight,(_rect.size.height - MARGIN_TOP - MARGIN_BOTTOM ));

			if(columnHeight < 10){
				vNumber++;
				continue;
			}
			
			//画右侧面
			CGContextSetFillColorWithColor(context, [UIColor colorWithRed:.9 green:0 blue:0 alpha:1].CGColor);
			points[0] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + columnWidth, baseGroundY - columnHeight -10);
			points[1] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + columnWidth + sideWidth, baseGroundY - columnHeight -10 );
			points[2] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + columnWidth + sideWidth, baseGroundY );
			points[3] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + columnWidth, baseGroundY );
			
			CGContextAddLines(context, points, 4);
			CGContextDrawPath(context, kCGPathFill);
			
			//画上面
			CGContextSetFillColorWithColor(context, [UIColor colorWithRed:1 green:.4 blue:.4 alpha:1].CGColor);
			points[0] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber , baseGroundY - columnHeight );
			points[1] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + sideWidth, baseGroundY - columnHeight -10 );
			points[2] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + columnWidth + sideWidth , baseGroundY - columnHeight -10 );
			points[3] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + columnWidth, baseGroundY - columnHeight );
			
			CGContextAddLines(context, points, 4);
			CGContextDrawPath(context, kCGPathFill);
			
			
			vNumber++;
		}
		gNumber ++; 
	}
	
	
}

-(void)calcScales:(CGRect)_rect{
	int columnCount = 0;
	for(NSArray *g in groupData){
		for(NSNumber *v in g){
			if(maxValue<[v floatValue]) maxValue = [v floatValue];
			if(minValue>[v floatValue]) minValue = [v floatValue];
			columnCount++;
		}
	}
	
	maxScaleValue = ((int)ceil(maxValue) + (SHOW_SCALE_NUM - (int)ceil(maxValue) % SHOW_SCALE_NUM));
	
	columnWidth = (_rect.size.width - MARGIN_LEFT * 2) / (columnCount + 1);
	sideWidth = columnWidth *.2;
	columnWidth *= .8;	
}

@end

- (void)viewDidLoad {
    [super viewDidLoad];
	
	NTChartView *v = [[NTChartView alloc] initWithFrame:CGRectMake(10, 10, 300, 300)];
	
	NSArray *g = [NSArray arrayWithObject:[NSMutableArray arrayWithObjects:
				  [NSNumber numberWithFloat:18],
				  [NSNumber numberWithFloat:30],
				  [NSNumber numberWithFloat:16.5],
				  [NSNumber numberWithFloat:55],
				  [NSNumber numberWithFloat:40],nil]];
	v.groupData = g;
	
	[self.view addSubview:v];
	
	[v release];
}

尊重作者的劳动成果,佩服作者的功底。

posted @ 2010-10-19 00:15  苏瑞强  阅读(2929)  评论(0编辑  收藏  举报