1、封装类
.h
#import <UIKit/UIKit.h>
#define XYQColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define XYQRandomColor XYQColor(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))
@interface BezierCurveView : UIView
//初始化画布
+(instancetype)initWithFrame:(CGRect)frame;
-(void)drawPieChartViewWithX_Value_Names:(NSMutableArray *)x_names TargetValues:(NSMutableArray *)targetValues;
@end
.m
#import "BezierCurveView.h"
static CGRect myFrame;
@interface BezierCurveView ()
@end
@implementation BezierCurveView
//初始化画布
+(instancetype)initWithFrame:(CGRect)frame{
BezierCurveView *bezierCurveView = [[BezierCurveView alloc]init];
bezierCurveView.frame = frame;
//背景视图
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
backView.backgroundColor = [UIColor clearColor];
[bezierCurveView addSubview:backView];
myFrame = frame;
return bezierCurveView;
}
// 画饼状图
-(void)drawPieChartViewWithX_Value_Names:(NSMutableArray *)x_names TargetValues:(NSMutableArray *)targetValues{
//设置圆点
CGPoint point = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
CGFloat startAngle = 0;
CGFloat endAngle ;
CGFloat radius = 100;
//计算总数
__block CGFloat allValue = 0;
[targetValues enumerateObjectsUsingBlock:^(NSNumber *targetNumber, NSUInteger idx, BOOL * _Nonnull stop) {
allValue += [targetNumber floatValue];
}];
//画图
for (int i =0; i<targetValues.count; i++) {
CGFloat targetValue = [targetValues[i] floatValue];
endAngle = startAngle + targetValue/allValue*2*M_PI;
//bezierPath形成闭合的扇形路径
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:point
radius:radius
startAngle:startAngle endAngle:endAngle
clockwise:YES];
[bezierPath addLineToPoint:point];
[bezierPath closePath];
//添加文字
CGFloat X = point.x + 120*cos(startAngle+(endAngle-startAngle)/2) - 10;
CGFloat Y = point.y + 110*sin(startAngle+(endAngle-startAngle)/2) - 10;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(X, Y, 30, 20)];
label.text = x_names[i];
label.font = [UIFont systemFontOfSize:11];
label.textColor = XYQColor(13, 195, 176);
[self.subviews[0] addSubview:label];
//渲染
CAShapeLayer *shapeLayer=[CAShapeLayer layer];
shapeLayer.lineWidth = 1;
shapeLayer.fillColor = XYQRandomColor.CGColor;
shapeLayer.path = bezierPath.CGPath;
[self.layer addSublayer:shapeLayer];
startAngle = endAngle;
}
//画一个白色的圆
UIBezierPath *radiusPath = [UIBezierPath bezierPathWithArcCenter:point radius:50 startAngle:0 endAngle:2*M_PI clockwise:YES];
[radiusPath addLineToPoint:point];
[radiusPath closePath];
CAShapeLayer *shapeLayer=[CAShapeLayer layer];
shapeLayer.lineWidth = 1;
shapeLayer.fillColor = [UIColor whiteColor].CGColor;
shapeLayer.path = radiusPath.CGPath;
[self.layer addSublayer:shapeLayer];
}
2、调用
#define SCREEN_W [UIScreen mainScreen].bounds.size.width
#define SCREEN_H [UIScreen mainScreen].bounds.size.height
//1.初始化
_bezierView = [BezierCurveView initWithFrame:CGRectMake(30, 30, SCREEN_W-60, 280)];
_bezierView.center = self.view.center;
[self.view addSubview:_bezierView];
[_bezierView drawPieChartViewWithX_Value_Names:(NSMutableArray *)@[@"语文",@"数学",@"英语",@"物理",@"化学",@"生物",@"政治",@"历史",@"地理"] TargetValues:(NSMutableArray *)@[@60,@20,@50,@30,@90,@30,@100,@70, @20]];
本站以现代、古代情诗为主,情诗网创办于2013年,以原创爱情诗歌、经典情诗、现代情诗、古代情诗、英文情诗、情诗绝句为主并收集古诗、古诗词、诗歌大全、诗词名句的文学门户。方便您下次继续阅读;可以放在浏览器的收藏夹中(快捷键Ctrl+D);或者看到喜欢或者有趣的诗词可以通过分享按钮给你的好友分享;情诗网是目前最全情诗大全网站之一。并欢迎广大诗歌爱好者阅览投稿!喜欢本站的话请大家把本站告诉给你朋友哦!地址是 www.qingshiwang.com !