// // SalesPaymentRankChartViewController.m // IBOSS // // Created by guan hong hou on 2017/8/23. // Copyright © 2017年 elongtian. All rights reserved. // // 功能描述:销售回款排行图表控制器 #import "SalesPaymentRankChartViewController.h" #import "SalesPaymentRankModel.h" #import "SalesPaymentRankFrame.h" #define kTextFont [UIFont systemFontOfSize:LabelAndTextFontOfSize] @interface SalesPaymentRankChartViewController (){ /** scrollview */ UIScrollView * _scroll; } @end @implementation SalesPaymentRankChartViewController #pragma mark - 公共函数 /** 视图加载完成函数 */ - (void)viewDidLoad { [super viewDidLoad]; [self initUI]; } #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)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]; } /** 刷新数据 */ - (void)refreshData{ NSRange range=NSMakeRange(0, 10); if(_rankList.count > 10){ NSRange range = NSMakeRange(0, 10); _rankList = [_rankList subarrayWithRange:range]; } Boolean flag = [self isHaveNegativeValue]; if(flag){ [self loadNegativeHistogram]; } else{ [self loadPositiveHistogram]; } } /** 判断是否有负数 @return <#return value description#> */ - (Boolean) isHaveNegativeValue{ Boolean flag = NO; for(int i = 0;i < _rankList.count;i++){ SalesPaymentRankFrame *frame = [_rankList objectAtIndex:i]; double backAmount = frame.paymentRankModel.paymentAmount; if(backAmount < 0){ flag = true; break; } } return flag; } /** 绘制向右的柱状图 @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]; } /** 刷新正向柱状图 */ - (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]; SalesPaymentRankFrame *maxBackFrame= [_rankList objectAtIndex:0]; double maxBackAmount=maxBackFrame.paymentRankModel.paymentAmount ; for(int i=0;i<_rankList.count;i++){ SalesPaymentRankFrame *frame= [_rankList objectAtIndex:i]; double backAmount= frame.paymentRankModel.paymentAmount; NSString *objectName= frame.paymentRankModel.objectName; CGFloat width; if(backAmount=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; backAmountFrame.origin.x = xTextSpacing+Screen_Width/2; backAmountFrame.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(backAmount < 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; backAmountFrame.origin.x = Screen_Width/2-xTextSpacing-CGRectGetWidth(backAmountFrame); backAmountFrame.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]; lblBackAmount.textAlignment = NSTextAlignmentRight; lblBackAmount.frame = backAmountFrame; lblBackAmount.font = kTextFont; lblBackAmount.text = backAmountStr; [_histogramView addSubview:lblBackAmount]; } _scroll.contentSize = CGSizeMake(self.view.frame.size.width, CGRectGetMaxY(_histogramView.frame)+10+rectStatusHeight+rectNavHeight + 150); [_scroll addSubview:_histogramView]; } /** 计算柱状图的高度 @param dataList <#dataList description#> @return <#return value description#> */ -(CGFloat)getHistogramHeight:(NSArray*)dataList{ // 上下间隔已经在frame上做了 NSInteger row = dataList.count; return (row * 40 + (row * 15)); } @end