iOS 折线图(1)

新建一个基于UIView的类

//
//  LineGraph.h
//  LineChart
//
//  Created by 马家俊 on 17/3/20.
//  Copyright © 2017年 MJJ. All rights reserved.
//

#import 

@interface LineGraph : UIView
@property (nonatomic, strong) NSArray* pointArray;      //传入的point数组
@property (nonatomic, strong) NSArray* XArray;          //传入的X轴数据数组
@property (nonatomic, strong) NSArray* YArray;          //传入的Y轴数据数组
@property (nonatomic, assign) CGFloat MaxX;             //X轴最大值
@property (nonatomic, assign) CGFloat MaxY;             //Y轴最大值
@property (nonatomic, assign) UIColor*  lineColor;      //线条颜色
@property (nonatomic, strong) NSString* xUnit;          //X轴单位
@property (nonatomic, strong) NSString* yUnit;          //Y轴单位
@end

#import "LineGraph.h"

#define defaultX    18
#define defalutY    18
@implementation LineGraph

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
    }
    return self;
}

在drawRect方法中绘制折线图的XY轴

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 1.0);
    CGContextSetStrokeColorWithColor(context,[UIColor redColor].CGColor);
    CGContextMoveToPoint(context, defaultX, defalutY);
    CGContextAddLineToPoint(context, defaultX, rect.size.height - defalutY);
    CGContextAddLineToPoint(context,rect.size.width - defaultX, rect.size.height - defalutY);
    CGContextStrokePath(context);
}

添加XY轴的数据及虚线

-(void)drawXYAndVirtualLine
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    for (int i = 0; i

下班下班,回头再说,先这样....

iOS 折线图(1)_第1张图片
屏幕快照 2017-03-23 17.29.31 1.png

你可能感兴趣的:(iOS 折线图(1))