SalesAnalysisListNewChartViewController.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. //
  2. // SalesAnalysisListNewChartViewController.m
  3. // IBOSS
  4. //
  5. // Created by Simon on 2019/6/10.
  6. // Copyright © 2019 elongtian. All rights reserved.
  7. //
  8. #import "SalesAnalysisListNewChartViewController.h"
  9. @interface SalesAnalysisListNewChartViewController (){
  10. UIScrollView * _scroll;
  11. NSMutableArray *chartArray;
  12. }
  13. @end
  14. @implementation SalesAnalysisListNewChartViewController
  15. #pragma mark - 公共函数
  16. /**
  17. 视图加载完成函数
  18. */
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. [self initUI];
  22. }
  23. #pragma mark - 私有函数
  24. /**
  25. 初始化UI
  26. */
  27. -(void)initUI{
  28. UIView *separatorView = [UIView new];
  29. separatorView.tag = 11;
  30. separatorView.frame = CGRectMake(0, 0, Screen_Width, 10);
  31. separatorView.backgroundColor = LineBackgroundColor;
  32. [self.view addSubview:separatorView];
  33. //柱状图左间距
  34. _scroll = [UIScrollView new];
  35. _scroll.frame = CGRectMake(0, CGRectGetMaxY(separatorView.frame), SCREENWIDTH, self.view.frame.size.height-CGRectGetMaxY(separatorView.frame));
  36. [self.view addSubview:_scroll];
  37. self.view.backgroundColor = [UIColor whiteColor];
  38. UILabel *lblTitle = [[UILabel alloc]init];
  39. lblTitle.tag = 10;
  40. lblTitle.frame = CGRectMake((Screen_Width-160)/2, CGRectGetMaxY(separatorView.frame)+5, 160, 25);
  41. lblTitle.text = @"畅滞销排行";
  42. lblTitle.textAlignment = 1;
  43. lblTitle.textColor = LabelGrayTextColor;
  44. lblTitle.font = [UIFont systemFontOfSize:14];
  45. [_scroll addSubview:lblTitle];
  46. _bottomSeparatorView = [UIView new];
  47. _bottomSeparatorView.tag = 10;
  48. _bottomSeparatorView.hidden = YES;
  49. _bottomSeparatorView.frame = CGRectMake(0,CGRectGetMaxY(lblTitle.frame)+5, Screen_Width,2);
  50. _bottomSeparatorView.backgroundColor = LineBackgroundColor;
  51. [_scroll addSubview:_bottomSeparatorView];
  52. chartArray = [[NSMutableArray alloc] init];
  53. }
  54. /**
  55. 刷新数据
  56. */
  57. -(void)refreshData{
  58. if(_rankList.count > 10){
  59. NSRange range = NSMakeRange(0, 10);
  60. chartArray = [NSMutableArray arrayWithArray:[_rankList subarrayWithRange:range]];
  61. }
  62. // Boolean flag = [self isHaveNegativeValue];
  63. // if(flag){
  64. //
  65. // [self loadNegativeHistogram];
  66. // }
  67. //
  68. // else{
  69. [self loadPositiveHistogram];
  70. // }
  71. }
  72. /**
  73. 刷新正数柱状图
  74. */
  75. - (void) loadPositiveHistogram{
  76. for (UIView *subview in _scroll.subviews) {
  77. if(subview.tag != 10) {
  78. [subview removeFromSuperview];
  79. }
  80. }
  81. if (chartArray.count<=0) {
  82. return;
  83. }
  84. self.view.backgroundColor = [UIColor whiteColor];
  85. CGFloat xSpacing = 30;
  86. //柱状图高度
  87. CGFloat height = 40;
  88. //第一个柱状图上间距
  89. CGFloat fySpacing = 15;
  90. //柱状图间距
  91. CGFloat ySpacing = 10;
  92. //柱状图内文字左间距
  93. CGFloat xTextSpacing = 5;
  94. //柱状图最大长度
  95. CGFloat maxWidth = SCREENWIDTH - xSpacing - 10;
  96. _histogramView = [[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(_bottomSeparatorView.frame),Screen_Width,[self getHistogramHeight:chartArray])];
  97. [_scroll addSubview:_histogramView];
  98. _histogramView.backgroundColor = [UIColor whiteColor];
  99. // 判断是按照销售数量、销售金额、销售次数中的哪种方式排序
  100. int sumNumber = 0;
  101. double sumAmount = 0.0;
  102. int sumTimes = 0;
  103. SalesAnalysisModel *model = [chartArray objectAtIndex:0];
  104. // for (SalesAnalysisModel *model in _rankList) {
  105. sumNumber+=[model.number intValue];
  106. sumAmount+=[model.salesAmount doubleValue];
  107. sumTimes+=[model.salesTimes intValue];
  108. // }
  109. double maxUnit = 0.0;
  110. if ([_sortType isEqualToString:@"1"]) {
  111. maxUnit = sumNumber;
  112. }else if ([_sortType isEqualToString:@"2"]){
  113. maxUnit = sumAmount;
  114. }else if ([_sortType isEqualToString:@"3"]){
  115. maxUnit = sumTimes;
  116. }
  117. //>=3不画轴线
  118. if(chartArray.count>=3){
  119. [self createAxis:maxWidth maxAmount:maxUnit];
  120. }
  121. //画水平柱状图
  122. for(int i = 0;i < chartArray.count;i++){
  123. SalesAnalysisModel *analysisModel = [chartArray objectAtIndex:i];
  124. //销售数量
  125. int number = [analysisModel.number intValue];
  126. //销售金额
  127. double salesAmount = [analysisModel.salesAmount doubleValue];
  128. //销售次数
  129. int salesTimes = [analysisModel.salesTimes intValue];
  130. //商品品牌
  131. NSString *commodityBrand = analysisModel.commodityBrand==nil?@"":analysisModel.commodityBrand;
  132. //商品名称
  133. NSString *commodityName = analysisModel.commodityName==nil?@"":analysisModel.commodityName;
  134. //排序方式单位
  135. NSString *goodsAmountStr = @"";
  136. double loopUnit = 0.0;
  137. if ([_sortType isEqualToString:@"1"]) {
  138. loopUnit = number;
  139. goodsAmountStr = [NSString stringWithFormat:@"%d", number];
  140. }else if ([_sortType isEqualToString:@"2"]){
  141. loopUnit = salesAmount;
  142. goodsAmountStr = [NSString stringWithFormat:@"¥%.2f", loopUnit];
  143. }else if ([_sortType isEqualToString:@"3"]){
  144. loopUnit = salesTimes;
  145. goodsAmountStr = [NSString stringWithFormat:@"%d", salesTimes];
  146. }
  147. NSString *objectName = [NSString stringWithFormat:@"%@%@",commodityBrand,commodityName];
  148. CGFloat width;
  149. //计算长度
  150. if(loopUnit < maxUnit){
  151. width = maxWidth * (fabs(loopUnit)/maxUnit);
  152. if(width < 1){
  153. width = 1;
  154. }
  155. }
  156. else{
  157. width = maxWidth;
  158. }
  159. [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]];
  160. [self.view addSubview:_histogramView];
  161. UILabel *lblOrderSort = [[UILabel alloc] init];
  162. NSString *orderSortStr = analysisModel.ranking;
  163. CGRect orderSortFrame = [orderSortStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}];
  164. orderSortFrame.origin.x = 10;
  165. orderSortFrame.origin.y = (height - orderSortFrame.size.height)/2 + fySpacing * (i+1) + height * i;
  166. lblOrderSort.frame = orderSortFrame;
  167. lblOrderSort.font = kTextFont;;
  168. lblOrderSort.text = orderSortStr;
  169. [_histogramView addSubview:lblOrderSort];
  170. UILabel *lblDepartmentName = [[UILabel alloc] init];
  171. NSString *departmentNameStr = objectName;
  172. CGRect departmentNameFrame = [departmentNameStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}];
  173. departmentNameFrame.origin.x = xTextSpacing + xSpacing;
  174. departmentNameFrame.origin.y = 5 + fySpacing*(i+1)+height*i;
  175. lblDepartmentName.textAlignment = NSTextAlignmentRight;
  176. lblDepartmentName.frame = departmentNameFrame;
  177. lblDepartmentName.font = kTextFont;
  178. lblDepartmentName.text = departmentNameStr;
  179. [_histogramView addSubview:lblDepartmentName];
  180. UILabel *lblGoodsAmount = [[UILabel alloc] init];
  181. CGRect goodsAmountFrame = [goodsAmountStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}];
  182. goodsAmountFrame.origin.x= xTextSpacing + xSpacing;
  183. goodsAmountFrame.origin.y=CGRectGetMaxY( lblDepartmentName.frame);
  184. lblGoodsAmount.textAlignment = NSTextAlignmentRight;
  185. lblGoodsAmount.frame = goodsAmountFrame;
  186. lblGoodsAmount.font = kTextFont;
  187. lblGoodsAmount.text = goodsAmountStr;
  188. [_histogramView addSubview:lblGoodsAmount];
  189. }
  190. _scroll.contentSize = CGSizeMake(self.view.frame.size.width, CGRectGetMaxY(_histogramView.frame)+10 + rectStatusHeight +rectNavHeight + 150);
  191. [_scroll addSubview:_histogramView];
  192. }
  193. /**
  194. 创建负数的轴线
  195. @param width 最大长度
  196. @param maxAmount 最大金额
  197. */
  198. - (void) createNegativeAxis:(CGFloat) width maxAmount:(CGFloat)maxAmount{
  199. // 线的路径
  200. UIBezierPath *leveLinePath = [UIBezierPath bezierPath];
  201. // 起点
  202. [leveLinePath moveToPoint:CGPointMake((_histogramView.frame.size.width - (2 * width))/2, 0)];
  203. // 其他点
  204. [leveLinePath addLineToPoint:CGPointMake(2 * width + 10, 0)];
  205. CAShapeLayer *leveLineLayer = [CAShapeLayer layer];
  206. leveLineLayer.lineWidth = 1;
  207. leveLineLayer.strokeColor = LineBackgroundColor.CGColor;
  208. leveLineLayer.path = leveLinePath.CGPath;
  209. leveLineLayer.fillColor = nil; // 默认为blackColor
  210. [_histogramView.layer addSublayer:leveLineLayer];
  211. NSString *str = [NSString stringWithFormat:@"%d",(int)maxAmount];
  212. int m = ceil([str integerValue]/pow(10, str.length-1));
  213. m = m * pow(10, str.length-1);
  214. //右侧垂直轴线
  215. for (int i = 0; i <= 3; i++) {
  216. if(i != 0){
  217. CGFloat c = (width/3)*i+width+10;
  218. // if(i != 0){
  219. // UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(c-20, _histogramView.frame.origin.y-17, 200, 17)];
  220. // lb.textAlignment = NSTextAlignmentCenter;
  221. // lb.center = CGPointMake(c-10, _histogramView.frame.origin.y-10);
  222. // lb.text = [NSString stringWithFormat:@"%d",(m/5)*i];
  223. // lb.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
  224. // lb.font = [UIFont systemFontOfSize:9];
  225. // [_scroll addSubview:lb];
  226. // }
  227. UIBezierPath *linePath = [UIBezierPath bezierPath];
  228. [linePath moveToPoint:CGPointMake(c, 0)];
  229. [linePath addLineToPoint:CGPointMake(c, _histogramView.frame.size.height)];
  230. CAShapeLayer *lineLayer = [CAShapeLayer layer];
  231. lineLayer.lineWidth = 1;
  232. lineLayer.strokeColor = LineBackgroundColor.CGColor;
  233. lineLayer.path = linePath.CGPath;
  234. lineLayer.fillColor = nil; // 默认为blackColor
  235. [_histogramView.layer addSublayer:lineLayer];
  236. }
  237. }
  238. //左侧垂直轴线
  239. for (int i = 0; i <= 3; i++) {
  240. //计算等分间距
  241. CGFloat c = width + 10 -(width/3)*i;
  242. if(i != 0){
  243. UIBezierPath *linePath = [UIBezierPath bezierPath];
  244. [linePath moveToPoint:CGPointMake(c, 0)];
  245. [linePath addLineToPoint:CGPointMake(c, _histogramView.frame.size.height)];
  246. CAShapeLayer *lineLayer = [CAShapeLayer layer];
  247. lineLayer.lineWidth = 1;
  248. lineLayer.strokeColor = LineBackgroundColor.CGColor;
  249. lineLayer.path = linePath.CGPath;
  250. lineLayer.fillColor = nil; // 默认为blackColor
  251. [_histogramView.layer addSublayer:lineLayer];
  252. }
  253. }
  254. }
  255. /**
  256. 创建正数的轴线
  257. @param width <#width description#>
  258. @param maxAmount <#maxAmount description#>
  259. */
  260. - (void) createAxis:(CGFloat) width maxAmount:(CGFloat)maxAmount{
  261. // 线的路径
  262. UIBezierPath *linePath = [UIBezierPath bezierPath];
  263. // 起点
  264. [linePath moveToPoint:CGPointMake(width+30, 0)];
  265. // 其他点
  266. [linePath addLineToPoint:CGPointMake(30, 0)];
  267. [linePath addLineToPoint:CGPointMake(30,_histogramView.frame.size.height)];
  268. CAShapeLayer *lineLayer = [CAShapeLayer layer];
  269. lineLayer.lineWidth = 1;
  270. lineLayer.strokeColor = LineBackgroundColor.CGColor;
  271. lineLayer.path = linePath.CGPath;
  272. lineLayer.fillColor = nil; // 默认为blackColor
  273. [_histogramView.layer addSublayer:lineLayer];
  274. NSString *str = [NSString stringWithFormat:@"%d",(int)maxAmount];
  275. int m = ceil([str integerValue]/pow(10, str.length-1));
  276. m = m * pow(10, str.length-1);
  277. //垂直五条轴线
  278. for (int i = 0; i <= 5; i++) {
  279. CGFloat c = (width/5)*i+30;
  280. // if(i != 0){
  281. // UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(c-20, _histogramView.frame.origin.y-17, 200, 17)];
  282. // lb.textAlignment = NSTextAlignmentCenter;
  283. // lb.center = CGPointMake(c-10, _histogramView.frame.origin.y-10);
  284. // lb.text = [NSString stringWithFormat:@"%d",(m/5)*i];
  285. // lb.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
  286. // lb.font = [UIFont systemFontOfSize:9];
  287. // [_scroll addSubview:lb];
  288. // }
  289. UIBezierPath *linePath = [UIBezierPath bezierPath];
  290. [linePath moveToPoint:CGPointMake(c, 0)];
  291. [linePath addLineToPoint:CGPointMake(c, _histogramView.frame.size.height)];
  292. CAShapeLayer *lineLayer = [CAShapeLayer layer];
  293. lineLayer.lineWidth = 1;
  294. lineLayer.strokeColor = LineBackgroundColor.CGColor;
  295. lineLayer.path = linePath.CGPath;
  296. lineLayer.fillColor = nil; // 默认为blackColor
  297. [_histogramView.layer addSublayer:lineLayer];
  298. }
  299. }
  300. /**
  301. 刷新负数柱状图
  302. */
  303. - (void) loadNegativeHistogram{
  304. for (UIView *subview in _scroll.subviews) {
  305. if(subview.tag != 10) {
  306. [subview removeFromSuperview];
  307. }
  308. }
  309. //柱状图左间距
  310. CGFloat xSpacing = 15;
  311. //柱状图高度
  312. CGFloat height = 40;
  313. //第一个柱状图上间距
  314. CGFloat fySpacing = 15;
  315. //柱状图间距
  316. CGFloat ySpacing = 10;
  317. //柱状图内文字左间距
  318. CGFloat xTextSpacing = 5;
  319. //柱状图最大长度
  320. CGFloat maxWidth = SCREENWIDTH/2 - 10;
  321. _histogramView = [[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(_bottomSeparatorView.frame),Screen_Width,[self getHistogramHeight:_rankList])];
  322. [_scroll addSubview:_histogramView];
  323. _histogramView.backgroundColor = [UIColor whiteColor];
  324. SalesAnalysisModel *maxBackModel = [chartArray objectAtIndex:0];
  325. SalesAnalysisModel *minBackModel = [chartArray objectAtIndex:chartArray.count - 1];
  326. //获得最大值绝对值
  327. double maxUnit = 0.0;
  328. double firstAmount = 0.0;
  329. // maxBackFrame.achievementRankModel.goodsAmount;
  330. double lastAmount = 0.0;
  331. // minBackFrame.achievementRankModel.goodsAmount;
  332. // 判断是按照销售数量、销售金额、销售次数中的哪种方式排序
  333. // int sumNumber = 0;
  334. // double sumAmount = 0.0;
  335. // int sumTimes = 0;
  336. // for (SalesAnalysisModel *model in _rankList) {
  337. // sumNumber+=[model.number intValue];
  338. // sumAmount+=[model.salesAmount doubleValue];
  339. // sumTimes+=[model.salesTimes intValue];
  340. // }
  341. if ([_sortType isEqualToString:@"1"]) {
  342. maxUnit = [maxBackModel.number floatValue];
  343. firstAmount = [maxBackModel.number floatValue];
  344. lastAmount = [minBackModel.number floatValue];
  345. }else if ([_sortType isEqualToString:@"2"]){
  346. maxUnit = [maxBackModel.salesAmount floatValue];
  347. firstAmount = [maxBackModel.salesAmount floatValue];
  348. lastAmount = [minBackModel.salesAmount floatValue];
  349. }else if ([_sortType isEqualToString:@"3"]){
  350. maxUnit = [maxBackModel.salesTimes floatValue];
  351. firstAmount = [maxBackModel.salesTimes floatValue];
  352. lastAmount = [minBackModel.salesTimes floatValue];
  353. }
  354. if(fabs(firstAmount)>fabs(lastAmount)){
  355. maxUnit = firstAmount;
  356. }else{
  357. maxUnit = fabs(lastAmount);
  358. }
  359. if(_rankList.count>=3){
  360. [self createNegativeAxis:maxWidth maxAmount:maxUnit];
  361. }
  362. for(int i = 0;i < chartArray.count;i++){
  363. SalesAnalysisModel *maxAnalysisModel = [chartArray objectAtIndex:i];
  364. //销售数量
  365. int number = [maxAnalysisModel.number intValue];
  366. //销售金额
  367. double salesAmount = [maxAnalysisModel.salesAmount doubleValue];
  368. //销售次数
  369. int salesTimes = [maxAnalysisModel.salesTimes intValue];
  370. //商品品牌
  371. NSString *commodityBrand = maxAnalysisModel.commodityBrand==nil?@"":maxAnalysisModel.commodityBrand;
  372. //商品名称
  373. NSString *commodityName = maxAnalysisModel.commodityName==nil?@"":maxAnalysisModel.commodityName;
  374. //排序方式单位
  375. NSString *goodsAmountStr = @"";
  376. double goodsAmount = 0.0;
  377. if ([_sortType isEqualToString:@"1"]) {
  378. goodsAmount = number;
  379. goodsAmountStr = [NSString stringWithFormat:@"%d", number];
  380. }else if ([_sortType isEqualToString:@"2"]){
  381. goodsAmount = salesAmount;
  382. goodsAmountStr = [NSString stringWithFormat:@"%.2f", salesAmount];
  383. }else if ([_sortType isEqualToString:@"3"]){
  384. goodsAmount = salesTimes;
  385. goodsAmountStr = [NSString stringWithFormat:@"%d", salesTimes];
  386. }
  387. NSString *objectName = [NSString stringWithFormat:@"%@%@",commodityBrand,commodityName];
  388. CGFloat width;
  389. if(goodsAmount < maxUnit){
  390. width = maxWidth * (fabs(goodsAmount)/maxUnit);
  391. if(width < 1){
  392. width = 1;
  393. }
  394. }
  395. else{
  396. width = maxWidth;
  397. }
  398. UILabel *lblGoodsAmount = [[UILabel alloc] init];
  399. CGRect goodsAmountFrame = [goodsAmountStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}];
  400. UILabel *lblOrderSort = [[UILabel alloc] init];
  401. NSString *orderSortStr = maxAnalysisModel.ranking;
  402. CGRect orderSortFrame = [orderSortStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}];
  403. UILabel *lblDepartmentName = [[UILabel alloc] init];
  404. NSString *objectNameStr = objectName;
  405. CGRect objectNameFrame = [objectNameStr textRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{NSFontAttributeName:kTextFont}];
  406. //画右侧柱状图
  407. if(goodsAmount >=0){
  408. [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]];
  409. objectNameFrame.origin.x = xTextSpacing+Screen_Width/2;
  410. objectNameFrame.origin.y = 5 + fySpacing*(i+1)+height*i;
  411. goodsAmountFrame.origin.x=xTextSpacing+Screen_Width/2;
  412. goodsAmountFrame.origin.y=CGRectGetMaxY(objectNameFrame);
  413. orderSortFrame.origin.x = Screen_Width/2-xSpacing;
  414. orderSortFrame.origin.y = (height - orderSortFrame.size.height)/2 + fySpacing*(i+1)+height*i;
  415. //跳转按钮frame
  416. // detailBtnFrame = CGRectMake(SCREENWIDTH/2, fySpacing*(i+1)+height*i, SCREENWIDTH/2, height);
  417. }
  418. //画左侧柱状图
  419. else if(goodsAmount < 0){
  420. [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]];
  421. objectNameFrame.origin.x=Screen_Width/2- xTextSpacing-CGRectGetWidth(objectNameFrame);
  422. objectNameFrame.origin.y = 5 + fySpacing*(i+1)+height*i;
  423. goodsAmountFrame.origin.x=Screen_Width/2-xTextSpacing-CGRectGetWidth(goodsAmountFrame);
  424. goodsAmountFrame.origin.y=CGRectGetMaxY(objectNameFrame);
  425. orderSortFrame.origin.x =Screen_Width/2+xTextSpacing;
  426. orderSortFrame.origin.y = (height - orderSortFrame.size.height)/2 + fySpacing*(i+1)+height*i;
  427. //跳转按钮frame
  428. // detailBtnFrame = CGRectMake(0, fySpacing*(i+1)+height*i, SCREENWIDTH/2, height);
  429. }
  430. [self.view addSubview:_histogramView];
  431. UIView *yAxis=[[UIView alloc]init];
  432. yAxis.frame=CGRectMake(Screen_Width/2, 0, 2.5, [self getHistogramHeight:_rankList]);
  433. yAxis.backgroundColor=LineBackgroundColor;
  434. [_histogramView addSubview:yAxis];
  435. lblDepartmentName.frame = objectNameFrame;
  436. lblDepartmentName.font = kTextFont;
  437. lblDepartmentName.text = objectNameStr;
  438. [_histogramView addSubview:lblDepartmentName];
  439. lblOrderSort.frame = orderSortFrame;
  440. lblOrderSort.font = kTextFont;
  441. lblOrderSort.text = orderSortStr;
  442. [_histogramView addSubview:lblOrderSort];
  443. lblGoodsAmount.textAlignment = NSTextAlignmentRight;
  444. lblGoodsAmount.frame = goodsAmountFrame;
  445. lblGoodsAmount.font = kTextFont;
  446. lblGoodsAmount.text = goodsAmountStr;
  447. [_histogramView addSubview:lblGoodsAmount];
  448. }
  449. _scroll.contentSize = CGSizeMake(self.view.frame.size.width, CGRectGetMaxY(_histogramView.frame)+10+rectStatusHeight+rectNavHeight + 150);
  450. [_scroll addSubview:_histogramView];
  451. }
  452. /**
  453. 判断是否有负数
  454. @return <#return value description#>
  455. */
  456. -(Boolean) isHaveNegativeValue{
  457. Boolean flag=NO;
  458. for(int i = 0;i < _rankList.count;i++){
  459. SalesAnalysisModel *model = [chartArray objectAtIndex:i];
  460. double loopUnit = 0.0;
  461. if ([_sortType isEqualToString:@"1"]) {
  462. loopUnit = [model.number doubleValue];
  463. }else if ([_sortType isEqualToString:@"2"]){
  464. loopUnit = [model.salesAmount doubleValue];
  465. }else if ([_sortType isEqualToString:@"3"]){
  466. loopUnit = [model.salesTimes doubleValue];
  467. }
  468. if(loopUnit < 0){
  469. flag = true;
  470. break;
  471. }
  472. }
  473. return flag;
  474. }
  475. /**
  476. 计算柱状图高度
  477. @param dataList <#dataList description#>
  478. @return <#return value description#>
  479. */
  480. -(CGFloat)getHistogramHeight:(NSArray*)dataList{
  481. NSInteger row = dataList.count;
  482. return (row * 40 + (row * 15));
  483. }
  484. /**
  485. 绘制向右的柱状图
  486. @param subview
  487. @param frame
  488. */
  489. - (void)layerToRight:(UIView *) subview withFrame:(CGRect) frame withColor:(UIColor *) color
  490. {
  491. CGFloat x = frame.origin.x;
  492. CGFloat y = frame.origin.y;
  493. CGFloat height = frame.size.height;
  494. CGFloat width = frame.size.width;
  495. CAGradientLayer *itemLayer = [CAGradientLayer layer];
  496. itemLayer.frame = frame;
  497. itemLayer.colors = [self getColorArr];;
  498. itemLayer.locations = @[@0,@0.5];
  499. itemLayer.startPoint = CGPointMake(0, 0);
  500. itemLayer.endPoint = CGPointMake(0, 1.0);
  501. [subview.layer addSublayer:itemLayer];
  502. CABasicAnimation *aniBounds = [CABasicAnimation animationWithKeyPath:@"bounds"];
  503. aniBounds.fromValue = [NSValue valueWithCGRect:CGRectMake(x, y, 0, height)];
  504. aniBounds.toValue = [NSValue valueWithCGRect:CGRectMake(x, y, CGRectGetWidth(frame), CGRectGetHeight(frame))];
  505. CABasicAnimation *aniPosition = [CABasicAnimation animationWithKeyPath:@"position"];
  506. aniPosition.fromValue = [NSValue valueWithCGPoint:CGPointMake(x, frame.size.height/2 + y)];
  507. aniPosition.toValue = [NSValue valueWithCGPoint:CGPointMake(x + (CGRectGetMaxX(frame)-x)/2,y + (CGRectGetMaxY(frame)-y)/2)];
  508. CAAnimationGroup *anis = [CAAnimationGroup animation];
  509. anis.animations = @[aniBounds,aniPosition];
  510. anis.duration = 1;
  511. anis.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
  512. anis.removedOnCompletion = NO;
  513. anis.fillMode=kCAFillModeForwards;
  514. [itemLayer addAnimation:anis forKey:nil];
  515. }
  516. /**
  517. 绘制向左的柱状图
  518. @param subview
  519. @param frame
  520. */
  521. - (void)layerToLeft:(UIView *) subview withFrame:(CGRect) frame withColor:(UIColor *) color
  522. {
  523. CGFloat x = frame.origin.x;
  524. CGFloat y = frame.origin.y;
  525. CGFloat height = frame.size.height;
  526. CGFloat width = frame.size.width;
  527. CAGradientLayer *itemLayer = [CAGradientLayer layer];
  528. itemLayer.frame = frame;
  529. itemLayer.colors = [self getColorArr];
  530. itemLayer.locations = @[@0,@0.5];
  531. itemLayer.startPoint = CGPointMake(0, 0);
  532. itemLayer.endPoint = CGPointMake(0, 1.0);
  533. [subview.layer addSublayer:itemLayer];
  534. CABasicAnimation *aniBounds = [CABasicAnimation animationWithKeyPath:@"bounds"];
  535. aniBounds.fromValue = [NSValue valueWithCGRect:CGRectMake(x, y, 0, height)];
  536. aniBounds.toValue = [NSValue valueWithCGRect:CGRectMake(x, y, CGRectGetWidth(frame), CGRectGetHeight(frame))];
  537. CABasicAnimation *aniPosition = [CABasicAnimation animationWithKeyPath:@"position"];
  538. aniPosition.toValue = [NSValue valueWithCGPoint:CGPointMake(x+ (CGRectGetMaxX(frame)-x)/2, frame.size.height/2 + y)];
  539. aniPosition.fromValue = [NSValue valueWithCGPoint:CGPointMake(x+width,y + (CGRectGetMaxY(frame)-y)/2)];
  540. CAAnimationGroup *anis = [CAAnimationGroup animation];
  541. anis.animations = @[aniBounds,aniPosition];
  542. anis.duration = 1;
  543. anis.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
  544. anis.removedOnCompletion = NO;
  545. anis.fillMode=kCAFillModeForwards;
  546. [itemLayer addAnimation:anis forKey:nil];
  547. }
  548. /**
  549. 渐变色
  550. @return <#return value description#>
  551. */
  552. -(NSMutableArray *) getColorArr{
  553. NSMutableArray *arr = [NSMutableArray array];
  554. [arr addObject:(__bridge id)[UIColor colorWithRed:236/255.0 green:134/255.0 blue:40/255.0 alpha:1].CGColor];
  555. [arr addObject:(__bridge id)[UIColor colorWithRed:236/255.0 green:134/255.0 blue:40/255.0 alpha:0.4].CGColor];
  556. [arr addObject:(__bridge id)[UIColor colorWithRed:236/255.0 green:134/255.0 blue:40/255.0 alpha:1].CGColor];
  557. return arr;
  558. }
  559. @end