Skip to content

Commit

Permalink
增加关于柱状 饼图的实例
Browse files Browse the repository at this point in the history
  • Loading branch information
wujunyang committed May 20, 2017
1 parent 9210b85 commit f819192
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 13 deletions.
16 changes: 16 additions & 0 deletions MobileProject.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,10 @@
34B8DB001E82A5ED009649A5 /* MPModelBlockViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 34B8DAFE1E82A5ED009649A5 /* MPModelBlockViewController.m */; };
34C089681EC1AAAF004A6B03 /* MPVideoClipViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 34C089671EC1AAAF004A6B03 /* MPVideoClipViewController.m */; };
34C089691EC1AAAF004A6B03 /* MPVideoClipViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 34C089671EC1AAAF004A6B03 /* MPVideoClipViewController.m */; };
34D5A1771ECE8B4E0093FAE0 /* MPBarChartView.m in Sources */ = {isa = PBXBuildFile; fileRef = 34D5A1761ECE8B4E0093FAE0 /* MPBarChartView.m */; };
34D5A1781ECE8B4E0093FAE0 /* MPBarChartView.m in Sources */ = {isa = PBXBuildFile; fileRef = 34D5A1761ECE8B4E0093FAE0 /* MPBarChartView.m */; };
34D5A17B1ECE8FD70093FAE0 /* MPPieChartView.m in Sources */ = {isa = PBXBuildFile; fileRef = 34D5A17A1ECE8FD60093FAE0 /* MPPieChartView.m */; };
34D5A17C1ECE8FD70093FAE0 /* MPPieChartView.m in Sources */ = {isa = PBXBuildFile; fileRef = 34D5A17A1ECE8FD60093FAE0 /* MPPieChartView.m */; };
34E08A551C3BE80F005C6CA3 /* GVUserDefaults.m in Sources */ = {isa = PBXBuildFile; fileRef = 34E08A541C3BE80F005C6CA3 /* GVUserDefaults.m */; };
34E08A591C3BEA89005C6CA3 /* GVUserDefaults+BBProperties.m in Sources */ = {isa = PBXBuildFile; fileRef = 34E08A581C3BEA89005C6CA3 /* GVUserDefaults+BBProperties.m */; };
34E08A5D1C3BEEF2005C6CA3 /* MyFileLogger.m in Sources */ = {isa = PBXBuildFile; fileRef = 34E08A5C1C3BEEF2005C6CA3 /* MyFileLogger.m */; };
Expand Down Expand Up @@ -1337,6 +1341,10 @@
34B8DAFE1E82A5ED009649A5 /* MPModelBlockViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPModelBlockViewController.m; sourceTree = "<group>"; };
34C089661EC1AAAF004A6B03 /* MPVideoClipViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPVideoClipViewController.h; sourceTree = "<group>"; };
34C089671EC1AAAF004A6B03 /* MPVideoClipViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPVideoClipViewController.m; sourceTree = "<group>"; };
34D5A1751ECE8B4E0093FAE0 /* MPBarChartView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPBarChartView.h; sourceTree = "<group>"; };
34D5A1761ECE8B4E0093FAE0 /* MPBarChartView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPBarChartView.m; sourceTree = "<group>"; };
34D5A1791ECE8FD60093FAE0 /* MPPieChartView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPPieChartView.h; sourceTree = "<group>"; };
34D5A17A1ECE8FD60093FAE0 /* MPPieChartView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPPieChartView.m; sourceTree = "<group>"; };
34E08A531C3BE80F005C6CA3 /* GVUserDefaults.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GVUserDefaults.h; sourceTree = "<group>"; };
34E08A541C3BE80F005C6CA3 /* GVUserDefaults.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GVUserDefaults.m; sourceTree = "<group>"; };
34E08A571C3BEA89005C6CA3 /* GVUserDefaults+BBProperties.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GVUserDefaults+BBProperties.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2542,6 +2550,10 @@
34FB62C21E8DFFBC0036AA3D /* MPPhotoCell+ConfigureForPhoto.m */,
340793831EA9A49400AF184A /* MPContextView.h */,
340793841EA9A49400AF184A /* MPContextView.m */,
34D5A1751ECE8B4E0093FAE0 /* MPBarChartView.h */,
34D5A1761ECE8B4E0093FAE0 /* MPBarChartView.m */,
34D5A1791ECE8FD60093FAE0 /* MPPieChartView.h */,
34D5A17A1ECE8FD60093FAE0 /* MPPieChartView.m */,
);
path = View;
sourceTree = "<group>";
Expand Down Expand Up @@ -5349,11 +5361,13 @@
34E08B3C1C3BFB90005C6CA3 /* NSNumber+CGFloat.m in Sources */,
34FA475F1D50961600D5B729 /* MPChildrenViewController.m in Sources */,
34FE0B921D50FF7C00991E65 /* MPHideNavigationViewController.m in Sources */,
34D5A1781ECE8B4E0093FAE0 /* MPBarChartView.m in Sources */,
34E08B3F1C3BFB90005C6CA3 /* NSString+Ruby.m in Sources */,
34E08B401C3BFB90005C6CA3 /* UISplitViewController+QuickAccess.m in Sources */,
34F72FD91E8C95FF002B0709 /* YTKChainRequest.m in Sources */,
34E08B421C3BFB90005C6CA3 /* NSURL+QueryDictionary.m in Sources */,
3400E4C41D35EEF00056B673 /* MPLKDBHelper.m in Sources */,
34D5A17C1ECE8FD70093FAE0 /* MPPieChartView.m in Sources */,
34E08B431C3BFB90005C6CA3 /* MJRefreshBackStateFooter.m in Sources */,
34E08B441C3BFB90005C6CA3 /* NSPersistentStoreCoordinator+Custom.m in Sources */,
34799A641CD34C5A003518C5 /* UIDevice+FCUUID.m in Sources */,
Expand Down Expand Up @@ -5733,6 +5747,7 @@
34E893A41C3B9A03001327C8 /* NSString+Encrypt.m in Sources */,
3411EF101D6FEC1A00A6C7F3 /* CanvasView.m in Sources */,
34E893A61C3B9A03001327C8 /* NSString+Matcher.m in Sources */,
34D5A1771ECE8B4E0093FAE0 /* MPBarChartView.m in Sources */,
34F72FD31E8C95E9002B0709 /* YTKNetworkConfig.m in Sources */,
340C853D1E724BCF00B59C2E /* MPProtocolOptionalViewController.m in Sources */,
34E894111C3B9A03001327C8 /* UITableView+iOS7Style.m in Sources */,
Expand Down Expand Up @@ -5823,6 +5838,7 @@
340793811EA9A3BE00AF184A /* MPCGContextViewController.m in Sources */,
34E894101C3B9A03001327C8 /* UISplitViewController+QuickAccess.m in Sources */,
340D87991D3F6310003D9C88 /* dateTimeHelper.m in Sources */,
34D5A17B1ECE8FD70093FAE0 /* MPPieChartView.m in Sources */,
34E893B51C3B9A03001327C8 /* NSURL+QueryDictionary.m in Sources */,
34E891211C3B92AA001327C8 /* MJRefreshBackStateFooter.m in Sources */,
34E893691C3B9A03001327C8 /* NSPersistentStoreCoordinator+Custom.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,4 @@
<Bucket
type = "0"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.ExceptionBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
scope = "0"
stopOnStyle = "0">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ - (void)viewDidLoad {
[self masonryLabelHeightLayout];

[self masonryPriorityLayout];

[self masonryPriorityOtherLayout];

[self masonryEdgesCenterLayout];

[self masonryScrollViewLayout];

[self masonryUpdateLayout];

}

- (void)didReceiveMemoryWarning {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//

#import "MPUIBezierPathViewController.h"
#import "MPBarChartView.h"
#import "MPPieChartView.h"

@interface MPUIBezierPathViewController ()<CAAnimationDelegate>
{
Expand Down Expand Up @@ -43,6 +45,17 @@ - (void)viewDidLoad {
[self Bubble];

[self circleAnimation];


//画柱状图
MPBarChartView *barChartView=[[MPBarChartView alloc]initWithFrame:CGRectMake(20, 440, 200, 150)];
barChartView.backgroundColor=[UIColor grayColor];
[self.view addSubview:barChartView];

//画饼图
MPPieChartView *pieChartView=[[MPPieChartView alloc]initWithFrame:CGRectMake(230, 350, 150, 150)];
pieChartView.backgroundColor=[UIColor whiteColor];
[self.view addSubview:pieChartView];
}


Expand Down
13 changes: 13 additions & 0 deletions MobileProject/Main/Theory/View/MPBarChartView.h
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
54 changes: 54 additions & 0 deletions MobileProject/Main/Theory/View/MPBarChartView.m
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
13 changes: 13 additions & 0 deletions MobileProject/Main/Theory/View/MPPieChartView.h
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
58 changes: 58 additions & 0 deletions MobileProject/Main/Theory/View/MPPieChartView.m
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

0 comments on commit f819192

Please sign in to comment.