DVXBarView.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. //
  2. // DVXBarView.m
  3. // xxxxx
  4. //
  5. // Created by Fire on 15/11/11.
  6. // Copyright © 2015年 DuoLaiDian. All rights reserved.
  7. //
  8. #import "DVXBarView.h"
  9. #import "UIView+Extension.h"
  10. #import "DVPlot.h"
  11. //#import "UIButton+Extension.h"
  12. #define random(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/255.0]
  13. #define randomColor random(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))
  14. @interface DVXBarView ()
  15. /**
  16. * 图表顶部留白区域
  17. */
  18. @property (assign, nonatomic) CGFloat topMargin;
  19. /**
  20. * 记录图表区域的高度
  21. */
  22. @property (assign, nonatomic) CGFloat chartHeight;
  23. /**
  24. * 记录坐标轴Label的高度
  25. */
  26. @property (assign, nonatomic) CGFloat textHeight;
  27. /**
  28. * 存放坐标轴的label(底部的)
  29. */
  30. @property (strong, nonatomic) NSMutableArray *titleButtonArray;
  31. /**
  32. * 存放柱子顶部的label
  33. */
  34. @property (strong, nonatomic) NSMutableArray *barTopButtonArray;
  35. /**
  36. * 记录点按钮的集合
  37. */
  38. @property (strong, nonatomic) NSMutableArray *barButtonArray;
  39. /**
  40. * 记录点按钮的集合2
  41. */
  42. @property (strong, nonatomic) NSMutableArray *barButtonArray2;
  43. /**
  44. * 记录点按钮的集合3
  45. */
  46. @property (strong, nonatomic) NSMutableArray *barButtonArray3;
  47. /**
  48. * 选中的点
  49. */
  50. @property (strong, nonatomic) UIButton *selectedBarButton;
  51. /**
  52. * 选中的点
  53. */
  54. @property (strong, nonatomic) UIButton *selectedBarButton2;
  55. /**
  56. * 选中的点
  57. */
  58. @property (strong, nonatomic) UIButton *selectedBarButton3;
  59. /**
  60. * 选中的柱子顶部的label
  61. */
  62. @property (strong, nonatomic) UIButton *selectedBarTopButton;
  63. /**
  64. * 选中的坐标轴的label
  65. */
  66. @property (strong, nonatomic) UIButton *selectedTitleButton;
  67. @end
  68. @implementation DVXBarView
  69. - (NSMutableArray *)titleButtonArray {
  70. if (_titleButtonArray == nil) {
  71. _titleButtonArray = [NSMutableArray array];
  72. }
  73. return _titleButtonArray;
  74. }
  75. - (NSMutableArray *)barTopButtonArray {
  76. if (_barTopButtonArray == nil) {
  77. _barTopButtonArray = [NSMutableArray array];
  78. }
  79. return _barTopButtonArray;
  80. }
  81. - (NSMutableArray *)barButtonArray {
  82. if (_barButtonArray == nil) {
  83. _barButtonArray = [NSMutableArray array];
  84. }
  85. return _barButtonArray;
  86. }
  87. - (NSMutableArray *)barButtonArray2 {
  88. if (_barButtonArray2 == nil) {
  89. _barButtonArray2 = [NSMutableArray array];
  90. }
  91. return _barButtonArray2;
  92. }
  93. - (NSMutableArray *)barButtonArray3 {
  94. if (_barButtonArray3 == nil) {
  95. _barButtonArray3 = [NSMutableArray array];
  96. }
  97. return _barButtonArray3;
  98. }
  99. - (void)draw {
  100. self.backgroundColor = self.backColor;
  101. // 移除先前存在的所有视图
  102. for (UIView *view in self.subviews) {
  103. [view removeFromSuperview];
  104. }
  105. // 移除数组内所有的Label元素
  106. [self.titleButtonArray removeAllObjects];
  107. [self.barTopButtonArray removeAllObjects];
  108. [self.barButtonArray removeAllObjects];
  109. [self.barButtonArray2 removeAllObjects];
  110. [self.barButtonArray3 removeAllObjects];
  111. CGSize labelSize = CGSizeMake(self.barWidth, 29);
  112. // 添加坐标轴Label
  113. for (int i = 0; i < self.xAxisTitleArray.count; i++) {
  114. NSString *title = self.xAxisTitleArray[i];
  115. UIButton *button = [[UIButton alloc] init];
  116. [button setTitle:title forState:UIControlStateNormal];
  117. // button.backgroundColor = [UIColor redColor ];
  118. button.titleLabel.font = self.textFont;
  119. [button setTitleColor:self.textColor forState:UIControlStateNormal];
  120. [button setTitleColor:self.barSelectedColor forState:UIControlStateSelected];
  121. button.tag = 100 + i;
  122. button.titleLabel.font = [UIFont boldSystemFontOfSize:11];
  123. [button addTarget:self action:@selector(barDidClicked:) forControlEvents:UIControlEventTouchUpInside];
  124. button.titleLabel.numberOfLines = 0;
  125. button.x = (i + 1) * self.barGap + i * labelSize.width - 12.5 - self.barGap / 2;
  126. // button.y = self.height - labelSize.height;
  127. if (self.landspace) {
  128. button.y = self.height - labelSize.height;
  129. } else {
  130. button.y = 0;
  131. }
  132. button.width = labelSize.width + 25;
  133. button.height = labelSize.height;
  134. [self.titleButtonArray addObject:button];
  135. [self addSubview:button];
  136. }
  137. // 添加坐标轴
  138. self.textHeight = labelSize.height;
  139. UIView *view = [[UIView alloc] init];
  140. view.backgroundColor = self.axisColor;
  141. view.height = 1;
  142. view.width = self.width + 200;
  143. view.x = -200;
  144. // view.y = self.height - labelSize.height - self.xAxisTextGap;
  145. if (self.landspace) {
  146. view.y = self.height - labelSize.height - self.xAxisTextGap;
  147. } else {
  148. view.y = self.height - 0 - self.xAxisTextGap;
  149. }
  150. [self addSubview:view];
  151. self.topMargin = 50;
  152. if (self.landspace) {
  153. self.chartHeight = self.height - labelSize.height - self.xAxisTextGap - self.topMargin;
  154. } else {
  155. self.chartHeight = self.height - self.xAxisTextGap - self.topMargin;
  156. }
  157. for (int i = 0; i < self.xValues.count; i++) {
  158. NSNumber *value = self.xValues[i];
  159. NSNumber *value2 = self.xValues2[i];
  160. NSString *title = [self decimalwithFormat:@"0" floatV:value.floatValue];
  161. // NSString *title2 = [self decimalwithFormat:@"0" floatV:value2.floatValue];
  162. if (value.floatValue < 0) {
  163. value = @(0);
  164. }
  165. if (value2.floatValue < 0) {
  166. value2 = @(0);
  167. }
  168. // if (i == 0 ) {
  169. //
  170. // }else {
  171. //
  172. // }
  173. CGPoint center = CGPointMake((i+1)*self.barGap + i*self.barWidth - self.barGap / 2, self.chartHeight - value.floatValue/self.yAxisMaxValue * self.chartHeight + self.topMargin);
  174. CGFloat a1 = (i+1)*self.barGap + i*self.barWidth - self.barGap / 2;
  175. // CGFloat b1 = (i+1)*self.barGap + i*self.barWidth;
  176. CGPoint center2 = CGPointMake(a1, self.chartHeight - value.floatValue/self.yAxisMaxValue * self.chartHeight- value2.floatValue/self.yAxisMaxValue * self.chartHeight + self.topMargin);
  177. if (self.yAxisMaxValue * self.chartHeight == 0) {
  178. center = CGPointMake((i+1)*self.barGap + i*self.barWidth, self.chartHeight + self.topMargin);
  179. }
  180. // 添加point处的Label
  181. if (self.isShowPointLabel) {
  182. [self addLabelWithTitle:title atLocation:center andTag:i];
  183. }
  184. [self addVerticalBGViewAtLocationIndex:i];
  185. CGPoint linePoint = CGPointMake((i+1)*(self.barGap + self.barWidth ) , 0);
  186. [self addVerticalLineAtLocation:linePoint];
  187. //第一组
  188. UIButton *button = [[UIButton alloc] init];
  189. button.tag = i + 300;
  190. // [button setBackgroundColor:self.barColor forState:UIControlStateNormal];
  191. // [button setBackgroundColor:self.barSelectedColor forState:UIControlStateSelected];
  192. [button setBackgroundImage:[self imageWithColor:self.barColor] forState:UIControlStateNormal];
  193. [button setBackgroundImage:[self imageWithColor:self.barColor] forState:UIControlStateSelected];
  194. button.titleLabel.numberOfLines = 0;
  195. button.titleLabel.textAlignment = NSTextAlignmentCenter;
  196. [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  197. [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
  198. button.titleLabel.font = self.textFont;
  199. // button.layer.backgroundColor = self.backColor.CGColor;
  200. CGSize buttonSize = CGSizeMake(self.barWidth, value.floatValue/self.yAxisMaxValue * self.chartHeight);
  201. if (self.yAxisMaxValue == 0) {
  202. button.size = CGSizeMake(self.barWidth, 0);
  203. }else{
  204. button.size = buttonSize;
  205. }
  206. button.x = center.x;
  207. button.y = center.y;
  208. if (self.shotImage) {
  209. }
  210. button.userInteractionEnabled = self.isBarUserInteractionEnabled;
  211. [button addTarget:self action:@selector(barDidClicked:) forControlEvents:UIControlEventTouchUpInside];
  212. [self.barButtonArray addObject:button];
  213. if (button.userInteractionEnabled) {
  214. if (self.index < 0) {
  215. if (i == 0) {
  216. // [self barDidClicked:button];
  217. }
  218. }else{
  219. if (i == self.index) {
  220. //[self barDidClicked:button];
  221. }
  222. }
  223. }
  224. [self addSubview:button];
  225. if (self.shotImage == true) {
  226. button.width = 45;
  227. button.height = (value.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight;
  228. button.x = (i)*self.barGap + i*self.barWidth + 2.5;
  229. button.y = self.chartHeight - (value.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight + self.topMargin;
  230. [button setTitle:[NSString stringWithFormat:@"%@",self.xTitles[i]] forState:UIControlStateNormal];
  231. }
  232. //第二组
  233. if (self.xValues2.count != 0) {
  234. UIButton *button2 = [[UIButton alloc] init];
  235. button2.tag = i + 400;
  236. [button2 setBackgroundImage:[self imageWithColor:self.barColor2] forState:UIControlStateNormal];
  237. [button2 setBackgroundImage:[self imageWithColor:self.barColor2] forState:UIControlStateSelected];
  238. [button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  239. [button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
  240. button2.titleLabel.font = self.textFont;
  241. button2.titleLabel.numberOfLines = 0;
  242. button2.titleLabel.textAlignment = NSTextAlignmentCenter;
  243. CGSize buttonSize2 = CGSizeMake(self.barWidth , value2.floatValue/self.yAxisMaxValue * self.chartHeight);
  244. if (self.yAxisMaxValue == 0) {
  245. button2.size = CGSizeMake(self.barWidth, 0);
  246. }else{
  247. button2.size = buttonSize2;
  248. }
  249. button2.x = center2.x;
  250. button2.y = center2.y;
  251. button2.userInteractionEnabled = self.isBarUserInteractionEnabled;
  252. [button2 addTarget:self action:@selector(barDidClicked:) forControlEvents:UIControlEventTouchUpInside];
  253. [self.barButtonArray2 addObject:button2];
  254. if (button.userInteractionEnabled) {
  255. if (self.index < 0) {
  256. if (i == 0) {
  257. // [self barDidClicked:button2];
  258. }
  259. }else{
  260. if (i == self.index) {
  261. // [self barDidClicked:button2];
  262. }
  263. }
  264. }
  265. [self addSubview:button2];
  266. if (self.shotImage == YES) {
  267. button2.width = 45;
  268. button2.height = (value2.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight;
  269. button2.x = (i)*self.barGap + i*self.barWidth + 2.5;
  270. button2.y = self.chartHeight - (value.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight- (value2.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight + self.topMargin;
  271. [button2 setTitle:[NSString stringWithFormat:@"%@",self.xTitles2[i]] forState:UIControlStateNormal];
  272. }
  273. }
  274. //第三组
  275. if ( self.xValues3.count != 0) {
  276. NSNumber *value3 = self.xValues3[i];
  277. // NSString *title3 = [self decimalwithFormat:@"0" floatV:value3.floatValue];
  278. if (value3.floatValue < 0) {
  279. value3 = @(0);
  280. }
  281. CGPoint center3 = CGPointMake(a1, self.chartHeight - value.floatValue/self.yAxisMaxValue * self.chartHeight- value2.floatValue/self.yAxisMaxValue * self.chartHeight - value3.floatValue/self.yAxisMaxValue * self.chartHeight + self.topMargin);
  282. UIButton *button3 = [[UIButton alloc] init];
  283. button3.tag = i + 500;
  284. [button3 setBackgroundImage:[self imageWithColor:self.barColor3] forState:UIControlStateNormal];
  285. [button3 setBackgroundImage:[self imageWithColor:self.barColor3] forState:UIControlStateSelected];
  286. [button3 setTitleColor:self.textColor forState:UIControlStateNormal];
  287. button3.titleLabel.numberOfLines = 0;
  288. button3.titleLabel.textAlignment = NSTextAlignmentCenter;
  289. [button3 setTitleColor:self.barSelectedColor forState:UIControlStateSelected];
  290. button3.titleLabel.font = self.textFont;
  291. //button.layer.backgroundColor = self.backColor.CGColor;
  292. CGSize buttonSize3 = CGSizeMake(self.barWidth, value3.floatValue/self.yAxisMaxValue * self.chartHeight);
  293. if (self.yAxisMaxValue == 0) {
  294. button3.size = CGSizeMake(self.barWidth, 0);
  295. }else{
  296. button3.size = buttonSize3;
  297. }
  298. button3.x = center3.x;
  299. button3.y = center3.y;
  300. button3.userInteractionEnabled = self.isBarUserInteractionEnabled;
  301. [button3 addTarget:self action:@selector(barDidClicked:) forControlEvents:UIControlEventTouchUpInside];
  302. [self.barButtonArray3 addObject:button3];
  303. if (button3.userInteractionEnabled) {
  304. if (self.index < 0) {
  305. if (i == 0) {
  306. // [self barDidClicked:button2];
  307. }
  308. }else{
  309. if (i == self.index) {
  310. // [self barDidClicked:button2];
  311. }
  312. }
  313. }
  314. [self addSubview:button3];
  315. }
  316. }
  317. [self setNeedsDisplay];
  318. }
  319. - (NSString *) decimalwithFormat:(NSString *)format floatV:(float)floatV
  320. {
  321. NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
  322. [numberFormatter setPositiveFormat:format];
  323. return [numberFormatter stringFromNumber:[NSNumber numberWithFloat:floatV]];
  324. }
  325. //添加竖线
  326. -(void)addVerticalLineAtLocation:(CGPoint)location{
  327. UIView *line = [[UIView alloc]init];
  328. line.backgroundColor = self.axisColor;
  329. line.x = location.x;
  330. line.y = location.y;
  331. line.width = 1;
  332. if (self.landspace) {
  333. line.height = self.height - self.xAxisTextGap - 29;
  334. } else {
  335. line.height = self.height - self.xAxisTextGap;
  336. }
  337. [self addSubview:line];
  338. }
  339. //添加bgview
  340. -(void)addVerticalBGViewAtLocationIndex:(int)index{
  341. CGPoint location = CGPointMake((index)*(self.barGap + self.barWidth ) , 0);
  342. UIView *bgView = [[UIView alloc]init];
  343. //bgView.backgroundColor = randomColor;
  344. bgView.x = location.x;
  345. bgView.y = location.y;
  346. bgView.width = self.barGap + self.barWidth ;
  347. if (self.landspace) {
  348. bgView.height = self.height - self.xAxisTextGap - 29;
  349. } else {
  350. bgView.height = self.height - self.xAxisTextGap;
  351. }
  352. bgView.tag = 200000 + index;
  353. [self addSubview:bgView];
  354. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
  355. [bgView addGestureRecognizer:tap];
  356. }
  357. -(void)tapAction:(UITapGestureRecognizer *)tap{
  358. NSInteger index = tap.view.tag - 200000 ;
  359. NSLog(@"点击了 第%ld块区域 ",index);
  360. [self selectedIndex:index];
  361. if ([self.delegate respondsToSelector:@selector(xBarView:didClickButtonAtIndex:)]) {
  362. [self.delegate xBarView:self didClickButtonAtIndex:index];
  363. }
  364. }
  365. // 添加pointLabel的方法
  366. - (void)addLabelWithTitle:(NSString *)title atLocation:(CGPoint)location andTag:(NSInteger)tag {
  367. UIButton *button = [[UIButton alloc] init];
  368. if (self.isPercent) {
  369. [button setTitle:[NSString stringWithFormat:@"%@%%", title] forState:UIControlStateNormal];
  370. }else{
  371. [button setTitle:title forState:UIControlStateNormal];
  372. }
  373. [button setTitleColor:self.textColor forState:UIControlStateNormal];
  374. [button setTitleColor:self.barSelectedColor forState:UIControlStateSelected];
  375. button.titleLabel.font = self.textFont;
  376. button.layer.backgroundColor = self.backColor.CGColor;
  377. // NSDictionary *attr = @{NSFontAttributeName : self.textFont};
  378. // CGSize labelSize = [label.text sizeWithAttributes:attr];
  379. CGSize labelSize = CGSizeMake(self.barWidth, 12);
  380. button.titleLabel.textAlignment = NSTextAlignmentCenter;
  381. button.width = labelSize.width;
  382. button.height = labelSize.height;
  383. button.x = location.x; // - label.width / 2;
  384. button.y = location.y - button.height - 3;
  385. button.tag = 200+tag;
  386. [button addTarget:self action:@selector(barDidClicked:) forControlEvents:UIControlEventTouchUpInside];
  387. button.hidden = YES;
  388. [self.barTopButtonArray addObject:button];
  389. [self addSubview:button];
  390. }
  391. - (void)barDidClicked:(UIButton *)button {
  392. NSInteger index = 0;
  393. if (button.tag >= 100 && button.tag < 200) {
  394. index = button.tag - 100;
  395. } else if (button.tag >= 200 && button.tag < 300) {
  396. index = button.tag - 200 ;
  397. } else if ( button.tag >= 300 && button.tag < 400){
  398. index = button.tag - 300 ;
  399. }else if ( button.tag >= 400 && button.tag < 500){
  400. index = button.tag - 400 ;
  401. }else if ( button.tag >= 500 ){
  402. index = button.tag - 500 ;
  403. }
  404. self.selectedBarButton.selected = NO;
  405. self.selectedBarTopButton.selected = NO;
  406. self.selectedTitleButton.selected = NO;
  407. UIButton *barButton = self.barButtonArray[index];
  408. if (self.xValues2.count != 0) {
  409. self.selectedBarButton2.selected = NO;
  410. UIButton *barButton2 = self.barButtonArray2[index];
  411. barButton2.selected = YES;
  412. self.selectedBarButton2 = barButton2;
  413. }
  414. if (self.xValues3.count != 0) {
  415. self.selectedBarButton3.selected = NO;
  416. UIButton *barButton3 = self.barButtonArray3[index];
  417. barButton3.selected = YES;
  418. self.selectedBarButton3 = barButton3;
  419. }
  420. UIButton *barTopButton = self.barTopButtonArray[index];
  421. UIButton *titleButton = self.titleButtonArray[index];
  422. barButton.selected = YES;
  423. barTopButton.selected = YES;
  424. titleButton.selected = YES;
  425. self.selectedBarButton = barButton;
  426. self.selectedBarTopButton = barTopButton;
  427. self.selectedTitleButton = titleButton;
  428. [self selectedIndex:index];
  429. if ([self.delegate respondsToSelector:@selector(xBarView:didClickButtonAtIndex:)]) {
  430. [self.delegate xBarView:self didClickButtonAtIndex:index];
  431. }
  432. }
  433. -(void)reversalSelected:(BOOL)selected{
  434. if (selected == YES) {
  435. }
  436. }
  437. -(void)selectedIndex:(NSInteger)index{
  438. if (self.barButtonArray.count != 0 && self.barButtonArray2.count != 0 && self.barButtonArray3.count == 0) {
  439. for (int i = 0; i < self.barButtonArray.count; i ++ ) {
  440. NSNumber *value = self.xValues[i];
  441. NSNumber *value2 = self.xValues2[i];
  442. if (value.floatValue < 0) {
  443. value = @(0);
  444. }
  445. if (value2.floatValue < 0) {
  446. value2 = @(0);
  447. }
  448. CGPoint center = CGPointMake((i+1)*self.barGap + i*self.barWidth - self.barGap / 2, self.chartHeight - value.floatValue/self.yAxisMaxValue * self.chartHeight + self.topMargin);
  449. CGFloat a1 = (i+1)*self.barGap + i*self.barWidth - self.barGap / 2;
  450. // CGFloat b1 = (i+1)*self.barGap + i*self.barWidth;
  451. CGPoint center2 = CGPointMake(a1, self.chartHeight - value.floatValue/self.yAxisMaxValue * self.chartHeight- value2.floatValue/self.yAxisMaxValue * self.chartHeight + self.topMargin);
  452. CGSize buttonSize1 = CGSizeMake(self.barWidth, value.floatValue/self.yAxisMaxValue * self.chartHeight);
  453. CGSize buttonSize2 = CGSizeMake(self.barWidth, value2.floatValue/self.yAxisMaxValue * self.chartHeight);
  454. UIButton *barButton = self.barButtonArray[i];
  455. UIButton *barButton2 = self.barButtonArray2[i];
  456. if (i == index) {
  457. if (i == 0 ) {//如果是第一个 往右边
  458. barButton.width = 78;
  459. barButton.height = (value.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight;
  460. barButton.x = i*(self.barWidth + self.barGap) ;
  461. barButton.y = self.chartHeight - (value.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight + self.topMargin;
  462. [barButton setTitle:[NSString stringWithFormat:@"%@",self.xTitles[i]] forState:UIControlStateNormal];
  463. barButton2.width = 78;
  464. barButton2.height = (value2.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight;
  465. barButton2.x = i*(self.barWidth + self.barGap) ;
  466. barButton2.y = self.chartHeight - (value.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight- (value2.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight + self.topMargin;
  467. [barButton2 setTitle:[NSString stringWithFormat:@"%@",self.xTitles2[i]] forState:UIControlStateNormal];
  468. }else{
  469. barButton.width = 78;
  470. barButton.height = (value.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight;
  471. barButton.x = i*(self.barWidth + self.barGap) - 14;
  472. barButton.y = self.chartHeight - (value.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight + self.topMargin;
  473. [barButton setTitle:[NSString stringWithFormat:@"%@",self.xTitles[i]] forState:UIControlStateNormal];
  474. barButton2.width = 78;
  475. barButton2.height = (value2.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight;
  476. barButton2.x = i*(self.barWidth + self.barGap) - 14;
  477. barButton2.y = self.chartHeight - (value.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight- (value2.floatValue/self.yAxisMaxValue + 0.1) * self.chartHeight + self.topMargin;
  478. [barButton2 setTitle:[NSString stringWithFormat:@"%@",self.xTitles2[i]] forState:UIControlStateNormal];
  479. }
  480. }else{
  481. barButton.size = buttonSize1;
  482. barButton.y = center.y;
  483. barButton.x = (i+1)*self.barGap + i*self.barWidth - self.barGap / 2;
  484. [barButton setTitle:@"" forState:UIControlStateNormal];
  485. barButton2.size = buttonSize2;
  486. barButton2.y = center2.y;
  487. barButton2.x = (i+1)*self.barGap + i*self.barWidth - self.barGap / 2;
  488. [barButton2 setTitle:@"" forState:UIControlStateNormal];
  489. }
  490. }
  491. }
  492. }
  493. -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  494. NSLog(@"%s",__func__);
  495. if (self.barButtonArray.count != 0 && self.barButtonArray2.count != 0 && self.barButtonArray3.count == 0) {
  496. for (int i = 0; i < self.barButtonArray.count; i ++ ) {
  497. UIButton *barButton = self.barButtonArray[i];
  498. UIButton *barButton2 = self.barButtonArray2[i];
  499. barButton.width = 30;
  500. barButton.x = (i+1)*self.barGap + i*self.barWidth - self.barGap / 2 ;
  501. [barButton setTitle:@"" forState:UIControlStateNormal];
  502. barButton2.width = 30;
  503. barButton2.x = (i+1)*self.barGap + i*self.barWidth - self.barGap / 2;
  504. [barButton2 setTitle:@"" forState:UIControlStateNormal];
  505. }
  506. }
  507. }
  508. - (UIImage *)imageWithColor:(UIColor *)color {
  509. CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
  510. UIGraphicsBeginImageContext(rect.size);
  511. CGContextRef context = UIGraphicsGetCurrentContext();
  512. CGContextSetFillColorWithColor(context, [color CGColor]);
  513. CGContextFillRect(context, rect);
  514. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  515. UIGraphicsEndImageContext();
  516. return image;
  517. }
  518. @end