码迷,mamicode.com
首页 > 移动开发 > 详细

iOS-柱状图

时间:2015-09-09 21:05:47      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

和饼图差不多,代码差别不大

//
//  HMPieView.m
//  饼图
//
//  Created by YaguangZhu on 15/9/9.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "HMPieView.h"
#import "UIColor+Random.h"

@implementation HMPieView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    NSArray *data = @[@25,@25,@50];
    int count = data.count;
    
    CGFloat w = rect.size.width/(2*count -1);
    CGFloat h =0;
    CGFloat x =0;
    CGFloat y =0;
    CGFloat viewH = rect.size.height;
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    for (int i  = 0 ; i< count; i++) {
        h = viewH * [data[i] intValue]/100.0;
        x= 2 * w * i;
        y= viewH - h;
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(x, y, w, h)];
        CGContextAddPath(ctx, path.CGPath);
        
        [[UIColor randomColor]set];
        CGContextFillPath(ctx);
        
    }
    
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self setNeedsDisplay];
}

@end

 

iOS-柱状图

标签:

原文地址:http://www.cnblogs.com/zhuyaguang/p/4795751.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!