SalesPaymentRankChartViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. //
  2. // SalesPaymentRankChartViewController.m
  3. // IBOSS
  4. //
  5. // Created by guan hong hou on 2017/8/23.
  6. // Copyright © 2017年 elongtian. All rights reserved.
  7. //
  8. // 功能描述:销售回款排行图表控制器
  9. #import "SalesPaymentRankChartViewController.h"
  10. #import "SalesPaymentRankModel.h"
  11. #import "SalesPaymentRankFrame.h"
  12. #define kTextFont [UIFont systemFontOfSize:LabelAndTextFontOfSize]
  13. @interface SalesPaymentRankChartViewController (){
  14. /**
  15. scrollview
  16. */
  17. UIScrollView * _scroll;
  18. }
  19. @end
  20. @implementation SalesPaymentRankChartViewController
  21. #pragma mark - 公共函数
  22. /**
  23. 视图加载完成函数
  24. */
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. [self initUI];
  28. }
  29. #pragma mark - 私有函数
  30. /**
  31. 初始化UI
  32. */
  33. - (void)initUI{
  34. UIView *separatorView = [UIView new];
  35. separatorView.frame = CGRectMake(0, 0, Screen_Width, 10);
  36. separatorView.backgroundColor = LineBackgroundColor;
  37. [self.view addSubview:separatorView];
  38. UILabel *lblTitle = [[UILabel alloc]init];
  39. lblTitle.frame = CGRectMake((Screen_Width-160)/2, CGRectGetMaxY(separatorView.frame)+5, 160, 25);
  40. lblTitle.text = @"销售回款排行 (单位:元)";
  41. lblTitle.textColor = LabelGrayTextColor;
  42. lblTitle.font = [UIFont systemFontOfSize:14];
  43. [self.view addSubview:lblTitle];
  44. _bottomSeparatorView = [UIView new];
  45. _bottomSeparatorView.frame = CGRectMake(0,CGRectGetMaxY(lblTitle.frame)+5, Screen_Width,2);
  46. _bottomSeparatorView.backgroundColor=LineBackgroundColor;
  47. [self.view addSubview:_bottomSeparatorView];
  48. //柱状图左间距
  49. _scroll = [UIScrollView new];
  50. _scroll.frame = self.view.bounds;
  51. [self.view addSubview:_scroll];
  52. self.view.backgroundColor = [UIColor whiteColor];
  53. }
  54. /**
  55. 安全区视图发生变化
  56. */
  57. -(void)viewSafeAreaInsetsDidChange{
  58. _scroll.frame = CGRectMake(0, 0, SCREENWIDTH, self.view.superview.frame.size.height);
  59. self.view.frame = CGRectMake(0, 0, SCREENWIDTH, self.view.superview.frame.size.height);
  60. [super viewSafeAreaInsetsDidChange];
  61. }
  62. /**
  63. 刷新数据
  64. */
  65. - (void)refreshData{
  66. NSRange range=NSMakeRange(0, 10);
  67. if(_rankList.count > 10){
  68. NSRange range = NSMakeRange(0, 10);
  69. _rankList = [_rankList subarrayWithRange:range];
  70. }
  71. Boolean flag = [self isHaveNegativeValue];
  72. if(flag){
  73. [self loadNegativeHistogram];
  74. }
  75. else{
  76. [self loadPositiveHistogram];
  77. }
  78. }
  79. /**
  80. 判断是否有负数
  81. @return <#return value description#>
  82. */
  83. - (Boolean) isHaveNegativeValue{
  84. Boolean flag = NO;
  85. for(int i = 0;i < _rankList.count;i++){
  86. SalesPaymentRankFrame *frame = [_rankList objectAtIndex:i];
  87. double backAmount = frame.paymentRankModel.paymentAmount;
  88. if(backAmount < 0){
  89. flag = true;
  90. break;
  91. }
  92. }
  93. return flag;
  94. }
  95. /**
  96. 绘制向右的柱状图
  97. @param subview
  98. @param frame
  99. */
  100. - (void)layerToRight:(UIView *) subview withFrame:(CGRect) frame withColor:(UIColor *) color
  101. {
  102. CGFloat x = frame.origin.x;
  103. CGFloat y = frame.origin.y;
  104. CGFloat height = frame.size.height;
  105. CGFloat width = frame.size.width;
  106. CALayer *itemLayer = [CALayer layer];
  107. itemLayer.frame = frame;
  108. itemLayer.backgroundColor = color.CGColor;
  109. [subview.layer addSublayer:itemLayer];
  110. CABasicAnimation *aniBounds = [CABasicAnimation animationWithKeyPath:@"bounds"];
  111. aniBounds.fromValue = [NSValue valueWithCGRect:CGRectMake(x, y, 0, height)];
  112. aniBounds.toValue = [NSValue valueWithCGRect:CGRectMake(x, y, CGRectGetWidth(frame), CGRectGetHeight(frame))];
  113. CABasicAnimation *aniPosition = [CABasicAnimation animationWithKeyPath:@"position"];
  114. aniPosition.fromValue = [NSValue valueWithCGPoint:CGPointMake(x, frame.size.height/2 + y)];
  115. aniPosition.toValue = [NSValue valueWithCGPoint:CGPointMake(x + (CGRectGetMaxX(frame)-x)/2,y + (CGRectGetMaxY(frame)-y)/2)];
  116. CAAnimationGroup *anis = [CAAnimationGroup animation];
  117. anis.animations = @[aniBounds,aniPosition];
  118. anis.duration = 1;
  119. anis.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
  120. anis.removedOnCompletion = NO;
  121. anis.fillMode=kCAFillModeForwards;
  122. [itemLayer addAnimation:anis forKey:nil];
  123. }
  124. /**
  125. 绘制向左的柱状图
  126. @param subview
  127. @param frame
  128. */
  129. - (void)layerToLeft:(UIView *) subview withFrame:(CGRect) frame withColor:(UIColor *) color
  130. {
  131. CGFloat x = frame.origin.x;
  132. CGFloat y = frame.origin.y;
  133. CGFloat height = frame.size.height;
  134. CGFloat width = frame.size.width;
  135. CALayer *itemLayer = [CALayer layer];
  136. itemLayer.frame = frame;
  137. itemLayer.backgroundColor = color.CGColor;
  138. [subview.layer addSublayer:itemLayer];
  139. CABasicAnimation *aniBounds = [CABasicAnimation animationWithKeyPath:@"bounds"];
  140. aniBounds.fromValue = [NSValue valueWithCGRect:CGRectMake(x, y, 0, height)];
  141. aniBounds.toValue = [NSValue valueWithCGRect:CGRectMake(x, y, CGRectGetWidth(frame), CGRectGetHeight(frame))];
  142. CABasicAnimation *aniPosition = [CABasicAnimation animationWithKeyPath:@"position"];
  143. aniPosition.toValue = [NSValue valueWithCGPoint:CGPointMake(x+ (CGRectGetMaxX(frame)-x)/2, frame.size.height/2 + y)];
  144. aniPosition.fromValue = [NSValue valueWithCGPoint:CGPointMake(x+width,y + (CGRectGetMaxY(frame)-y)/2)];
  145. CAAnimationGroup *anis = [CAAnimationGroup animation];
  146. anis.animations = @[aniBounds,aniPosition];
  147. anis.duration = 1;
  148. anis.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
  149. anis.removedOnCompletion = NO;
  150. anis.fillMode=kCAFillModeForwards;
  151. [itemLayer addAnimation:anis forKey:nil];
  152. }
  153. /**
  154. 刷新正向柱状图
  155. */
  156. - (void) loadPositiveHistogram{
  157. if(_histogramView!=nil){
  158. [_histogramView removeFromSuperview];
  159. }
  160. self.view.backgroundColor = [UIColor whiteColor];
  161. CGFloat xSpacing =30;
  162. //柱状图高度
  163. CGFloat height = 40;
  164. //第一个柱状图上间距
  165. CGFloat fySpacing = 15;
  166. //柱状图间距
  167. CGFloat ySpacing = 10;
  168. //柱状图内文字左间距
  169. CGFloat xTextSpacing = 5;
  170. //柱状图最大长度
  171. CGFloat maxWidth = SCREENWIDTH - xSpacing - 10;
  172. _histogramView = [[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(_bottomSeparatorView.frame),Screen_Width,[self getHistogramHeight:_rankList])];
  173. [_scroll addSubview:_histogramView];
  174. _histogramView.backgroundColor = [UIColor whiteColor];
  175. SalesPaymentRankFrame *maxBackFrame= [_rankList objectAtIndex:0];
  176. double maxBackAmount=maxBackFrame.paymentRankModel.paymentAmount ;
  177. for(int i=0;i<_rankList.count;i++){
  178. SalesPaymentRankFrame *frame= [_rankList objectAtIndex:i];
  179. double backAmount= frame.paymentRankModel.paymentAmount;
  180. NSString *objectName= frame.paymentRankModel.objectName;
  181. CGFloat width;
  182. if(backAmount<maxBackAmount){
  183. width=maxWidth*(fabs(backAmount)/maxBackAmount);
  184. if(width<1){
  185. width=1;
  186. }
  187. }
  188. else{
  189. width=maxWidth;
  190. }
  191. [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]];
  192. [self.view addSubview:_histogramView];
  193. UILabel *lblOrderSort = [[UILabel alloc] init];
  194. NSString *orderSortStr =[NSString stringWithFormat:@"%ld", (long)frame.paymentRankModel.orderSort];
  195. CGRect orderSortFrame = [orderSortStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}];
  196. orderSortFrame.origin.x=10;
  197. orderSortFrame.origin.y=(height - orderSortFrame.size.height)/2 + fySpacing*(i+1)+height*i;
  198. lblOrderSort.frame=orderSortFrame;
  199. lblOrderSort.font = kTextFont;
  200. lblOrderSort.text = orderSortStr;
  201. [_histogramView addSubview:lblOrderSort];
  202. UILabel *lblDepartmentName = [[UILabel alloc] init];
  203. NSString *departmentNameStr =frame.paymentRankModel.objectName;
  204. CGRect departmentNameFrame = [departmentNameStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}];
  205. departmentNameFrame.origin.x = xTextSpacing + xSpacing;
  206. departmentNameFrame.origin.y = 5+ fySpacing*(i+1)+height*i;
  207. lblDepartmentName.textAlignment = NSTextAlignmentRight;
  208. lblDepartmentName.frame=departmentNameFrame;
  209. lblDepartmentName.font = kTextFont;
  210. lblDepartmentName.text = departmentNameStr;
  211. [_histogramView addSubview:lblDepartmentName];
  212. UILabel *lblBackAmount = [[UILabel alloc] init];
  213. NSString *backAmountStr = [NSString stringWithFormat:@"¥%.2f", frame.paymentRankModel.paymentAmount];
  214. CGRect backAmountFrame = [backAmountStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}];
  215. backAmountFrame.origin.x = xTextSpacing + xSpacing;
  216. backAmountFrame.origin.y = CGRectGetMaxY(lblDepartmentName.frame);
  217. lblBackAmount.textAlignment = NSTextAlignmentRight;
  218. lblBackAmount.frame=backAmountFrame;
  219. lblBackAmount.font = kTextFont;
  220. lblBackAmount.text = backAmountStr;
  221. [_histogramView addSubview:lblBackAmount];
  222. }
  223. _scroll.contentSize = CGSizeMake(self.view.frame.size.width, CGRectGetMaxY(_histogramView.frame)+10+rectStatusHeight+rectNavHeight + 150);
  224. [_scroll addSubview:_histogramView];
  225. }
  226. /**
  227. 刷新反向柱状图
  228. */
  229. - (void) loadNegativeHistogram{
  230. if(_histogramView != nil){
  231. [_histogramView removeFromSuperview];
  232. }
  233. //柱状图左间距
  234. CGFloat xSpacing = 15;
  235. //柱状图高度
  236. CGFloat height = 40;
  237. //第一个柱状图上间距
  238. CGFloat fySpacing =15;
  239. //柱状图间距
  240. CGFloat ySpacing = 10;
  241. //柱状图内文字左间距
  242. CGFloat xTextSpacing = 5;
  243. //柱状图最大长度
  244. CGFloat maxWidth = SCREENWIDTH/2 - xSpacing - 10;
  245. _histogramView = [[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(_bottomSeparatorView.frame),Screen_Width,[self getHistogramHeight:_rankList])];
  246. [_scroll addSubview:_histogramView];
  247. _histogramView.backgroundColor = [UIColor whiteColor];
  248. SalesPaymentRankFrame *maxBackFrame = [_rankList objectAtIndex:0];
  249. double maxBackAmount = maxBackFrame.paymentRankModel.paymentAmount;
  250. for(int i=0;i<_rankList.count;i++){
  251. SalesPaymentRankFrame *frame = [_rankList objectAtIndex:i];
  252. double backAmount = frame.paymentRankModel.paymentAmount;
  253. NSString *objectName = frame.paymentRankModel.objectName;
  254. CGFloat width;
  255. if(backAmount<maxBackAmount){
  256. width = maxWidth*(fabs(backAmount)/maxBackAmount);
  257. if(width < 1){
  258. width = 1;
  259. }
  260. }
  261. else{
  262. width=maxWidth;
  263. }
  264. UILabel *lblBackAmount = [[UILabel alloc] init];
  265. NSString *backAmountStr = [NSString stringWithFormat:@"¥%.2f",frame.paymentRankModel.paymentAmount];
  266. CGRect backAmountFrame = [backAmountStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}];
  267. UILabel *lblOrderSort = [[UILabel alloc] init];
  268. NSString *orderSortStr = [NSString stringWithFormat:@"%ld",(long)frame.paymentRankModel.orderSort];
  269. CGRect orderSortFrame = [orderSortStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}];
  270. UILabel *lblDepartmentName = [[UILabel alloc] init];
  271. NSString *objectNameStr =[frame.paymentRankModel objectName];
  272. CGRect objectNameFrame = [objectNameStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}];
  273. if(backAmount>=0){
  274. [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]];
  275. objectNameFrame.origin.x = xTextSpacing+Screen_Width/2;
  276. objectNameFrame.origin.y = 5 + fySpacing*(i+1)+height*i;
  277. backAmountFrame.origin.x = xTextSpacing+Screen_Width/2;
  278. backAmountFrame.origin.y = CGRectGetMaxY(objectNameFrame);
  279. orderSortFrame.origin.x = Screen_Width/2-xSpacing;
  280. orderSortFrame.origin.y = (height - orderSortFrame.size.height)/2 + fySpacing*(i+1)+height*i;
  281. }
  282. else if(backAmount < 0){
  283. [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]];
  284. objectNameFrame.origin.x = Screen_Width/2-xTextSpacing-CGRectGetWidth(objectNameFrame);
  285. objectNameFrame.origin.y = 5 + fySpacing*(i+1)+height*i;
  286. backAmountFrame.origin.x = Screen_Width/2-xTextSpacing-CGRectGetWidth(backAmountFrame);
  287. backAmountFrame.origin.y = CGRectGetMaxY(objectNameFrame);
  288. orderSortFrame.origin.x =Screen_Width/2+xTextSpacing;
  289. orderSortFrame.origin.y = (height - orderSortFrame.size.height)/2 + fySpacing*(i+1)+height*i;
  290. }
  291. [self.view addSubview:_histogramView];
  292. UIView *yAxis=[[UIView alloc]init];
  293. yAxis.frame=CGRectMake(Screen_Width/2, 0, 2.5, [self getHistogramHeight:_rankList]);
  294. yAxis.backgroundColor=LineBackgroundColor;
  295. [_histogramView addSubview:yAxis];
  296. lblDepartmentName.frame = objectNameFrame;
  297. lblDepartmentName.font = kTextFont;
  298. lblDepartmentName.text = objectNameStr;
  299. [_histogramView addSubview:lblDepartmentName];
  300. lblOrderSort.frame = orderSortFrame;
  301. lblOrderSort.font = kTextFont;
  302. lblOrderSort.text = orderSortStr;
  303. [_histogramView addSubview:lblOrderSort];
  304. lblBackAmount.textAlignment = NSTextAlignmentRight;
  305. lblBackAmount.frame = backAmountFrame;
  306. lblBackAmount.font = kTextFont;
  307. lblBackAmount.text = backAmountStr;
  308. [_histogramView addSubview:lblBackAmount];
  309. }
  310. _scroll.contentSize = CGSizeMake(self.view.frame.size.width, CGRectGetMaxY(_histogramView.frame)+10+rectStatusHeight+rectNavHeight + 150);
  311. [_scroll addSubview:_histogramView];
  312. }
  313. /**
  314. 计算柱状图的高度
  315. @param dataList <#dataList description#>
  316. @return <#return value description#>
  317. */
  318. -(CGFloat)getHistogramHeight:(NSArray*)dataList{
  319. // 上下间隔已经在frame上做了
  320. NSInteger row = dataList.count;
  321. return (row * 40 + (row * 15));
  322. }
  323. @end