饼图
(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
{
//总百分比是100
float 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
{
//总百分比是100
float 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];
//矩形frame
CGFloat 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可以切换比例:
这里介绍的是简易的画图,复杂点的网上有第三方的框架可供使用。