酷店

SSWlineIncomeChartView.m 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. //
  2. // ZSIncomeChartView.m
  3. // ZBProject
  4. //
  5. // Created by 学丽 on 2019/4/9.
  6. // Copyright © 2019 ZB. All rights reserved.
  7. //
  8. #import "SSWlineIncomeChartView.h"
  9. @interface SSWlineIncomeChartView (){
  10. CGFloat _totalWidth;
  11. CGFloat _totalHeight;
  12. CAShapeLayer *_AxiasLineLayer;
  13. CAShapeLayer *_lineLayer;
  14. }
  15. @property(nonatomic)UIScrollView *scrollView;
  16. @property(nonatomic)UIView *contentView;
  17. @property(nonatomic)NSMutableArray *barsStartPointsArr;
  18. @property(nonatomic)NSMutableArray *barsEndPointsArr;
  19. @property(nonatomic)NSMutableArray *linePointPathArr;
  20. @property(nonatomic)NSMutableArray *linePointLayerArr;
  21. @property(nonatomic)UILabel *unitLab;
  22. @property(nonatomic)UIButton *yesterdayBtn;
  23. @property(nonatomic)UIButton *monthBtn;
  24. @property(nonatomic)UIButton *TodayBtn;
  25. @end
  26. @implementation SSWlineIncomeChartView
  27. -(instancetype)initWithChartType:(SSWChartsType)type{
  28. self = [super initWithChartType:type];
  29. if (self) {
  30. [self setUp];
  31. }
  32. return self;
  33. }
  34. -(void)setUp{
  35. self.xValuesArr = [@[] mutableCopy];
  36. self.yValuesArr = [@[] mutableCopy];
  37. self.barsStartPointsArr = [@[] mutableCopy];
  38. self.barsEndPointsArr = [@[] mutableCopy];
  39. self.linePointPathArr = [@[] mutableCopy];
  40. self.linePointLayerArr = [@[] mutableCopy];
  41. self.barWidth =SCREEN_WIDTH/5-16;
  42. self.gapWidth = 0;
  43. self.yScaleValue = 50;
  44. self.yAxisCount = 5;
  45. self.showEachYValus=YES;
  46. self.lineColor = [UIColor colorWithHexString:ThemeColor];
  47. self.userInteractionEnabled=YES;
  48. [self addSubview:self.scrollView];
  49. [self.scrollView addSubview:self.contentView];
  50. [self.scrollView addSubview:self.unitLab];
  51. [self addSubview:self.monthBtn];
  52. [self addSubview:self.TodayBtn];
  53. [self addSubview:self.yesterdayBtn];
  54. self.scrollView.userInteractionEnabled=YES;
  55. [self addTap];
  56. [self.contentView addSubview:self.bubbleLab];
  57. }
  58. -(void)setYValuesArr:(NSMutableArray *)yValuesArr
  59. {
  60. _yValuesArr = yValuesArr;
  61. [self layoutSubviews];
  62. [self draWUpdate];
  63. }
  64. -(void)addTap{
  65. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
  66. [self.contentView addGestureRecognizer:tap];
  67. }
  68. -(void)tap:(UITapGestureRecognizer *)tap{
  69. CGPoint point = [tap locationInView:self.contentView];
  70. for (UIBezierPath *path in self.linePointPathArr) {
  71. if ([path containsPoint:point]) {
  72. NSInteger index = [self.linePointPathArr indexOfObject:path];
  73. CGPoint endPoint = [self.barsEndPointsArr[index] CGPointValue];
  74. // NSLog(@"点击了第%ld个柱形图",index);
  75. // self.bubbleLab.hidden=NO;
  76. // self.bubbleLab.text = self.yValuesArr[index];
  77. // self.bubbleLab.center = CGPointMake(endPoint.x, endPoint.y-10);
  78. if (self.delegate && [self.delegate respondsToSelector:@selector(SSWChartView:didSelectIndex:)]) {
  79. [self.delegate SSWChartView:self didSelectIndex:index];
  80. }
  81. }
  82. }
  83. }
  84. -(void)setUnit:(NSString *)unit{
  85. self.unitLab.hidden=NO;
  86. _unit=unit;
  87. self.unitLab.text =unit;
  88. }
  89. -(UIScrollView *)scrollView{
  90. if (!_scrollView) {
  91. _scrollView = [[UIScrollView alloc]init];
  92. _scrollView.showsVerticalScrollIndicator = NO;
  93. _scrollView.showsHorizontalScrollIndicator=NO;
  94. _scrollView.clipsToBounds=NO;
  95. }
  96. return _scrollView;
  97. }
  98. -(UIView *)contentView{
  99. if (!_contentView) {
  100. _contentView = [[UIView alloc]init];
  101. // _contentView.clipsToBounds = YES;
  102. }
  103. return _contentView;
  104. }
  105. -(UILabel *)unitLab{
  106. if (!_unitLab) {
  107. _unitLab = [[UILabel alloc]init];
  108. _unitLab.font = [UIFont systemFontOfSize:12];
  109. _unitLab.textColor=[UIColor colorWithHexString:@"#333333"];
  110. }
  111. return _unitLab;
  112. }
  113. -(UIButton *)TodayBtn
  114. {
  115. if (!_TodayBtn) {
  116. _TodayBtn =[[UIButton alloc]init];
  117. _TodayBtn.titleLabel.font =[UIFont systemFontOfSize:12];
  118. [_TodayBtn setTitle:@"今天" forState:UIControlStateNormal];
  119. [_TodayBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
  120. _TodayBtn.tag = 3001;
  121. [_TodayBtn addTarget:self action:@selector(changeValues:) forControlEvents:UIControlEventTouchUpInside];
  122. }
  123. return _TodayBtn;
  124. }
  125. -(UIButton *)monthBtn
  126. {
  127. if (!_monthBtn) {
  128. _monthBtn =[[UIButton alloc]init];
  129. _monthBtn.titleLabel.font =[UIFont systemFontOfSize:12];
  130. [_monthBtn setTitle:@"昨天" forState:UIControlStateNormal];
  131. [_monthBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal];
  132. _monthBtn.tag = 1001;
  133. [_monthBtn addTarget:self action:@selector(changeValues:) forControlEvents:UIControlEventTouchUpInside];
  134. }
  135. return _monthBtn;
  136. }
  137. -(UIButton *)yesterdayBtn
  138. {
  139. if (!_yesterdayBtn) {
  140. _yesterdayBtn =[[UIButton alloc]init];
  141. _yesterdayBtn.titleLabel.font =[UIFont systemFontOfSize:12];
  142. _yesterdayBtn.tag = 2001;
  143. [_yesterdayBtn setTitle:@"本月" forState:UIControlStateNormal];
  144. [_yesterdayBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal];
  145. [_yesterdayBtn addTarget:self action:@selector(changeValues:) forControlEvents:UIControlEventTouchUpInside];
  146. }
  147. return _yesterdayBtn;
  148. }
  149. -(void)layoutSubviews{
  150. self.scrollView.frame =CGRectMake(0, 40, self.width, self.height-50);
  151. self.unitLab.frame = CGRectMake(20, -40, 30, 20);
  152. self.TodayBtn.frame=CGRectMake(self.bounds.size.width-150, 0, 40, 15); self.yesterdayBtn.frame=CGRectMake(self.bounds.size.width-50, 0, 40, 15);
  153. self.monthBtn.frame=CGRectMake(self.bounds.size.width-100, 0, 40, 15);
  154. }
  155. -(void)draWUpdate
  156. {
  157. _totalWidth= self.width-60;
  158. _totalHeight=self.scrollView.bounds.size.height-30;
  159. self.scrollView.contentSize = CGSizeMake(30+_totalWidth, 0);
  160. self.contentView.frame = CGRectMake(40,10, _totalWidth,_totalHeight);
  161. [self drawLineChart];
  162. }
  163. //开始绘制图表
  164. -(void)drawLineChart{
  165. [self clear];
  166. [self drawAxis];
  167. [self addYAxisLabs];
  168. [self addXAxisLabs];
  169. [self calculationTheLinePoints];
  170. [self addLine];
  171. [self addLinePoints];
  172. [self showBarChartYValus];
  173. }
  174. //触发界面布局之前清除掉之前的绘制
  175. -(void)clear{
  176. [self.barsStartPointsArr removeAllObjects];
  177. [self.barsEndPointsArr removeAllObjects];
  178. for (CAShapeLayer *layer in self.linePointLayerArr) {
  179. [layer removeFromSuperlayer];
  180. }
  181. [self.linePointLayerArr removeAllObjects];
  182. [_AxiasLineLayer removeFromSuperlayer];
  183. [_lineLayer removeFromSuperlayer];
  184. _AxiasLineLayer=nil;
  185. _lineLayer=nil;
  186. for (UIView *view in self.contentView.subviews) {
  187. if([view isEqual:self.unitLab])continue;
  188. [view removeFromSuperview];
  189. }
  190. }
  191. //画坐标轴
  192. -(void)drawAxis{
  193. UIBezierPath *path = [UIBezierPath bezierPath];
  194. //画x轴的刻度
  195. for (int i = 0; i<self.xValuesArr.count; i++) {
  196. CGPoint startPoint = CGPointMake(i*(self.barWidth+self.gapWidth)+self.gapWidth+self.barWidth/2, _totalHeight);
  197. [self.barsStartPointsArr addObject:[NSValue valueWithCGPoint:startPoint]];
  198. [path moveToPoint:startPoint];
  199. [path addLineToPoint:CGPointMake(i*(self.barWidth+self.gapWidth)+self.gapWidth+self.barWidth/2, _totalHeight-5)];
  200. }
  201. //先画整体的线
  202. [path moveToPoint:CGPointMake(0, _totalHeight)];
  203. [path addLineToPoint:CGPointMake(0, _totalHeight)];
  204. [path addLineToPoint:CGPointMake(_totalWidth-10, _totalHeight)];
  205. CAShapeLayer *lineLayer = [CAShapeLayer layer];
  206. lineLayer.strokeColor = [UIColor colorWithHexString:ThemeColor].CGColor;
  207. lineLayer.lineWidth = 0.5;
  208. lineLayer.path = path.CGPath;
  209. lineLayer.fillColor = [UIColor clearColor].CGColor;
  210. [self.contentView.layer addSublayer:lineLayer];
  211. _AxiasLineLayer = lineLayer;
  212. }
  213. //给y轴添加刻度显示
  214. -(void)addYAxisLabs{
  215. for (int i =self.yAxisCount ; i>0; i--) {
  216. CGFloat yAxis = self.yScaleValue*i;
  217. UILabel *lab = [[UILabel alloc]init];
  218. lab.frame = CGRectMake(-5, (self.yAxisCount-i)*(_totalHeight/self.yAxisCount)-10, -25, 20);
  219. lab.text = [NSString stringWithFormat:@"%.f",yAxis];
  220. lab.font = [UIFont systemFontOfSize:10];
  221. lab.textAlignment = NSTextAlignmentRight;
  222. UIView *views =[[UIView alloc]initWithFrame:CGRectMake(20, (self.yAxisCount-i)*(_totalHeight/self.yAxisCount), _totalWidth-30, 0.5)];
  223. [self drawDashLine:views lineLength:_totalWidth lineSpacing:0.5 lineColor:[UIColor colorWithHexString:@"#878585"]];
  224. [self.contentView addSubview:views];
  225. [self.contentView addSubview:lab];
  226. }
  227. }
  228. /**
  229. ** lineView: 需要绘制成虚线的view
  230. ** lineLength: 虚线的宽度
  231. ** lineSpacing: 虚线的间距
  232. ** lineColor: 虚线的颜色
  233. **/
  234. - (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor
  235. {
  236. CAShapeLayer *shapeLayer = [CAShapeLayer layer];
  237. [shapeLayer setBounds:lineView.bounds];
  238. [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
  239. [shapeLayer setFillColor:[UIColor clearColor].CGColor];
  240. // 设置虚线颜色为
  241. [shapeLayer setStrokeColor:lineColor.CGColor];
  242. // 设置虚线宽度
  243. [shapeLayer setLineWidth:0.2];
  244. [shapeLayer setLineJoin:kCALineJoinRound];
  245. // 设置线宽,线间距
  246. [shapeLayer setLineDashPattern:
  247. [NSArray arrayWithObjects:[NSNumber numberWithInt:3],
  248. [NSNumber numberWithInt:1],nil]];
  249. // 设置路径
  250. CGMutablePathRef path = CGPathCreateMutable();
  251. CGPathMoveToPoint(path, NULL, 0, 0);
  252. CGPathAddLineToPoint(path, NULL, CGRectGetWidth(lineView.frame), 0);
  253. [shapeLayer setPath:path];
  254. CGPathRelease(path);
  255. // 把绘制好的虚线添加上来
  256. [lineView.layer addSublayer:shapeLayer];
  257. }
  258. //给x轴添加刻度显示
  259. -(void)addXAxisLabs{
  260. for (int i = 0 ; i<self.xValuesArr.count; i++) {
  261. CGPoint point = [self.barsStartPointsArr[i] CGPointValue];
  262. UILabel *lab = [[UILabel alloc]init];
  263. lab.bounds = CGRectMake(0, 0, self.barWidth+self.gapWidth*4/5, 20);
  264. lab.center = CGPointMake(point.x, point.y+lab.bounds.size.height/2);
  265. // lab.text = [NSString stringWithFormat:@"%@",self.xValuesArr[i]];
  266. lab.textColor=[UIColor colorWithHexString:@"#333333"];
  267. lab.font = [UIFont systemFontOfSize:12];
  268. lab.textAlignment = NSTextAlignmentCenter;
  269. [self.contentView addSubview:lab];
  270. }
  271. }
  272. //计算折线的点坐标
  273. -(void)calculationTheLinePoints{
  274. for (int i = 0 ; i<self.yValuesArr.count; i++) {
  275. CGFloat Y = [(NSString *)self.yValuesArr[i] floatValue];
  276. CGFloat barHeight = (Y/(self.yAxisCount*self.yScaleValue))*_totalHeight;
  277. CGPoint startPoint = [self.barsStartPointsArr[i] CGPointValue];
  278. CGPoint endPoint = CGPointMake(startPoint.x, startPoint.y-barHeight);
  279. [self.barsEndPointsArr addObject:[NSValue valueWithCGPoint:endPoint]];
  280. }
  281. }
  282. //添加折线的点
  283. -(void)addLinePoints{
  284. for (int i = 0 ; i<self.xValuesArr.count; i++) {
  285. CGPoint point = [self.barsEndPointsArr[i] CGPointValue];
  286. UIBezierPath *path = [UIBezierPath bezierPath];
  287. [path addArcWithCenter:point radius:2 startAngle:0 endAngle:M_PI*2 clockwise:YES];
  288. // [path closePath];
  289. [self.linePointPathArr addObject:path];
  290. CAShapeLayer *layer = [CAShapeLayer layer];
  291. layer.strokeColor = self.lineColor.CGColor;
  292. layer.fillColor = self.lineColor.CGColor;
  293. layer.lineWidth=1;
  294. layer.path = path.CGPath;
  295. [layer addAnimation:[self animationWithDuration:0.3*i] forKey:nil];
  296. [self.contentView.layer addSublayer:layer];
  297. [self.linePointLayerArr addObject:layer];
  298. }
  299. }
  300. //添加折线
  301. -(void)addLine{
  302. UIBezierPath *linePath = [UIBezierPath bezierPath];
  303. if (self.barsStartPointsArr.count > 0) {
  304. [linePath moveToPoint:[self.barsEndPointsArr[0] CGPointValue]];
  305. }
  306. for (int i = 1; i<self.barsEndPointsArr.count; i++) {
  307. [linePath addLineToPoint:[self.barsEndPointsArr[i] CGPointValue]];
  308. }
  309. CAShapeLayer *lineLayer = [CAShapeLayer layer];
  310. lineLayer.strokeColor = self.lineColor.CGColor;
  311. lineLayer.lineWidth = 1;
  312. lineLayer.fillColor = [UIColor clearColor].CGColor;
  313. lineLayer.path = linePath.CGPath;
  314. [lineLayer addAnimation:[self animationWithDuration:0.3*self.barsEndPointsArr.count] forKey:nil];
  315. [self.contentView.layer addSublayer:lineLayer];
  316. _lineLayer = lineLayer;
  317. }
  318. //添加动画
  319. -(CABasicAnimation *)animationWithDuration:(CFTimeInterval)duration{
  320. CABasicAnimation *anmiation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
  321. anmiation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  322. anmiation.duration =duration;
  323. anmiation.fromValue=@(0);
  324. anmiation.toValue = @(1);
  325. return anmiation;
  326. }
  327. //显示每个柱形图的值
  328. -(void)showBarChartYValus{
  329. if(!self.showEachYValus)return;
  330. for (int i = 0; i<self.xValuesArr.count; i++) {
  331. CGPoint point = [self.barsEndPointsArr[i] CGPointValue];
  332. UILabel *lab = [[UILabel alloc]init];
  333. lab.textColor = self.lineColor;
  334. lab.font = [UIFont systemFontOfSize:10];
  335. // lab.text = self.yValuesArr[i];
  336. lab.textAlignment = NSTextAlignmentCenter;
  337. lab.bounds = CGRectMake(0, 0, self.barWidth+self.gapWidth*4/5, 20);
  338. lab.center = CGPointMake(point.x, point.y-10);
  339. [self.contentView addSubview:lab];
  340. }
  341. }
  342. #pragma mark---小时和天切换
  343. -(void)changeValues:(UIButton *)btn
  344. {
  345. if (btn.tag == 1001) {//昨天天
  346. [self.monthBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
  347. [self.yesterdayBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal];
  348. [self.TodayBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal];
  349. [self OrderStatistics:@"1"];
  350. }else if(btn.tag == 2001){//本月
  351. [self.yesterdayBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
  352. [self.monthBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal];
  353. [self.TodayBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal];
  354. [self OrderStatistics:@"2"];
  355. }else{
  356. [self.TodayBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
  357. [self.yesterdayBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal];
  358. [self.monthBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal];
  359. [self OrderStatistics:@"0"];
  360. }
  361. if (self.changeMineBtn) {
  362. self.changeMineBtn(btn);
  363. }
  364. }
  365. #pragma mark---订单统计
  366. -(void)OrderStatistics:(NSString *)type
  367. {
  368. }
  369. @end