代码例子效果:  下载地址:http://download.csdn.net/detail/qqmcy/6983187

LineChartViewDemo.h

  1. #import <UIKit/UIKit.h>
  2. @interface LineChartViewDemo : UIView
  3. //横竖轴距离间隔
  4. @property (assign) NSInteger hInterval;
  5. @property (assign) NSInteger vInterval;
  6. //横竖轴显示标签
  7. @property (nonatomic, strong) NSArray *hDesc;
  8. @property (nonatomic, strong) NSArray *vDesc;
  9. //点信息
  10. @property (nonatomic, strong) NSArray *array;
  11. @property (nonatomic, strong) NSArray* array1;
  12. @end

LineChartViewDemo.m

  1. #import "LineChartViewDemo.h"
  2. #import "EColumnChartLabel.h"
  3. #define KlineHeight 30
  4. @interface LineChartViewDemo()
  5. {
  6. CALayer *linesLayer;
  7. UIView *popView;
  8. UILabel *disLabel;
  9. int x;
  10. int y;
  11. }
  12. @end
  13. @implementation LineChartViewDemo
  14. - (id)initWithFrame:(CGRect)frame
  15. {
  16. self = [super initWithFrame:frame];
  17. if (self) {
  18. // Initialization code
  19. self.backgroundColor = [UIColor clearColor];
  20. x = frame.size.width;
  21. y = frame.size.height;
  22. _hInterval = 10;
  23. _vInterval = 50;
  24. linesLayer = [[CALayer alloc] init];
  25. linesLayer.masksToBounds = YES;
  26. linesLayer.contentsGravity = kCAGravityLeft;
  27. linesLayer.backgroundColor = [[UIColor redColor] CGColor];
  28. //[self.layer addSublayer:linesLayer];
  29. //PopView
  30. popView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, 30)];
  31. [popView setBackgroundColor:[UIColor whiteColor]];
  32. [popView setAlpha:0.0f];
  33. disLabel = [[UILabel alloc]initWithFrame:popView.frame];
  34. [disLabel setTextAlignment:NSTextAlignmentCenter];
  35. [popView addSubview:disLabel];
  36. [self addSubview:popView];
  37. }
  38. return self;
  39. }
  40. // Only override drawRect: if you perform custom drawing.
  41. // An empty implementation adversely affects performance during animation.
  42. - (void)drawRect:(CGRect)rect
  43. {
  44. [self setClearsContextBeforeDrawing: YES];
  45. CGContextRef context = UIGraphicsGetCurrentContext();
  46. //画背景线条------------------
  47. CGColorRef backColorRef = [UIColor redColor].CGColor;
  48. CGFloat backLineWidth = 0.5f;
  49. CGFloat backMiterLimit = 0.f;
  50. CGContextSetLineWidth(context, backLineWidth);//主线宽度
  51. // CGContextSetMiterLimit(context, backMiterLimit);//投影角度
  52. //CGContextSetShadowWithColor(context, CGSizeMake(3, 5), 8, backColorRef);//设置双条线
  53. CGContextSetLineJoin(context, kCGLineJoinRound);
  54. CGContextSetLineCap(context, kCGLineCapRound );
  55. CGContextSetBlendMode(context, kCGBlendModeNormal);
  56. CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
  57. // CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:225.0f / 255.0f green:225.0f / 255.0f blue:225.0f / 255.0f alpha:1.0].CGColor);
  58. //    int x = 400 ;
  59. //    //Y轴横线
  60. //    int y = 300 ;
  61. int tempY = y;
  62. //添加纵轴标签和线
  63. for (int i=0; i<_vDesc.count; i++) {
  64. CGPoint bPoint = CGPointMake(30, tempY);
  65. CGPoint ePoint = CGPointMake(x, tempY);
  66. EColumnChartLabel *label = [[EColumnChartLabel alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
  67. [label setCenter:CGPointMake(bPoint.x-15, bPoint.y-30)];
  68. [label setTextAlignment:NSTextAlignmentCenter];
  69. [label setBackgroundColor:[UIColor clearColor]];
  70. [label setTextColor:[UIColor blackColor]];
  71. [label setText:[_vDesc objectAtIndex:i]];
  72. [self addSubview:label];
  73. CGContextMoveToPoint(context, bPoint.x, bPoint.y-30);
  74. CGContextAddLineToPoint(context, ePoint.x, ePoint.y-30);
  75. tempY -= y * 0.1;
  76. }
  77. for (int i=0; i<_array.count-1; i++) {
  78. EColumnChartLabel *label = [[EColumnChartLabel alloc]initWithFrame:CGRectMake(i*KlineHeight+30, y - 30, 40, 30)];
  79. [label setTextAlignment:NSTextAlignmentCenter];
  80. [label setBackgroundColor:[UIColor clearColor]];
  81. [label setTextColor:[UIColor redColor]];
  82. label.numberOfLines = 1;
  83. label.adjustsFontSizeToFitWidth = YES;
  84. label . minimumFontSize = 1.0f;
  85. [label setText:[_hDesc objectAtIndex:i]];
  86. [self addSubview:label];
  87. }
  88. CGContextStrokePath(context);
  89. //    //画点线条------------------
  90. CGColorRef pointColorRef = [UIColor colorWithRed:24.0f/255.0f green:116.0f/255.0f blue:205.0f/255.0f alpha:1.0].CGColor;
  91. CGFloat pointLineWidth = 1.5f;
  92. CGFloat pointMiterLimit = 5.0f;
  93. CGContextSetLineWidth(context, pointLineWidth);//主线宽度
  94. CGContextSetMiterLimit(context, pointMiterLimit);//投影角度
  95. //CGContextSetShadowWithColor(context, CGSizeMake(3, 5), 8, pointColorRef);//设置双条线
  96. CGContextSetLineJoin(context, kCGLineJoinRound);
  97. CGContextSetLineCap(context, kCGLineCapRound );
  98. CGContextSetBlendMode(context, kCGBlendModeNormal);
  99. //CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
  100. UIColor* color1 = c_BeforeLastYear;
  101. [color1 set];
  102. //绘图
  103. CGPoint p1 = [[_array objectAtIndex:0] CGPointValue];
  104. int i = 0;
  105. //获取视图的高
  106. int tempY1 = y;
  107. int tempWidth = y * 0.1f;
  108. float tempHeight = y * (270.0 / 320.0);
  109. float tempHeight1 = y * (20.0f / 320.0f);
  110. //   NSLog(@"%f");
  111. CGContextMoveToPoint(context, p1.x + 20, tempHeight-p1.y*tempWidth/tempHeight1);
  112. for (; i<[_array count]; i++)
  113. {
  114. p1 = [[_array objectAtIndex:i] CGPointValue];
  115. CGPoint goPoint = CGPointMake(p1.x + 20, tempHeight-p1.y*tempWidth/tempHeight1);
  116. CGContextAddLineToPoint(context, goPoint.x, goPoint.y);;
  117. //添加触摸点
  118. UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
  119. [bt setBackgroundColor:[UIColor redColor]];
  120. [bt setFrame:CGRectMake(0, 0, 10, 10)];
  121. [bt setCenter:goPoint];
  122. [bt addTarget:self
  123. action:@selector(btAction:)
  124. forControlEvents:UIControlEventTouchUpInside];
  125. [self addSubview:bt];
  126. }
  127. CGContextStrokePath(context);
  128. // CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
  129. UIColor* color = c_LastYear;
  130. [color set];
  131. //绘图
  132. p1 = [[_array1 objectAtIndex:0] CGPointValue];
  133. i = 0;
  134. CGContextMoveToPoint(context, p1.x + 20, tempHeight-p1.y*tempWidth/tempHeight1);
  135. for (; i<[_array1 count]; i++)
  136. {
  137. p1 = [[_array1 objectAtIndex:i] CGPointValue];
  138. CGPoint goPoint = CGPointMake(p1.x + 20, tempHeight-p1.y*tempWidth/tempHeight1);
  139. CGContextAddLineToPoint(context, goPoint.x, goPoint.y);;
  140. //添加触摸点
  141. UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
  142. [bt setBackgroundColor:[UIColor redColor]];
  143. [bt setFrame:CGRectMake(0, 0, 10, 10)];
  144. [bt setCenter:goPoint];
  145. [bt addTarget:self
  146. action:@selector(btAction:)
  147. forControlEvents:UIControlEventTouchUpInside];
  148. [self addSubview:bt];
  149. }
  150. CGContextStrokePath(context);
  151. }
  152. - (void)btAction:(id)sender{
  153. [disLabel setText:@"400"];
  154. UIButton *bt = (UIButton*)sender;
  155. popView.center = CGPointMake(bt.center.x, bt.center.y - popView.frame.size.height/2);
  156. [popView setAlpha:1.0f];
  157. }

ViewController.m

    1. #import "LineChartViewDemo.h"
    2. #define CC_DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) * 0.01745329252f)
    3. #define KlineHeight 20
    4. #define KlineWidth 30
    5. @interface ViewController ()
    6. @end
    7. @implementation ViewController
    8. - (void)viewDidLoad
    9. {
    10. [super viewDidLoad];
    11. // Do any additional setup after loading the view, typically from a nib.
    12. LineChartViewDemo* line = [[LineChartViewDemo alloc] initWithFrame:CGRectMake(0, 0, 548, 250)];
    13. // line.layer.transform =  CATransform3DMakeRotation(CC_DEGREES_TO_RADIANS(90), 0, 0, 1);
    14. NSMutableArray *pointArr = [[NSMutableArray alloc]init];
    15. //生成随机点   1
    16. //[pointArr addObject:[NSValue valueWithCGPoint:CGPointMake(KlineWidth*0, 0)]];
    17. for (int i = 0; i < 12; i++) {
    18. [pointArr addObject:[NSValue valueWithCGPoint:CGPointMake(KlineWidth* (i+1), 55 * i)]];
    19. }
    20. NSMutableArray* pointArr2 = [NSMutableArray array];
    21. //生成随机点   2
    22. for (int i = 0; i < 12; i++) {
    23. [pointArr2 addObject:[NSValue valueWithCGPoint:CGPointMake(KlineWidth* (i + 1), 110 * i)]];
    24. }
    25. //竖轴
    26. NSMutableArray *vArr = [[NSMutableArray alloc]initWithCapacity:pointArr.count-1];
    27. for (int i=0; i<9; i++) {
    28. [vArr addObject:[NSString stringWithFormat:@"%d",i*20]];
    29. }
    30. //横轴
    31. NSMutableArray *hArr = [[NSMutableArray alloc]initWithCapacity:pointArr.count-1];
    32. for (int i = 1; i <= 12; i++) {
    33. [hArr addObject:[NSString stringWithFormat:@"%d",i]];
    34. }
    35. [line setHDesc:hArr];
    36. [line setVDesc:vArr];
    37. [line setArray:pointArr];
    38. [line setArray1:pointArr2];
    39. [self.view addSubview:line];
    40. }

iOS画折线图的更多相关文章

  1. python中matplotlib画折线图实例(坐标轴数字、字符串混搭及标题中文显示)

    最近在用python中的matplotlib画折线图,遇到了坐标轴 "数字+刻度" 混合显示.标题中文显示.批量处理等诸多问题.通过学习解决了,来记录下.如有错误或不足之处,望请指 ...

  2. SAS 画折线图PROC GPLOT

    虽然最后做成PPT里的图表会被要求用EXCEL画,但当我们只是在分析的过程中,想看看数据的走势,直接在SAS里画会比EXCEL画便捷的多. 修改起来也会更加的简单,,不用不断的修改程序然后刷新EXCE ...

  3. Matplotlib学习---用matplotlib画折线图(line chart)

    这里利用Jake Vanderplas所著的<Python数据科学手册>一书中的数据,学习画图. 数据地址:https://raw.githubusercontent.com/jakevd ...

  4. echars画折线图的一种数据处理方式

    echars画折线图的一种数据处理方式 <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...

  5. 使用OpenCV画折线图

    使用OpenCV画直方图是一件轻松的事情,画折线图就没有那么Easy了,还是使用一个库吧: GraphUtils 源代码添加入工程 原文链接:http://www.360doc.com/content ...

  6. IOS使用Core-Plot画折线图

    关于Core-Plot的配置.大家能够參考我的上一篇博客:http://1.wildcat.sinaapp.com/?p=99 版权全部.转载请注明原文转自:http://blog.csdn.net/ ...

  7. gnuplot画折线图

    之前尝试用jfreechart画自定义横坐标的折线图或时序图,发现很复杂,后来改用gnuplot了. gnuplot在网上一搜就能找到下载地址. 安装完成后,主要是命令行形式的交互界面,至少比jfre ...

  8. python的turtle模块画折线图

    代码如下: import turtle yValues = [10.0,7.4,6.4,5.3,4.4,3.7,2.6] def main(): t = turtle.Turtle() t.hidet ...

  9. echarts入门基础,画折线图

    注意:一定要自己引入echarts库 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

随机推荐

  1. 【noip模拟赛5】任务分配 降维dp

    描述 现有n个任务,要交给A和B完成.每个任务给A或给B完成,所需的时间分别为ai和bi.问他们完成所有的任务至少要多少时间. 输入 第一行一个正整数n,表示有n个任务.接下来有n行,每行两个正整数a ...

  2. P1019 单词接龙 字符串回溯

    题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合 ...

  3. printf的执行顺序

    printf()函数的参数,在printf()函数读取时是从左往右读取的,然后将读取到的参数放到栈里面去, 最后读取到的就放在栈顶,处理参数的时候是从栈顶开始的,所以是从右边开始处理的. --prin ...

  4. BASH if/while/until loop

    #/bin/bash ]; then counter=" counter1=" echo "for loop:" $); do echo $i done ); ...

  5. Linux虚拟机上安装redis

    1.检查安装依赖程序 yum install gcc-c++ yum install -y tcl yum install wget 2.获取安装文件 wget http://download.red ...

  6. CF 1131 E. String Multiplication

    E. String Multiplication 题意 分析: 从后往前考虑字符串变成什么样子. 设$S_i = p_1 \cdot p_2 \dots p_{i}$,最后一定是$S_{n - 1} ...

  7. 非常简单的方法实现ViewPager自动循环轮播

    非常简单的方法实现ViewPager自动循环轮播,见红色代码部分,其它的代码可以忽略不看. 简洁高效是我解决问题的首要出发点. package com.shuivy.happylendandreadb ...

  8. bzoj 2013 上升计数

    题意: 给一个数集和一个数d,问满足下列要求的排列数(相同的数要区分):  a[i]+d>=a[i+1] ( i in [1,n) ) 因为数的给出顺序不重要,所以先排序,假如我们已经解决了前i ...

  9. C++.Linux下redis编程:error while loading shared libraries: libhiredis.so.0.13

    编译 sudo gcc -o sltest01 sltest01.c -L/usr/local/lib/ -lhiredis 运行 sudo ./sltest01 编译成功后运行报错信息: ./slt ...

  10. MyEclipse设置文件的编码格式

    在MyEclipse中复制properties文件的时候,发现一个问题,在EditPlus中打开文件中文可以正常显示,并且是UTF-8的编码格式. 但是将这个文件复制到MyEclipse中再打开时,中 ...