酷店

SSWlineIncomeChartView.m 15KB

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