全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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

前言

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

效果图如下:

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];
}
/**
 * 画多根折线图
 */
-(void)drawMoreLineChartViewWithX_Value_Names:(NSMutableArray *)x_names TargetValues:(NSMutableArray *)targetValues LineType:(LineType) lineType{

 //1.画坐标轴
 [self drawXYLine:x_names];

 for (int j=0; j<targetValues.count; j++) {
 //2.获取目标值点坐标
 NSMutableArray *allPoints = [NSMutableArray array];
 for (int i=0; i<[targetValues[j] count]; i++) {
  CGFloat doubleValue = 2*[targetValues[j][i] floatValue]; //目标值放大两倍
  CGFloat X = MARGIN + (CGRectGetWidth(myFrame)-30)/x_names.count*(i+1)-(CGRectGetWidth(myFrame)-30)/x_names.count/2.0;
  CGFloat Y = CGRectGetHeight(myFrame)-MARGIN-doubleValue;
  CGPoint point = CGPointMake(X,Y);
  UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(point.x-1, point.y-1, 2.5, 2.5) cornerRadius:2.5];
  CAShapeLayer *layer = [CAShapeLayer layer];
  layer.strokeColor = [UIColor purpleColor].CGColor;
  layer.fillColor = [UIColor purpleColor].CGColor;
  layer.path = path.CGPath;
  [self.subviews[0].layer addSublayer:layer];
  [allPoints addObject:[NSValue valueWithCGPoint:point]];
 }

 //3.坐标连线
 UIBezierPath *path = [UIBezierPath bezierPath];
 [path moveToPoint:[allPoints[0] CGPointValue]];

 CGPoint PrePonit;
 switch (lineType) {
  case LineType_Straight: //直线
  for (int i =1; i<allPoints.count; i++) {
   CGPoint point = [allPoints[i] CGPointValue];
   [path addLineToPoint:point];
  }
  break;
  case LineType_Curve: //曲线
  for (int i =0; i<allPoints.count; i++) {
   if (i==0) {
   PrePonit = [allPoints[0] CGPointValue];
   }else{
   CGPoint NowPoint = [allPoints[i] CGPointValue];
   [path addCurveToPoint:NowPoint controlPoint1:CGPointMake((PrePonit.x+NowPoint.x)/2, PrePonit.y) controlPoint2:CGPointMake((PrePonit.x+NowPoint.x)/2, NowPoint.y)]; //三次曲线
   PrePonit = NowPoint;
   }
  }
  break;
 }

 CAShapeLayer *shapeLayer = [CAShapeLayer layer];
 shapeLayer.path = path.CGPath;
 shapeLayer.strokeColor = XYQRandomColor.CGColor;
 shapeLayer.fillColor = [UIColor clearColor].CGColor;
 shapeLayer.borderWidth = 2.0;
 [self.subviews[0].layer addSublayer:shapeLayer];
 }
}

2、调用

#define SCREEN_W [UIScreen mainScreen].bounds.size.width
#define SCREEN_H [UIScreen mainScreen].bounds.size.height
 //1.初始化
 _bezierView = [BezierCurveView initWithFrame:CGRectMake(30, 30, SCREEN_W-60, 280)];
 _bezierView.center = self.view.center;
 [self.view addSubview:_bezierView];
 // 多根折线图
 [_bezierView drawMoreLineChartViewWithX_Value_Names:(NSMutableArray *)@[@"语文",@"数学",@"英语",@"物理",@"化学",@"生物",@"政治",@"历史",@"地理"] TargetValues:(NSMutableArray *)@[@[@60,@20,@50,@30,@90,@30,@100,@70, @20],@[@20,@40,@20,@50,@30,@90,@30,@100,@70],@[@10,@30,@40,@70,@50,@30,@20,@10,@80]] LineType:LineType_Straight];

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。


# ios  # 多条折线的折线图  # 画折线图  # ios折线图绘制demo  # iOS使用Charts框架绘制折线图  # ios开发一个好看的折线图  # 折线图  # 相关内容  # 给大家  # 来看看  # 英语  # 这篇文章  # 谢谢大家  # 多说  # 一倍  # 两倍  # 多条  # 长度为  # 有疑问  # LineType  # void  # drawMoreLineChartViewWithX_Value_Names  # NSMutableArray  # targetValues 


相关文章: 建站主机服务器选购指南:轻量应用与VPS配置解析  Swift中swift中的switch 语句  大同网页,大同瑞慈医院官网?  如何解决ASP生成WAP建站中文乱码问题?  宝盒自助建站智能生成技巧:SEO优化与关键词设置指南  简单实现Android文件上传  公司网站制作价格怎么算,公司办个官网需要多少钱?  专业公司网站制作公司,用什么语言做企业网站比较好?  如何通过虚拟主机空间快速建站?  代刷网站制作软件,别人代刷火车票靠谱吗?  沈阳制作网站公司排名,沈阳装饰协会官方网站?  如何快速搭建二级域名独立网站?  建站之星北京办公室:智能建站系统与小程序生成方案解析  百度网页制作网站有哪些,谁能告诉我百度网站是怎么联系?  如何选择适配移动端的WAP自助建站平台?  如何在香港免费服务器上快速搭建网站?  再谈Python中的字符串与字符编码(推荐)  建站之星上传入口如何快速找到?  建站之星安装后如何自定义网站颜色与字体?  ,南京靠谱的征婚网站?  威客平台建站流程解析:高效搭建教程与设计优化方案  如何在Golang中引入测试模块_Golang测试包导入与使用实践  javascript中的try catch异常捕获机制用法分析  山东云建站价格为何差异显著?  网页制作模板网站推荐,网页设计海报之类的素材哪里好?  外贸公司网站制作哪家好,maersk船公司官网?  宝华建站服务条款解析:五站合一功能与SEO优化设置指南  网站设计制作公司地址,网站建设比较好的公司都有哪些?  无锡营销型网站制作公司,无锡网选车牌流程?  如何在阿里云通过域名搭建网站?  深圳网站制作设计招聘,关于服装设计的流行趋势,哪里的资料比较全面?  制作销售网站教学视频,销售网站有哪些?  零服务器AI建站解决方案:快速部署与云端平台低成本实践  XML的“混合内容”是什么 怎么用DTD或XSD定义  如何选择域名并搭建高效网站?  建站之星IIS配置教程:代码生成技巧与站点搭建指南  专业制作网站的公司哪家好,建立一个公司网站的费用.有哪些部分,分别要多少钱?  高性价比服务器租赁——企业级配置与24小时运维服务  建站之星好吗?新手能否轻松上手建站?  如何在云主机上快速搭建多站点网站?  娃派WAP自助建站:免费模板+移动优化,快速打造专业网站  建站之星如何优化SEO以实现高效排名?  建站主机选哪家性价比最高?  如何选择CMS系统实现快速建站与SEO优化?  建站IDE高效指南:快速搭建+SEO优化+自适应模板全解析  表情包在线制作网站免费,表情包怎么弄?  如何快速建站并高效导出源代码?  完全自定义免费建站平台:主题模板在线生成一站式服务  php能控制zigbee模块吗_php通过串口与cc2530 zigbee通信【介绍】  网站制作培训多少钱一个月,网站优化seo培训课程有哪些? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。