-
Notifications
You must be signed in to change notification settings - Fork 683
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
168 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// MPBarChartView.h | ||
// MobileProject | ||
// | ||
// Created by wujunyang on 2017/5/19. | ||
// Copyright © 2017年 wujunyang. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
@interface MPBarChartView : UIView | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// MPBarChartView.m | ||
// MobileProject | ||
// | ||
// Created by wujunyang on 2017/5/19. | ||
// Copyright © 2017年 wujunyang. All rights reserved. | ||
// | ||
|
||
#import "MPBarChartView.h" | ||
|
||
#define BAR_HEIGHT_COEFFICIENT 0.9 | ||
|
||
@implementation MPBarChartView | ||
|
||
- (void)drawRect:(CGRect)rect{ | ||
NSArray *arry = @[@300,@232.233,@324.324,@34,@435,@43.0]; | ||
|
||
// 计算bar的宽度 | ||
CGFloat barW = self.bounds.size.width / (arry.count * 2 - 1); | ||
|
||
// 找出数组中的最大数值 | ||
CGFloat maxValue = [[arry valueForKeyPath:@"@max.floatValue"] floatValue]; | ||
|
||
for (NSInteger i = 0; i < arry.count; i++) { | ||
// 计算bar的高度 | ||
CGFloat barH = [arry[i] floatValue] * (self.bounds.size.height * BAR_HEIGHT_COEFFICIENT/ maxValue); | ||
|
||
// 计算bar的XY | ||
CGFloat barX = barW * i * 2; | ||
CGFloat barY = self.bounds.size.height - barH; | ||
|
||
// 绘制矩形 | ||
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(barX, barY, barW, barH)]; | ||
|
||
// 给矩形添加随机色 | ||
[[self randomUIColor] set]; | ||
|
||
[path fill]; | ||
|
||
|
||
} | ||
} | ||
|
||
- (UIColor *)randomUIColor{ | ||
UIColor *color = [UIColor colorWithRed:(arc4random_uniform(256) / 255.0) green:(arc4random_uniform(256) / 255.0) blue:(arc4random_uniform(256) / 255.0) alpha:(arc4random_uniform(256) / 255.0)]; | ||
return color; | ||
} | ||
|
||
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ | ||
|
||
[self setNeedsDisplay]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// MPPieChartView.h | ||
// MobileProject | ||
// | ||
// Created by wujunyang on 2017/5/19. | ||
// Copyright © 2017年 wujunyang. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
@interface MPPieChartView : UIView | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// MPPieChartView.m | ||
// MobileProject | ||
// | ||
// Created by wujunyang on 2017/5/19. | ||
// Copyright © 2017年 wujunyang. All rights reserved. | ||
// | ||
|
||
#import "MPPieChartView.h" | ||
|
||
@implementation MPPieChartView | ||
|
||
- (void)drawRect:(CGRect)rect { | ||
// Drawing code | ||
|
||
NSArray *arry = @[@300,@232.233,@324.324,@34,@4352,@43.0]; | ||
|
||
// 计算数组中所有数值之和 | ||
CGFloat sumValue = [[arry valueForKeyPath:@"@sum.floatValue"] floatValue]; | ||
|
||
//设定圆弧的圆点、起始弧度 | ||
CGPoint origin = CGPointMake(80, 80); | ||
CGFloat startAngle = 0; | ||
CGFloat endAngle = 0; | ||
|
||
|
||
for (NSInteger i = 0 ; i < arry.count; i++) { | ||
|
||
// 每个数据的弧度 | ||
CGFloat angle = [arry[i] floatValue] * M_PI * 2 / sumValue; | ||
|
||
// 计算这一段弧度的结束为止 | ||
endAngle = startAngle + angle; | ||
|
||
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:origin radius:40 startAngle:startAngle endAngle:endAngle clockwise:YES]; | ||
|
||
// 计算下一段弧度开始的位置 | ||
startAngle = endAngle; | ||
|
||
// 从弧边,绘制到原点。用于封闭路径,可以绘制扇形 | ||
[path addLineToPoint:origin]; | ||
// 给扇形添加随机色 | ||
[[self randomUIColor] set]; | ||
[path fill]; | ||
} | ||
|
||
} | ||
|
||
- (UIColor *)randomUIColor{ | ||
UIColor *color = [UIColor colorWithRed:(arc4random_uniform(256) / 255.0) green:(arc4random_uniform(256) / 255.0) blue:(arc4random_uniform(256) / 255.0) alpha:(arc4random_uniform(256) / 255.0)]; | ||
return color; | ||
} | ||
//触屏后重新渲染 | ||
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ | ||
[self setNeedsDisplay]; | ||
} | ||
|
||
@end |