UIBezierPath 画折线图

2024-04-22 03:58
文章标签 折线图 uibezierpath

本文主要是介绍UIBezierPath 画折线图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 效果图

实现

原理很简单,直接发代码

//创建数据NSMutableArray * dataArray = [[NSMutableArray alloc]init];for (int i=0; i<10; i++) {int a = arc4random()%150;[dataArray addObject:[NSNumber numberWithInt:a]];}//创建坐标轴CGFloat XAxisWidth = Device_Width - FIT_WIDTH(23)*2; // X宽度CGFloat YAxisHeight = FIT_HEIGHT(150); // Y高度CGPoint axisOriginPoint = CGPointMake(FIT_WIDTH(23), FIT_HEIGHT(350)); // 起始点坐标//创建路径UIBezierPath * bezierPath = [[UIBezierPath alloc]init];[bezierPath moveToPoint:CGPointMake(FIT_WIDTH(23), axisOriginPoint.y - YAxisHeight)];[bezierPath addLineToPoint:CGPointMake(FIT_WIDTH(23), axisOriginPoint.y)];[bezierPath addLineToPoint:CGPointMake(FIT_WIDTH(23) + XAxisWidth, axisOriginPoint.y)];//画坐标轴CAShapeLayer * XAxisShapeLayer = [[CAShapeLayer alloc]init];XAxisShapeLayer.path = bezierPath.CGPath;XAxisShapeLayer.lineWidth = 2;XAxisShapeLayer.lineJoin = kCALineJoinRound;XAxisShapeLayer.strokeColor = [UIColor lightGrayColor].CGColor;XAxisShapeLayer.fillColor = nil;[self.layer addSublayer:XAxisShapeLayer];[bezierPath removeAllPoints];//创建折线点for (int i=0; i<dataArray.count; i++) {//X坐标文字CATextLayer * textLayer = [[CATextLayer alloc]init];textLayer.string = [NSString stringWithFormat:@"%d",i];textLayer.fontSize = FIT_HEIGHT(11);textLayer.foregroundColor = [UIColor lightGrayColor].CGColor;textLayer.frame = CGRectMake(FIT_WIDTH(23)+XAxisWidth/dataArray.count * i, axisOriginPoint.y +FIT_HEIGHT(5), XAxisWidth/dataArray.count, FIT_HEIGHT(11));textLayer.alignmentMode = kCAAlignmentLeft;textLayer.contentsScale = [UIScreen mainScreen].scale; // 以屏幕适配文字,避免拉伸或者模糊[self.layer addSublayer:textLayer];//x坐标标志UIBezierPath * bezierPath = [[UIBezierPath alloc]init];[bezierPath moveToPoint:CGPointMake(FIT_HEIGHT(23) + XAxisWidth/dataArray.count * i, axisOriginPoint.y)];[bezierPath addLineToPoint:CGPointMake(FIT_HEIGHT(23) + XAxisWidth/dataArray.count * i, axisOriginPoint.y - FIT_HEIGHT(3))];CAShapeLayer * shapeLayerX = [[CAShapeLayer alloc]init];shapeLayerX.path = bezierPath.CGPath;shapeLayerX.lineWidth = 1;shapeLayerX.strokeColor = [UIColor lightGrayColor].CGColor;shapeLayerX.fillColor = nil;[self.layer addSublayer:shapeLayerX];[bezierPath removeAllPoints];//y坐标标志bezierPath = [[UIBezierPath alloc]init];[bezierPath moveToPoint:CGPointMake(FIT_HEIGHT(23) + XAxisWidth/dataArray.count * i, axisOriginPoint.y)];[bezierPath addLineToPoint:CGPointMake(FIT_HEIGHT(23) + XAxisWidth/dataArray.count * i, axisOriginPoint.y - FIT_HEIGHT(3))];CAShapeLayer * shapeLayerY = [[CAShapeLayer alloc]init];shapeLayerY.path = bezierPath.CGPath;shapeLayerY.lineWidth = 1;shapeLayerY.strokeColor = [UIColor lightGrayColor].CGColor;shapeLayerY.fillColor = nil;[self.layer addSublayer:shapeLayerY];[bezierPath removeAllPoints];}//创建数据折线UIBezierPath * brokenBezierPath = [[UIBezierPath alloc]init];CAShapeLayer * brokenShaperLayer = [[CAShapeLayer alloc]init];for (int i=0; i<dataArray.count; i++) {CGFloat height = axisOriginPoint.y - [dataArray[i] floatValue] ;if (i == 0) {[brokenBezierPath moveToPoint:CGPointMake(FIT_WIDTH(23), height)];}else[brokenBezierPath addLineToPoint:CGPointMake(FIT_WIDTH(23) + XAxisWidth/dataArray.count * i , height)];}brokenShaperLayer.path = brokenBezierPath.CGPath;brokenShaperLayer.lineWidth = 2;brokenShaperLayer.strokeColor = Color_248.CGColor;brokenShaperLayer.fillColor = nil;brokenShaperLayer.lineJoin = kCALineJoinRound;[self.layer addSublayer:brokenShaperLayer];/*•速度控制函数(CAMediaTimingFunction)1.kCAMediaTimingFunctionLinear(线性):匀速,给你一个相对静态的感觉2.kCAMediaTimingFunctionEaseIn(渐进):动画缓慢进入,然后加速离开3.kCAMediaTimingFunctionEaseOut(渐出):动画全速进入,然后减速的到达目的地4.kCAMediaTimingFunctionEaseInEaseOut(渐进渐出):动画缓慢的进入,中间加速,然后减速的到达目的地。这个是默认的动画行为。*///通过 strokeEnd 添加简单的动画效果CABasicAnimation * brokenAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];brokenAnimation.fromValue = @0;brokenAnimation.toValue = @1;brokenAnimation.duration = 2;brokenAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];[brokenShaperLayer addAnimation:brokenAnimation forKey:@"brokenAnimation"];/*几种动画方式transform.scale            比例转化     @(0.8)transform.scale.x          宽的比例     @(0.8)transform.scale.y          高的比例     @(0.8)transform.rotation.x       围绕x轴旋转     @(M_PI)transform.rotation.y       围绕y轴旋转     @(M_PI)transform.rotation.z       围绕z轴旋转     @(M_PI)cornerRadius               圆角的设置     @(50)backgroundColor            背景颜色的变化     (id)[UIColor purpleColor].CGColorbounds                     大小,中心不变     [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];position                   位置(中心点的改变)     [NSValue valueWithCGPoint:CGPointMake(300, 300)];contents                   内容,比如UIImageView的图片     imageAnima.toValue = (id)[UIImage imageNamed:@"to"].CGImage;opacity                    透明度     @(0.7)contentsRect.size.width     横向拉伸缩放     @(0.4)最好是0~1之间的*/

总结

柱状图,饼状图都可以这样实现,原理简单就不贴代码了。

 

这篇关于UIBezierPath 画折线图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/924766

相关文章

HighCharts 折线图的实现

Java和HighCharts结合,实现折线图; 代码下载:Java和Highcharts实现折线图

怎么使用matplotlib绘制一个从-2π到2π的sin(x)的折线图-学习篇

首先:如果你的环境中没有安装matplotlib,使用以下命令可以直接安装 pip install matplotlib 如何画一个这样的折线图呢?往下看 想要画一个简单的sin(x)在-2π到2π的折线图,我们要拆分成以下步骤: 先导入相关的库文件 我们需要创建一个数学函数相关的图,需要引入 numpy 我们需要绘制图表,所以需要引入matplotlib来绘制图表创建一个x值的数组从

科研绘图系列:R语言折线图(linechart plots)

介绍 在R语言中,折线图(Line Plot)是一种常用的数据可视化类型,用于展示数据随时间或有序类别变化的趋势。折线图通过连接数据点来形成一条或多条线,这些线条可以清晰地表示数据的变化方向、速度和模式。 加载R包 knitr::opts_chunk$set(warning = F, message = F)library(tidyverse)library(patchwork)li

echarts遍历区域折线图,单线和多线

// 单线折线图drawonelineCharts(){var echarts = require("echarts");var lineCharts = document.getElementsByClassName('lineChart'); // 对应地使用ByClassNamethis.linecolor=['#01FFD4','#1C70DD','#01FFD4','#1C70DD'

【Qt】QChart折线图

引言 Qt绘图(Qt Charts)基于Qt的Graphics View架构,其核心组件是QChartView 和 QChart QChartView是显示图标的视图,基类为QGraphicsViewQChart的基类是QGraphicsltem 可以看作是视图和数据分离,即ViewModel QChartView QChartView 是一个独立的小部件,继承于 QGraphicsVie

vue3中,vue-echarts基本使用(关系图、知识图谱、柱状图、饼图、折线图)

vue3 安装vue-echartsnpm i -S vue-echarts echarts//cnpm 安装cnpm i -S vue-echarts echarts vue2 注意:Vue 2 下使用 vue-echarts,必须还要安装 @vue/composition-api : npm i -D @vue/composition-api//cnpm 安装cnpm i

echarts-折线图

效果图: 源码: <!DOCTYPE html><html style="overflow-x:hidden;overflow-y:auto;"><head><title>折线图</title><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><meta http-equiv="Cache-

canvas 画折线图方法

function creatCanvas(){//需要传入的数据var data = [80,92,104,110,68,50,45];//需要传入的x坐标var time =["11","1.1","13.1","14.1","15.1","16.1","17.1"];// 获取上下文var a_canvas = document.getElementById('a_canvas

QChart绘制折线图

这一篇我们详细介绍图表各个部分的设置和操作,包括图表的标题、图例、边距等属性设置,QLineSeries序列的属性设置,QValueAxis坐标轴的属性设置,以及图标的缩放。(这些应该都是在实际的Qt开发中比较常用的图表操作)先看运行时的界面: 界面设计 工具栏:创建几个Action,并创建工具栏,实现图表数据刷新和缩放功能。 主工作区图标视图:从组件面板放置一个QGraphics V

八爪鱼现金流-034,实际使用,资产折线图

每个月发工资后,记账月报。 回顾资产折线图。 比较大的波动,一次是22年2月左右贷款买房。一次是24年8月宝宝出生。 孩子开销太大。呜呜呜。 话说,这个折线图,能不能加一个标签或者备注呢? 这个需求功能稍后我研究一下。 八爪鱼现金流 八爪鱼