日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IOS开发-画饼图画柱状图

發布時間:2024/1/18 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS开发-画饼图画柱状图 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

餅圖

(1)自定義一個view
(2)導入QuartzCore框架
(3)m文件代碼如下:

#import "ZCView.h" #import <QuartzCore/QuartzCore.h>//隨機浮點數 #define ARC4RANDOM_MAX 0x100000000 // 顏色 #define ZCColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0] // 隨機色 #define ZCRandomColor ZCColor(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))@interface ZCView () @property(nonatomic,weak)UILabel *progressLabel; @end@implementation ZCView- (NSArray *)getPercentArray {//總百分比是100float total = 100.0;//要把餅圖分成count份int count = arc4random_uniform(15)+1;//記錄的數組NSMutableArray *tempArray = [NSMutableArray array];//臨時存值float temp = 0;for (int i = 0; i<count; i++) {//使用arc4random() 來獲取0到100之間浮點數了(精度是rand()的兩倍),代碼如下:temp = floorf(((double)arc4random() / ARC4RANDOM_MAX) * total);[tempArray addObject:@(temp)];total -= temp;}if(total){[tempArray addObject:@(total)];}return tempArray; }- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {[self setNeedsDisplay]; }- (void)drawRect:(CGRect)rect {//線寬CGFloat lineWidth = 5;//半徑CGFloat radius = (rect.size.width-lineWidth*4)/2;//圓心CGPoint center = CGPointMake(rect.size.width/2, rect.size.width/2);//比例數組NSArray *percentArray = [self getPercentArray];//開始弧度CGFloat startAngle = 0;//所占弧度CGFloat angle = 0;//結束弧度CGFloat endAngle = 0;for (int i = 0; i<percentArray.count; i++) {startAngle = endAngle;angle = [percentArray[i] floatValue] / 100.0 * M_PI * 2;endAngle = startAngle + angle;UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];[path addLineToPoint:center];[ZCRandomColor set];[path fill];} }

效果如下(點擊自定義view會更換比例):

柱狀圖

(1)自定義一個view
(2)導入QuartzCore框架
(3)m文件代碼如下:

#import "ZCView.h" #import <QuartzCore/QuartzCore.h>//隨機浮點數 #define ARC4RANDOM_MAX 0x100000000 // 顏色 #define ZCColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0] // 隨機色 #define ZCRandomColor ZCColor(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))@interface ZCView () @property(nonatomic,weak)UILabel *progressLabel; @end@implementation ZCView - (NSArray *)getPercentArray {//總百分比是100float total = 100.0;//要把餅圖分成count份int count = arc4random_uniform(6)+1;//記錄的數組NSMutableArray *tempArray = [NSMutableArray array];//臨時存值float temp = 0;for (int i = 0; i<count; i++) {//使用arc4random() 來獲取0到100之間浮點數了(精度是rand()的兩倍),代碼如下:temp = floorf(((double)arc4random() / ARC4RANDOM_MAX) * total);[tempArray addObject:@(temp)];total -= temp;}if(total){[tempArray addObject:@(total)];}return tempArray; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {[self setNeedsDisplay]; } - (void)drawRect:(CGRect)rect {//比例數組NSArray *percentArray = [self getPercentArray];//矩形frameCGFloat X = 0;CGFloat Y = 0;CGFloat W = 0;CGFloat H = 0;for (int i = 0; i < percentArray.count; i++) {W = rect.size.width / (2*percentArray.count-1);X = 2 * W * i;H = [percentArray[i] floatValue] / 100.0 * rect.size.height;Y = rect.size.height - H;UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(X, Y, W, H)];[ZCRandomColor set];[path fill];} }

效果如下,點擊自定義view可以切換比例:

這里介紹的是簡易的畫圖,復雜點的網上有第三方的框架可供使用。

總結

以上是生活随笔為你收集整理的IOS开发-画饼图画柱状图的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。