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

NTChartView.h

 

01#import <Foundation/Foundation.h>
02  
03  
04@interface NTChartView : UIView {
05      
06    //组数据,只实现一个组
07    NSArray *groupData;
08      
09    //最大值,最小值, 列宽度, 
10    float maxValue,minValue,columnWidth,sideWidth,maxScaleValue,maxScaleHeight;
11      
12      
13}
14  
15@property(retain,nonatomic) NSArray *groupData;
16@end

 

NTChartView.m

 

001#import "NTChartView.h"
002  
003static int MARGIN_LEFT = 50;
004static int MARGIN_BOTTOM = 30;
005static int MARGIN_TOP = 20;
006static int SHOW_SCALE_NUM = 5; 
007  
008  
009@interface NTChartView(private)
010-(void)drawColumn:(CGContextRef)context rect:(CGRect)_rect;
011-(void)drawScale:(CGContextRef)context rect:(CGRect)_rect;
012-(void)calcScales:(CGRect)_rect;
013@end
014  
015@implementation NTChartView
016@synthesize groupData;
017  
018  
019- (void) dealloc
020{
021    [groupData release];
022    [super dealloc];
023}
024  
025  
026-(void)drawRect:(CGRect)_rect{
027      
028    CGContextRef context = UIGraphicsGetCurrentContext();
029      
030    CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
031    CGFloat colors[] =
032    {     250/255.0,  175/255.0, 64/255.0, 10,
033        240.0 / 255.0, 90.0 / 255.0, 40.0 / 255.0, 1.0};
034    CGGradientRef gradient = CGGradientCreateWithColorComponents(rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
035     CGContextDrawLinearGradient(context, gradient, CGPointMake(100,20), CGPointMake(100,200), (CGGradientDrawingOptions)NULL);
036    CGGradientRelease(gradient);
037    CGColorSpaceRelease(rgb);
038    //CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
039//  CGContextFillRect(context, _rect);
040//  
041//  //计算刻度
042//  [self calcScales:_rect];
043//  
044//  //画刻度
045//  [self drawScale:context rect:_rect];
046//  
047//  //画柱
048//  [self drawColumn:context rect:_rect];
049          
050}
051  
052-(void)drawScale:(CGContextRef)context rect:(CGRect)_rect{
053    CGPoint points[3];
054    points[0] = CGPointMake(MARGIN_LEFT - 10, MARGIN_TOP);
055    points[1] = CGPointMake(MARGIN_LEFT -10, _rect.size.height - MARGIN_BOTTOM + 1);
056    points[2] = CGPointMake(_rect.size.width - 10, _rect.size.height - MARGIN_BOTTOM + 1);
057    CGContextSetAllowsAntialiasing(context, NO);
058    CGContextAddLines(context, points, 3);
059      
060      
061    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor); 
062      
063    for(int i=0;i<SHOW_SCALE_NUM + 1; i++){
064        maxScaleHeight = (_rect.size.height - MARGIN_BOTTOM) * ( i ) / (SHOW_SCALE_NUM + 1);
065        int vScal = ceil(1.0 * maxScaleValue / (SHOW_SCALE_NUM ) * (i ));
066        float y = (_rect.size.height - MARGIN_BOTTOM) -
067            maxScaleHeight;
068          
069        NSString *scaleStr = [NSString stringWithFormat:@"%d",vScal];
070        [scaleStr
071            drawAtPoint:CGPointMake(MARGIN_LEFT - 20 - [scaleStr sizeWithFont:[UIFont systemFontOfSize:12]].width, y - 10) withFont:[UIFont systemFontOfSize:12]];
072        points[0] = CGPointMake(MARGIN_LEFT - 10, y);
073        points[1] = CGPointMake(MARGIN_LEFT - 13, y);
074        CGContextSetLineDash(context, 0, NULL, 0);
075        CGContextAddLines(context, points, 2);
076        CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
077  
078        CGContextDrawPath(context, kCGPathStroke);
079  
080          
081        points[0] = CGPointMake(MARGIN_LEFT - 10, y);
082        points[1] = CGPointMake(_rect.size.width - 10 , y);
083        float partren[] = {2,3};
084        CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:.7 green:.7 blue:.7 alpha:1].CGColor);
085  
086        CGContextSetLineDash(context, 0,partren , 2);
087        CGContextAddLines(context, points, 2);
088        CGContextDrawPath(context, kCGPathStroke);
089          
090    }
091      
092    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
093  
094    CGContextDrawPath(context, kCGPathStroke);
095    CGContextSetAllowsAntialiasing(context, YES);
096  
097      
098}
099  
100-(void)drawColumn:(CGContextRef)context rect:(CGRect)_rect{
101    int gNumber = 0, vNumber = 0;
102    int baseGroundY = _rect.size.height - MARGIN_BOTTOM, baseGroundX = MARGIN_LEFT;
103    CGPoint points[4];
104      
105      
106      
107      
108    UIColor *columnColor = [UIColor redColor];
109      
110    CGContextSetFillColorWithColor(context, columnColor.CGColor);
111    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
112      
113    for(NSArray *g in groupData){
114        vNumber = 0;
115        for(NSNumber *v in g){
116  
117            float columnHeight = [v floatValue] / maxScaleValue * maxScaleHeight ;
118              
119            //画正面
120            CGContextSetFillColorWithColor(context, columnColor.CGColor);
121            CGContextAddRect(context, CGRectMake(vNumber * 20 + baseGroundX + columnWidth * vNumber
122                                                 , baseGroundY - columnHeight 
123                                                 , columnWidth
124                                                 , columnHeight));
125            CGContextDrawPath(context, kCGPathFill);
126            NSLog(@"columnHeight:%f, (_rect.size.height - MARGIN_TOP - MARGIN_BOTTOM ):%f",columnHeight,(_rect.size.height - MARGIN_TOP - MARGIN_BOTTOM ));
127  
128            if(columnHeight < 10){
129                vNumber++;
130                continue;
131            }
132              
133            //画右侧面
134            CGContextSetFillColorWithColor(context, [UIColor colorWithRed:.9 green:0 blue:0 alpha:1].CGColor);
135            points[0] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + columnWidth, baseGroundY - columnHeight -10);
136            points[1] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + columnWidth + sideWidth, baseGroundY - columnHeight -10 );
137            points[2] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + columnWidth + sideWidth, baseGroundY );
138            points[3] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + columnWidth, baseGroundY );
139              
140            CGContextAddLines(context, points, 4);
141            CGContextDrawPath(context, kCGPathFill);
142              
143            //画上面
144            CGContextSetFillColorWithColor(context, [UIColor colorWithRed:1 green:.4 blue:.4 alpha:1].CGColor);
145            points[0] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber , baseGroundY - columnHeight );
146            points[1] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + sideWidth, baseGroundY - columnHeight -10 );
147            points[2] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + columnWidth + sideWidth , baseGroundY - columnHeight -10 );
148            points[3] = CGPointMake(vNumber * 20 + baseGroundX + columnWidth * vNumber + columnWidth, baseGroundY - columnHeight );
149              
150            CGContextAddLines(context, points, 4);
151            CGContextDrawPath(context, kCGPathFill);
152              
153              
154            vNumber++;
155        }
156        gNumber ++; 
157    }
158      
159      
160}
161  
162-(void)calcScales:(CGRect)_rect{
163    int columnCount = 0;
164    for(NSArray *g in groupData){
165        for(NSNumber *v in g){
166            if(maxValue<[v floatValue]) maxValue = [v floatValue];
167            if(minValue>[v floatValue]) minValue = [v floatValue];
168            columnCount++;
169        }
170    }
171      
172    maxScaleValue = ((int)ceil(maxValue) + (SHOW_SCALE_NUM - (int)ceil(maxValue) % SHOW_SCALE_NUM));
173      
174    columnWidth = (_rect.size.width - MARGIN_LEFT * 2) / (columnCount + 1);
175    sideWidth = columnWidth *.2;
176    columnWidth *= .8;  
177}
178  
179@end

01- (void)viewDidLoad {
02    [super viewDidLoad];
03      
04    NTChartView *v = [[NTChartView alloc] initWithFrame:CGRectMake(10, 10, 300, 300)];
05      
06    NSArray *g = [NSArray arrayWithObject:[NSMutableArray arrayWithObjects:
07                  [NSNumber numberWithFloat:18],
08                  [NSNumber numberWithFloat:30],
09                  [NSNumber numberWithFloat:16.5],
10                  [NSNumber numberWithFloat:55],
11                  [NSNumber numberWithFloat:40],nil]];
12    v.groupData = g;
13      
14    [self.view addSubview:v];
15      
16    [v release];
17}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值