酷店

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.width-60;
  162. _totalHeight=self.scrollView.bounds.size.height-30;
  163. self.scrollView.contentSize = CGSizeMake(30+_totalWidth, 0);
  164. self.contentView.frame = CGRectMake(40,10, _totalWidth,_totalHeight);
  165. [self drawLineChart];
  166. }
  167. //开始绘制图表
  168. -(void)drawLineChart{
  169. [self clear];
  170. [self drawAxis];
  171. [self addYAxisLabs];
  172. [self addXAxisLabs];
  173. [self calculationTheLinePoints];
  174. [self addLine];
  175. [self addLinePoints];
  176. [self showBarChartYValus];
  177. }
  178. //触发界面布局之前清除掉之前的绘制
  179. -(void)clear{
  180. [self.barsStartPointsArr removeAllObjects];
  181. [self.barsEndPointsArr removeAllObjects];
  182. for (CAShapeLayer *layer in self.linePointLayerArr) {
  183. [layer removeFromSuperlayer];
  184. }
  185. [self.linePointLayerArr removeAllObjects];
  186. [_AxiasLineLayer removeFromSuperlayer];
  187. [_lineLayer removeFromSuperlayer];
  188. _AxiasLineLayer=nil;
  189. _lineLayer=nil;
  190. for (UIView *view in self.contentView.subviews) {
  191. if([view isEqual:self.unitLab])continue;
  192. [view removeFromSuperview];
  193. }
  194. }
  195. //画坐标轴
  196. -(void)drawAxis{
  197. UIBezierPath *path = [UIBezierPath bezierPath];
  198. //画x轴的刻度
  199. for (int i = 0; i<self.xValuesArr.count; i++) {
  200. CGPoint startPoint = CGPointMake(i*(self.barWidth+self.gapWidth)+self.gapWidth+self.barWidth/2, _totalHeight);
  201. [self.barsStartPointsArr addObject:[NSValue valueWithCGPoint:startPoint]];
  202. [path moveToPoint:startPoint];
  203. [path addLineToPoint:CGPointMake(i*(self.barWidth+self.gapWidth)+self.gapWidth+self.barWidth/2, _totalHeight-5)];
  204. }
  205. //先画整体的线
  206. [path moveToPoint:CGPointMake(0, _totalHeight)];
  207. [path addLineToPoint:CGPointMake(0, _totalHeight)];
  208. [path addLineToPoint:CGPointMake(_totalWidth-10, _totalHeight)];
  209. CAShapeLayer *lineLayer = [CAShapeLayer layer];
  210. lineLayer.strokeColor = [UIColor colorWithHexString:ThemeColor].CGColor;
  211. lineLayer.lineWidth = 0.5;
  212. lineLayer.path = path.CGPath;
  213. lineLayer.fillColor = [UIColor clearColor].CGColor;
  214. [self.contentView.layer addSublayer:lineLayer];
  215. _AxiasLineLayer = lineLayer;
  216. }
  217. //给y轴添加刻度显示
  218. -(void)addYAxisLabs{
  219. for (int i =self.yAxisCount ; i>0; i--) {
  220. CGFloat yAxis = self.yScaleValue*i;
  221. UILabel *lab = [[UILabel alloc]init];
  222. lab.frame = CGRectMake(-5, (self.yAxisCount-i)*(_totalHeight/self.yAxisCount)-10, -25, 20);
  223. lab.text = [NSString stringWithFormat:@"%.f",yAxis];
  224. lab.font = [UIFont systemFontOfSize:10];
  225. lab.textAlignment = NSTextAlignmentRight;
  226. UIView *views =[[UIView alloc]initWithFrame:CGRectMake(20, (self.yAxisCount-i)*(_totalHeight/self.yAxisCount), _totalWidth-30, 0.5)];
  227. [self drawDashLine:views lineLength:_totalWidth lineSpacing:0.5 lineColor:[UIColor colorWithHexString:@"#878585"]];
  228. [self.contentView addSubview:views];
  229. [self.contentView addSubview:lab];
  230. }
  231. }
  232. /**
  233. ** lineView: 需要绘制成虚线的view
  234. ** lineLength: 虚线的宽度
  235. ** lineSpacing: 虚线的间距
  236. ** lineColor: 虚线的颜色
  237. **/
  238. - (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor
  239. {
  240. CAShapeLayer *shapeLayer = [CAShapeLayer layer];
  241. [shapeLayer setBounds:lineView.bounds];
  242. [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
  243. [shapeLayer setFillColor:[UIColor clearColor].CGColor];
  244. // 设置虚线颜色为
  245. [shapeLayer setStrokeColor:lineColor.CGColor];
  246. // 设置虚线宽度
  247. [shapeLayer setLineWidth:0.2];
  248. [shapeLayer setLineJoin:kCALineJoinRound];
  249. // 设置线宽,线间距
  250. [shapeLayer setLineDashPattern:
  251. [NSArray arrayWithObjects:[NSNumber numberWithInt:3],
  252. [NSNumber numberWithInt:1],nil]];
  253. // 设置路径
  254. CGMutablePathRef path = CGPathCreateMutable();
  255. CGPathMoveToPoint(path, NULL, 0, 0);
  256. CGPathAddLineToPoint(path, NULL, CGRectGetWidth(lineView.frame), 0);
  257. [shapeLayer setPath:path];
  258. CGPathRelease(path);
  259. // 把绘制好的虚线添加上来
  260. [lineView.layer addSublayer:shapeLayer];
  261. }
  262. //给x轴添加刻度显示
  263. -(void)addXAxisLabs{
  264. for (int i = 0 ; i<self.xValuesArr.count; i++) {
  265. CGPoint point = [self.barsStartPointsArr[i] CGPointValue];
  266. UILabel *lab = [[UILabel alloc]init];
  267. lab.bounds = CGRectMake(0, 0, self.barWidth+self.gapWidth*4/5, 20);
  268. lab.center = CGPointMake(point.x, point.y+lab.bounds.size.height/2);
  269. if (self.xValuesArr.count<=6) {
  270. lab.text = [NSString stringWithFormat:@"%@",self.xValuesArr[i]];
  271. }
  272. lab.textColor=[UIColor colorWithHexString:@"#333333"];
  273. lab.font = [UIFont systemFontOfSize:12];
  274. lab.textAlignment = NSTextAlignmentCenter;
  275. [self.contentView addSubview:lab];
  276. }
  277. }
  278. //计算折线的点坐标
  279. -(void)calculationTheLinePoints{
  280. for (int i = 0 ; i<self.yValuesArr.count; i++) {
  281. CGFloat Y = [(NSString *)self.yValuesArr[i] floatValue];
  282. CGFloat barHeight = (Y/(self.yAxisCount*self.yScaleValue))*_totalHeight;
  283. CGPoint startPoint = [self.barsStartPointsArr[i] CGPointValue];
  284. CGPoint endPoint = CGPointMake(startPoint.x, startPoint.y-barHeight);
  285. [self.barsEndPointsArr addObject:[NSValue valueWithCGPoint:endPoint]];
  286. }
  287. }
  288. //添加折线的点
  289. -(void)addLinePoints{
  290. for (int i = 0 ; i<self.xValuesArr.count; i++) {
  291. CGPoint point = [self.barsEndPointsArr[i] CGPointValue];
  292. UIBezierPath *path = [UIBezierPath bezierPath];
  293. [path addArcWithCenter:point radius:2 startAngle:0 endAngle:M_PI*2 clockwise:YES];
  294. // [path closePath];
  295. [self.linePointPathArr addObject:path];
  296. CAShapeLayer *layer = [CAShapeLayer layer];
  297. layer.strokeColor = self.lineColor.CGColor;
  298. layer.fillColor = self.lineColor.CGColor;
  299. layer.lineWidth=1;
  300. layer.path = path.CGPath;
  301. [layer addAnimation:[self animationWithDuration:0.3*i] forKey:nil];
  302. [self.contentView.layer addSublayer:layer];
  303. [self.linePointLayerArr addObject:layer];
  304. }
  305. }
  306. //添加折线
  307. -(void)addLine{
  308. UIBezierPath *linePath = [UIBezierPath bezierPath];
  309. if (self.barsStartPointsArr.count > 0) {
  310. [linePath moveToPoint:[self.barsEndPointsArr[0] CGPointValue]];
  311. }
  312. for (int i = 1; i<self.barsEndPointsArr.count; i++) {
  313. [linePath addLineToPoint:[self.barsEndPointsArr[i] CGPointValue]];
  314. }
  315. CAShapeLayer *lineLayer = [CAShapeLayer layer];
  316. lineLayer.strokeColor = self.lineColor.CGColor;
  317. lineLayer.lineWidth = 1;
  318. lineLayer.fillColor = [UIColor clearColor].CGColor;
  319. lineLayer.path = linePath.CGPath;
  320. [lineLayer addAnimation:[self animationWithDuration:0.3*self.barsEndPointsArr.count] forKey:nil];
  321. [self.contentView.layer addSublayer:lineLayer];
  322. _lineLayer = lineLayer;
  323. }
  324. //添加动画
  325. -(CABasicAnimation *)animationWithDuration:(CFTimeInterval)duration{
  326. CABasicAnimation *anmiation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
  327. anmiation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  328. anmiation.duration =duration;
  329. anmiation.fromValue=@(0);
  330. anmiation.toValue = @(1);
  331. return anmiation;
  332. }
  333. //显示每个柱形图的值
  334. -(void)showBarChartYValus{
  335. if(!self.showEachYValus)return;
  336. for (int i = 0; i<self.xValuesArr.count; i++) {
  337. CGPoint point = [self.barsEndPointsArr[i] CGPointValue];
  338. UILabel *lab = [[UILabel alloc]init];
  339. lab.textColor = self.lineColor;
  340. lab.font = [UIFont systemFontOfSize:10];
  341. // lab.text = self.yValuesArr[i];
  342. lab.textAlignment = NSTextAlignmentCenter;
  343. lab.bounds = CGRectMake(0, 0, self.barWidth+self.gapWidth*4/5, 20);
  344. lab.center = CGPointMake(point.x, point.y-10);
  345. [self.contentView addSubview:lab];
  346. }
  347. }
  348. #pragma mark---小时和天切换
  349. -(void)changeValues:(UIButton *)btn
  350. {
  351. if (btn.tag == 1001) {//昨天天
  352. [self.monthBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
  353. [self.yesterdayBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal];
  354. [self.TodayBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal];
  355. [self OrderStatistics:@"1"];
  356. }else if(btn.tag == 2001){//本月
  357. [self.yesterdayBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
  358. [self.monthBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal];
  359. [self.TodayBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal];
  360. [self OrderStatistics:@"2"];
  361. }else{
  362. [self.TodayBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
  363. [self.yesterdayBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal];
  364. [self.monthBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal];
  365. [self OrderStatistics:@"0"];
  366. }
  367. if (self.changeMineBtn) {
  368. self.changeMineBtn(btn);
  369. }
  370. }
  371. #pragma mark---订单统计
  372. -(void)OrderStatistics:(NSString *)type
  373. {
  374. }
  375. @end