//
// 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