iOS 对折线图的封装

//
//  DrawScrollView.h
 
//
//  Created by yinbo on 2016/11/10.
//  Copyright © 2016年 yinbo. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface DrawScrollView : UIView
-(void)setHorizontalArr:(NSArray *)hArr withVerticalArr:(NSArray *)vArr withHorWidth:(float)hwidth withHorHigth:(float)horhigth withVertical:(float)vwidth  withdataArr:(NSArray *)dataArr ;//harr横坐标集合,varr竖坐标集合,hwidth横向每项宽度,horhigth横向每项的高度,vwidth竖向每项的宽度,dataArr数组集
//harr横坐标集合,varr竖坐标集合,hwidth横向每项宽度,horhigth横向每项的高度,vwidth竖向每项的宽度,dataArr数组集
-(void)setHorizontalArr:(NSArray *)hArr withVerticalArr:(NSArray *)vArr withHorWidth:(float)hwidth withHorHigth:(float)horhigth withVertical:(float)vwidth  withdataArr:(NSArray *)dataArr withZwidth:(float)zwidth;

@property(nonatomic,strong)NSArray *harr;
@property(nonatomic,strong)NSArray *varr;
@property(nonatomic,assign)float hwidth;
@property(nonatomic,assign)float horhigth;
@property(nonatomic,assign)float vwidth;
@property(nonatomic,assign)float zwidth;
@property(nonatomic,assign)BOOL isLine;

@property(nonatomic,strong)NSArray *dataArr;
@end




//
//  DrawScrollView.m
//   
//
//  Created by yinbo on 2016/11/10.
//  Copyright © 2016年 yinbo. All rights reserved.
//

#import "DrawScrollView.h"

@implementation DrawScrollView
-(id)initWithFrame:(CGRect)frame{
    
    self=[super initWithFrame:frame];
  
    return  self;
}
-(void)setHorizontalArr:(NSArray *)hArr withVerticalArr:(NSArray *)vArr withHorWidth:(float)hwidth withHorHigth:(float)horhigth withVertical:(float)vwidth  withdataArr:(NSArray *)dataArr {
 
    _harr=hArr;
    _varr=vArr;
    _hwidth=hwidth;
    _horhigth=horhigth;
    _vwidth=vwidth;
    _dataArr=dataArr;
    _isLine=YES;
    for (int i=0; i<hArr.count; i++) {
        UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(hwidth*i, self.frame.size.height-horhigth,hwidth, horhigth)];
        lab.textAlignment=NSTextAlignmentCenter;
        lab.text=hArr[i];
        [self addSubview:lab];
    }
}

-(void)setHorizontalArr:(NSArray *)hArr withVerticalArr:(NSArray *)vArr withHorWidth:(float)hwidth withHorHigth:(float)horhigth withVertical:(float)vwidth withdataArr:(NSArray *)dataArr withZwidth:(float)zwidth{
    
    _harr=hArr;
    _varr=vArr;
    _hwidth=hwidth;
    _horhigth=horhigth;
    _vwidth=vwidth;
    _dataArr=dataArr;
    _zwidth=zwidth;
    _isLine=NO;
    
    for (int i=0; i<hArr.count; i++) {
        UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(hwidth*i, self.frame.size.height-horhigth,hwidth, horhigth)];
        lab.textAlignment=NSTextAlignmentCenter;
        lab.text=hArr[i];
        [self addSubview:lab];
    }
    //    [self setNeedsLayout];
}
-(void)drawRect:(CGRect)rect{
    UIBezierPath *path = [UIBezierPath bezierPath];
    // 添加路径[1条点(100,100)到点(200,100)的线段]到path
    [path moveToPoint:CGPointMake(0,self.frame.size.height-self.horhigth)];
    [path addLineToPoint:CGPointMake(self.frame.size.width,self.frame.size.height-self.horhigth)];
    path.lineWidth=1;
    // 将path绘制出来
    [path stroke];
    float varhight=(self.frame.size.height-_horhigth)/_varr.count;
    
    for (int i=0; i<_varr.count; i++) {
        UIBezierPath *path = [UIBezierPath bezierPath];
        // 添加路径[1条点(100,100)到点(200,100)的线段]到path
        [path moveToPoint:CGPointMake(0,varhight*(i))];
        [path addLineToPoint:CGPointMake(self.frame.size.width,varhight*(i))];
        path.lineWidth=1;
        [[UIColor grayColor]setFill];
        // 将path绘制出来
        [path stroke];
    }
    float horw=_hwidth/2;
    for (int i=0; i<_harr.count; i++) {
        UIBezierPath *path = [UIBezierPath bezierPath];
        // 添加路径[1条点(100,100)到点(200,100)的线段]到path
        [path moveToPoint:CGPointMake(_hwidth*(i+1),self.frame.size.height-_horhigth)];
        [path addLineToPoint:CGPointMake(_hwidth*(i+1),0)];
        path.lineWidth=1;
        [[UIColor grayColor]setFill];
        // 将path绘制出来
        [path stroke];
    }

    if(_isLine==YES){
        BOOL isAllArr = false;
        for (id p in self.dataArr) {
            if ([p isKindOfClass:[NSArray class]]) {
                isAllArr=YES;
                break;
            }else{
                isAllArr=NO;
                break;
            }
        }
        for (int i=0; i<self.dataArr.count; i++) {
            if (isAllArr) {
                NSArray *arr=(NSArray *)self.dataArr[i];
                for (int j=0; j<arr.count-1; j++) {
                    int value1=[arr[j] intValue];
                    int value2=[arr[j+1] intValue];
                    float allheight=self.frame.size.height-_horhigth;
                    int lastVervalue=[_varr[_varr.count-1] intValue];
                    int fistVervalue=[_varr[0] intValue];
                    UIBezierPath *path = [UIBezierPath bezierPath];
                    // 添加路径[1条点(100,100)到点(200,100)的线段]到path
                    int num1=lastVervalue-value1;
                    int rangenum=lastVervalue-fistVervalue;
                    int num2=lastVervalue-value2;
                    
                    float f1=(float)num1/(float)rangenum;
                    float f2=(float)num2/(float)rangenum;
                    [path moveToPoint:CGPointMake(_hwidth/2+_hwidth*j, f1*(self.frame.size.height-_horhigth))];
                    [path addLineToPoint:CGPointMake(_hwidth/2+_hwidth*(j+1),f2*(self.frame.size.height-_horhigth))];
                    [path addArcWithCenter:CGPointMake(_hwidth/2+_hwidth*j, f1*(self.frame.size.height-_horhigth)) radius:1.0 startAngle:0.0 endAngle:180.0 clockwise:YES];
                    
                    path.lineWidth=1;
                    // 将path绘制出来
                    [path stroke];
                }
            }else{
                
            }
        }
    }else{
        BOOL isAllArr = false;
        for (id p in self.dataArr) {
            if ([p isKindOfClass:[NSArray class]]) {
                isAllArr=YES;
                break;
            }else{
                isAllArr=NO;
                break;
            }
        }
        for (int i=0; i<self.dataArr.count; i++) {
            if (isAllArr) {
                NSArray *arr=(NSArray *)self.dataArr[i];
                for (int j=0; j<arr.count-1; j++) {
                    int value1=[arr[j] intValue];
                    int value2=[arr[j+1] intValue];
                    float allheight=self.frame.size.height-_horhigth;
                    int lastVervalue=[_varr[_varr.count-1] intValue];
                    int fistVervalue=[_varr[0] intValue];
                    UIBezierPath *path = [UIBezierPath bezierPath];
                    // 添加路径[1条点(100,100)到点(200,100)的线段]到path
                    int num1=lastVervalue-value1;
                    int rangenum=lastVervalue-fistVervalue;
                    int num2=lastVervalue-value2;
                    
                    float f1=(float)num1/(float)rangenum;
                    float f2=(float)num2/(float)rangenum;
                    [path moveToPoint:CGPointMake(_hwidth/2+_hwidth*j, f1*(self.frame.size.height-_horhigth))];
                    [path addLineToPoint:CGPointMake(_hwidth/2+_hwidth*(j+1),f2*(self.frame.size.height-_horhigth))];
                    [path addArcWithCenter:CGPointMake(_hwidth/2+_hwidth*j, f1*(self.frame.size.height-_horhigth)) radius:1.0 startAngle:0.0 endAngle:180.0 clockwise:YES];
                    
                    path.lineWidth=1;
                    // 将path绘制出来
                    [path stroke];
                }
            }else{
                float value=[self.dataArr[i] floatValue];
                float lastValue=[self.varr[self.varr.count-1] floatValue];
                float fistvalue=[self.varr[0] floatValue];
                UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake((_hwidth-_zwidth)/2+_hwidth*i,(lastValue-value)/(lastValue-fistvalue)*(self.frame.size.height-_horhigth),(_hwidth+_zwidth)/2+_hwidth*i, self.frame.size.height-_horhigth)];
                
                path.lineWidth = 1.5;
                path.lineCapStyle = kCGLineCapRound;
                path.lineJoinStyle = kCGLineJoinBevel;
                
                // 设置填充颜色
                UIColor *fillColor = [UIColor greenColor];
                [fillColor set];
                [path fill];
                
                // 设置画笔颜色
                UIColor *strokeColor = [UIColor blueColor];
                [strokeColor set];
                
                // 根据我们设置的各个点连线
                [path stroke];
            }
        }

    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
     Drawing code
}
*/

@end


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值