1、封裝類
.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))
#define MARGIN 30 // 坐標(biāo)軸與畫(huà)布間距
#define Y_EVERY_MARGIN 20 // y軸每一個(gè)值的間隔數(shù)
#import <UIKit/UIKit.h>
@interface BezierCurveView : UIView
//初始化畫(huà)布
+(instancetype)initWithFrame:(CGRect)frame;
/**
* 畫(huà)柱狀圖
* @param x_names x軸值的所有值名稱
* @param targetValues 所有目標(biāo)值
*/
-(void)drawBarChartViewWithX_Value_Names:(NSMutableArray *)x_names TargetValues:(NSMutableArray *)targetValues;
@end
.m
#import "BezierCurveView.h"
static CGRect myFrame;
@interface BezierCurveView ()
@end
@implementation BezierCurveView
//初始化畫(huà)布
+(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;
}
/**
* 畫(huà)坐標(biāo)軸
*/
-(void)drawXYLine:(NSMutableArray *)x_names{
UIBezierPath *path = [UIBezierPath bezierPath];
//1.Y軸偿短、X軸的直線
[path moveToPoint:CGPointMake(MARGIN, CGRectGetHeight(myFrame)-MARGIN)];
[path addLineToPoint:CGPointMake(MARGIN, MARGIN)];
[path moveToPoint:CGPointMake(MARGIN, CGRectGetHeight(myFrame)-MARGIN)];
[path addLineToPoint:CGPointMake(CGRectGetWidth(myFrame), CGRectGetHeight(myFrame)-MARGIN)];
// //2.添加箭頭
// [path moveToPoint:CGPointMake(MARGIN, MARGIN)];
// [path addLineToPoint:CGPointMake(MARGIN-5, MARGIN+5)];
// [path moveToPoint:CGPointMake(MARGIN, MARGIN)];
// [path addLineToPoint:CGPointMake(MARGIN+5, MARGIN+5)];
//
// [path moveToPoint:CGPointMake(CGRectGetWidth(myFrame), CGRectGetHeight(myFrame)-MARGIN)];
// [path addLineToPoint:CGPointMake(CGRectGetWidth(myFrame)-5, CGRectGetHeight(myFrame)-MARGIN-5)];
// [path moveToPoint:CGPointMake(CGRectGetWidth(myFrame), CGRectGetHeight(myFrame)-MARGIN)];
// [path addLineToPoint:CGPointMake(CGRectGetWidth(myFrame)-5, CGRectGetHeight(myFrame)-MARGIN+5)];
//3.添加索引格
//X軸
for (int i=0; i<x_names.count; i++) {
CGFloat X = MARGIN + (CGRectGetWidth(myFrame)-60)/x_names.count*(i+1);
CGPoint point = CGPointMake(X,CGRectGetHeight(myFrame)-MARGIN);
[path moveToPoint:point];
[path addLineToPoint:CGPointMake(point.x, point.y-3)];
}
//Y軸(實(shí)際長(zhǎng)度為200,此處比例縮小一倍使用)
for (int i=0; i<11; i++) {
CGFloat Y = CGRectGetHeight(myFrame)-MARGIN-Y_EVERY_MARGIN*i;
CGPoint point = CGPointMake(MARGIN,Y);
[path moveToPoint:point];
[path addLineToPoint:CGPointMake(point.x+3, point.y)];
}
//4.添加索引格文字
//X軸
for (int i=0; i<x_names.count; i++) {
CGFloat X = MARGIN + (CGRectGetWidth(myFrame)-60)/x_names.count/2.0 + (CGRectGetWidth(myFrame)-60)/x_names.count*i;
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(X, CGRectGetHeight(myFrame)-MARGIN, (CGRectGetWidth(myFrame)-60)/x_names.count, 20)];
textLabel.text = x_names[i];
textLabel.font = [UIFont systemFontOfSize:10];
textLabel.textAlignment = NSTextAlignmentCenter;
textLabel.textColor = [UIColor blueColor];
[self addSubview:textLabel];
}
//Y軸
for (int i=0; i<11; i++) {
CGFloat Y = CGRectGetHeight(myFrame)-MARGIN-Y_EVERY_MARGIN*i;
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, Y-5, MARGIN, 10)];
textLabel.text = [NSString stringWithFormat:@"%d",10*i];
textLabel.font = [UIFont systemFontOfSize:10];
textLabel.textAlignment = NSTextAlignmentCenter;
textLabel.textColor = [UIColor redColor];
[self addSubview:textLabel];
}
//5.渲染路徑
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = path.CGPath;
shapeLayer.strokeColor = [UIColor blackColor].CGColor;
shapeLayer.fillColor = [UIColor clearColor].CGColor;
shapeLayer.borderWidth = 2.0;
[self.subviews[0].layer addSublayer:shapeLayer];
}
/**
* 畫(huà)柱狀圖
*/
-(void)drawBarChartViewWithX_Value_Names:(NSMutableArray *)x_names TargetValues:(NSMutableArray *)targetValues{
//1.畫(huà)坐標(biāo)軸
[self drawXYLine:x_names];
//2.每一個(gè)目標(biāo)值點(diǎn)坐標(biāo)
for (int i=0; i<targetValues.count; i++) {
CGFloat doubleValue = 2*[targetValues[i] floatValue]; //目標(biāo)值放大兩倍
CGFloat X = MARGIN + (CGRectGetWidth(myFrame)-30)/x_names.count*(i+1)-(CGRectGetWidth(myFrame)-30)/x_names.count/2.0;
CGFloat Y = CGRectGetHeight(myFrame)-MARGIN-doubleValue;
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(X-MARGIN/2, Y, MARGIN-10, doubleValue)];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = path.CGPath;
shapeLayer.strokeColor = [UIColor clearColor].CGColor;
shapeLayer.fillColor = XYQRandomColor.CGColor;
shapeLayer.borderWidth = 2.0;
[self.subviews[0].layer addSublayer:shapeLayer];
//3.添加文字
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(X-MARGIN/2, Y-20, MARGIN-10, 20)];
label.text = [NSString stringWithFormat:@"%.0lf",(CGRectGetHeight(myFrame)-Y-MARGIN)/2];
label.textColor = [UIColor purpleColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont systemFontOfSize:10];
[self.subviews[0] addSubview:label];
}
}
2帽芽、調(diào)用
#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 drawBarChartViewWithX_Value_Names:(NSMutableArray *)@[@"語(yǔ)文",@"數(shù)學(xué)",@"英語(yǔ)",@"物理",@"化學(xué)",@"生物",@"政治",@"歷史",@"地理"] TargetValues:(NSMutableArray *)@[@60,@20,@50,@30,@90,@30,@100,@70, @20]];