DVBarChartView.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // DVBarChartView.m
  3. // xxxxx
  4. //
  5. // Created by Fire on 15/11/11.
  6. // Copyright © 2015年 DuoLaiDian. All rights reserved.
  7. //
  8. #import "DVBarChartView.h"
  9. #import "DVXBarView.h"
  10. #import "DVYBarView.h"
  11. #import "UIColor+Hex.h"
  12. #import "UIView+Extension.h"
  13. @interface DVBarChartView () <DVXBarViewDelegate>
  14. @property (strong, nonatomic) UIScrollView *scrollView;
  15. @property (strong, nonatomic) DVXBarView *xBarView;
  16. @property (strong, nonatomic) DVYBarView *yBarView;
  17. @property (assign, nonatomic) CGFloat gap;
  18. @end
  19. @implementation DVBarChartView
  20. -(NSMutableArray *)xValues{
  21. if (!_xValues) {
  22. _xValues = [NSMutableArray array];
  23. }
  24. return _xValues;
  25. }
  26. -(NSMutableArray *)xValues2{
  27. if (!_xValues2) {
  28. _xValues2 = [NSMutableArray array];
  29. }
  30. return _xValues2;
  31. }
  32. -(NSMutableArray *)xValues3{
  33. if (!_xValues3) {
  34. _xValues3 = [NSMutableArray array];
  35. }
  36. return _xValues3;
  37. }
  38. - (void)draw {
  39. if (self.xValues.count == 0) return;
  40. // 设置y轴视图的尺寸
  41. self.yBarView.width = self.yAxisViewWidth;
  42. self.yBarView.x = 0;
  43. self.yBarView.height = self.height;
  44. self.yBarView.y = 0;
  45. // 设置scrollView的尺寸
  46. self.scrollView.x = self.yBarView.width;
  47. self.scrollView.y = 0;
  48. self.scrollView.width = self.width - self.scrollView.x;
  49. self.scrollView.height = self.height;
  50. // 设置x轴视图的尺寸
  51. self.xBarView.x = 0;
  52. self.xBarView.y = 0;
  53. self.xBarView.height = self.scrollView.height;
  54. self.xBarView.width = self.xAxisTitleArray.count * (self.barGap + self.barWidth) + 50;
  55. self.scrollView.contentSize = self.xBarView.frame.size;
  56. // 给y轴视图传递参数
  57. self.yBarView.landspace = self.landspace;
  58. self.yBarView.xAxisTextGap = self.xAxisTextGap;
  59. self.yBarView.yAxisTextGap = self.yAxisTextGap;
  60. self.yBarView.textColor = self.textColor;
  61. self.yBarView.textFont = self.textFont;
  62. self.yBarView.percent = self.isPercent;
  63. self.yBarView.axisColor = self.axisColor;
  64. self.yBarView.numberOfYAxisElements = self.numberOfYAxisElements;
  65. self.yBarView.yAxisMaxValue = self.yAxisMaxValue;
  66. self.yBarView.backColor = self.backColor;
  67. self.yBarView.barWidth = self.barWidth;
  68. self.yBarView.legendTitle1 = self.legendTitle1;
  69. self.yBarView.legendTitle2 = self.legendTitle2;
  70. [self.yBarView draw];
  71. self.xBarView.landspace = self.landspace;
  72. self.xBarView.shotImage = self.shotImage;
  73. self.xBarView.xAxisTitleArray = self.xAxisTitleArray;
  74. self.xBarView.barGap = self.barGap;
  75. self.xBarView.xAxisTextGap = self.xAxisTextGap;
  76. self.xBarView.axisColor = self.axisColor;
  77. self.xBarView.numberOfYAxisElements = self.numberOfYAxisElements;
  78. self.xBarView.xValues = self.xValues;
  79. self.xBarView.xValues2 = self.xValues2;
  80. self.xBarView.xValues3 = self.xValues3;
  81. self.xBarView.xTitles = self.xTitles;
  82. self.xBarView.xTitles2 = self.xTitles2;
  83. self.xBarView.xTitles3 = self.xTitles3;
  84. self.xBarView.yAxisMaxValue = self.yAxisMaxValue;
  85. self.xBarView.showPointLabel = self.isShowPointLabel;
  86. self.xBarView.backColor = self.backColor;
  87. self.xBarView.textFont = self.textFont;
  88. self.xBarView.textColor = self.textColor;
  89. self.xBarView.percent = self.isPercent;
  90. self.xBarView.barUserInteractionEnabled = self.isBarUserInteractionEnabled;
  91. self.xBarView.barWidth = self.barWidth;
  92. self.xBarView.barColor = self.barColor;
  93. self.xBarView.barColor2 = self.barColor2;
  94. self.xBarView.barColor3 = self.barColor3;
  95. self.xBarView.barSelectedColor = self.barSelectedColor;
  96. self.xBarView.index = self.index;
  97. [self.xBarView draw];
  98. if (self.index < 0) {
  99. if (self.index * (self.barGap + self.barWidth) > self.scrollView.width * 0.5) {
  100. [self.scrollView setContentOffset:CGPointMake(self.index * (self.barGap + self.barWidth) - self.scrollView.width * 0.5, 0) animated:YES];
  101. }else{
  102. [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
  103. }
  104. }
  105. //初始值
  106. [self.xBarView selectedIndex:0];
  107. //else if (self.index * (self.barGap + self.barWidth) < self.scrollView.width &&
  108. // self.index * (self.barGap + self.barWidth) > self.scrollView.width * 0.5) {
  109. // [self.scrollView setContentOffset:CGPointMake(self.index * (self.barGap + self.barWidth) - self.scrollView.width * 0.5, 0) animated:NO];
  110. //}else
  111. }
  112. -(void)selectedIndex:(NSInteger)index{
  113. [self.xBarView selectedIndex:index];
  114. }
  115. - (void)commonInit {
  116. // 初始化某些属性值
  117. self.xAxisTextGap = 10;
  118. self.yAxisTextGap = 10;
  119. self.barGap = 1;
  120. self.axisColor = [UIColor colorWithHexString:@"67707c"];
  121. self.textColor = [UIColor colorWithHexString:@"9aafc1"];
  122. self.textFont = [UIFont systemFontOfSize:9];
  123. self.numberOfYAxisElements = 5;
  124. self.percent = NO;
  125. self.showPointLabel = YES;
  126. self.backColor = [UIColor colorWithHexString:@"3e4a59"];
  127. self.barUserInteractionEnabled = YES;
  128. self.barColor = [UIColor colorWithRed:0.99 green: 0.55 blue:0.14 alpha:1];
  129. self.barColor2 = [UIColor colorWithRed:0.99 green: 0.55 blue:0.14 alpha:0.5];
  130. self.barColor3 = [UIColor colorWithRed:0.99 green: 0.55 blue:0.14 alpha:0.1];
  131. self.barSelectedColor = [UIColor colorWithHexString:@"fdb302"];
  132. self.barWidth = 57;
  133. self.index = -1;
  134. self.landspace = NO;
  135. self.shotImage = NO;
  136. self.legendTitle1 = @"图例1";
  137. self.legendTitle2 = @"图例2";
  138. // 添加x轴与y轴视图
  139. DVYBarView *yBarView = [[DVYBarView alloc] init];
  140. [self addSubview:yBarView];
  141. UIScrollView *scrollView = [[UIScrollView alloc] init];
  142. DVXBarView *xBarView = [[DVXBarView alloc] init];
  143. [scrollView addSubview:xBarView];
  144. [self addSubview:scrollView];
  145. self.scrollView = scrollView;
  146. self.xBarView = xBarView;
  147. self.xBarView.delegate = self;
  148. self.yBarView = yBarView;
  149. self.scrollView.showsHorizontalScrollIndicator = NO;
  150. self.scrollView.bounces = NO;
  151. }
  152. -(instancetype)init{
  153. if (self == [super init]) {
  154. [self commonInit];
  155. }
  156. return self;
  157. }
  158. - (instancetype)initWithFrame:(CGRect)frame {
  159. if (self == [super initWithFrame:frame]) {
  160. [self commonInit];
  161. }
  162. return self;
  163. }
  164. - (void)awakeFromNib {
  165. [self commonInit];
  166. }
  167. + (instancetype)barChartView {
  168. return [[self alloc] init];
  169. }
  170. - (void)xBarView:(DVXBarView *)xBarView didClickButtonAtIndex:(NSInteger)index {
  171. if ([self.delegate respondsToSelector:@selector(barChartView:didSelectedBarAtIndex:)]) {
  172. [self.delegate barChartView:self didSelectedBarAtIndex:index];
  173. }
  174. }
  175. - (void)addPlot:(NSArray *)plot {
  176. //先清空数组里边的数据
  177. [self.xValues removeAllObjects];
  178. if (plot == nil) return;
  179. if (plot.count == 0) return;
  180. [self.xValues addObjectsFromArray:plot];
  181. // NSLog(@"%s,%@",__func__,self.xValues);
  182. }
  183. - (void)addPlot2:(NSArray *)plot {
  184. [self.xValues2 removeAllObjects];
  185. if (plot == nil) return;
  186. if (plot.count == 0) return;
  187. [self.xValues2 addObjectsFromArray:plot];
  188. // NSLog(@"%s,%@",__func__,self.xValues2);
  189. }
  190. - (void)addPlot3:(NSArray *)plot {
  191. [self.xValues3 removeAllObjects];
  192. if (plot == nil) return;
  193. if (plot.count == 0) return;
  194. [self.xValues3 addObjectsFromArray:plot];
  195. // NSLog(@"%s,%@",__func__,self.xValues2);
  196. }
  197. - (void)removePlot{
  198. [self.xValues removeAllObjects];
  199. }
  200. - (void)removeAllPlot {
  201. [self.xValues removeAllObjects];
  202. [self.xValues2 removeAllObjects];
  203. [self.xValues3 removeAllObjects];
  204. }
  205. @end