封装的部分
#import <UIKit/UIKit.h>
@interface CharLineView : UIView
//y坐标轴数组
@property (nonatomic,strong)NSArray *arrY;
//x坐标轴数组
@property (nonatomic,strong)NSArray *arrX;
//折线点数组
@property (nonatomic,strong)NSArray *pointY;
//折线的颜色
@property (nonatomic,strong)UIColor *color;
@end
#import "CharLineView.h"
#define LineWidth 20
#define startWidth 40
#define startHeight 270
#define EndY 60
#define magin 10
#define StartPoint CGPointMake(startWidth, startHeight)
#define WZPBgColor(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.00]
#define COLOR_BACK_3 WZPBgColor(236,240,243)
#define COLOR_BACK_4 WZPBgColor(204,222,251)
@interface CharLineView ()
@property (nonatomic,assign)CGPoint start;
@property (nonatomic,strong)NSMutableArray *pointArray;
@end
@implementation CharLineView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
// self.my
self.backgroundColor=[UIColor whiteColor];
//
// [self createLabelY];
// [self drawGradientBackgroundView];
// [self setLineDash];
}
return self;
}
- (void)drawRect:(CGRect)rect {
UIColor *color = COLOR_BACK_3;
[color set]; //设置线条颜色
UIBezierPath* aPath = [UIBezierPath bezierPath];
aPath.lineWidth = 2.0;
aPath.lineCapStyle = kCGLineCapRound; //线条拐角
aPath.lineJoinStyle = kCGLineCapRound; //终点处理
[aPath moveToPoint:StartPoint];
[aPath addLineToPoint:CGPointMake(StartPoint.x, EndY)];
[aPath moveToPoint:StartPoint];
[aPath addLineToPoint:CGPointMake(StartPoint.x+self.arrX.count*LineWidth+LineWidth, StartPoint.y)];
// [aPath closePath]; //第五条线通过调用closePath方法得到的
[aPath stroke]; //Draws line 根据坐标点连线
[self createLabelX];
[self createLabelY];
[self setLineDash];
// aPath=[UIBezierPath bezierPathWithArcCenter:CGPointMake(20, 20) radius:5 startAngle:0 endAngle:M_PI*2 clockwise:NO];
// [[UIColor redColor]setFill];
// [aPath fill];
// [aPath stroke];
}
-(void)createLabelY{
for (int i=0; i<self.arrY.count; i++) {
UILabel *label=[[UILabel alloc]init];
label.frame=CGRectMake(StartPoint.x, StartPoint.y-LineWidth*i, StartPoint.x, LineWidth);
label.center=CGPointMake(StartPoint.x-magin, StartPoint.y-LineWidth*i);
label.font=[UIFont systemFontOfSize:12];
label.text=self.arrY[i];
label.textColor=[UIColor grayColor];
[self addSubview:label];
}
}
-(void)createLabelX{
for (int i = 0; i < self.arrX.count; i++) {
UILabel *label=[[UILabel alloc]init];
label.frame=CGRectMake(StartPoint.x+LineWidth*(i+1), StartPoint.y+LineWidth+magin, EndY, LineWidth);
label.center=CGPointMake(StartPoint.x+LineWidth*(i+1), StartPoint.y+LineWidth+magin);
label.font = [UIFont systemFontOfSize:12];
label.text=self.arrX[i];
label.textColor=[UIColor grayColor];
label.transform = CGAffineTransformMakeRotation(M_PI * 0.5);
[self addSubview:label];
}
}
-(void)setLineDash{
for (int i=1; i<self.arrY.count; i++) {
CAShapeLayer * dashLayer = [CAShapeLayer layer];
dashLayer.strokeColor =COLOR_BACK_3.CGColor;
dashLayer.fillColor = [[UIColor clearColor] CGColor];
// 默认设置路径宽度为0,使其在起始状态下不显示
dashLayer.lineWidth = 2.0;
int y=StartPoint.y-LineWidth*i;
UIBezierPath* aPath = [[UIBezierPath alloc]init];
// aPath.lineWidth = 2.0;
[aPath moveToPoint:CGPointMake(StartPoint.x, y)];
[aPath addLineToPoint:CGPointMake(self.arrX.count*LineWidth+StartPoint.x, y)];
// [aPath stroke];
dashLayer.path = aPath.CGPath;
[self.layer insertSublayer:dashLayer atIndex:1];
}
for (int i=0; i<self.arrX.count; i++) {
CAShapeLayer * dashLayer = [CAShapeLayer layer];
dashLayer.strokeColor =COLOR_BACK_3.CGColor;
dashLayer.fillColor = [[UIColor clearColor] CGColor];
// 默认设置路径宽度为0,使其在起始状态下不显示
dashLayer.lineWidth = 1.0;
int x=StartPoint.x+LineWidth*(i+1);
UIBezierPath* aPath = [[UIBezierPath alloc]init];
// aPath.lineWidth = 2.0;
[aPath moveToPoint:CGPointMake(x, StartPoint.y)];
[aPath addLineToPoint:CGPointMake(x, EndY)];
// [aPath stroke];
dashLayer.path = aPath.CGPath;
[self.layer insertSublayer:dashLayer atIndex:1];
}
}
//先描点
-(void)setPointY:(NSArray *)pointY{
_pointY=pointY;
[self dashLayerView];
}
-(void)dashLayerView{
self.pointArray=[NSMutableArray array];
for (int i=0; i<self.pointY.count; i++) {
NSString *str=self.pointY[i];
CGFloat x=StartPoint.x+LineWidth*(i+1);
CGFloat y=[str floatValue];
CGFloat oy=StartPoint.y-(y-[self.arrY[0] floatValue])*StartPoint.x;
CAShapeLayer * dashLayer = [CAShapeLayer layer];
dashLayer.fillColor = self.color.CGColor;
dashLayer.strokeColor =[[UIColor clearColor] CGColor];
// 默认设置路径宽度为0,使其在起始状态下不显示
dashLayer.lineWidth = 2.0;
UIBezierPath* aPath=[UIBezierPath bezierPathWithArcCenter:CGPointMake(x, oy) radius:4 startAngle:0 endAngle:M_PI*2 clockwise:NO];
dashLayer.path = aPath.CGPath;
[self.layer insertSublayer:dashLayer atIndex:2];
if (i==0) {
self.start=CGPointMake(x, oy);
}
}
CAShapeLayer * dashLayer = [CAShapeLayer layer];
dashLayer.strokeColor = self.color.CGColor;
dashLayer.fillColor =[[UIColor clearColor] CGColor];
UIBezierPath* aPath = [[UIBezierPath alloc]init];
aPath.lineWidth = 2.0;
[aPath moveToPoint:self.start];
for (int i=1; i<self.pointY.count; i++) {
NSString *str=self.pointY[i];
CGFloat x=StartPoint.x+LineWidth*(i+1);
CGFloat y=[str floatValue];
NSString *str0=self.arrY[0];
CGFloat oy=StartPoint.y-(y-[str0 floatValue])*StartPoint.x;
[aPath addLineToPoint:CGPointMake(x, oy)];
dashLayer.path = aPath.CGPath;
[self.layer insertSublayer:dashLayer atIndex:2];
}
}
#import "ViewController.h"
#import "CharLineView.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
@interface ViewController ()
{
NSArray *arrY;
NSArray *arrX;
NSArray *pointY;
NSArray *pointY0;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor yellowColor];
self.automaticallyAdjustsScrollViewInsets=NO;
arrY=@[@"23.5",@"24",@"24.5",@"25",@"25.5",@"26",@"26.5",@"27",@"27.5",@"28"];
arrX=@[@"00:05",@"00:10",@"00:15",@"00:20",@"00:25",@"00:30",@"00:35",@"00:40",@"00:45",@"00:50",@"00:55",@"01:00",@"01:05",@"01:10",@"01:15",@"01:20",@"01:25",@"02:30",@"00:50",@"00:55",@"01:00",@"01:05",@"01:10",@"01:15",@"01:20",@"00:25"];
pointY=@[@"25.8",@"25",@"26.1",@"26.8",@"25.0",@"26.5",@"24.6",@"25.6",@"25",@"26",@"26.1",@"26.2",@"26.9",@"25",@"26",@"26",@"25.5",@"25.5",@"25.3",@"25",@"25.1",@"25.9",@"25",@"26",@"25",@"26.1"];
pointY0=@[@"27",@"28",@"24.1",@"26.8",@"25",@"26.5",@"24.6",@"25.6",@"25",@"25",@"26.1",@"27.2",@"26.9",@"23.9",@"25",@"26",@"25.6",@"27.5",@"26.3",@"25",@"26.1",@"23.9",@"25",@"25.5",@"23.9",@"26.1"];
UIScrollView *SCrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, SCREEN_WIDTH, 320)];
CGFloat width;
if (arrX.count*20+40>SCREEN_WIDTH) {
width=arrX.count*20+40*2;
}else{
width=SCREEN_WIDTH;
}
SCrollView.contentSize=CGSizeMake(width, 0);
CharLineView *view=[[CharLineView alloc]initWithFrame:CGRectMake(0, 0, width, 320)];
view.arrX=arrX;
view.arrY=arrY;
view.color=[UIColor redColor];
view.pointY=pointY;
view.color=[UIColor blueColor];
view.pointY=pointY0;
[SCrollView addSubview:view];
[self.view addSubview:SCrollView];
}