ios 绘图,绘制坐标系,画坐标系
先来看个效果:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
新建视图类。在直接加入代码:
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// 获取当前环境
CGContextRef context = UIGraphicsGetCurrentContext(); // 保存当前环境,便于以后恢复
CGContextSaveGState(context); // 把数据组织起来
for (int i = 0; i < self.dataArray.count; i++)
{
NSString * x_data = self.dataArray[i][@"xxx"];
NSString * y_data = self.dataArray[i][@"yyyy"];
NSString * rate_data = self.dataArray[i][@"rrr"]; [x_array addObject:x_data];
[y_array addObject:y_data];
[rate_array addObject:rate_data];
}
// 矩形绘图区域
CGRect Rectangle = rect;
// 定义一个矩形路径
UIBezierPath *path = [UIBezierPath bezierPathWithRect:Rectangle];
// 将矩形路径画出来
[path stroke]; /////////////////////////////////////////////////////////////////
// 公共数据
float fdistance_left_frame = 30.0; // 左側X轴距离边框的宽度。用于绘制文本
float fdistance_bottom_frame = 15.0; // 左側Y轴距离边框的宽度
float fdistance_right_frame = 10.0; // 左側X轴距离边框的宽度
float fdraw_line_height = rect.size.height - fdistance_bottom_frame; // 绘制坐标的高度
float fdraw_line_width = rect.size.width - fdistance_left_frame
- fdistance_right_frame; // 绘制坐标的宽度 float f_x_axis_scale_number = 7.0; // X轴大刻度数
float f_y_axis_scale_number = 7.0; // Y轴刻度数
float x_unit_distance_scale = 0.0; // X轴刻度的偏移量
float y_unit_distance_scale = 0.0; // Y轴刻度的偏移量
float x_unit_scale = 0.0; // X轴刻度的跨度(一个比例单元)
float y_unit_scale = 0.0; // Y轴刻度的跨度(一个比例单元) // 開始画X轴
float left_bottom_x = rect.origin.x + fdistance_left_frame;
float left_bottom_y = rect.origin.y + fdraw_line_height;
CGPoint point_origin = CGPointMake(left_bottom_x, left_bottom_y); // 坐标轴原点 // 定义一个開始路径
UIBezierPath * x_startPath = [UIBezierPath bezierPath];
[x_startPath setLineWidth:1.5]; [x_startPath moveToPoint:point_origin]; // 设置起点(坐标原点)
for (int x = 0; x < f_x_axis_scale_number; x++) // 画直线
{
x_unit_scale = fdraw_line_height/f_x_axis_scale_number; // 一级等分大刻度
x_unit_distance_scale = x * x_unit_scale; // 相对原点的偏移点
[x_startPath addLineToPoint:CGPointMake(left_bottom_x, left_bottom_y - x_unit_distance_scale)]; // “|”X轴左側绘制文本
float text_height_certer = left_bottom_y;
float text_rect_top = text_height_certer - 8 - x_unit_distance_scale;
float text_rect_bottom = text_height_certer + 8 - x_unit_distance_scale; // +8 -8 。给文字16个像素的高度
float text_rect_height = 16;
CGRect x_axis_rect = CGRectMake(2, text_rect_top, fdistance_left_frame, text_rect_height); CGContextSetLineWidth(context, 1.0);
CGContextSetRGBFillColor (context, 0.5, 0.5, 0.5, 0.5);
UIFont *font = [UIFont boldSystemFontOfSize:12.0]; // 字体用12号
NSString * x_strtext = [NSString stringWithFormat:@"%zi.00",x]; // 绘制X轴刻度值
[x_strtext drawInRect:x_axis_rect withFont:font]; if (0 == x)
{// 为“0”时。不或那个绘制刻度。直接在底部绘制横线“Y”轴
float y_width = fdraw_line_width;
CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色
CGContextMoveToPoint(context, left_bottom_x, left_bottom_y - x_unit_distance_scale);
CGContextAddLineToPoint(context, left_bottom_x + y_width, left_bottom_y - x_unit_distance_scale);
CGContextStrokePath(context); // 開始画Y轴
UIBezierPath * y_startPath = [UIBezierPath bezierPath];
[y_startPath setLineWidth:1.5];
[y_startPath moveToPoint:point_origin]; // Y轴的起始点也是X轴的刻度起始点 for (int y = 0; y < f_y_axis_scale_number + 1; y++) // 画直线
{
y_unit_scale = fdraw_line_width/f_y_axis_scale_number; // 一级等分大刻度
y_unit_distance_scale = y * y_unit_scale; // 相对原点的偏移点
[y_startPath addLineToPoint:CGPointMake(left_bottom_x, left_bottom_y - x_unit_distance_scale)]; // “—”Y轴下部绘制文本
float y_text_left_certer = left_bottom_x;
float y_text_rect_left = y_text_left_certer - 15 + y_unit_distance_scale;
float y_text_rect_top = left_bottom_y + 2;
float y_text_rect_width = y_text_left_certer + 15 + y_unit_distance_scale;
// +10 -10 ,给文字20个像素的宽度
float y_text_rect_height = 16; CGRect y_axis_rect = CGRectMake(y_text_rect_left, y_text_rect_top, y_text_rect_width, y_text_rect_height); CGContextSetLineWidth(context, 1.5); // 线宽度
CGContextSetRGBFillColor (context, 0.5, 0.5, 0.5, 0.5);
UIFont *font = [UIFont boldSystemFontOfSize:12.0]; // 字体用12号
// NSString * y_strtext = [NSString stringWithFormat:@"%zi.00",y];// 绘制Y轴刻度值 NSString * y_strtext = [y_array objectAtIndex:f_y_axis_scale_number - y];
y_strtext = [y_strtext substringFromIndex:5]; // 绘制Y轴刻度值
[y_strtext drawInRect:y_axis_rect withFont:font]; if (y == 0){ } else {
// “—”Y轴上部绘制刻度短线
float fscale_width = 5.0;
CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5); // 线条颜色
CGContextMoveToPoint(context, left_bottom_x + y_unit_distance_scale, left_bottom_y );
CGContextAddLineToPoint(context, left_bottom_x + y_unit_distance_scale, left_bottom_y - fscale_width);
CGContextStrokePath(context);
} }
[y_startPath stroke]; // Draws line 依据坐标点连线 } else
{
// "|"X轴绘制右側刻度短线
float fscale_width = 5.0;
CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);// 线条颜色
CGContextMoveToPoint(context, left_bottom_x, left_bottom_y - x_unit_distance_scale);
CGContextAddLineToPoint(context, left_bottom_x + fscale_width, left_bottom_y - x_unit_distance_scale);
CGContextStrokePath(context);
}
// // 绘制二级小刻度值
// for (int xx = 0; xx < 5; xx++)
// {
// float fsmall_scale_width = 3.0;
// CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);// 线条颜色
// CGContextSetLineWidth(context, 1.0); // 线宽度
// float fsmall_scale_height = x_unit_distance_scale/10.0; // 每一小刻度 的高度不变
// CGContextMoveToPoint(context, point_origin.x, point_origin.y - fsmall_scale_height);
// CGContextAddLineToPoint(context, point_origin.x + fsmall_scale_width, point_origin.y - fsmall_scale_height);
// CGContextStrokePath(context);
// } }
[x_startPath stroke]; // Draws line 依据坐标点连线 [[UIColor blueColor] setFill];
[x_startPath fill]; // "|"X轴绘制虚线。横向虚线
CGFloat dashArray[] = {2.0, 2.0};
CGContextSetLineDash(context, 0, dashArray, 2);
CGContextSetRGBStrokeColor(context,0.5, 0.5, 0.5, 0.5);// 线条颜色
for (int x = 1; x < f_x_axis_scale_number + 1; x++) // 画虚线
{
x_unit_distance_scale = x * (x_unit_scale); // 一级等分大刻度
CGContextMoveToPoint(context, left_bottom_x + 5, left_bottom_y - x_unit_distance_scale);
CGContextAddLineToPoint(context, left_bottom_x + fdraw_line_width, left_bottom_y - x_unit_distance_scale);
}
for (int y = 1; y < f_y_axis_scale_number + 1; y++) // 画虚线
{
y_unit_distance_scale = y * (y_unit_scale); // 一级等分大刻度
CGContextMoveToPoint(context, point_origin.x + y_unit_distance_scale, point_origin.y - 5);
CGContextAddLineToPoint(context, point_origin.x + y_unit_distance_scale, point_origin.y - fdraw_line_height + fdistance_bottom_frame + 3);
}
CGContextStrokePath(context); // 開始绘制曲线图
CGContextSetLineDash(context, 0.0,NULL, 0); // 还原画笔
CGContextSetLineWidth(context,1.0); // 设置为实线画笔
CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 0.5); // 线条颜色 for (int a = 0; a < x_array.count; a++)
{
// Y轴日期倒着遍历,这里数据也倒着遍历
float fdata = [[x_array objectAtIndex: x_array.count-1 - a] floatValue];
CGPoint data_point = CGPointMake(point_origin.x + a * y_unit_scale, point_origin.y - fdata * x_unit_scale); // 坐标轴原点 if (0 == a)
{
CGContextMoveToPoint(context, data_point.x, data_point.y);
}
else
{
CGContextAddLineToPoint(context, data_point.x, data_point.y);
}
NSLog(@"%zi == (%f, %f)", a, data_point.x, data_point.y); }
CGContextStrokePath(context); // 開始边框圆点
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);//画笔线的颜色
CGContextSetLineWidth(context, 2.0);//线的宽度
//void CGContextAddArc(CGContextRef c,CGFloat x, CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle, int clockwise)1弧度=180°/π (≈57.3°) 度=弧度×180°/π 360°=360×π/180 =2π 弧度
// x,y为圆点坐标,radius半径,startAngle为開始的弧度,endAngle为 结束的弧度,clockwise 0为顺时针,1为逆时针。 for (int a = 0; a < x_array.count; a++)
{
// Y轴日期倒着遍历,这里数据也倒着遍历
float fdata = [[x_array objectAtIndex: x_array.count-1 - a] floatValue];
CGPoint data_point = CGPointMake(point_origin.x + a * y_unit_scale, point_origin.y - fdata * x_unit_scale); // 坐标轴原点 CGContextAddArc(context, data_point.x, data_point.y, 1, 0, 2 * PI, 0); //加入一个圆 }
CGContextDrawPath(context, kCGPathStroke); //绘制路径
} Bible 2015-07-15 17:24:10 // 文件头部加入 宏定义
#define PI 3.1415926
源代码下载:http://download.csdn.net/detail/bible521125/8902893
c++ 转 ios 不久 用的有点乱,如须要能够自己整理。
ios 绘图,绘制坐标系,画坐标系的更多相关文章
- iOS开发必会的坐标系探究
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由落影发表于云+社区专栏 前言 app在渲染视图时,需要在坐标系中指定绘制区域. 这个概念看似乎简单,事实并非如此. When an a ...
- MFC图形绘制——绘制直尺和坐标系
一.实验目的 1.掌握建立MFC应用程序的方法: 2.掌握映射模式. 二.实验内容 1.在MFC中绘制直尺,直尺需要有刻度,类似于日常学生使用的透明塑料直尺,需要建立四个直尺,分别分布在屏幕客户区的上 ...
- [转]iOS开发中的火星坐标系及各种坐标系转换算法
iOS开发中的火星坐标系及各种坐标系转换算法 源:https://my.oschina.net/u/2607703/blog/619183 其原理是这样的:保密局开发了一个系统,能将实际的坐标转 ...
- iOS绘图教程 (转,拷贝以记录)
本文是<Programming iOS5>中Drawing一章的翻译,考虑到主题完整性,在翻译过程中我加入了一些书中没有涉及到的内容.希望本文能够对你有所帮助. 转自:http://www ...
- iOS绘图教程
本文是<Programming iOS5>中Drawing一章的翻译,考虑到主题完整性,翻译版本中加入了一些书中未涉及到的内容.希望本文能够对你有所帮助.(本文由海水的味道翻译整理,转载请 ...
- iOS绘图系统UIKit与Core Graphics
概述 iOS主要的绘图系统有UIKit,Core Graphics,Core Animation,Core Image,Open GL等,本片博文主要介绍UIKit与Core Graphics的绘图系 ...
- 论文第4章:iOS绘图平台的实现
面向移动设备的矢量绘图平台设计与实现 Design and Implementation of Mobile Device-oriented Vector Drawing Platform 引用本论文 ...
- iOS绘图框架CoreGraphics分析
由于CoreGraphics框架有太多的API,对于初次接触或者对该框架不是十分了解的人,在绘图时,对API的选择会感到有些迷茫,甚至会觉得iOS的图形绘制有些繁琐.因此,本文主要介绍一下iOS的绘图 ...
- IOS绘图
#import "ViewController.h" #import "DrawView.h" @interface ViewController () @pr ...
随机推荐
- 解决无法启动“start web server”:
1.提示1080端口被占用: Cmd——Netstat -ano——找到端口号为1080的pid——打开任务管理器——干掉pid 2.inter error:your request was unsu ...
- 使用Python进行多线程检查.moe三位剩余有效域名
翻看博客看到一段不错的代码 虽然近期没有购买域名的需求 不过日后有购买域名的需求的话 稍作修改直接使用还是很方便的 import threading import requests import js ...
- Unable to load annotation processor factory
很多人在项目开发中都会遇到项目名称左上角有个红叉,有些是Jar问题,有些是代码问题,有些是编译问题,对于我这种强迫症的是受不了这种情况发生的,如果不影响项目启动还好,废话少说,今天工作就出现了一个问题 ...
- redis做成windows服务
打开cmd切换到redis根目录 执行安装命令 redis-server.exe --service-install redis.windows.conf --loglevel verbose 卸载 ...
- mac pro配置php开发环境
mac pro自带php和apache,所以我们只要配置下就好了 // 启动Apache服务 sudo apachectl start // 重启Apache服务 sudo apachectl res ...
- python scrapy爬取HBS 汉堡南美航运公司柜号信息
下面分享个scrapy的例子 利用scrapy爬取HBS 船公司柜号信息 1.前期准备 查询提单号下的柜号有哪些,主要是在下面的网站上,输入提单号,然后点击查询 https://www.hamburg ...
- Python求阴影部分面积
一.前言说明 今天看到微信群里一道六年级数学题,如下图,求阴影部分面积 看起来似乎并不是很难,可是博主添加各种辅助线,写各种方法都没出来,不得已而改用写Python代码来求面积了 二.思路介绍 1.用 ...
- python 序列化和反序列化
概念 序列化: 将对象的状态信息转换为可以存储或传输的形式的过程.就是把对象转换成字符串的过程 反序列化: 把字符串转换成python可以识别的数据类型对象的过程 应用 #数据存储 #网络传输 模块 ...
- Unity Microphone 无限时长录制
原创文章:转载请标明出处--博客园 Jason_c Unity可以很方便的通过 Microphone.Start()方法来调用麦克风,但是有一个弊端是,必须传入时长,这就很尴尬了,因为大多数时间,我们 ...
- sshd修改监听端口
vi /etc/sshd/sshd_config ListenAddress 0.0.0.0 #修改为 ListenAddress 192.168.0.1 #代表只监听192.168.0.1的SSH请 ...