InventoryAnalysisViewChartViewController.m 21 KB

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