iOS实现的多条折线图封装实例

2020-01-20 23:36:46王振洲

前言

有时候我们在处理一些数据的时候,需要用到折线图来呈现数据,让用户能够对数据更加清晰明,本文主要给大家介绍了关于iOS实现多条折线图的相关内容,下面话不多说,来看看详细的介绍吧。

效果图如下:

ios,多条折线的折线图,画折线图,ios折线图绘制demo

ios,多条折线的折线图,画折线图,ios折线图绘制demo

1、封装类

.h


#define XYQColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define XYQRandomColor XYQColor(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))
#define MARGIN  30 // 坐标轴与画布间距
#define Y_EVERY_MARGIN 20 // y轴每一个值的间隔数

#import <UIKit/UIKit.h>
// 线条类型
typedef NS_ENUM(NSInteger, LineType) {
 LineType_Straight, // 折线
 LineType_Curve // 曲线
};
@interface BezierCurveView : UIView

//初始化画布
+(instancetype)initWithFrame:(CGRect)frame;
//画多根折线图
-(void)drawMoreLineChartViewWithX_Value_Names:(NSMutableArray *)x_names TargetValues:(NSMutableArray *)targetValues LineType:(LineType) lineType;
@end

.m


#import "BezierCurveView.h"

static CGRect myFrame;

@interface BezierCurveView ()

@end

@implementation BezierCurveView

//初始化画布
+(instancetype)initWithFrame:(CGRect)frame{

 BezierCurveView *bezierCurveView = [[BezierCurveView alloc]init]; 
 bezierCurveView.frame = frame;

 //背景视图
 UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
 backView.backgroundColor = [UIColor clearColor];
 [bezierCurveView addSubview:backView];

 myFrame = frame;
 return bezierCurveView;
}

/**
 * 画坐标轴
 */
-(void)drawXYLine:(NSMutableArray *)x_names{

 UIBezierPath *path = [UIBezierPath bezierPath];

 //1.Y轴、X轴的直线
 [path moveToPoint:CGPointMake(MARGIN, CGRectGetHeight(myFrame)-MARGIN)];
 [path addLineToPoint:CGPointMake(MARGIN, MARGIN)];

 [path moveToPoint:CGPointMake(MARGIN, CGRectGetHeight(myFrame)-MARGIN)];
 [path addLineToPoint:CGPointMake(CGRectGetWidth(myFrame), CGRectGetHeight(myFrame)-MARGIN)];

// //2.添加箭头
// [path moveToPoint:CGPointMake(MARGIN, MARGIN)];
// [path addLineToPoint:CGPointMake(MARGIN-5, MARGIN+5)];
// [path moveToPoint:CGPointMake(MARGIN, MARGIN)];
// [path addLineToPoint:CGPointMake(MARGIN+5, MARGIN+5)];
// 
// [path moveToPoint:CGPointMake(CGRectGetWidth(myFrame), CGRectGetHeight(myFrame)-MARGIN)];
// [path addLineToPoint:CGPointMake(CGRectGetWidth(myFrame)-5, CGRectGetHeight(myFrame)-MARGIN-5)];
// [path moveToPoint:CGPointMake(CGRectGetWidth(myFrame), CGRectGetHeight(myFrame)-MARGIN)];
// [path addLineToPoint:CGPointMake(CGRectGetWidth(myFrame)-5, CGRectGetHeight(myFrame)-MARGIN+5)];

 //3.添加索引格
 //X轴
 for (int i=0; i<x_names.count; i++) {
 CGFloat X = MARGIN + (CGRectGetWidth(myFrame)-30)/x_names.count*(i+1)-(CGRectGetWidth(myFrame)-30)/x_names.count/2.0;
 CGPoint point = CGPointMake(X,CGRectGetHeight(myFrame)-MARGIN);
 [path moveToPoint:point];
 [path addLineToPoint:CGPointMake(point.x, point.y-3)];
 }
 //Y轴(实际长度为200,此处比例缩小一倍使用)
 for (int i=0; i<11; i++) {
 CGFloat Y = CGRectGetHeight(myFrame)-MARGIN-Y_EVERY_MARGIN*i;
 CGPoint point = CGPointMake(MARGIN,Y);
 [path moveToPoint:point];
 [path addLineToPoint:CGPointMake(point.x+3, point.y)];
 }

 //4.添加索引格文字
 //X轴
 for (int i=0; i<x_names.count; i++) {
 CGFloat X = MARGIN + (CGRectGetWidth(myFrame)-30)/x_names.count/2.0 + (CGRectGetWidth(myFrame)-30)/x_names.count*i-(CGRectGetWidth(myFrame)-30)/x_names.count/2.0;
 UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(X, CGRectGetHeight(myFrame)-MARGIN, (CGRectGetWidth(myFrame)-60)/x_names.count, 20)];
 textLabel.text = x_names[i];
 textLabel.font = [UIFont systemFontOfSize:10];
 textLabel.textAlignment = NSTextAlignmentCenter;
 textLabel.textColor = [UIColor blueColor];
 [self addSubview:textLabel];
 }
 //Y轴
 for (int i=0; i<11; i++) {
 CGFloat Y = CGRectGetHeight(myFrame)-MARGIN-Y_EVERY_MARGIN*i;
 UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, Y-5, MARGIN, 10)];
 textLabel.text = [NSString stringWithFormat:@"%d",10*i];
 textLabel.font = [UIFont systemFontOfSize:10];
 textLabel.textAlignment = NSTextAlignmentCenter;
 textLabel.textColor = [UIColor redColor];
 [self addSubview:textLabel];
 }
 //5.渲染路径
 CAShapeLayer *shapeLayer = [CAShapeLayer layer];
 shapeLayer.path = path.CGPath;
 shapeLayer.strokeColor = [UIColor blackColor].CGColor;
 shapeLayer.fillColor = [UIColor clearColor].CGColor;
 shapeLayer.borderWidth = 2.0;
 [self.subviews[0].layer addSublayer:shapeLayer];
}