iOS画折线图
代码例子效果: 下载地址:http://download.csdn.net/detail/qqmcy/6983187
LineChartViewDemo.h
- #import <UIKit/UIKit.h>
- @interface LineChartViewDemo : UIView
- //横竖轴距离间隔
- @property (assign) NSInteger hInterval;
- @property (assign) NSInteger vInterval;
- //横竖轴显示标签
- @property (nonatomic, strong) NSArray *hDesc;
- @property (nonatomic, strong) NSArray *vDesc;
- //点信息
- @property (nonatomic, strong) NSArray *array;
- @property (nonatomic, strong) NSArray* array1;
- @end
LineChartViewDemo.m
- #import "LineChartViewDemo.h"
- #import "EColumnChartLabel.h"
- #define KlineHeight 30
- @interface LineChartViewDemo()
- {
- CALayer *linesLayer;
- UIView *popView;
- UILabel *disLabel;
- int x;
- int y;
- }
- @end
- @implementation LineChartViewDemo
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- // Initialization code
- self.backgroundColor = [UIColor clearColor];
- x = frame.size.width;
- y = frame.size.height;
- _hInterval = 10;
- _vInterval = 50;
- linesLayer = [[CALayer alloc] init];
- linesLayer.masksToBounds = YES;
- linesLayer.contentsGravity = kCAGravityLeft;
- linesLayer.backgroundColor = [[UIColor redColor] CGColor];
- //[self.layer addSublayer:linesLayer];
- //PopView
- popView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, 30)];
- [popView setBackgroundColor:[UIColor whiteColor]];
- [popView setAlpha:0.0f];
- disLabel = [[UILabel alloc]initWithFrame:popView.frame];
- [disLabel setTextAlignment:NSTextAlignmentCenter];
- [popView addSubview:disLabel];
- [self addSubview:popView];
- }
- return self;
- }
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect
- {
- [self setClearsContextBeforeDrawing: YES];
- CGContextRef context = UIGraphicsGetCurrentContext();
- //画背景线条------------------
- CGColorRef backColorRef = [UIColor redColor].CGColor;
- CGFloat backLineWidth = 0.5f;
- CGFloat backMiterLimit = 0.f;
- CGContextSetLineWidth(context, backLineWidth);//主线宽度
- // CGContextSetMiterLimit(context, backMiterLimit);//投影角度
- //CGContextSetShadowWithColor(context, CGSizeMake(3, 5), 8, backColorRef);//设置双条线
- CGContextSetLineJoin(context, kCGLineJoinRound);
- CGContextSetLineCap(context, kCGLineCapRound );
- CGContextSetBlendMode(context, kCGBlendModeNormal);
- CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
- // CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:225.0f / 255.0f green:225.0f / 255.0f blue:225.0f / 255.0f alpha:1.0].CGColor);
- // int x = 400 ;
- // //Y轴横线
- // int y = 300 ;
- int tempY = y;
- //添加纵轴标签和线
- for (int i=0; i<_vDesc.count; i++) {
- CGPoint bPoint = CGPointMake(30, tempY);
- CGPoint ePoint = CGPointMake(x, tempY);
- EColumnChartLabel *label = [[EColumnChartLabel alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
- [label setCenter:CGPointMake(bPoint.x-15, bPoint.y-30)];
- [label setTextAlignment:NSTextAlignmentCenter];
- [label setBackgroundColor:[UIColor clearColor]];
- [label setTextColor:[UIColor blackColor]];
- [label setText:[_vDesc objectAtIndex:i]];
- [self addSubview:label];
- CGContextMoveToPoint(context, bPoint.x, bPoint.y-30);
- CGContextAddLineToPoint(context, ePoint.x, ePoint.y-30);
- tempY -= y * 0.1;
- }
- for (int i=0; i<_array.count-1; i++) {
- EColumnChartLabel *label = [[EColumnChartLabel alloc]initWithFrame:CGRectMake(i*KlineHeight+30, y - 30, 40, 30)];
- [label setTextAlignment:NSTextAlignmentCenter];
- [label setBackgroundColor:[UIColor clearColor]];
- [label setTextColor:[UIColor redColor]];
- label.numberOfLines = 1;
- label.adjustsFontSizeToFitWidth = YES;
- label . minimumFontSize = 1.0f;
- [label setText:[_hDesc objectAtIndex:i]];
- [self addSubview:label];
- }
- CGContextStrokePath(context);
- // //画点线条------------------
- CGColorRef pointColorRef = [UIColor colorWithRed:24.0f/255.0f green:116.0f/255.0f blue:205.0f/255.0f alpha:1.0].CGColor;
- CGFloat pointLineWidth = 1.5f;
- CGFloat pointMiterLimit = 5.0f;
- CGContextSetLineWidth(context, pointLineWidth);//主线宽度
- CGContextSetMiterLimit(context, pointMiterLimit);//投影角度
- //CGContextSetShadowWithColor(context, CGSizeMake(3, 5), 8, pointColorRef);//设置双条线
- CGContextSetLineJoin(context, kCGLineJoinRound);
- CGContextSetLineCap(context, kCGLineCapRound );
- CGContextSetBlendMode(context, kCGBlendModeNormal);
- //CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
- UIColor* color1 = c_BeforeLastYear;
- [color1 set];
- //绘图
- CGPoint p1 = [[_array objectAtIndex:0] CGPointValue];
- int i = 0;
- //获取视图的高
- int tempY1 = y;
- int tempWidth = y * 0.1f;
- float tempHeight = y * (270.0 / 320.0);
- float tempHeight1 = y * (20.0f / 320.0f);
- // NSLog(@"%f");
- CGContextMoveToPoint(context, p1.x + 20, tempHeight-p1.y*tempWidth/tempHeight1);
- for (; i<[_array count]; i++)
- {
- p1 = [[_array objectAtIndex:i] CGPointValue];
- CGPoint goPoint = CGPointMake(p1.x + 20, tempHeight-p1.y*tempWidth/tempHeight1);
- CGContextAddLineToPoint(context, goPoint.x, goPoint.y);;
- //添加触摸点
- UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
- [bt setBackgroundColor:[UIColor redColor]];
- [bt setFrame:CGRectMake(0, 0, 10, 10)];
- [bt setCenter:goPoint];
- [bt addTarget:self
- action:@selector(btAction:)
- forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:bt];
- }
- CGContextStrokePath(context);
- // CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
- UIColor* color = c_LastYear;
- [color set];
- //绘图
- p1 = [[_array1 objectAtIndex:0] CGPointValue];
- i = 0;
- CGContextMoveToPoint(context, p1.x + 20, tempHeight-p1.y*tempWidth/tempHeight1);
- for (; i<[_array1 count]; i++)
- {
- p1 = [[_array1 objectAtIndex:i] CGPointValue];
- CGPoint goPoint = CGPointMake(p1.x + 20, tempHeight-p1.y*tempWidth/tempHeight1);
- CGContextAddLineToPoint(context, goPoint.x, goPoint.y);;
- //添加触摸点
- UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
- [bt setBackgroundColor:[UIColor redColor]];
- [bt setFrame:CGRectMake(0, 0, 10, 10)];
- [bt setCenter:goPoint];
- [bt addTarget:self
- action:@selector(btAction:)
- forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:bt];
- }
- CGContextStrokePath(context);
- }
- - (void)btAction:(id)sender{
- [disLabel setText:@"400"];
- UIButton *bt = (UIButton*)sender;
- popView.center = CGPointMake(bt.center.x, bt.center.y - popView.frame.size.height/2);
- [popView setAlpha:1.0f];
- }
ViewController.m
- #import "LineChartViewDemo.h"
- #define CC_DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) * 0.01745329252f)
- #define KlineHeight 20
- #define KlineWidth 30
- @interface ViewController ()
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- LineChartViewDemo* line = [[LineChartViewDemo alloc] initWithFrame:CGRectMake(0, 0, 548, 250)];
- // line.layer.transform = CATransform3DMakeRotation(CC_DEGREES_TO_RADIANS(90), 0, 0, 1);
- NSMutableArray *pointArr = [[NSMutableArray alloc]init];
- //生成随机点 1
- //[pointArr addObject:[NSValue valueWithCGPoint:CGPointMake(KlineWidth*0, 0)]];
- for (int i = 0; i < 12; i++) {
- [pointArr addObject:[NSValue valueWithCGPoint:CGPointMake(KlineWidth* (i+1), 55 * i)]];
- }
- NSMutableArray* pointArr2 = [NSMutableArray array];
- //生成随机点 2
- for (int i = 0; i < 12; i++) {
- [pointArr2 addObject:[NSValue valueWithCGPoint:CGPointMake(KlineWidth* (i + 1), 110 * i)]];
- }
- //竖轴
- NSMutableArray *vArr = [[NSMutableArray alloc]initWithCapacity:pointArr.count-1];
- for (int i=0; i<9; i++) {
- [vArr addObject:[NSString stringWithFormat:@"%d",i*20]];
- }
- //横轴
- NSMutableArray *hArr = [[NSMutableArray alloc]initWithCapacity:pointArr.count-1];
- for (int i = 1; i <= 12; i++) {
- [hArr addObject:[NSString stringWithFormat:@"%d",i]];
- }
- [line setHDesc:hArr];
- [line setVDesc:vArr];
- [line setArray:pointArr];
- [line setArray1:pointArr2];
- [self.view addSubview:line];
- }
iOS画折线图的更多相关文章
- python中matplotlib画折线图实例(坐标轴数字、字符串混搭及标题中文显示)
最近在用python中的matplotlib画折线图,遇到了坐标轴 "数字+刻度" 混合显示.标题中文显示.批量处理等诸多问题.通过学习解决了,来记录下.如有错误或不足之处,望请指 ...
- SAS 画折线图PROC GPLOT
虽然最后做成PPT里的图表会被要求用EXCEL画,但当我们只是在分析的过程中,想看看数据的走势,直接在SAS里画会比EXCEL画便捷的多. 修改起来也会更加的简单,,不用不断的修改程序然后刷新EXCE ...
- Matplotlib学习---用matplotlib画折线图(line chart)
这里利用Jake Vanderplas所著的<Python数据科学手册>一书中的数据,学习画图. 数据地址:https://raw.githubusercontent.com/jakevd ...
- echars画折线图的一种数据处理方式
echars画折线图的一种数据处理方式 <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...
- 使用OpenCV画折线图
使用OpenCV画直方图是一件轻松的事情,画折线图就没有那么Easy了,还是使用一个库吧: GraphUtils 源代码添加入工程 原文链接:http://www.360doc.com/content ...
- IOS使用Core-Plot画折线图
关于Core-Plot的配置.大家能够參考我的上一篇博客:http://1.wildcat.sinaapp.com/?p=99 版权全部.转载请注明原文转自:http://blog.csdn.net/ ...
- gnuplot画折线图
之前尝试用jfreechart画自定义横坐标的折线图或时序图,发现很复杂,后来改用gnuplot了. gnuplot在网上一搜就能找到下载地址. 安装完成后,主要是命令行形式的交互界面,至少比jfre ...
- 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 ...
- echarts入门基础,画折线图
注意:一定要自己引入echarts库 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
随机推荐
- Ajax提交form表单内容和文件(jQuery.form.js)
jQuery官网是这样介绍form.js A simple way to AJAX-ify any form on your page; with file upload and progress s ...
- 图论之二分图-HihoCoder1121
题目链接:https://hihocoder.com/problemset/problem/1121 二分图的相关概念:https://blog.csdn.net/qq_36345036/articl ...
- Linux学习之分区自动挂载与fstab文件修复(九)
linux分区自动挂载与fstab文件修复 在前面我们实现新添加硬盘,进行分区与格式化,然后手动挂载,这样做,在重启后,需要重新挂载才能使用. https://www.cnblogs.com/-wen ...
- Get package name
public class GetPackageName { public static void main(String[] args) { GetPackageName obj = new GetP ...
- 模拟页面获取的php数据(四)
<?php $tqzf = [ "aData" => [//通勤方式 "trafficType" => [ 0 => [ "t ...
- 8.6 正睿暑期集训营 Day3
目录 2018.8.6 正睿暑期集训营 Day3 A 亵渎(DP) B 绕口令(KMP) C 最远点(LCT) 考试代码 A B C 2018.8.6 正睿暑期集训营 Day3 时间:5h(实际) 期 ...
- Java多线程:CAS与java.util.concurrent.atomic
锁的几种概念 悲观锁 总是假设最坏的情况,每次获取数据都认为别人会修改,所以拿数据时会上锁,一直到释放锁不允许其他线程修改数据.Java中如synchronized和reentrantLock就是这种 ...
- Hbase多版本的读写(Shell&Java API版)
Hbase是基于HDFS的NOsql数据库,它很多地方跟数据库差不多,也有很多不同的地方.这里就不一一列举了,不过Hbase有个版本控制的特性,这个特性在很多场景下都会发挥很大的作用.本篇就介绍下基于 ...
- java合并单元格同时导出excel
POI进行跨行需要用到对象HSSFSheet对象,现在就当我们程序已经定义了一个HSSFSheet对象sheet. 跨第1行第1个到第2个单元格的操作为 sheet.addMergedRegion(n ...
- Java和.Net在做BS结构项目的比较
渊源: Java的J2EE在1999年形成了其成熟的架构,并且到今天已经有相当成熟的经过检验的企业应用系统.而.Net究其渊源是源自微软以前开发企业应用程序的平台DNA(DistributedNetw ...