// // ChartViewController.m // IBOSS // // Created by guan hong hou on 2017/8/21. // Copyright © 2017年 elongtian. All rights reserved. // // 功能描述:销售业绩图表控制器 #import "SalesAchievementChartViewController.h" #import "NSString+Tools.h" #import "SalesAchievementRankModel.h" #import "SalesAchievementListFrame.h" #define kTextFont [UIFont systemFontOfSize:LabelAndTextFontOfSize] @interface SalesAchievementChartViewController (){ UIScrollView * _scroll; } @end @implementation SalesAchievementChartViewController #pragma mark - 公共函数 /** 视图加载完成函数 */ - (void)viewDidLoad { [super viewDidLoad]; [self initUI]; } /** 安全区视图发生变化 */ -(void)viewSafeAreaInsetsDidChange{ _scroll.frame = CGRectMake(0, 0, SCREENWIDTH, self.view.superview.frame.size.height); self.view.frame = CGRectMake(0, 0, SCREENWIDTH, self.view.superview.frame.size.height); [super viewSafeAreaInsetsDidChange]; } #pragma mark - 私有函数 /** 初始化UI */ -(void)initUI{ UIView *separatorView = [UIView new]; separatorView.frame = CGRectMake(0, 0, Screen_Width, 10); separatorView.backgroundColor = LineBackgroundColor; [self.view addSubview:separatorView]; UILabel *lblTitle = [[UILabel alloc]init]; lblTitle.frame = CGRectMake((Screen_Width-160)/2, CGRectGetMaxY(separatorView.frame)+5, 160, 25); lblTitle.text = @"销售业绩排行 (单位:元)"; lblTitle.textColor = LabelGrayTextColor; lblTitle.font = [UIFont systemFontOfSize:14]; [self.view addSubview:lblTitle]; _bottomSeparatorView = [UIView new]; _bottomSeparatorView.frame = CGRectMake(0,CGRectGetMaxY(lblTitle.frame)+5, Screen_Width,2); _bottomSeparatorView.backgroundColor = LineBackgroundColor; [self.view addSubview:_bottomSeparatorView]; //柱状图左间距 _scroll = [UIScrollView new]; _scroll.frame = self.view.bounds; [self.view addSubview:_scroll]; self.view.backgroundColor = [UIColor whiteColor]; } /** 刷新数据 */ -(void)refreshData{ if(_rankList.count > 10){ NSRange range = NSMakeRange(0, 10); _rankList = [_rankList subarrayWithRange:range]; } Boolean flag = [self isHaveNegativeValue]; if(flag){ [self loadNegativeHistogram]; } else{ [self loadPositiveHistogram]; } } /** 刷新正数柱状图 */ - (void) loadPositiveHistogram{ if(_histogramView != nil){ [_histogramView removeFromSuperview]; } self.view.backgroundColor = [UIColor whiteColor]; CGFloat xSpacing = 30; //柱状图高度 CGFloat height = 40; //第一个柱状图上间距 CGFloat fySpacing = 15; //柱状图间距 // CGFloat ySpacing = 10; //柱状图内文字左间距 CGFloat xTextSpacing = 5; //柱状图最大长度 CGFloat maxWidth = SCREENWIDTH - xSpacing - 10; _histogramView = [[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(_bottomSeparatorView.frame),Screen_Width,[self getHistogramHeight:_rankList])]; [_scroll addSubview:_histogramView]; _histogramView.backgroundColor = [UIColor whiteColor]; SalesAchievementListFrame *maxAchievementFrame = [_rankList objectAtIndex:0]; double maxGoodsAmount = maxAchievementFrame.achievementRankModel.goodsAmount; for(int i = 0;i < _rankList.count;i++){ SalesAchievementListFrame *maxAchievementFrame = [_rankList objectAtIndex:i]; double goodsAmount = maxAchievementFrame.achievementRankModel.goodsAmount; // NSString *objectName = maxAchievementFrame.achievementRankModel.objectName; CGFloat width; if(goodsAmount < maxGoodsAmount){ width = maxWidth * (fabs(goodsAmount)/maxGoodsAmount); if(width < 1){ width = 1; } } else{ width = maxWidth; } [self layerToRight:_histogramView withFrame:CGRectMake(xSpacing, fySpacing*(i+1)+height*i,width, height)withColor:[UIColor colorWithRed:96.0/255.0 green:159.0/255.0 blue:233.0/255.0 alpha:1]]; [self.view addSubview:_histogramView]; UILabel *lblOrderSort = [[UILabel alloc] init]; NSString *orderSortStr = [NSString stringWithFormat:@"%ld", (long)maxAchievementFrame.achievementRankModel.orderSort]; CGRect orderSortFrame = [orderSortStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}]; orderSortFrame.origin.x = 10; orderSortFrame.origin.y = (height - orderSortFrame.size.height)/2 + fySpacing * (i+1) + height * i; lblOrderSort.frame = orderSortFrame; lblOrderSort.font = kTextFont; lblOrderSort.text = orderSortStr; [_histogramView addSubview:lblOrderSort]; UILabel *lblDepartmentName = [[UILabel alloc] init]; NSString *departmentNameStr = maxAchievementFrame.achievementRankModel.objectName; CGRect departmentNameFrame = [departmentNameStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}]; departmentNameFrame.origin.x = xTextSpacing + xSpacing; departmentNameFrame.origin.y = 5 + fySpacing*(i+1)+height*i; lblDepartmentName.textAlignment = NSTextAlignmentRight; lblDepartmentName.frame = departmentNameFrame; lblDepartmentName.font = kTextFont; lblDepartmentName.text = departmentNameStr; [_histogramView addSubview:lblDepartmentName]; UILabel *lblGoodsAmount = [[UILabel alloc] init]; NSString *goodsAmountStr = [NSString stringWithFormat:@"¥%.2f", maxAchievementFrame.achievementRankModel.goodsAmount]; CGRect goodsAmountFrame = [goodsAmountStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}]; goodsAmountFrame.origin.x= xTextSpacing + xSpacing; goodsAmountFrame.origin.y=CGRectGetMaxY( lblDepartmentName.frame); lblGoodsAmount.textAlignment = NSTextAlignmentRight; lblGoodsAmount.frame = goodsAmountFrame; lblGoodsAmount.font = kTextFont; lblGoodsAmount.text = goodsAmountStr; [_histogramView addSubview:lblGoodsAmount]; } _scroll.contentSize = CGSizeMake(self.view.frame.size.width, CGRectGetMaxY(_histogramView.frame)+10 + rectStatusHeight +rectNavHeight + 150); [_scroll addSubview:_histogramView]; } /** 刷新负数柱状图 */ - (void) loadNegativeHistogram{ if(_histogramView != nil){ [_histogramView removeFromSuperview]; } //柱状图左间距 CGFloat xSpacing = 15; //柱状图高度 CGFloat height = 40; //第一个柱状图上间距 CGFloat fySpacing = 15; //柱状图间距 // CGFloat ySpacing = 10; //柱状图内文字左间距 CGFloat xTextSpacing = 5; //柱状图最大长度 CGFloat maxWidth = SCREENWIDTH/2 - 10; _histogramView = [[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(_bottomSeparatorView.frame),Screen_Width,[self getHistogramHeight:_rankList])]; [_scroll addSubview:_histogramView]; _histogramView.backgroundColor = [UIColor whiteColor]; SalesAchievementListFrame *maxAchievementFrame = [_rankList objectAtIndex:0]; double maxGoodsAmount = maxAchievementFrame.achievementRankModel.goodsAmount; for(int i = 0;i < _rankList.count;i++){ SalesAchievementListFrame *maxAchievementFrame = [_rankList objectAtIndex:i]; double goodsAmount = maxAchievementFrame.achievementRankModel.goodsAmount; // NSString *objectName = maxAchievementFrame.achievementRankModel.objectName; CGFloat width; if(goodsAmount < maxGoodsAmount){ width = maxWidth * (fabs(goodsAmount)/maxGoodsAmount); if(width < 1){ width = 1; } } else{ width = maxWidth; } UILabel *lblGoodsAmount = [[UILabel alloc] init]; NSString *goodsAmountStr = [NSString stringWithFormat:@"¥%.2f", maxAchievementFrame.achievementRankModel.goodsAmount]; CGRect goodsAmountFrame = [goodsAmountStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}]; UILabel *lblOrderSort = [[UILabel alloc] init]; NSString *orderSortStr = [NSString stringWithFormat:@"%ld",(long) maxAchievementFrame.achievementRankModel.orderSort]; CGRect orderSortFrame = [orderSortStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}]; UILabel *lblDepartmentName = [[UILabel alloc] init]; NSString *objectNameStr = [maxAchievementFrame.achievementRankModel objectName]; CGRect objectNameFrame = [objectNameStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}]; if(goodsAmount >=0){ [self layerToRight:_histogramView withFrame:CGRectMake(Screen_Width/2, fySpacing*(i+1)+height*i,width, height)withColor:[UIColor colorWithRed:96.0/255.0 green:159.0/255.0 blue:233.0/255.0 alpha:1]]; objectNameFrame.origin.x = xTextSpacing+Screen_Width/2; objectNameFrame.origin.y = 5 + fySpacing*(i+1)+height*i; goodsAmountFrame.origin.x=xTextSpacing+Screen_Width/2; goodsAmountFrame.origin.y=CGRectGetMaxY(objectNameFrame); orderSortFrame.origin.x = Screen_Width/2-xSpacing; orderSortFrame.origin.y = (height - orderSortFrame.size.height)/2 + fySpacing*(i+1)+height*i; } else if(goodsAmount < 0){ [self layerToLeft:_histogramView withFrame:CGRectMake(Screen_Width/2-width, fySpacing*(i+1)+height*i,width, height)withColor:[UIColor colorWithRed:96.0/255.0 green:159.0/255.0 blue:233.0/255.0 alpha:1]]; objectNameFrame.origin.x=Screen_Width/2- xTextSpacing-CGRectGetWidth(objectNameFrame); objectNameFrame.origin.y = 5 + fySpacing*(i+1)+height*i; goodsAmountFrame.origin.x=Screen_Width/2-xTextSpacing-CGRectGetWidth(goodsAmountFrame); goodsAmountFrame.origin.y=CGRectGetMaxY(objectNameFrame); orderSortFrame.origin.x =Screen_Width/2+xTextSpacing; orderSortFrame.origin.y = (height - orderSortFrame.size.height)/2 + fySpacing*(i+1)+height*i; } [self.view addSubview:_histogramView]; UIView *yAxis=[[UIView alloc]init]; yAxis.frame=CGRectMake(Screen_Width/2, 0, 2.5, [self getHistogramHeight:_rankList]); yAxis.backgroundColor=LineBackgroundColor; [_histogramView addSubview:yAxis]; lblDepartmentName.frame = objectNameFrame; lblDepartmentName.font = kTextFont; lblDepartmentName.text = objectNameStr; [_histogramView addSubview:lblDepartmentName]; lblOrderSort.frame = orderSortFrame; lblOrderSort.font = kTextFont; lblOrderSort.text = orderSortStr; [_histogramView addSubview:lblOrderSort]; lblGoodsAmount.textAlignment = NSTextAlignmentRight; lblGoodsAmount.frame = goodsAmountFrame; lblGoodsAmount.font = kTextFont; lblGoodsAmount.text = goodsAmountStr; [_histogramView addSubview:lblGoodsAmount]; } _scroll.contentSize = CGSizeMake(self.view.frame.size.width, CGRectGetMaxY(_histogramView.frame)+10+rectStatusHeight+rectNavHeight + 150); [_scroll addSubview:_histogramView]; } /** 判断是否有负数 @return <#return value description#> */ -(Boolean) isHaveNegativeValue{ Boolean flag=NO; for(int i = 0;i < _rankList.count;i++){ SalesAchievementListFrame *frame = [_rankList objectAtIndex:i]; double goodsAmount = frame.achievementRankModel.goodsAmount; if(goodsAmount < 0){ flag = true; break; } } return flag; } /** 计算柱状图高度 @param dataList <#dataList description#> @return <#return value description#> */ -(CGFloat)getHistogramHeight:(NSArray*)dataList{ NSInteger row = dataList.count; return (row * 40 + (row * 20)); } /** 绘制向右的柱状图 @param subview @param frame */ - (void)layerToRight:(UIView *) subview withFrame:(CGRect) frame withColor:(UIColor *) color { CGFloat x = frame.origin.x; CGFloat y = frame.origin.y; CGFloat height = frame.size.height; CGFloat width = frame.size.width; CALayer *itemLayer = [CALayer layer]; itemLayer.frame = frame; itemLayer.backgroundColor = color.CGColor; [subview.layer addSublayer:itemLayer]; CABasicAnimation *aniBounds = [CABasicAnimation animationWithKeyPath:@"bounds"]; aniBounds.fromValue = [NSValue valueWithCGRect:CGRectMake(x, y, 0, height)]; aniBounds.toValue = [NSValue valueWithCGRect:CGRectMake(x, y, CGRectGetWidth(frame), CGRectGetHeight(frame))]; CABasicAnimation *aniPosition = [CABasicAnimation animationWithKeyPath:@"position"]; aniPosition.fromValue = [NSValue valueWithCGPoint:CGPointMake(x, frame.size.height/2 + y)]; aniPosition.toValue = [NSValue valueWithCGPoint:CGPointMake(x + (CGRectGetMaxX(frame)-x)/2,y + (CGRectGetMaxY(frame)-y)/2)]; CAAnimationGroup *anis = [CAAnimationGroup animation]; anis.animations = @[aniBounds,aniPosition]; anis.duration = 1; anis.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; anis.removedOnCompletion = NO; anis.fillMode=kCAFillModeForwards; [itemLayer addAnimation:anis forKey:nil]; } /** 绘制向左的柱状图 @param subview @param frame */ - (void)layerToLeft:(UIView *) subview withFrame:(CGRect) frame withColor:(UIColor *) color { CGFloat x = frame.origin.x; CGFloat y = frame.origin.y; CGFloat height = frame.size.height; CGFloat width = frame.size.width; CALayer *itemLayer = [CALayer layer]; itemLayer.frame = frame; itemLayer.backgroundColor = color.CGColor; [subview.layer addSublayer:itemLayer]; CABasicAnimation *aniBounds = [CABasicAnimation animationWithKeyPath:@"bounds"]; aniBounds.fromValue = [NSValue valueWithCGRect:CGRectMake(x, y, 0, height)]; aniBounds.toValue = [NSValue valueWithCGRect:CGRectMake(x, y, CGRectGetWidth(frame), CGRectGetHeight(frame))]; CABasicAnimation *aniPosition = [CABasicAnimation animationWithKeyPath:@"position"]; aniPosition.toValue = [NSValue valueWithCGPoint:CGPointMake(x+ (CGRectGetMaxX(frame)-x)/2, frame.size.height/2 + y)]; aniPosition.fromValue = [NSValue valueWithCGPoint:CGPointMake(x+width,y + (CGRectGetMaxY(frame)-y)/2)]; CAAnimationGroup *anis = [CAAnimationGroup animation]; anis.animations = @[aniBounds,aniPosition]; anis.duration = 1; anis.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; anis.removedOnCompletion = NO; anis.fillMode=kCAFillModeForwards; [itemLayer addAnimation:anis forKey:nil]; } @end