绘制曲线图/折线图只需4步

8390251284.gif

下载YJGraph文件拖入工程后

1、导入头文件

#import "YJGraphView.h"

#import "YJCoordinateItem.h"

2、给定一个存储YJCoordinateItem对象数组

NSMutableArray *coordiantes = [NSMutableArray array];

//x轴数据

NSArray *xText = @[@"第一天",@"第二天",@"第三天",@"第四天",@"第五天",@"第六天",@"第七天"];

//y轴数据

NSArray *yValue = @[@"50",@"66",@"30",@"100",@"72",@"85",@"45"];

for (NSInteger i = 0; i < 7; i++)

{

YJCoordinateItem *item = [YJCoordinateItem coordinateItemWithXText:xText[i]

yValue:[yValue[i] integerValue] ];

[coordiantes addObject:item];

}

3、创建曲线图

YJGraphView *graphView = [[YJGraphView alloc] initWithCoordiantes:coordiantes

graphColor:[UIColor redColor]

animated:YES];

graphView.frame = CGRectMake(0, 100, self.view.frame.size.width, 300);

[self.view addSubview:graphView];

4、开始绘制

[graphView stroke];

实现绘图的核心代码:

//在 drawRect 中进行绘图

- (void)drawRect:(CGRect)rect

{

//建立绘图 颜色 线宽

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetStrokeColorWithColor(context, [[UIColor lightGrayColor] CGColor]);

CGContextSetLineWidth(context, kCoordinateLineWitdth);

//1、绘制坐标轴

[self drawCoordinate:context];

//2、绘制X轴文字

[self drawXCoordinateText];

//3、绘制Y轴文字

[self drawYCoordinateText];

//4、绘制曲线图

[self drawGraph];

}

#pragma mark - 1绘制坐标轴

- (void)drawCoordinate:(CGContextRef)context

{

CGFloat width = self.frame.size.width;

CGFloat height = self.frame.size.height;

//绘制坐标框

CGContextAddRect(context, CGRectMake(kEdgeInsertSpace, kEdgeInsertSpace, width - 2 * kEdgeInsertSpace, height - 2 * kEdgeInsertSpace));

CGContextStrokePath(context);

//绘制虚线框

CGFloat lenths[1] = {5};

CGContextSetLineDash(context, 0, lenths, sizeof(lenths)/sizeof(lenths[0]));

//行高

CGFloat rowHeight = [self rowHeight];

for (int i = 0; i < kNumberOfRow; i++) {

CGFloat y = kEdgeInsertSpace + rowHeight * i;

CGContextMoveToPoint(context, kEdgeInsertSpace, y);

CGContextAddLineToPoint(context, kEdgeInsertSpace, y);

CGContextStrokePath(context);

}

}

#pragma mark - 2绘制X轴文字

- (void)drawXCoordinateText

{

for (int i = 0; i < _coordinates.count; i++) {

YJCoordinateItem *item = _coordinates[i];

//获取文字的宽度

CGFloat textWidth = [item.xValue sizeWithFontSize:kFontSize maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)].width;

//绘制点

CGPoint point = CGPointMake(kXSpace - textWidth / 2 + kEdgeInsertSpace + kDistanceBetweenPointAndPoit * i, self.frame.size.height - kEdgeInsertSpace);

[item.xValue drawAtPoint:point withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kFontSize]}];

}

}

#pragma mark - 3绘制Y轴文字

- (void)drawYCoordinateText

{

CGFloat maxY = [self maxYOfCoodinateYValue];

CGFloat rowHeight = [self rowHeight];

//创建一个段落

NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];

paragraph.alignment = NSTextAlignmentRight;

for (int i = 0; i < kNumberOfRow; i++) {

NSString *text = [NSString stringWithFormat:@"%.0f",maxY - maxY/kNumberOfRow * i];

//获取文字的高度

CGFloat textHeight = [text sizeWithFontSize:kFontSize maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)].height;

//绘图

[text drawInRect:CGRectMake(0, kEdgeInsertSpace + rowHeight * i - textHeight / 2, kEdgeInsertSpace - 5, rowHeight) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kFontSize],NSParagraphStyleAttributeName:paragraph}];

}

}

#pragma mark - 4绘制曲线图 及 动画

- (void)drawGraph

{

//x,y坐标

CGFloat startX,startY;

CGFloat x,y;

CGFloat maxY = [self maxYOfCoodinateYValue];

CGFloat coordinateHeight = self.frame.size.height - 2 * kEdgeInsertSpace;

//绘制图

CGMutablePathRef graphPath = CGPathCreateMutable();

for (int i = 0; i < _coordinates.count; i++) {

YJCoordinateItem *item = _coordinates[i];

CGFloat scale = item.yValue / maxY;

x = kEdgeInsertSpace + kXSpace + kDistanceBetweenPointAndPoit * i;

y = kEdgeInsertSpace + (1 - scale) * coordinateHeight;

if (i == 0) {

startX = x;

startY = y;

//CGPathMoveToPoint(graphPath, NULL, kEdgeInsertSpace,self.frame.size.height - kEdgeInsertSpace);

CGPathMoveToPoint(graphPath, NULL, x, y);

}else{

CGPathAddLineToPoint(graphPath, NULL, x, y);

}

}

CAShapeLayer *layer = [CAShapeLayer layer];

layer.fillColor = [UIColor clearColor].CGColor;

layer.strokeColor = [UIColor kGraphColor].CGColor;

//线条的宽度

layer.lineWidth = kCoordinateLineWitdth;

layer.lineCap = kCALineCapRound;

layer.path = graphPath;

[self.layer addSublayer:layer];

if (_animation) {

CABasicAnimation *animation = [ CABasicAnimation animationWithKeyPath : NSStringFromSelector ( @selector (strokeEnd))];

animation.fromValue = @0 ;

animation.toValue = @1 ;

animation.duration = kAnimationDuration ;

[layer addAnimation :animation forKey : NSStringFromSelector ( @selector (strokeEnd))];

}

CGPathRelease(graphPath);

}

如果觉得有用希望点个小星星噢~

layui做折线图_绘制曲线图/折线图只需4步相关推荐

  1. 手机mstsc远程工具_远程桌面连接,只需3步,轻松远程操控电脑!

    远程桌面的好处 远程桌面有很多好处的 1.对于运维技术人员来说,可以随时随地管理远程主机,查看系统信息和硬件信息等系统性能诊断,远程应用管理内存.CPU等敏感信息报警提醒,对远程主机的一切尽收眼 2. ...

  2. 绘制测试集、训练集的每一个病人或者样本的raidomics signiture图(绘制raidomics signature图),以及ROC曲线图

    绘制测试集.训练集的每一个病人或者样本的raidomics signiture图(绘制raidomics signature图),以及ROC曲线图 受试者工作特征曲线 (receiver operat ...

  3. python_绘制玫瑰图_南丁格尔图

    python_绘制玫瑰图_南丁格尔图 通过加载execel文件绘制 通过直接造数看这: https://blog.csdn.net/kaikai_sk/article/details/10495430 ...

  4. 什么是思维导图  绘制思维导图用什么工具

    思维导图作为一种工具.思想表达方式,已经越来越被人们接受和使用,但是有的人用思维导图方便了自己的工作.生活,而有的人就只限于随便涂抹. 思维导图能否真正的为人们所用,关键在于人们有没有思考,有没有行动 ...

  5. 怎样利用思维导图模板绘制思维导图?分享几款常用的思维导图模板

    对于思维导图的绘制使用是较为普遍的,随着其优势逐渐增加,很多朋友都愿意使用思维导图来对工作或者是学习进行总结,下面是分享的几款思维导图模板以及怎样使用思维导图模板绘制思维导图的简单方法,希望可以帮助到 ...

  6. python发音机器人_只需三步,菜鸟也能用Python做一个简易版Siri

    原标题:只需三步,菜鸟也能用Python做一个简易版Siri 当下,各个手机厂商都陆续的推出了属于自己的智能手机机器人,像是苹果的Siri,小米的小爱,还有等等.这些智能机器人不仅仅方便了我们对于手机 ...

  7. 计算机电池电源转换,图吧小白教程 篇七十七:只需一步,延长MACBOOK电池寿命(硬改电源)...

    图吧小白教程 篇七十七:只需一步,延长MACBOOK电池寿命(硬改电源) 2020-04-17 11:42:40 6点赞 24收藏 4评论 创作立场声明:看来就算到了最后这世界上也只是有IBM兼容机和 ...

  8. 还在为动态太大不能用发愁么?只需3步学会高效压缩GIF动图

    不管是做自媒体,还是上传文件到网页上,总是会遇到一些文件大小限制的情况. 如何在保障动图质量的情况下,可以最大程度上压缩动图的体积呢?别急,小编这就带你瞧瞧Mac只需3步快速压缩GIF图片,亲测将6. ...

  9. 如何将瀑布流里的图片加链接_只需5步!魔幻丛林瀑布后期揭秘

    去年的旅行,我跋山涉水,翻山越岭,探索了很多的幽谷密林. "海蓝时见鲸,林深处遇水"我记录下了很多瀑布的影像. 下面是一组后期处理前后的对比图 如何将原本干涩直白的画风打造出如梦游 ...

最新文章

  1. div+css中clear用法
  2. ubuntu下vi/vim 的基本使用法
  3. Spring Boot 项目如何做性能监控?
  4. 我是如何学习写一个操作系统(四):操作系统之系统调用
  5. Rochambeau POJ - 2912 (枚举和加权并查集+路径压缩)找唯一裁判
  6. Idea和redis的坑
  7. MySQL索引类型总结和使用技巧以及注意事项
  8. 网易丁磊:创造中国特色文化精品,助力实现中国文化大时代
  9. jquery设置video的宽度_使用jQuery和CSS自定义HTML5 Video 控件 简单适用
  10. linux文字大小,Qt 字体大小的计算
  11. SAP License:2021年:传统ERP丧钟响起
  12. 【编程题目】12 个高矮不同的人,排成两排,每排必须是从矮到高排列,而且第二排比对应的第一排的人高,...
  13. 1.6 回归评估准确性的指标
  14. sql-插入当前时间
  15. Padavan完整编译教程
  16. 收费英超与中国球迷相互抛弃
  17. 计算机怎么输入2的15次方,Word 2的15次方怎么打
  18. 深度学习、机器学习交流群
  19. Full CAN与Basic CAN主要区别
  20. android手机通过wifi控制数码管,淫技:android无屏操作之adb操控wifi

热门文章

  1. 阿里云SLS正则提取
  2. 使用adb操作app
  3. Linux下使用C语言查找一个文件夹下指定扩展名的所有文件
  4. 移动电源哪个品牌好?质量最好的移动电源是什么品牌
  5. linux安装powerline字体,CentOS 7.3安装配置Powerline
  6. 电子商务架构6——订单及发货单
  7. 卷积、池化、激活函数、初始化、归一化、正则化、学习率——深度学习基础总结
  8. 电商API接口用处以及未来的趋势
  9. 《影响力》| 我们是如何被说服的?
  10. 魔兽转移账号的服务器,魔兽世界国服免费角色转移操作过程及问题汇总解答