我是自定義封裝了view, 用起來比較方便:
詳細(xì)代碼如下: (包括.h和.m文件)
// .h文件
#import <UIKit/UIKit.h>
@interface LineChartView : UIView
@property (nonatomic, strong) NSArray *titleForYArr;
@property (nonatomic, strong) NSArray *titleForXArr;
@property (nonatomic, strong) NSArray *valueArr;
@property (nonatomic, strong) UIColor *lineColor;
- (instancetype)initWithFrame:(CGRect)frame;
- (void)startDraw;
@end
// .m文件
#import "LineChartView.h"
@interface LineChartView()<CAAnimationDelegate>
@property (nonatomic, strong) CAShapeLayer *lineChartLayer;
@end
@implementation LineChartView {
CGFloat width;
CGFloat height;
}
static CGFloat edgeLeft = 30;
static CGFloat edgeRight = 20;
static CGFloat bounceY = 40;
static CGFloat edgeUp = 50;
static CGFloat edgeDown = 30;
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor lightGrayColor];
width = frame.size.width - edgeLeft - edgeRight;
height = frame.size.height - edgeUp - edgeDown;
}
return self;
}
// 畫出坐標(biāo)軸
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 1.0);
CGContextSetRGBStrokeColor(context, 0.6, 0.6, 0.6, 1);
CGContextMoveToPoint(context, edgeLeft, edgeUp);
CGContextAddLineToPoint(context, edgeLeft, edgeUp + height);
CGContextAddLineToPoint(context,edgeLeft + width, edgeUp + height);
CGContextStrokePath(context);
}
#pragma mark 畫折線圖
- (void)dravLine{
NSInteger yearNum = self.titleForXArr.count;
if (yearNum <= 0) {
return;
}
CGFloat widthForX = width / yearNum;
CGFloat maxValue = [[self.titleForYArr lastObject] floatValue];
if (maxValue <= 0) {
return;
}
UIBezierPath *pathLine = [[UIBezierPath alloc] init];
[pathLine moveToPoint:CGPointMake(edgeLeft + widthForX * 0.5, ((maxValue - [self.valueArr[0] floatValue]) / maxValue) * height + edgeUp)];
// 創(chuàng)建折線點(diǎn)標(biāo)記
for (NSInteger i = 1; i < self.valueArr.count; i++) {
CGPoint pointCenter = CGPointMake(edgeLeft + widthForX * (i + 0.5), (maxValue - [self.valueArr[i] floatValue]) / maxValue * height + edgeUp);
UIImageView *pointImg = [[UIImageView alloc] init];
pointImg.bounds = CGRectMake(0, 0, 5, 5);
pointImg.center = pointCenter;
pointImg.backgroundColor = _lineColor;
[self addSubview:pointImg];
[pathLine addLineToPoint:pointCenter];
}
CAShapeLayer *lineChartLayer = [CAShapeLayer layer];
lineChartLayer.path = pathLine.CGPath;
lineChartLayer.strokeColor = _lineColor.CGColor;
lineChartLayer.fillColor = [[UIColor clearColor] CGColor];
lineChartLayer.lineWidth = 2.0;
lineChartLayer.lineCap = kCALineCapRound;
lineChartLayer.lineJoin = kCALineJoinRound;
[self.layer addSublayer:lineChartLayer];
}
#pragma mark 創(chuàng)建x軸的數(shù)據(jù)
- (void)setTitleForXArr:(NSArray *)titleForXArr {
_titleForXArr = titleForXArr;
[self createLabelX];
}
- (void)createLabelX{
NSInteger yearNum = self.titleForXArr.count;
if (yearNum <= 0) {
return;
}
CGFloat widthForX = width / yearNum;
for (NSInteger i = 0; i < yearNum; i++) {
UILabel *labelYear = [[UILabel alloc] initWithFrame:CGRectMake(edgeLeft + widthForX * i, height + edgeUp, widthForX, edgeDown)];
labelYear.tag = 1000 + i;
labelYear.text = self.titleForXArr[i];
labelYear.font = [UIFont systemFontOfSize:14];
labelYear.textAlignment = NSTextAlignmentCenter;
[self addSubview:labelYear];
}
}
#pragma mark 創(chuàng)建y軸數(shù)據(jù)及虛線
- (void)setTitleForYArr:(NSArray *)titleForYArr {
_titleForYArr = titleForYArr;
[self createLabelY];
[self setLineDash];
}
- (void)createLabelY{
NSInteger numForY = _titleForYArr.count;
if (numForY <= 1) {
return;
}
CGFloat widthForY = edgeLeft;
CGFloat heightForY = height / (numForY - 1);
for (NSInteger i = 0; i < numForY; i++) {
UILabel *labelForY = [[UILabel alloc] initWithFrame:CGRectMake(0, edgeUp + (i - 0.5) * heightForY, widthForY, heightForY)];
labelForY.tag = 2000 + i;
labelForY.text = _titleForYArr[numForY - i - 1];
labelForY.font = [UIFont systemFontOfSize:12];
labelForY.textAlignment = NSTextAlignmentCenter;
[self addSubview:labelForY];
}
}
#pragma mark - 添加虛線
- (void)setLineDash {
NSInteger numForY = _titleForYArr.count - 1;
if (numForY <= 0) {
return;
}
CGFloat heightForY = height / numForY;
for (NSInteger i = 0; i < numForY; i++) {
CAShapeLayer *dashLayer = [CAShapeLayer layer];
dashLayer.strokeColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1].CGColor;
dashLayer.lineWidth = 1.0;
UIBezierPath *path = [[UIBezierPath alloc] init];
path.lineWidth = 1.0;
[path moveToPoint:CGPointMake(edgeLeft, edgeUp + i * heightForY)];
[path addLineToPoint:CGPointMake(edgeLeft + width, edgeUp + i * heightForY)];
CGFloat dash[] = {10,10};
[path setLineDash:dash count:2 phase:10];
[path stroke];
dashLayer.path = path.CGPath;
[self.layer addSublayer:dashLayer];
}
}
#pragma mark - 開始畫折線
- (void)startDraw {
[self dravLine];
}
@end
// 使用方法
- (void)viewDidLoad {
[super viewDidLoad];
LineChartView *lineView = [[LineChartView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 190)];
lineView.titleForYArr = @[@"0",@"1",@"2",@"3",@"4",@"5"];
lineView.titleForXArr = @[@"2013年",@"2014年",@"2015年",@"2016年",@"2017年"];
lineView.lineColor = [UIColor redColor];
lineView.valueArr = @[@"0.8",@"1.9",@"4.0",@"1.3",@"2.5"];
[self.view addSubview:lineView];
[lineView startDraw];
}