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

发布时间 - 2026-01-11 02:10:54    点击率:

前言

有时候我们在处理一些数据的时候,需要用到折线图来呈现数据,让用户能够对数据更加清晰明,本文主要给大家介绍了关于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 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Android使用GridView实现日历的简单功能  Windows Hello人脸识别突然无法使用  详解Android——蓝牙技术 带你实现终端间数据传输  Laravel数据库迁移怎么用_Laravel Migration管理数据库结构的正确姿势  详解Nginx + Tomcat 反向代理 负载均衡 集群 部署指南  Laravel如何实现图片防盗链功能_Laravel中间件验证Referer来源请求【方案】  Java垃圾回收器的方法和原理总结  Laravel如何实现邮箱地址验证功能_Laravel邮件验证流程与配置  Win11怎么关闭专注助手 Win11关闭免打扰模式设置【操作】  JavaScript如何操作视频_媒体API怎么控制播放  Win11怎么恢复误删照片_Win11数据恢复工具使用【推荐】  制作公司内部网站有哪些,内网如何建网站?  Laravel如何发送系统通知?(Notification渠道示例)  Laravel如何处理文件下载请求?(Response示例)  Android okhttputils现在进度显示实例代码  JavaScript中如何操作剪贴板_ClipboardAPI怎么用  标题:Vue + Vuex 项目中正确使用 JWT 进行身份认证的实践指南  PHP 500报错的快速解决方法  如何制作公司的网站链接,公司想做一个网站,一般需要花多少钱?  VIVO手机上del键无效OnKeyListener不响应的原因及解决方法  ChatGPT回答中断怎么办 引导AI继续输出完整内容的方法  如何正确选择百度移动适配建站域名?  如何在七牛云存储上搭建网站并设置自定义域名?  西安市网站制作公司,哪个相亲网站比较好?西安比较好的相亲网站?  javascript和jQuery中的AJAX技术详解【包含AJAX各种跨域技术】  SQL查询语句优化的实用方法总结  Python图片处理进阶教程_Pillow滤镜与图像增强  JS经典正则表达式笔试题汇总  香港服务器部署网站为何提示未备案?  HTML5空格在Angular项目里怎么处理_Angular中空格的渲染问题【详解】  清除minerd进程的简单方法  如何获取PHP WAP自助建站系统源码?  如何打造高效商业网站?建站目的决定转化率  企业在线网站设计制作流程,想建设一个属于自己的企业网站,该如何去做?  如何有效防御Web建站篡改攻击?  如何在VPS电脑上快速搭建网站?  Laravel如何创建自定义Facades?(详细步骤)  JavaScript如何实现路由_前端路由原理是什么  Edge浏览器怎么启用睡眠标签页_节省电脑内存占用优化技巧  如何基于云服务器快速搭建网站及云盘系统?  Laravel模型关联查询教程_Laravel Eloquent一对多关联写法  详解jQuery中基本的动画方法  Python并发异常传播_错误处理解析【教程】  ,在苏州找工作,上哪个网站比较好?  Laravel如何实现邮件验证激活账户_Laravel内置MustVerifyEmail接口配置【步骤】  谷歌浏览器如何更改浏览器主题 Google Chrome主题设置教程  浅谈redis在项目中的应用  详解Oracle修改字段类型方法总结  如何在IIS中新建站点并配置端口与物理路径?  Laravel如何使用.env文件管理环境变量?(最佳实践)