Skip to content

Commit 74de5c0

Browse files
committed
BAR GRAPH
1 parent 94bdf00 commit 74de5c0

File tree

9 files changed

+252
-7
lines changed

9 files changed

+252
-7
lines changed

MPBarsGraphView.h

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// MPBarsGraphView.h
3+
// MPPlot
4+
//
5+
// Created by Alex Manzella on 22/05/14.
6+
// Copyright (c) 2014 mpow. All rights reserved.
7+
//
8+
9+
#import "MPPlot.h"
10+
11+
@interface MPBarsGraphView : MPPlot{
12+
13+
BOOL shouldAnimate;
14+
}
15+
16+
@property (nonatomic,readwrite) CGFloat topCornerRadius;
17+
18+
19+
@end

MPBarsGraphView.m

+177
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
//
2+
// MPBarsGraphView.m
3+
// MPPlot
4+
//
5+
// Created by Alex Manzella on 22/05/14.
6+
// Copyright (c) 2014 mpow. All rights reserved.
7+
//
8+
9+
#import "MPBarsGraphView.h"
10+
11+
@implementation MPBarsGraphView
12+
13+
14+
- (id)initWithFrame:(CGRect)frame
15+
{
16+
self = [super initWithFrame:frame];
17+
if (self) {
18+
// Initialization code
19+
self.backgroundColor=[UIColor clearColor];
20+
21+
currentTag=-1;
22+
23+
self.topCornerRadius=-1;
24+
}
25+
return self;
26+
}
27+
28+
29+
30+
- (void)drawRect:(CGRect)rect
31+
{
32+
[super drawRect:rect];
33+
34+
35+
36+
if (self.values.count && !self.waitToUpdate) {
37+
38+
[label removeFromSuperview]; label=nil;
39+
40+
for (UIView *subview in self.subviews) {
41+
[subview removeFromSuperview];
42+
}
43+
44+
[self addBarsAnimated:shouldAnimate];
45+
46+
[self.graphColor setStroke];
47+
48+
UIBezierPath *line=[UIBezierPath bezierPath];
49+
50+
[line moveToPoint:CGPointMake(PADDING, self.height)];
51+
[line addLineToPoint:CGPointMake(self.width-PADDING, self.height)];
52+
[line setLineWidth:1];
53+
[line stroke];
54+
}
55+
}
56+
57+
- (void)addBarsAnimated:(BOOL)animated{
58+
59+
60+
for (UIButton* button in buttons) {
61+
[button removeFromSuperview];
62+
}
63+
64+
buttons=[[NSMutableArray alloc] init];
65+
66+
if (animated) {
67+
self.layer.masksToBounds=YES;
68+
}
69+
70+
CGFloat barWidth=self.width/(points.count*2+1);
71+
CGFloat radius=barWidth*(self.topCornerRadius >=0 ? self.topCornerRadius : 0.3);
72+
for (NSInteger i=0;i<points.count;i++) {
73+
74+
CGFloat height=[[points objectAtIndex:i] floatValue]*(self.height-PADDING*2)+PADDING;
75+
76+
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
77+
[button setBackgroundColor:self.graphColor];
78+
button.frame=CGRectMake(barWidth+(barWidth*i+barWidth*i), animated ? self.height : self.height-height, barWidth, animated ? height+20 : height);
79+
80+
81+
CAShapeLayer *maskLayer = [CAShapeLayer layer];
82+
maskLayer.frame = button.bounds;
83+
84+
85+
maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:button.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(radius, radius)].CGPath;
86+
87+
button.layer.mask=maskLayer;
88+
89+
[button addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside];
90+
button.tag=i;
91+
[self addSubview:button];
92+
93+
if (animated) {
94+
[UIView animateWithDuration:self.animationDuration delay:i*0.1 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
95+
button.y=self.height-height-20;
96+
}completion:^(BOOL finished) {
97+
[UIView animateWithDuration:.15 animations:^{
98+
button.frame=CGRectMake(barWidth+(barWidth*i+barWidth*i), self.height-height, barWidth, height);
99+
}];
100+
}];
101+
}
102+
103+
[buttons addObject:button];
104+
105+
106+
107+
108+
}
109+
110+
111+
112+
shouldAnimate=NO;
113+
114+
}
115+
116+
- (CGFloat)animationDuration{
117+
return _animationDuration>0.0 ? _animationDuration : .25;
118+
}
119+
120+
121+
122+
- (void)tap:(UIButton *)button{
123+
124+
125+
if (label.superview) {
126+
__block UILabel *labelBK=label;
127+
[UIView animateWithDuration:.15 animations:^{
128+
labelBK.transform=CGAffineTransformMakeScale(0, 0);
129+
}completion:^(BOOL finished) {
130+
[labelBK removeFromSuperview]; labelBK=nil;
131+
}];
132+
}
133+
134+
135+
if (button.tag==currentTag) {
136+
currentTag=-1;
137+
return;
138+
}else currentTag=button.tag;
139+
140+
label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 20)];
141+
label.textAlignment=NSTextAlignmentCenter;
142+
label.textColor=self.detailTextColor ? self.detailTextColor : ( self.graphColor );
143+
label.backgroundColor=self.detailBackgroundColor;
144+
label.layer.borderColor=label.textColor.CGColor;
145+
label.layer.borderWidth=.5;
146+
if ([[self.values objectAtIndex:currentTag] isKindOfClass:[NSString class]]) {
147+
label.text=[self.values objectAtIndex:currentTag];
148+
}else label.text=[NSString stringWithFormat:@"%@",[self.detailLabelFormatter stringFromNumber:[self.values objectAtIndex:currentTag]]];
149+
label.center=CGPointMake(button.center.x,button.center.y);
150+
label.layer.cornerRadius=3;
151+
label.clipsToBounds=YES;
152+
label.transform=CGAffineTransformMakeScale(0, 0);
153+
[self addSubview:label];
154+
[UIView animateWithDuration:.2 animations:^{
155+
label.transform=CGAffineTransformMakeScale(1, 1);
156+
}];
157+
158+
159+
}
160+
161+
162+
- (void)animate{
163+
164+
self.waitToUpdate=NO;
165+
166+
shouldAnimate=YES;
167+
168+
[self setNeedsDisplay];
169+
}
170+
171+
172+
173+
174+
175+
176+
177+
@end

MPGraphView.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ - (void)tap:(UIButton *)button{
155155

156156
label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 20)];
157157
label.textAlignment=NSTextAlignmentCenter;
158-
label.textColor=self.detailTextColor ? self.detailTextColor : ( self.graphColor ? self.graphColor : (self.fillColors.count ? [UIColor colorWithCGColor:(CGColorRef)[self.fillColors firstObject]] : self.graphColor) );
158+
label.textColor=self.detailTextColor ? self.detailTextColor : ( _graphColor ? _graphColor : (self.fillColors.count ? [UIColor colorWithCGColor:(CGColorRef)[self.fillColors firstObject]] : self.graphColor) );
159159
label.backgroundColor=self.detailBackgroundColor;
160160
label.layer.borderColor=label.textColor.CGColor;
161161
label.layer.borderWidth=.5;

MPPlot.h

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import <UIKit/UIKit.h>
1010
#import "UIView+Frame.h"
1111

12+
1213
#define PADDING 12
1314

1415
#define ANIMATIONDURATION 1.5
@@ -27,6 +28,10 @@ typedef CGFloat(^GraphPointsAlgorithm)(CGFloat x);
2728

2829
MPPlotType plotType;
2930

31+
UIColor *_graphColor;
32+
33+
CGFloat _animationDuration;
34+
3035
NSArray *points;
3136

3237
NSMutableArray *buttons;

MPPlot.m

+12-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import "MPPlot.h"
1010
#import "MPGraphView.h"
11+
#import "MPBarsGraphView.h"
1112

1213
@implementation MPPlot
1314

@@ -38,6 +39,10 @@ + (id)plotWithType:(MPPlotType)type frame:(CGRect)frame{
3839
case MPPlotTypeGraph:
3940
return [[MPGraphView alloc] initWithFrame:frame];
4041
break;
42+
43+
case MPPlotTypeBars:
44+
return [[MPBarsGraphView alloc] initWithFrame:frame];
45+
break;
4146

4247
default:
4348
break;
@@ -136,10 +141,12 @@ - (void)setAlgorithm:(GraphPointsAlgorithm)customAlgorithm numberOfPoints:(NSUIn
136141

137142
self.values=values;
138143

139-
[self setNeedsDisplay];
140144
}
141145

142-
146+
-(void)setFrame:(CGRect)frame{
147+
[super setFrame:frame];
148+
[self setNeedsDisplay];
149+
}
143150

144151

145152
#pragma mark Getters
@@ -196,6 +203,9 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
196203

197204
- (void)animate{
198205

206+
self.waitToUpdate=NO;
207+
208+
[self setNeedsDisplay];
199209
}
200210

201211

example/MPPlot.xcodeproj/project.pbxproj

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
DE4FB93E192A5752004A1C1E /* UIView+Frame.m in Sources */ = {isa = PBXBuildFile; fileRef = DE4FB93D192A5752004A1C1E /* UIView+Frame.m */; };
2626
DEC71AD0192E69E20023E89E /* MPPlot.m in Sources */ = {isa = PBXBuildFile; fileRef = DEC71ACF192E69E20023E89E /* MPPlot.m */; };
2727
DEC71AD3192E6D500023E89E /* UIBezierPath+curved.m in Sources */ = {isa = PBXBuildFile; fileRef = DEC71AD2192E6D500023E89E /* UIBezierPath+curved.m */; };
28+
DEC71AD7192E719B0023E89E /* MPBarsGraphView.m in Sources */ = {isa = PBXBuildFile; fileRef = DEC71AD6192E719B0023E89E /* MPBarsGraphView.m */; };
2829
/* End PBXBuildFile section */
2930

3031
/* Begin PBXContainerItemProxy section */
@@ -65,6 +66,8 @@
6566
DEC71ACF192E69E20023E89E /* MPPlot.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = MPPlot.m; path = ../MPPlot.m; sourceTree = "<group>"; };
6667
DEC71AD1192E6D500023E89E /* UIBezierPath+curved.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIBezierPath+curved.h"; path = "../UIBezierPath+curved.h"; sourceTree = "<group>"; };
6768
DEC71AD2192E6D500023E89E /* UIBezierPath+curved.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIBezierPath+curved.m"; path = "../UIBezierPath+curved.m"; sourceTree = "<group>"; };
69+
DEC71AD5192E719B0023E89E /* MPBarsGraphView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MPBarsGraphView.h; path = ../MPBarsGraphView.h; sourceTree = "<group>"; };
70+
DEC71AD6192E719B0023E89E /* MPBarsGraphView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = MPBarsGraphView.m; path = ../MPBarsGraphView.m; sourceTree = "<group>"; };
6871
/* End PBXFileReference section */
6972

7073
/* Begin PBXFrameworksBuildPhase section */
@@ -99,6 +102,8 @@
99102
DEC71ACF192E69E20023E89E /* MPPlot.m */,
100103
DE4FB939192A56BD004A1C1E /* MPGraphView.h */,
101104
DE4FB93A192A56BD004A1C1E /* MPGraphView.m */,
105+
DEC71AD5192E719B0023E89E /* MPBarsGraphView.h */,
106+
DEC71AD6192E719B0023E89E /* MPBarsGraphView.m */,
102107
DE4FB90A192A569E004A1C1E /* MPPlot */,
103108
DE4FB929192A569E004A1C1E /* MPPlotTests */,
104109
DE4FB903192A569E004A1C1E /* Frameworks */,
@@ -284,6 +289,7 @@
284289
DE4FB93B192A56BD004A1C1E /* MPGraphView.m in Sources */,
285290
DEC71AD0192E69E20023E89E /* MPPlot.m in Sources */,
286291
DE4FB93E192A5752004A1C1E /* UIView+Frame.m in Sources */,
292+
DEC71AD7192E719B0023E89E /* MPBarsGraphView.m in Sources */,
287293
DE4FB915192A569E004A1C1E /* MPAppDelegate.m in Sources */,
288294
);
289295
runOnlyForDeploymentPostprocessing = 0;

example/MPPlot/MPViewController.h

+4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@
99
#import <UIKit/UIKit.h>
1010
#import "MPGraphView.h"
1111
#import "MPPlot.h"
12+
#import "MPBarsGraphView.h"
1213

1314
@interface MPViewController : UIViewController{
1415

1516
MPGraphView *graph,*graph2,*graph3,*graph4;
17+
18+
MPBarsGraphView *graph5;
19+
1620
}
1721

1822
@end

example/MPPlot/MPViewController.m

+28-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ - (void)viewDidLoad
1818
{
1919
[super viewDidLoad];
2020
// Do any additional setup after loading the view, typically from a nib.
21-
graph=[MPPlot plotWithType:MPPlotTypeGraph frame:CGRectMake(0, 30, 320, 200)];
21+
graph=[MPPlot plotWithType:MPPlotTypeGraph frame:CGRectMake(0, 30, 320, 150)];
2222

2323
// NSMutableArray *arr=[[NSMutableArray alloc] init];
2424
//
@@ -44,7 +44,7 @@ - (void)viewDidLoad
4444
[self.view addSubview:graph2];
4545

4646

47-
graph3=[[MPGraphView alloc] initWithFrame:CGRectMake(0, 280, 320, 200)];
47+
graph3=[[MPGraphView alloc] initWithFrame:CGRectMake(0, 200, 320, 150)];
4848
graph3.waitToUpdate=YES;
4949

5050
[graph3 setAlgorithm:^CGFloat(CGFloat x) {
@@ -66,6 +66,23 @@ - (void)viewDidLoad
6666
[self.view addSubview:graph4];
6767

6868
[self.view addSubview:graph3];
69+
70+
71+
graph5=[MPPlot plotWithType:MPPlotTypeBars frame:CGRectMake(0, 360, 320, 150)];
72+
graph5.waitToUpdate=YES;
73+
[graph5 setAlgorithm:^CGFloat(CGFloat x) {
74+
return tan(x);
75+
} numberOfPoints:8];
76+
graph5.graphColor=[UIColor colorWithRed:0.120 green:0.806 blue:0.157 alpha:1.000];
77+
[self.view addSubview:graph5];
78+
79+
80+
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
81+
[button setTitle:@"Animate" forState:UIControlStateNormal];
82+
button.frame=CGRectMake(30, self.view.height-40, self.view.width-60, 40);
83+
[button setBackgroundColor:[UIColor redColor]];
84+
[self.view addSubview:button];
85+
[button addTarget:self action:@selector(animate) forControlEvents:UIControlEventTouchUpInside];
6986

7087
}
7188

@@ -74,8 +91,15 @@ - (void)viewDidAppear:(BOOL)animated{
7491

7592
[super viewDidAppear:animated];
7693

77-
[graph2 performSelector:@selector(animate) withObject:nil afterDelay:1];
78-
[graph3 performSelector:@selector(animate) withObject:nil afterDelay:1];
94+
[self performSelector:@selector(animate) withObject:nil afterDelay:1];
95+
96+
}
97+
98+
- (void)animate{
99+
100+
[graph2 animate];
101+
[graph3 animate];
102+
[graph5 animate];
79103

80104
}
81105

0 commit comments

Comments
 (0)