一个简单易用的折线图控件,最近项目工程中需要用到一个折现图,看了网上的一些例子,但都不能满足UED的特殊要求,所以只能自己写了一个。

先来看下效果图:

基本实现以下功能:

  • 支持自定义Y轴坐标数
  • 支持自定义X轴显示索引
  • 添加参考线、点击标线
  • 支持自定义弹出说明视图
    ·····

Demo见github YASimpleGraph,喜欢的话请star下^_^

使用说明

YASimpleGraph 目前已经支持CocoaPods,可以在很短的时间内被添加到任何工程中。

安装

YASimpleGraph 的安装,最简单的方法是使用CocoaPods,在PodFile里添加如下:

pod 'YASimpleGraph', '~> 0.0.1'

或者直接将YASimpleGraphView.hYASimpleGraphView.m两个源文件直接拖进自己的项目工程中。

集成

  • 首先导入头文件
#import "YASimpleGraphView.h"
  • 遵循相应协议
@interface ViewController () <YASimpleGraphDelegate> 
  • 初始化
//初始化数据源
allValues = @[@"20.00",@"0",@"110.00",@"70",@"80",@"40"];
allDates = @[@"06/01",@"06/02",@"06/03",@"06/04",@"06/05",@"06/06"];//初始化折线图并设置相应属性
YASimpleGraphView *graphView = [[YASimpleGraphView alloc]init];
graphView.frame = CGRectMake(15, 200, 375-30, 200);
graphView.backgroundColor = [UIColor whiteColor];
graphView.allValues = allValues;
graphView.allDates = allDates;
graphView.defaultShowIndex = allDates.count-1;
graphView.delegate = self;
graphView.lineColor = [UIColor grayColor];
graphView.lineWidth = 1.0/[UIScreen mainScreen].scale;
graphView.lineAlpha = 1.0;
graphView.enableTouchLine = YES;
[self.view addSubview:graphView];
  • 开始绘制
[graphView startDraw];
  • 实现相应协议
//自定义X轴 显示标签索引
- (NSArray *)incrementPositionsForXAxisOnLineGraph:(YASimpleGraphView *)graph {return @[@0,@1,@2,@3,@4,@5];
}//Y轴坐标点数
- (NSInteger)numberOfYAxisLabelsOnLineGraph:(YASimpleGraphView *)graph {return 5;
}//自定义popUpView
- (UIView *)popUpViewForLineGraph:(YASimpleGraphView *)graph {UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];label.backgroundColor = [UIColor colorWithRed:146/255.0 green:191/255.0 blue:239/255.0 alpha:1];label.numberOfLines = 0;label.font = [UIFont systemFontOfSize:10];label.textAlignment = NSTextAlignmentCenter;return label;
}//修改相应点位弹出视图
- (void)lineGraph:(YASimpleGraphView *)graph modifyPopupView:(UIView *)popupView forIndex:(NSUInteger)index {UILabel *label = (UILabel*)popupView;NSString *date = [NSString stringWithFormat:@"%@",allDates[index]];NSString *str = [NSString stringWithFormat:@" %@ \n %@元 ",date,allValues[index]];CGRect rect = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, 40) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]} context:nil];[label setFrame:CGRectMake(0, 0, rect.size.width, rect.size.height)];label.textColor = [UIColor whiteColor];label.text = str;
}

完成上述步骤,折线图控件已经集成到我们的项目中了,当然YASimpleGraph还提供了一系列的对外属性变量,使我们可以高度自定义折线图控件,如下:

/// The line color
@property (nonatomic, strong) UIColor *lineColor;/// The line width
@property (nonatomic, assign) CGFloat lineWidth;/// The line alpha
@property (assign, nonatomic) float lineAlpha;/// The Dot color
@property (nonatomic, strong) UIColor *dotColor;/// The Dot borderColor
@property (nonatomic, strong) UIColor *dotBorderColor;/// The Dot width
@property (nonatomic, assign) CGFloat dotWidth;/// The Dot borderWidth
@property (nonatomic, assign) CGFloat dotBorderWidth;/// The dashLine color
@property (nonatomic, strong) UIColor *dashLineColor;/// The dashLine width
@property (nonatomic, assign) CGFloat dashLineWidth;/// The bottomLine color
@property (nonatomic, strong) UIColor *bottomLineColor;/// The bottomLine width
@property (nonatomic, assign) CGFloat bottomLineHeight;····

等等一系列,具体可参考YASimpleGraphView.h

实现过程

实现过程大致分为以下几步:

  1. draw 坐标轴&点、参考线
  2. draw 数据点、折线
  3. 处理手势点击

简要列出上述3步中需要注意的一些地方:

a. 横坐标轴根据点的个数直接等分就好,纵坐标轴有点难度,这里参考了同事的纵坐标取整算法和最小值优化,实现主要如下:

- (NSInteger) calcIntegerDeltaValue:(double) maxValue minValue:(double) minValue{NSInteger integerDetla = ceil((maxValue - minValue) / (numberOfYAxis-1));//对得到的整数进一步取整 如: 101 -> 110, 1001 -> 1100if (integerDetla == 0) { //值为0的水平直线则返回100一个值的间距return 100;}else{return [self ajustIntegerValue:integerDetla];}}- (NSInteger)ajustIntegerValue:(NSInteger) origalValue{if (origalValue < 100) {return origalValue;}else{NSInteger base = origalValue;NSInteger round = 1;while (origalValue > 100) {origalValue = origalValue / 10;round *= 10;}return base - base % round + round;}
}

b. 折线直接连接坐标点即可,当画曲线时,为使曲线更加曲滑,采取两点之间两次二次贝塞尔,即先取中点,然后分别对起始点进行二次贝塞尔,具体实现如下:

+ (UIBezierPath *)quadCurvedPathWithPoints:(NSArray *)points {UIBezierPath *path = [UIBezierPath bezierPath];NSValue *value = points[0];CGPoint p1 = [value CGPointValue];[path moveToPoint:p1];if (points.count == 2) {value = points[1];CGPoint p2 = [value CGPointValue];[path addLineToPoint:p2];return path;}for (NSUInteger i = 1; i < points.count; i++) {value = points[i];CGPoint p2 = [value CGPointValue];CGPoint midPoint = midPointForPoints(p1, p2);[path addQuadCurveToPoint:midPoint controlPoint:controlPointForPoints(midPoint, p1)];[path addQuadCurveToPoint:p2 controlPoint:controlPointForPoints(midPoint, p2)];p1 = p2;}return path;
}static CGPoint midPointForPoints(CGPoint p1, CGPoint p2) {return CGPointMake((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
}static CGPoint controlPointForPoints(CGPoint p1, CGPoint p2) {CGPoint controlPoint = midPointForPoints(p1, p2);CGFloat diffY = fabs(p2.y - controlPoint.y);if (p1.y < p2.y)controlPoint.y += diffY;else if (p1.y > p2.y)controlPoint.y -= diffY;return controlPoint;
}

【iOS】简单易用的折线图控件相关推荐

  1. axurerp折线图控件_Axure教程:如何画出曲线图?

    在做数据可视化类型的页面时,会遇到这样的问题:我想在我的柱状图上画出一条曲线怎么办? 第一步: 在左侧的元件库中点击水平线(或者垂直线都可以),拖动到图中 第二步: 选中水平线,右键将水平线转换为自定 ...

  2. Android自定义控件之轮播图控件

    背景 最近要做一个轮播图的效果,网上看了几篇文章,基本上都能找到实现,效果还挺不错,但是在写的时候感觉每次都要单独去重新在Activity里写一堆代码.于是自己封装了一下.这里只是做了下封装成一个控件 ...

  3. 日历控件如何切换语言 vant_看看甘特图控件VARCHART XGantt的亮点

    XGantt从1998年的第一个商用版本开始就致力于计划编制和项目管理方面控件的研究和开发,经过20多年的积累和沉淀,目前可为软件开发商和最终用户提供最顶级的计划编制和项目管理的控件产品,帮助用户快速 ...

  4. 一款开源且功能强大的C#甘特图控件.NET Winforms Gantt Chart Control

    甘特图在项目管理中非常重要,甘特图的思想比较简单,即以图示的方式通过活动列表和时间刻度形象地表示出任何特定项目的活动顺序与持续时间.它直观地表明任务计划在什么时候进行,及实际进展与计划要求的对比.管理 ...

  5. Android手工打造脑图控件

    背景 所有的开发背景都是项目需要.先上屌炸天的设计图. 效果 导出效果不清晰,尽量看吧. 功能 脑图展示 样式订制(文字颜色.图标.样式.边框..) 折叠方式支持两种:a.同侧折叠不影响其他.b.同侧 ...

  6. android自定义波浪图,Android自定义控件--波浪图控件

    今天给大家分享一个android的波浪图控件制作.具体效果如下图所示: 上次有个app使用了这个控件,感觉特别酷炫.今天讲解一下这个控件的思路分析与代码编写. 思路分析: 1.绘制波浪图 2.移动波浪 ...

  7. 甘特图控件VARCHART XGantt的功能亮点

    甘特图(Xgantt)从1998年的第一个商用版本开始就致力于计划编制和项目管理方面控件的研究和开发,经过20多年的积累和沉淀,目前可为软件开发商和最终用户提供最顶级的计划编制和项目管理的控件产品,帮 ...

  8. HQChart使用教程5- K线图控件操作函数说明

    K线图控件操作函数说明 周期切换 切换股票 切换指标 增加一个窗口指标 AddIndexWindow indexName option 增加一个自定义通达信脚本指标窗口 AddScriptIndexW ...

  9. Ext Scheduler Web资源甘特图控件

    原文来自 http://www.fanganwang.com/Product-detail-item-1430.html欢迎转载. 关键字: 资源甘特图又叫负荷图,其纵轴不再列出活动,而是列出整个部门 ...

最新文章

  1. [译]React高级话题之Context
  2. (兼容IE6)又一个提示框思密达,腾讯UED 201401242352
  3. #论文 《Wide Deep Learning for Recommender System》翻译
  4. c++仿函数 functor
  5. ecshop程序设置伪静态简单三步骤
  6. python从爬虫到数据分析项目_零基础学习Python web开发、Python爬虫、Python数据分析,从基础到项目实战!...
  7. 数据结构与算法-笨办法解决问题1909
  8. 你所不知道的模块调试技巧 - npm link #17
  9. python面对对象计算矩形_python第九课 面向对象
  10. PyPI可以使用的几个国内源
  11. To prevent a memory leak the JDBC Driver has been forcibly
  12. SSL证书有什么用? SSL证书错误怎么办?
  13. Android源码解析系列
  14. Ztree Fa-Awesome 图标使用
  15. Free SQL Server tools that might make your life a little easier
  16. 缓冲区溢出的基本原理
  17. (附源码)ssm美通留学管理系统 毕业设计 130854
  18. 关系代数(数据库笔记)
  19. 卡巴斯基KAV KIS6.0激活码大全
  20. 达内培训python费用

热门文章

  1. 高新技术企业申请流程如何
  2. CSS-背景颜色 | background-color
  3. js 中的正则捕获组
  4. refers to an unmapped class
  5. 网络云:云技术是网络世界的真假美猴王Ya
  6. cvte暑期实习经历
  7. 量化交易 第二课 平台介绍
  8. 【Java开发环境搭建】
  9. 【python】python matplotlib绘制并保存多张图片+绘制多张子图
  10. require.js的用法: