diff --git a/BarChart.xcodeproj/project.pbxproj b/BarChart.xcodeproj/project.pbxproj index b05a46a..d82b799 100644 --- a/BarChart.xcodeproj/project.pbxproj +++ b/BarChart.xcodeproj/project.pbxproj @@ -88,8 +88,8 @@ 9C88AFC5188D76E9005441A8 /* BarChartLib */ = { isa = PBXGroup; children = ( - 9CCAAE5D18AE568A0034F4C4 /* DrawerBarChart.m */, 9CCAAE5E18AE568A0034F4C4 /* DrawerBarChart.h */, + 9CCAAE5D18AE568A0034F4C4 /* DrawerBarChart.m */, 9CCAAE5F18AE568A0034F4C4 /* BarDetail.h */, 9CCAAE6018AE568A0034F4C4 /* BarDetail.m */, ); diff --git a/BarChart.xcodeproj/project.xcworkspace/xcshareddata/BarChart.xccheckout b/BarChart.xcodeproj/project.xcworkspace/xcshareddata/BarChart.xccheckout index c5b24be..ffa89ac 100644 --- a/BarChart.xcodeproj/project.xcworkspace/xcshareddata/BarChart.xccheckout +++ b/BarChart.xcodeproj/project.xcworkspace/xcshareddata/BarChart.xccheckout @@ -5,34 +5,34 @@ IDESourceControlProjectFavoriteDictionaryKey IDESourceControlProjectIdentifier - EA166A49-32C2-4993-AAE8-DD8809560EDC + 1035916B-4E61-4D89-BC0C-5B9204A984EE IDESourceControlProjectName BarChart IDESourceControlProjectOriginsDictionary - E19D7A37-F6B2-41CB-86D9-09D5E23DC124 - https://github.com/marcelobraga/barChart.git + DE79D932-47D1-4F67-83DB-EFBBBB133D2E + ssh://github.com/lduraes/barChart.git IDESourceControlProjectPath BarChart.xcodeproj/project.xcworkspace IDESourceControlProjectRelativeInstallPathDictionary - E19D7A37-F6B2-41CB-86D9-09D5E23DC124 + DE79D932-47D1-4F67-83DB-EFBBBB133D2E ../.. IDESourceControlProjectURL - https://github.com/marcelobraga/barChart.git + ssh://github.com/lduraes/barChart.git IDESourceControlProjectVersion 110 IDESourceControlProjectWCCIdentifier - E19D7A37-F6B2-41CB-86D9-09D5E23DC124 + DE79D932-47D1-4F67-83DB-EFBBBB133D2E IDESourceControlProjectWCConfigurations IDESourceControlRepositoryExtensionIdentifierKey public.vcs.git IDESourceControlWCCIdentifierKey - E19D7A37-F6B2-41CB-86D9-09D5E23DC124 + DE79D932-47D1-4F67-83DB-EFBBBB133D2E IDESourceControlWCCName barChart diff --git a/BarChart.xcodeproj/project.xcworkspace/xcuserdata/lduraes.xcuserdatad/UserInterfaceState.xcuserstate b/BarChart.xcodeproj/project.xcworkspace/xcuserdata/lduraes.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..feebd4a Binary files /dev/null and b/BarChart.xcodeproj/project.xcworkspace/xcuserdata/lduraes.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/BarChart.xcodeproj/xcuserdata/lduraes.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/BarChart.xcodeproj/xcuserdata/lduraes.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist new file mode 100644 index 0000000..fe2b454 --- /dev/null +++ b/BarChart.xcodeproj/xcuserdata/lduraes.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,5 @@ + + + diff --git a/BarChart.xcodeproj/xcuserdata/lduraes.xcuserdatad/xcschemes/BarChart.xcscheme b/BarChart.xcodeproj/xcuserdata/lduraes.xcuserdatad/xcschemes/BarChart.xcscheme new file mode 100644 index 0000000..7423247 --- /dev/null +++ b/BarChart.xcodeproj/xcuserdata/lduraes.xcuserdatad/xcschemes/BarChart.xcscheme @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/BarChart.xcodeproj/xcuserdata/lduraes.xcuserdatad/xcschemes/xcschememanagement.plist b/BarChart.xcodeproj/xcuserdata/lduraes.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..0b56442 --- /dev/null +++ b/BarChart.xcodeproj/xcuserdata/lduraes.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,27 @@ + + + + + SchemeUserState + + BarChart.xcscheme + + orderHint + 0 + + + SuppressBuildableAutocreation + + 9CCC91A31889909700521410 + + primary + + + 9CCC91C41889909700521410 + + primary + + + + + diff --git a/BarChart/Images.xcassets/popup.imageset/Contents.json b/BarChart/Images.xcassets/popup.imageset/Contents.json new file mode 100644 index 0000000..2b71a47 --- /dev/null +++ b/BarChart/Images.xcassets/popup.imageset/Contents.json @@ -0,0 +1,17 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x", + "filename" : "popup-items.png" + }, + { + "idiom" : "universal", + "scale" : "2x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/BarChart/Images.xcassets/popup.imageset/popup-items.png b/BarChart/Images.xcassets/popup.imageset/popup-items.png new file mode 100644 index 0000000..84a2674 Binary files /dev/null and b/BarChart/Images.xcassets/popup.imageset/popup-items.png differ diff --git a/BarChart/Library/DrawerBarChart.h b/BarChart/Library/DrawerBarChart.h index 7dca3b6..8b35ed1 100644 --- a/BarChart/Library/DrawerBarChart.h +++ b/BarChart/Library/DrawerBarChart.h @@ -7,9 +7,8 @@ // #import -#import "BarDetail.h" - +@class BarDetail; @class DrawerBarChart; @protocol DrawerBarChartDelegate @@ -20,8 +19,6 @@ @end - - @interface DrawerBarChart : UIView { NSMutableArray * arrayBar; } @@ -39,12 +36,11 @@ @property (nonatomic, assign) BOOL abortRotate; @property (nonatomic, assign) NSString * title; @property (nonatomic, assign) UIColor * masterColor; -@property (nonatomic, assign) int barSize; @property(assign,nonatomic) iddelegate; -(void) drawChart; -(void) initChart; +-(void)resetChart; -(void) setBar; - @end diff --git a/BarChart/Library/DrawerBarChart.m b/BarChart/Library/DrawerBarChart.m index df5c19a..91a5e52 100644 --- a/BarChart/Library/DrawerBarChart.m +++ b/BarChart/Library/DrawerBarChart.m @@ -7,40 +7,33 @@ // #import "DrawerBarChart.h" +#import "BarDetail.h" -#define MARGIN_VERTICAL 30 +#define MARGIN_VERTICAL 55 #define MARGIN_HORIZONTAL 30 @implementation DrawerBarChart -- (id)initWithFrame:(CGRect)frame -{ - self = [super initWithFrame:frame]; - if (self) { - // Initialization code +#pragma mark - Public methods + +-(void) clickBar : (UIButton *) btn { + if((self.delegate) && ([self.delegate respondsToSelector:@selector(didSelectBar: )])) { + [self.delegate didSelectBar:[arrayBar objectAtIndex:btn.tag]]; } - return self; } -/* -// Only override drawRect: if you perform custom drawing. -// An empty implementation adversely affects performance during animation. -- (void)drawRect:(CGRect)rect +- (UIColor *)darkerColorForColor:(UIColor *)c { - // Drawing code -} -*/ - - --(id) init { - self = [super init]; - self.barDetail = [BarDetail new]; - return self; + CGFloat r, g, b, a; + if ([c getRed:&r green:&g blue:&b alpha:&a]) + return [UIColor colorWithRed:MAX(r - 0.9, 0.0) + green:MAX(g - 0.9, 0.0) + blue:MAX(b - 0.9, 0.0) + alpha:a]; + return nil; } - -(void) drawChart { - if (!self.lineColor) { self.lineColor = [UIColor blackColor]; } @@ -48,7 +41,6 @@ -(void) drawChart { [self getMaxValue]; [self getMinValue]; - UIView * limitVerticalView = [UIView new]; UIView * limiteHorizontalView = [UIView new]; CGRect frameVertical = limitVerticalView.frame; @@ -69,7 +61,6 @@ -(void) drawChart { limiteHorizontalView.backgroundColor = self.lineColor; limiteHorizontalView.frame = frameHorizontal; - UIView * lineViewTop = [UIView new]; UIView * lineViewTop2 = [UIView new]; UIView * lineViewMiddle = [UIView new]; @@ -84,8 +75,6 @@ -(void) drawChart { lineViewTop.alpha = .1; lineViewTop.frame = frameLineTop; - - CGRect frameLineTop2 = lineViewTop2.frame; frameLineTop2.size.width = frameHorizontal.size.width; frameLineTop2.origin.y = frameVertical.origin.y + (frameVertical.size.height / 4); @@ -103,7 +92,7 @@ -(void) drawChart { lineViewMiddle.backgroundColor = [UIColor blackColor]; lineViewMiddle.alpha = .1; lineViewMiddle.frame = frameLineMiddle; - + CGRect frameLineBottom = lineViewBottom.frame; frameLineBottom.size.width = frameHorizontal.size.width; frameLineBottom.origin.y = frameLineMiddle.origin.y + (frameVertical.size.height / 4); @@ -113,8 +102,6 @@ -(void) drawChart { lineViewBottom.alpha = .1; lineViewBottom.frame = frameLineBottom; - - [self addSubview:lineViewTop]; [self addSubview:lineViewTop2]; [self addSubview:lineViewMiddle]; @@ -122,9 +109,8 @@ -(void) drawChart { [self addSubview:limitVerticalView]; [self addSubview:limiteHorizontalView]; - if (self.fontSize == 0) { - self.fontSize = 8; + self.fontSize = 9; } UILabel * labelMax = [UILabel new]; @@ -138,7 +124,7 @@ -(void) drawChart { labelMax.frame = frameLabelMax; labelMax.text = [NSString stringWithFormat:@"%i", self.maxValue]; [self addSubview:labelMax]; - + UILabel * labelMax2 = [UILabel new]; [labelMax2 setFont:[UIFont systemFontOfSize:self.fontSize]]; labelMax2.textColor = self.lineColor; @@ -200,15 +186,9 @@ -(void) drawChart { int originX = frameLineTop.origin.x + 10; int widthBar = limiteHorizontalView.frame.size.width / arrayBar.count; - if (self.barSize != 0) { - widthBar = self.barSize; - } - int index = 0; + for (BarDetail * barDetail in arrayBar) { - - - double heightBar = barDetail.value * heightDefinition; double heightSubBar = 0; if (barDetail.subValue != 0) { @@ -220,11 +200,42 @@ -(void) drawChart { frameBar.origin.x = originX; frameBar.origin.y = frameHorizontal.origin.y - heightBar; frameBar.size.width = widthBar - 10; + + //===== reduce bar width + + if(frameBar.size.width > 40) { + frameBar.size.width = 30; + } + + //===== + frameBar.size.height = heightBar + 1; bar.frame = frameBar; + if (self.masterColor) { barDetail.color = self.masterColor; } + + //===== add a popup image above a bar including its value + + UIImageView *imgPopUp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"popup-item.png"]]; + float barWidth = frameBar.size.width; + float imgPadding = 0; + + if(barWidth > 30) { + imgPadding = (barWidth-30); + } + + imgPopUp.frame = CGRectMake(((imgPadding/2)+frameBar.origin.x),(frameBar.origin.y-30),(barWidth-imgPadding),25); + [self addSubview:imgPopUp]; + UILabel *uilValue = [[UILabel alloc] initWithFrame:CGRectMake((imgPopUp.frame.origin.x+10),(imgPopUp.frame.origin.y-2),imgPopUp.frame.size.width,imgPopUp.frame.size.height)]; + uilValue.text = [NSString stringWithFormat:@"%i",(int)barDetail.value]; + uilValue.textColor = [UIColor whiteColor]; + uilValue.font = [UIFont fontWithName:@"HelveticaNeue" size:13]; + [self addSubview:uilValue]; + + //===== + if (self.showGradient) { CAGradientLayer *gradient = [CAGradientLayer layer]; gradient.frame = bar.bounds; @@ -235,6 +246,7 @@ -(void) drawChart { } [self addSubview:bar]; + if (self.showBorder) { bar.layer.borderWidth = 1; bar.layer.borderColor = self.lineColor.CGColor; @@ -264,16 +276,15 @@ -(void) drawChart { subBar.backgroundColor = barDetail.subColor; } - subBar.layer.borderWidth = 1; if (self.showBorder) { subBar.layer.borderColor = self.lineColor.CGColor; } else { subBar.layer.borderColor = [self lighterColorForColor:barDetail.subColor].CGColor; } - + [self addSubview:subBar]; - + frameHorizontal.size.width += 5; limiteHorizontalView.frame = frameHorizontal; @@ -282,10 +293,8 @@ -(void) drawChart { btn.tag = index; [btn addTarget:self action:@selector(clickBar:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:btn]; - } - UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = frameBar; btn.tag = index; @@ -295,20 +304,30 @@ -(void) drawChart { UILabel * labelTitle = [UILabel new]; labelTitle.text = barDetail.title; [labelTitle setFont:[UIFont boldSystemFontOfSize:self.fontSize]]; + + CGRect frameTitle = labelTitle.frame; + //frameTitle.origin.x = originX; + //frameTitle.origin.y = frameBar.origin.y + frameBar.size.height + 5 ; + //frameTitle.origin.y += 5; + //frameTitle.size.width = widthBar; + frameTitle.origin.x = (originX-30); + frameTitle.origin.y = (frameBar.origin.y+frameBar.size.height+10); + frameTitle.size.width = [self getTextWidth:barDetail.title withFontName:@"HelveticaNeue" withFontSize:10]; + if (self.rotate45Degress) { - [labelTitle setTransform:CGAffineTransformMakeRotation(-M_PI_4)]; - [labelTitle setTextAlignment:NSTextAlignmentRight]; + frameTitle.origin.x = (originX-50); + frameTitle.size.width *= 2; + [labelTitle setTransform:CGAffineTransformMakeRotation(-0.9)]; + [labelTitle setTextAlignment:NSTextAlignmentLeft]; } else if (self.rotate90Degress) { [labelTitle setTransform:CGAffineTransformMakeRotation(-M_PI_2)]; [labelTitle setTextAlignment:NSTextAlignmentRight]; + frameTitle.origin.x = (originX-13); + frameTitle.origin.y = (frameBar.origin.y+frameBar.size.height+50); } else { [labelTitle setTextAlignment:NSTextAlignmentCenter]; } - CGRect frameTitle = labelTitle.frame; - frameTitle.origin.x = originX; - frameTitle.origin.y = frameBar.origin.y + frameBar.size.height + 5 ; - frameTitle.origin.y += 5; - frameTitle.size.width = widthBar; + if (self.abortRotate) { frameTitle.size.height = 25; } else if (self.rotate90Degress) { @@ -316,15 +335,17 @@ -(void) drawChart { } else if (self.rotate45Degress) { frameTitle.size.height = 25; } + labelTitle.frame = frameTitle; labelTitle.backgroundColor = [UIColor clearColor]; + if (barDetail.titleColor) { labelTitle.textColor = barDetail.titleColor; } else { labelTitle.textColor = self.lineColor; } - [self addSubview:labelTitle]; + [self addSubview:labelTitle]; originX += widthBar ; index++; @@ -332,7 +353,7 @@ -(void) drawChart { if (self.title) { UILabel * labelTitle = [UILabel new]; - [labelTitle setFont:[UIFont boldSystemFontOfSize:10]]; + [labelTitle setFont:[UIFont boldSystemFontOfSize:15]]; CGRect frameTitle = labelTitle.frame; frameTitle.origin.x = MARGIN_HORIZONTAL; frameTitle.origin.y = 0; @@ -346,7 +367,6 @@ -(void) drawChart { [self addSubview:labelTitle]; } - [self bringSubviewToFront:lineViewTop]; [self bringSubviewToFront:lineViewTop2]; [self bringSubviewToFront:lineViewMiddle]; @@ -371,7 +391,7 @@ -(void) drawChart { self.layer.borderWidth = 1; } - if (self.showShadow) { + if(self.showShadow) { self.layer.shadowColor = [UIColor blackColor].CGColor; self.layer.shadowOffset = CGSizeMake(0,2); self.layer.shadowOpacity = 0.9; @@ -379,22 +399,6 @@ -(void) drawChart { } } - --(void) initChart { - self.barDetail = [BarDetail new]; - arrayBar = [NSMutableArray new]; - self.showBorder = NO; - self.abortRotate = YES; - self.showGradient = YES; - self.showShadow = NO; -} - --(void) setBar { - [arrayBar addObject:self.barDetail]; - self.barDetail = [BarDetail new]; -} - - -(void) getMaxValue { int maxValue = 0; for (BarDetail * barDetail in arrayBar) { @@ -405,15 +409,66 @@ -(void) getMaxValue { maxValue = barDetail.subValue; } } - self.maxValue = maxValue * 1.1; if (maxValue < 10) { self.maxValue = maxValue + 1; } else { self.maxValue = maxValue * 1.1; } +} + +-(void) getMinValue { + int minValue = 0; + BOOL firstOperation = YES; + for (BarDetail * barDetail in arrayBar) { + if (barDetail.value < minValue || firstOperation){ + minValue = barDetail.value; + firstOperation = NO; + } + } + + self.minValue = minValue; +} + +// calculate label's width according to font informed +-(int)getTextWidth:(NSString *)text withFontName:(NSString *)fontName withFontSize:(float)fontSize { + NSString *label = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + int lblWidth = [label boundingRectWithSize:CGSizeMake(285,4000) options:NSStringDrawingUsesFontLeading attributes:nil context:nil].size.width; + return lblWidth; +} + +-(void) initChart { + self.barDetail = [BarDetail new]; + arrayBar = [NSMutableArray new]; + self.showBorder = NO; + self.abortRotate = YES; + self.showGradient = YES; + self.showShadow = NO; +} + +- (UIColor *)lighterColorForColor:(UIColor *)c +{ + CGFloat r, g, b, a; + if ([c getRed:&r green:&g blue:&b alpha:&a]) + return [UIColor colorWithRed:MIN(r + 0.6, 1.0) + green:MIN(g + 0.6, 1.0) + blue:MIN(b + 0.6, 1.0) + alpha:a]; + return nil; } +-(void)resetChart { + self.barDetail = nil; + arrayBar = nil; + self.showBorder = NO; + self.abortRotate = NO; + self.showGradient = NO; + self.showShadow = NO; + + for(UIView *view in self.subviews) { + [view removeFromSuperview]; + } +} -(void) setAbortRotate:(BOOL)abortRotate { if (abortRotate) { @@ -423,6 +478,10 @@ -(void) setAbortRotate:(BOOL)abortRotate { } } +-(void) setBar { + [arrayBar addObject:self.barDetail]; + self.barDetail = [BarDetail new]; +} -(void) setRotate45Degress:(BOOL)rotate45Degress { if (rotate45Degress) { @@ -432,7 +491,6 @@ -(void) setRotate45Degress:(BOOL)rotate45Degress { } } - -(void) setRotate90Degress:(BOOL)rotate90Degress { if (rotate90Degress) { _abortRotate = NO; @@ -441,49 +499,12 @@ -(void) setRotate90Degress:(BOOL)rotate90Degress { } } +#pragma mark - Lifecycle methods --(void) getMinValue { - int minValue = 0; - BOOL firstOperation = YES; - for (BarDetail * barDetail in arrayBar) { - if (barDetail.value < minValue || firstOperation){ - minValue = barDetail.value; - firstOperation = NO; - } - } - self.minValue = minValue; -} - - --(void) clickBar : (UIButton *) btn { - - if((self.delegate) && ([self.delegate respondsToSelector:@selector(didSelectBar: )])) { - [self.delegate didSelectBar:[arrayBar objectAtIndex:btn.tag]]; - } - -} - - -- (UIColor *)darkerColorForColor:(UIColor *)c -{ - CGFloat r, g, b, a; - if ([c getRed:&r green:&g blue:&b alpha:&a]) - return [UIColor colorWithRed:MAX(r - 0.9, 0.0) - green:MAX(g - 0.9, 0.0) - blue:MAX(b - 0.9, 0.0) - alpha:a]; - return nil; +-(id) init { + self = [super init]; + self.barDetail = [BarDetail new]; + return self; } - -- (UIColor *)lighterColorForColor:(UIColor *)c -{ - CGFloat r, g, b, a; - if ([c getRed:&r green:&g blue:&b alpha:&a]) - return [UIColor colorWithRed:MIN(r + 0.6, 1.0) - green:MIN(g + 0.6, 1.0) - blue:MIN(b + 0.6, 1.0) - alpha:a]; - return nil; -} @end