dkahgld

SSWLineChartView.m 16KB

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