| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379 |
- //
- // LHYChartView.m
- // LHYChartView
- //
- // Created by lreson on 16/7/21.
- // Copyright © 2016年 lreson. All rights reserved.
- //
- #import "LHYChartView.h"
- #import "YJYTouchCollectionView.h"
- #import "YJYTouchScroll.h"
- #import "YJYLinesCell.h"
- #import "LHYLinesPaoPaoView.h"
- #define btnW 12
- #define kPaoPaoWidth 75.f
- #define chartViewHeight self.bounds.size.height - 30
- #define KCircleRadius 3 //线条上圆圈半径
- #define KCircleRadius1 5 //线条上圆圈半径
- @interface LHYChartView ()<UIScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,CAAnimationDelegate>
- {
- CGFloat currentPage;//当前页数
- //CGFloat Xmargin;//X轴方向的偏移
- CGFloat Ymargin;//Y轴方向的偏移
- CGPoint lastPoint;//最后一个坐标点
- UIButton *firstBtn;
- UIButton *lastBtn;
- CGFloat titleWOfY;
- }
- @property (nonatomic,strong)YJYTouchScroll *chartScrollView;
- @property (nonatomic,strong)YJYTouchCollectionView * xAxiCollectionView;
- @property (nonatomic,strong)UIPageControl *pageControl;//分页
- @property (nonatomic,strong)NSMutableArray *leftPointArr;//左边的数据源
- @property (nonatomic,strong)NSMutableArray *rightPointArr;//左边的数据源
- @property (nonatomic,strong)NSMutableArray *leftBtnArr;//左边按钮
- @property (nonatomic, strong)NSMutableArray *detailLabelArr;
- @property (nonatomic,strong)NSArray *leftScaleArr;
- @property (nonatomic,strong)NSArray *rightScaleArr;
- @property (nonatomic,strong)NSMutableArray *leftScaleViewArr;//左边的点击显示图
- @property (nonatomic,strong)UIView *scaleBgView;
- @property (nonatomic,strong)UILabel *lineLabel;
- @property (nonatomic,strong)UILabel *scaleLabel;
- @property (nonatomic,strong)UILabel *dateTimeLabel;
- @property (nonatomic,assign)CGFloat leftJiange;
- @property (nonatomic,assign)CGFloat jiange;
- @property (nonatomic,assign)CGFloat rightJiange;
- @property (nonatomic,assign)BOOL showSelect;
- @property (nonatomic,assign) NSInteger selectIndex;
- @property (nonatomic,strong)UIView *selectView;
- @property (nonatomic,strong)LHYLinesPaoPaoView * paopaoView;
- @property (nonatomic,strong)NSMutableArray *charCircleViewArr;
- @property (strong,nonatomic) UIBezierPath *circlePath;
- @property (strong,nonatomic) CAGradientLayer *gradientlayer;
- @property (strong,nonatomic) CAShapeLayer *percentLayer;
- @property (nonatomic,strong) UIView * circleView;
- @property (nonatomic,assign) NSInteger indexPathIndex;
- @property (nonatomic,strong) NSMutableArray * markArray;
- @property (nonatomic,strong) CAShapeLayer * dashLayer;
- //最后点击
- @property (nonatomic,assign) NSInteger lastIndex;
- //是否是刷新
- @property (nonatomic,assign) BOOL reLoading;
- //记录滚动位置
- @property (nonatomic,assign) CGPoint contentOffset;
- //最后一次点击的坐标
- @property (nonatomic,strong) NSValue * lastValue;
- //选中的泡泡Tag
- @property (nonatomic,assign) NSInteger selectTag;
- @end
- @implementation LHYChartView
- #pragma mark --------初始化-----------
- -(instancetype)initWithFrame:(CGRect)frame{
- if (self = [super initWithFrame:frame]) {
- [self initNew];
- }
- return self;
- }
- -(void)awakeFromNib{
- [super awakeFromNib];
- [self initNew];
- }
- -(instancetype)init{
- if (self = [super init]) {
- [self initNew];
- }
- return self;
- }
- -(void)initNew {
- currentPage = 0;
- _precisionScale = 1;
- _indexPathIndex = -1;
- self.leftPointArr = [NSMutableArray array];
- self.rightPointArr = [NSMutableArray array];
- self.leftBtnArr = [NSMutableArray array];
- self.detailLabelArr = [NSMutableArray array];
- self.leftScaleArr = [NSArray array];
- self.leftScaleViewArr = [NSMutableArray array];
- self.markArray = [NSMutableArray array];
- self.borderLineColor = [UIColor colorWithRed:224/255.0f green:224/255.0f blue:224/255.0f alpha:1];
- self.borderTriangleColor = [UIColor colorWithRed:224/255.0f green:224/255.0f blue:224/255.0f alpha:1];
- self.showSelect = NO;
- self.isFloating = NO;
- self.chartViewStyle = 0;
- self.chartLayerStyle = 0;
- self.lineLayerStyle = 0;
- self.proportion = 0.5;
- self.colors = [NSArray array];
- self.lineWidth = 1;
- self.isShow = NO;
- self.markColor = [UIColor whiteColor];
- self.paopaoDataArray = [NSArray new];
- self.unitStyle = LHYUnitDefault;
- self.paopaoTitleArray = [NSArray array];
- self.isSelect = YES;
- self.leftJiange = 1;
- self.rightJiange = 1;
- self.jiange = 1;
- self.isShowYtext = YES;
- self.isShowFirstPaoPao = NO;
- self.unitName = @"";
- self.selectTag = -1;
- self.middleLineColor = [UIColor colorWithHexString:@"e0e0e0"];
- UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewClick:)];
- [self addGestureRecognizer:tap];
- titleWOfY = 0;
- _Xmargin = 50;
- }
- -(UILabel *)scaleLabel{
- if (!_scaleLabel) {
- _scaleLabel = [[UILabel alloc]init];
- _scaleLabel.textAlignment = 1;
- _scaleLabel.text = @"3.3681%";
- _scaleLabel.font = [UIFont systemFontOfSize:11];
- _scaleLabel.backgroundColor = [UIColor colorWithRed:255/255.0 green:159/255.0 blue:106/255.0 alpha:1];
- _scaleLabel.textColor = [UIColor whiteColor];
- }
- return _scaleLabel;
- }
- -(UILabel *)dateTimeLabel{
- if (!_dateTimeLabel) {
- _dateTimeLabel = [[UILabel alloc]init];
- _dateTimeLabel.textAlignment = 1;
- _dateTimeLabel.text = @"2016.04.16";
- _dateTimeLabel.font = [UIFont systemFontOfSize:11];
- _dateTimeLabel.backgroundColor = [UIColor whiteColor];
- _dateTimeLabel.textColor = [UIColor colorWithRed:181/255.0 green:181/255.0 blue:181/255.0 alpha:1];
- }
- return _dateTimeLabel;
- }
- -(NSMutableArray *)charCircleViewArr{
- if (!_charCircleViewArr) {
- _charCircleViewArr = [NSMutableArray new];
- }
- return _charCircleViewArr;
- }
- -(YJYTouchCollectionView *)xAxiCollectionView{
- if (!_xAxiCollectionView) {
- UICollectionViewFlowLayout *collectionViewLayout = [[UICollectionViewFlowLayout alloc]init];
- collectionViewLayout.minimumInteritemSpacing = 0;
- collectionViewLayout.minimumLineSpacing = 0;
- collectionViewLayout.sectionInset = UIEdgeInsetsMake(0, 4, 0, 0);
- collectionViewLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
- _xAxiCollectionView = [[YJYTouchCollectionView alloc]initWithFrame:CGRectMake(CGRectGetMinX(_chartScrollView.frame), CGRectGetMaxY(_chartScrollView.frame) + 10, CGRectGetWidth(_chartScrollView.frame) + CGRectGetWidth(_chartScrollView.frame) / (_xRow - 1), 20) collectionViewLayout:collectionViewLayout];
- _xAxiCollectionView.backgroundColor = [UIColor clearColor];
- [_xAxiCollectionView registerNib:[UINib nibWithNibName:@"YJYLinesCell" bundle:nil] forCellWithReuseIdentifier:@"YJYLinesCell"];
- _xAxiCollectionView.delegate = self;
- _xAxiCollectionView.dataSource = self;
- _xAxiCollectionView.bounces = NO;
- _xAxiCollectionView.showsHorizontalScrollIndicator = NO;
- _xAxiCollectionView.userInteractionEnabled = YES;
- [self addSubview:_xAxiCollectionView];
- }
- return _xAxiCollectionView;
- }
- - (UIView *)selectView {
- if (!_selectView) {
- _selectView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0.5, self.chartScrollView.frame.size.height)];
- _selectView.backgroundColor = _markColor;
- [self.chartScrollView addSubview:_selectView];
- }
- return _selectView;
- }
- -(void)setMarkColor:(UIColor *)markColor{
- _markColor = markColor;
- }
- -(LHYLinesPaoPaoView *)paopaoView{
- if (!_paopaoView) {
- _paopaoView = [[LHYLinesPaoPaoView alloc] initWithFrame:CGRectZero];
- _paopaoView.backgroundColor = _paopaoBackGroundColor;
- _paopaoView.layer.shadowColor = [UIColor blackColor].CGColor;
- _paopaoView.layer.shadowOffset = CGSizeMake(0, 3);
- _paopaoView.layer.shadowOpacity = 0.5;
- [self.chartScrollView addSubview:_paopaoView];
- }
- return _paopaoView;
- }
- -(UILabel *)lineLabel{
- if (!_lineLabel) {
- _lineLabel = [[UILabel alloc]init];
- _lineLabel.backgroundColor = [UIColor colorWithRed:255/255.0 green:159/255.0 blue:106/255.0 alpha:1];
- }
- return _lineLabel;
- }
- #pragma -mark -------------scrollViewDelegate----------------
- -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
- self.contentOffset = scrollView.contentOffset;
- if (scrollView == _chartScrollView) {
- _xAxiCollectionView.contentOffset = scrollView.contentOffset;
- }else{
- _chartScrollView.contentOffset = scrollView.contentOffset;
- }
- for (UIView * markCrossView in self.markArray) {
- CGRect frame = markCrossView.frame;
- frame.origin.x = scrollView.contentOffset.x;
- [markCrossView setFrame:frame];
- }
- }
- #pragma -mark --------------collViewDelegate----------------
- -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
- if (self.dataArrOfX.count > 0) {
- return self.dataArrOfX.count + 2;
- }
- return self.dataArrOfX.count;
- }
- -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
- YJYLinesCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YJYLinesCell" forIndexPath:indexPath];
- if (indexPath.row == _indexPathIndex + 1) {
- if (self.x_Select_Font) {
- cell.titleLB.font = self.x_Select_Font;
- }else{
- cell.titleLB.font = self.x_Font;
- }
- if (self.x_Select_Color) {
- cell.titleLB.textColor = self.x_Select_Color;
- }else{
- cell.titleLB.textColor = self.x_Color;
- }
- }else{
- cell.titleLB.font = self.x_Font;
- cell.titleLB.textColor = self.x_Color;
- }
- if (indexPath.row == 0 || indexPath.row == self.dataArrOfX.count + 1) {
- cell.titleLB.text = @"";
- }else{
- cell.titleLB.text = self.dataArrOfX[indexPath.row - 1];
- }
- cell.titleLB.textAlignment=NSTextAlignmentCenter;
- return cell;
- }
- -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
- if (indexPath.row == 0 || indexPath.row == self.dataArrOfX.count + 1) {
- return CGSizeMake(CGRectGetWidth(_chartScrollView.frame) / (_xRow - 1) / 2, 20);
- }else{
- return CGSizeMake(CGRectGetWidth(self.chartScrollView.frame) / (self.xRow - 1), 20);
- }
-
- }
- -(void)setLeftDataArr:(NSArray *)leftDataArr{
- _leftDataArr = leftDataArr;
- }
- -(void)setRightDataArr:(NSArray *)rightDataArr{
- _rightDataArr = rightDataArr;
- self.pageControl.numberOfPages = 1;
- }
- // 获取数据最大值,并计算每一行间隔值
- - (CGFloat)spaceValue:(NSArray *)array{
- CGFloat minValue = MAXFLOAT;
- CGFloat maxValue = -MAXFLOAT;
- for (int i = 0; i < [array count]; i++) {
- if ([array[i] floatValue] * _precisionScale> maxValue) {
- maxValue = [array[i] floatValue] * _precisionScale;
- }
- if ([array[i] floatValue] * _precisionScale < minValue) {
- minValue = [array[i] floatValue] * _precisionScale;
- }
- }
- NSInteger max = [self getNumber:maxValue];
- if (_unitStyle == LHYUnitMillion) {
- NSInteger maxValueInterger = max / 100000000.f + 0.99;
- NSInteger xMaxValue = maxValueInterger % (_row - 1);
- if (xMaxValue != 0) {
- xMaxValue = (_row - 1) - xMaxValue + maxValueInterger;
- }else{
- xMaxValue = maxValueInterger;
- }
- return xMaxValue / (_row - 1) * 100000000.f;
- }else if (_unitStyle == LHYUnitWan) {
- NSInteger maxValueInterger = max / 10000.f + 0.99;
- NSInteger xMaxValue = maxValueInterger % (_row - 1);
- if (xMaxValue != 0) {
- xMaxValue = (_row - 1) - xMaxValue + maxValueInterger;
- }else{
- xMaxValue = maxValueInterger;
- }
- return xMaxValue / (_row - 1) * 10000.f;
- }else if (_unitStyle == LHYUnitThousand) {
- NSInteger maxValueInterger = max / 1000.f + 0.99;
- NSInteger xMaxValue = maxValueInterger % (_row - 1);
- if (xMaxValue != 0) {
- xMaxValue = (_row - 1) - xMaxValue + maxValueInterger;
- }else{
- xMaxValue = maxValueInterger;
- }
- return xMaxValue / (_row - 1) * 1000.f;
- }else if (_unitStyle == LHYUnitMoneyDefault) {
- if (max == 0) {
- return 10000.f;
- }
- NSInteger maxValueInterger = 0;
- if (max < 100000000) {
- maxValueInterger = max / 1000.f + 0.999;
- }else{
- maxValueInterger = max / 10000000.f + 0.9999999;
- }
- NSInteger xMaxValue = maxValueInterger % (_row - 1);
- if (xMaxValue != 0) {
- xMaxValue = (_row - 1) - xMaxValue + maxValueInterger;
- }else{
- if (maxValueInterger == 0) {
- xMaxValue = _row - 1;
- }else{
- xMaxValue = maxValueInterger;
- }
- }
- if (max >= 100000000) {
- return xMaxValue / (_row - 1) * 10000000.f;
- }
- return xMaxValue / (_row - 1) * 1000.f;
- }else{
- NSInteger maxValueInterger = 0;
- if (max >= 1000) {
- maxValueInterger = max / 100.f + 0.99;
- }else{
- maxValueInterger = max;
- }
- NSInteger xMaxValue = maxValueInterger % (_row - 1);
- if (xMaxValue != 0) {
- xMaxValue = (_row - 1) - xMaxValue + maxValueInterger;
- }else{
- xMaxValue = maxValueInterger;
- }
- if (max >= 1000) {
- return xMaxValue / (_row - 1) * 100;
- }
- return xMaxValue / (_row - 1);
- }
- }
- // 只取小数点之前的数字
- - (CGFloat)getNumber:(CGFloat)value{
- CGFloat upValue = ceil(value);
- NSString *string = [NSString stringWithFormat:@"%.0f",upValue];
- return [string integerValue];
- }
- #pragma mark ----------显示---------------
- -(void)show{
- self.reLoading = NO;
- self.contentOffset = CGPointMake(0, 0);
- self.lastIndex = 0;
- [self onLoading];
- }
- -(void)reShow{
- self.reLoading = YES;
- self.selectIndex = -1;
- [self onLoading];
- }
- -(void)onLoading{
- if (_dashLayer) {
- [_dashLayer removeFromSuperlayer];
- _dashLayer = nil;
- }
- [self.chartScrollView removeFromSuperview];
- [self.xAxiCollectionView removeFromSuperview];
- [self.paopaoView removeFromSuperview];
- self.chartScrollView = nil;
- self.xAxiCollectionView = nil;
- self.paopaoView = nil;
- [self colorConversion];
- self.selectView = nil;
- /// 添加连线
- [self addDetailViews];
- [self.xAxiCollectionView reloadData];
- switch (_chartViewStyle) {
- case 0:
- [self showLeftRightView];
- break;
- case 1:
- [self showLeftRightView];
- break;
- case 2:
- [self showLeftRightView];
- break;
- default:
- break;
- }
- [_chartScrollView setContentOffset:self.contentOffset];
- if (!self.reLoading) {
- if (self.leftPointArr.count > 0) {
- NSArray * pointArray = self.leftPointArr[0];
- if (self.isShowFirstPaoPao && pointArray.count > 0) {
- NSValue * pointValue = pointArray[0];
- CGPoint point = [pointValue CGPointValue];
- point.x += titleWOfY;
- [self viewClick:[NSValue valueWithCGPoint:point]];
- if (self.paopaoView.hidden) {
- [self viewClick:[NSValue valueWithCGPoint:point]];
- }
- }
- if (self.isShowLastPaoPao && pointArray.count > 0) {
- CGFloat contentSizeWidth = self.chartScrollView.contentSize.width;
- CGFloat offsetX = contentSizeWidth - CGRectGetWidth(self.chartScrollView.frame);
- CGPoint contentPoint = CGPointMake(offsetX < 0 ? 0 : offsetX, 0);
- [self.chartScrollView setContentOffset:contentPoint];
- }
- }
- }else{
- if (self.leftPointArr.count > 0) {
- if (self.lastIndex) {
- CGPoint point = self.lastValue.CGPointValue;
- self.lastValue = [NSValue valueWithCGPoint:point];
- point.x -= titleWOfY;
- if (self.selectTag >= 0) {
- [self drawOtherLin:self.selectTag AndPoint:point];
- }
- }
- }
- }
- }
- //转换颜色
- -(void)colorConversion{
- NSMutableArray * colorArray = [NSMutableArray array];
- for (id obj in _leftColorStrArr) {
- UIColor * color;
- if ([obj isKindOfClass:[UIColor class]]) {
- color = obj;
- }else{
- NSString * colorStr = obj;
- color = [UIColor colorWithHexString:colorStr];
- }
- [colorArray addObject:color];
- }
- _leftColorStrArr = [NSArray arrayWithArray:colorArray];
- [colorArray removeAllObjects];
- for (id obj in _rightColorStrArr) {
- UIColor * color;
- if ([obj isKindOfClass:[UIColor class]]) {
- color = obj;
- }else{
- NSString * colorStr = obj;
- color = [UIColor colorWithHexString:colorStr];
- }
- [colorArray addObject:color];
- }
- _rightColorStrArr = [NSArray arrayWithArray:colorArray];
- }
- /// 显示左右两种标线
- -(void)showLeftRightView{
- [self.leftPointArr removeAllObjects];
-
- if (_leftDataArr.count > 0) {
- if (!self.reLoading) {
- if (_unitStyle == LHYUnitThousand) {
- _jiange = 1000;
- _leftJiange = 1000;
- }else if (_unitStyle == LHYUnitWan){
- _jiange = 10000;
- _leftJiange = 10000;
- }else if (_unitStyle == LHYUnitMillion){
- _jiange = 100000000;
- _leftJiange = 100000000;
- }else{
- _jiange = 1;
- _leftJiange = 1;
- }
- }
- if (self.max) {
- CGFloat jiange = [self spaceValue:@[[NSString stringWithFormat:@"%f",self.max]]];
- if (jiange > _leftJiange) {
- _leftJiange = jiange;
- }
- if (jiange > _jiange) {
- _jiange = jiange;
- }
- } else{
- for (int i = 0; i < _leftDataArr.count; i++) {
- CGFloat jiange = [self spaceValue:_leftDataArr[i]];
- if (jiange > _leftJiange) {
- _leftJiange = jiange;
- }
- if (jiange > _jiange) {
- _jiange = jiange;
- }
- }
- }
- [self addLeftViews];
- [self setChartViewContentSize];
- if (!_rightDataArr || _rightDataArr.count == 0) {
- [self calculateLeftLabelMaxWidth];
- [self addLines1With:self.chartScrollView];
- }
- NSMutableArray * pointMarray = [NSMutableArray array];
- for (int i = 0; i < _leftDataArr.count; i++) {
- NSArray * dataArray = _leftDataArr[i];
- ///添加点
- [pointMarray addObject:[self addDataPointWith:self.chartScrollView andArr:dataArray andInterval:_leftJiange]];
- }
-
- /// 添加点
- [self.leftPointArr addObjectsFromArray:pointMarray];
- for (int i = 0; i<pointMarray.count; i++) {
- NSArray * colorArray = [NSArray array];
- if (i < _colors.count) {
- colorArray = _colors[i];
- }
- [self addBezierPoint:pointMarray[i] andColor:_leftColorStrArr[i<_leftColorStrArr.count?i:_leftColorStrArr.count-1] andColors:colorArray];
- }
- }
- if (_rightDataArr.count > 0) {
- if (self.max) {
- CGFloat jiange = [self spaceValue:@[[NSString stringWithFormat:@"%f",self.max]]];
- if (jiange > _leftJiange) {
- _leftJiange = jiange;
- }
- if (jiange > _jiange) {
- _jiange = jiange;
- }
- }else{
- for (int i = 0; i < _rightDataArr.count; i++) {
- if (_unitStyle == LHYUnitThousand) {
- _rightJiange = 1000;
- }else if (_unitStyle == LHYUnitWan){
- _rightJiange = 10000;
- }else if (_unitStyle == LHYUnitMillion){
- _rightJiange = 100000000;
- }else{
- _rightJiange = 1;
- }
- CGFloat jiange = [self spaceValue:_rightDataArr[i]];
- if (jiange > _rightJiange) {
- _rightJiange = jiange;
- }
- }
- }
-
- [self addLeftViews];
- [self setChartViewContentSize];
- [self calculateLeftLabelMaxWidth];
- [self addLines1With:self.chartScrollView];
- NSMutableArray * pointMarray = [NSMutableArray array];
- for (int i = 0; i < _rightDataArr.count; i++) {
- NSArray * dataArray = _rightDataArr[i];
- [pointMarray addObject:[self addDataPointWith:self.chartScrollView andArr:dataArray andInterval:_rightJiange]];//添加点
- }
- [self.leftPointArr addObjectsFromArray:pointMarray];
- for (int i = 0; i<pointMarray.count; i++) {
- NSArray * colorArray = [NSArray array];
- if (i < _colors.count) {
- colorArray = _colors[i];
- }
- [self addBezierPoint:pointMarray[i] andColor:_rightColorStrArr[i<_rightColorStrArr.count?i:_rightColorStrArr.count-1] andColors:colorArray];
- }
- /// 添加连线
- [self addRightViews];
- }
-
- if (_leftDataArr.count <= 0 && _rightDataArr.count <= 0) {
- [self addLeftViews];
- [self setChartViewContentSize];
- [self addLines1With:self.chartScrollView];
- }
- if (self.leftPointArr.count > 0) {
- for (int i = 0; i < self.leftPointArr.count; i++) {
- NSMutableArray * pointMarray = [NSMutableArray arrayWithArray:self.leftPointArr[i]];
- if (pointMarray.count > 2 && pointMarray.count == self.dataArrOfX.count) {
- [pointMarray removeObjectAtIndex:pointMarray.count - 1];
- [pointMarray removeObjectAtIndex:0];
- }else if (pointMarray.count > 0){
- [pointMarray removeObjectAtIndex:0];
- }
- self.leftPointArr[i] = pointMarray;
- }
- }
- [self addBottomViewsWith:self.chartScrollView];
- }
- -(void)setChartViewContentSize{
- CGFloat chartScrollViewwidth = _chartViewStyle == LHYChartViewLeftRightLine ? self.bounds.size.width-titleWOfY * 2 :self.bounds.size.width-titleWOfY;
- CGFloat xMargin = chartScrollViewwidth / (_xRow - 1);
- if (self.dataArrOfX.count > 0) {
- self.chartScrollView.contentSize = CGSizeMake(xMargin*(self.dataArrOfX.count + 1), 0);
- }
- }
- -(void)setIsShowYtext:(BOOL)isShowYtext{
- _isShowYtext = isShowYtext;
- titleWOfY = isShowYtext ? titleWOfY : 0;
- }
- #pragma mark *******************数据源************************
- -(void)setDataArrOfX:(NSArray *)dataArrOfX{
- _dataArrOfX = dataArrOfX;
- }
- #pragma mark *******************分割线************************
- -(void)addDetailViews{
- CGFloat width = 0;
- width = _chartViewStyle == LHYChartViewLeftRightLine ? self.bounds.size.width-titleWOfY * 2 :self.bounds.size.width-titleWOfY;
- self.chartScrollView = [[YJYTouchScroll alloc]initWithFrame:CGRectMake(titleWOfY, 0, width, chartViewHeight)];
- self.chartScrollView.backgroundColor = [UIColor clearColor];
- self.chartScrollView.bounces = NO;
- self.chartScrollView.delegate = self;
- self.chartScrollView.showsHorizontalScrollIndicator = NO;
- self.chartScrollView.userInteractionEnabled = YES;
- [self addSubview:self.chartScrollView];
- [self addSubview:self.xAxiCollectionView];
- }
- #pragma mark 渐变线条
- -(void)buildBGCircleLayer:(NSArray *)colors
- {
- CAShapeLayer *bgCircleLayer = [CAShapeLayer layer];
- bgCircleLayer.path = _circlePath.CGPath;
- bgCircleLayer.strokeColor = [UIColor lightGrayColor].CGColor;
- bgCircleLayer.fillColor = [UIColor clearColor].CGColor;
- bgCircleLayer.lineWidth = _lineWidth;
- bgCircleLayer.lineCap = kCALineCapRound; // 截面形状
- [self addShowPercentLayer:colors];
- [self percentAnimation];
- }
- -(void)addShowPercentLayer:(NSArray *)colors
- {
- _gradientlayer = (id)[CAGradientLayer layer];
- if (colors && colors.count > 0) {
- NSMutableArray * colorMarray = [NSMutableArray array];
- for (int i = 0; i < colors.count; i++) {
- UIColor * color = colors[i];
- [colorMarray addObject:(id)color.CGColor];
- }
- _gradientlayer.colors = colorMarray;
- }else{
- _gradientlayer.colors = [NSArray arrayWithObjects:(id)[[UIColor redColor]CGColor],(id)[[UIColor blueColor]CGColor], nil];
- }
- _gradientlayer.startPoint= CGPointMake(0.10, 1);
- _gradientlayer.endPoint = CGPointMake(0.90, 1);
- if (_locations && _locations.count > 0) {
- _gradientlayer.locations = _locations;
- }else{
- _gradientlayer.locations = @[[NSNumber numberWithFloat:_proportion]];
- }
- _gradientlayer.frame = CGRectMake(0, 0, self.chartScrollView.contentSize.width, CGRectGetHeight(self.chartScrollView.frame));
- _percentLayer = [CAShapeLayer layer];
- _percentLayer.path = _circlePath.CGPath;
- _percentLayer.strokeColor = [UIColor redColor].CGColor;
- _percentLayer.fillColor = [UIColor clearColor].CGColor;
- _percentLayer.lineWidth = _lineWidth;
- _percentLayer.strokeStart = 0;
- _percentLayer.strokeEnd = 1;
- _percentLayer.lineCap = kCALineCapRound;
- if (_chartLayerStyle == 2) {
- _percentLayer.shadowColor = [UIColor redColor].CGColor;
- _percentLayer.shadowOffset = CGSizeMake(0,5);
- _percentLayer.shadowOpacity = 0.5;
- }
- [_gradientlayer setMask:_percentLayer];
- [self.chartScrollView.layer addSublayer:_gradientlayer];
-
- }
- -(void)percentAnimation
- {
- CABasicAnimation *strokeAnmi = [CABasicAnimation animation];
- strokeAnmi.keyPath = @"strokeEnd";
- strokeAnmi.fromValue = [NSNumber numberWithFloat:0];
- strokeAnmi.toValue = [NSNumber numberWithFloat:1.0f];
- strokeAnmi.duration =2.0f;
- strokeAnmi.delegate = self;
- strokeAnmi.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- strokeAnmi.autoreverses = NO;
- if (!self.reLoading) [_percentLayer addAnimation:strokeAnmi forKey:@"stroke"];
- }
- //等动画结束之后的操作
- -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
- {
- if (_isShow) {
- [self drawCircleArr:self.leftPointArr color:self.leftColorStrArr];
- }
- //是否动画结束后显示最后一条数据
- if (!self.leftPointArr || !self.leftPointArr.count) return;
- NSArray * pointArray = self.leftPointArr[0];
- if (self.isShowLastPaoPao && pointArray.count > 0) {
- CGPoint point = [pointArray[pointArray.count - 1] CGPointValue];
- if (point.x >= CGRectGetWidth(self.chartScrollView.frame)) {
- point.x = [pointArray[self.xRow - 3] CGPointValue].x;
- }
- point.x += titleWOfY;
- NSValue * value = [NSValue valueWithCGPoint:point];
- if (!self.paopaoView || !CGRectGetWidth(self.paopaoView.frame) || self.paopaoView.hidden) {
- [self viewClick:value];
- }
- }
- }
- #pragma mark ----------绘画折现----------------
- -(void)addBezierPoint:(NSArray *)pointArray andColor:(UIColor *)color andColors:(NSArray *)colors{
- if (pointArray.count <= 0) {
- return;
- }
- //取得起始点
- CGPoint startP = CGPointMake(0, 0);
- if (!self.hiddenZreo) {
- startP = [[pointArray objectAtIndex:0] CGPointValue];
- }else{
- startP = [[pointArray objectAtIndex:1] CGPointValue];
- }
- //直线的连线
- UIBezierPath *lineBeizer = [UIBezierPath bezierPath];
- [lineBeizer moveToPoint:startP];
- _circlePath = lineBeizer;
- //遮罩层的形状
- UIBezierPath *shelterBezier = [UIBezierPath bezierPath];
- shelterBezier.lineCapStyle = kCGLineCapRound;
- shelterBezier.lineJoinStyle = kCGLineJoinMiter;
- [shelterBezier moveToPoint:startP];
- for (int i = 0;i<pointArray.count;i++ ) {
- if (!self.hiddenZreo) {
- if (i > 0) {
- CGPoint prePoint = [[pointArray objectAtIndex:i-1] CGPointValue];
- CGPoint nowPoint = [[pointArray objectAtIndex:i] CGPointValue];
- [lineBeizer addCurveToPoint:nowPoint controlPoint1:CGPointMake((nowPoint.x+prePoint.x)/2, prePoint.y) controlPoint2:CGPointMake((nowPoint.x+prePoint.x)/2, nowPoint.y)];
- if (_chartLayerStyle == LHYChartGradient) [shelterBezier addCurveToPoint:nowPoint controlPoint1:CGPointMake((nowPoint.x+prePoint.x)/2, prePoint.y) controlPoint2:CGPointMake((nowPoint.x+prePoint.x)/2, nowPoint.y)];
- if (i == pointArray.count-1) {
- /// 添加连线
- [lineBeizer moveToPoint:nowPoint];
- lastPoint = nowPoint;
- }
- }
- }else{
- if (pointArray.count > 2) {
- if (i > 1) {
- CGPoint prePoint = [[pointArray objectAtIndex:i-1] CGPointValue];
- CGPoint nowPoint = [[pointArray objectAtIndex:i] CGPointValue];
- [lineBeizer addCurveToPoint:nowPoint controlPoint1:CGPointMake((nowPoint.x+prePoint.x)/2, prePoint.y) controlPoint2:CGPointMake((nowPoint.x+prePoint.x)/2, nowPoint.y)];
- if (_chartLayerStyle == LHYChartGradient) [shelterBezier addCurveToPoint:nowPoint controlPoint1:CGPointMake((nowPoint.x+prePoint.x)/2, prePoint.y) controlPoint2:CGPointMake((nowPoint.x+prePoint.x)/2, nowPoint.y)];
- if (i == pointArray.count-1) {
- ///添加连线
- [lineBeizer moveToPoint:nowPoint];
- lastPoint = nowPoint;
- }
- }
- }else{
- if (i > 0) {
- CGPoint prePoint = [[pointArray objectAtIndex:i] CGPointValue];
- CGPoint nowPoint = [[pointArray objectAtIndex:i] CGPointValue];
- nowPoint.x +=1;
- [lineBeizer addCurveToPoint:nowPoint controlPoint1:CGPointMake((nowPoint.x+prePoint.x)/2, prePoint.y) controlPoint2:CGPointMake((nowPoint.x+prePoint.x)/2, nowPoint.y)];
- if (_chartLayerStyle == LHYChartGradient) [shelterBezier addCurveToPoint:nowPoint controlPoint1:CGPointMake((nowPoint.x+prePoint.x)/2, prePoint.y) controlPoint2:CGPointMake((nowPoint.x+prePoint.x)/2, nowPoint.y)];
- if (i == pointArray.count-1) {
- /// 添加连线
- [lineBeizer moveToPoint:nowPoint];
- lastPoint = nowPoint;
- }
- }
- }
- }
- }
-
- CGFloat bgViewHeight = self.chartScrollView.bounds.size.height;
- //获取最后一个点的X值
- CGFloat lastPointX = lastPoint.x;
- //最后一个点对应的X轴的值
- CGPoint lastPointX1 = CGPointMake(lastPointX, bgViewHeight);
- [shelterBezier addLineToPoint:lastPointX1];
- /// 回到原点
- [shelterBezier addLineToPoint:CGPointMake(startP.x, bgViewHeight)];
- [shelterBezier addLineToPoint:startP];
- if (_lineLayerStyle == 1) {
- if (_chartLayerStyle == 1) {
- [self addGradientWithBezierPath:shelterBezier andColor:color];
- }
- [self buildBGCircleLayer:colors];
- return;
- }
- CAShapeLayer *shapeLayer = [CAShapeLayer layer];
- shapeLayer.path = lineBeizer.CGPath;
- shapeLayer.fillColor = [UIColor clearColor].CGColor;
- shapeLayer.strokeColor = color.CGColor;
- shapeLayer.lineWidth = 3;
- switch (_chartLayerStyle) {
- case 0:
- break;
- case 1:
- [self addGradientWithBezierPath:shelterBezier andColor:color];
- break;
- case 2:
- #pragma mark ------------阴影投影---------------
- shapeLayer.shadowOffset = CGSizeMake(0, 10);
- shapeLayer.shadowColor = color.CGColor;
- shapeLayer.shadowOpacity = 0.5;
- break;
- default:
- break;
- }
- [self.chartScrollView.layer addSublayer:shapeLayer];
- CABasicAnimation *strokeAnmi = [CABasicAnimation animation];
- strokeAnmi.keyPath = @"strokeEnd";
- strokeAnmi.fromValue = [NSNumber numberWithFloat:0];
- strokeAnmi.toValue = [NSNumber numberWithFloat:1.0f];
- strokeAnmi.duration =2.0f;
- strokeAnmi.delegate = self;
- strokeAnmi.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- strokeAnmi.autoreverses = NO;
- if (!self.reLoading) [shapeLayer addAnimation:strokeAnmi forKey:@"stroke"];
- }
- #pragma mark ------------渐变图层---------------
- -(void)addGradientWithBezierPath:(UIBezierPath *)beizer andColor:(UIColor *)color{
- /// 遮罩层
- CAShapeLayer *shadeLayer = [CAShapeLayer layer];
- shadeLayer.path = beizer.CGPath;
- shadeLayer.fillColor = [UIColor greenColor].CGColor;
- CAGradientLayer *gradientLayer = [CAGradientLayer layer];
- gradientLayer.frame = CGRectMake(0, 0, 0, self.chartScrollView.bounds.size.height);
- gradientLayer.startPoint = CGPointMake(0, 0);
- gradientLayer.endPoint = CGPointMake(0, 1);
- gradientLayer.cornerRadius = 5;
- gradientLayer.masksToBounds = YES;
- gradientLayer.colors = @[(__bridge id)[color colorWithAlphaComponent:0.3].CGColor,(__bridge id)[color colorWithAlphaComponent:0].CGColor];
- gradientLayer.locations = @[@(0.5f)];
- CALayer *baseLayer = [CALayer layer];
- [baseLayer addSublayer:gradientLayer];
- [baseLayer setMask:shadeLayer];
- [self.chartScrollView.layer addSublayer:baseLayer];
- CABasicAnimation *boundsAnmi = [CABasicAnimation animation];
- boundsAnmi.keyPath = @"bounds";
- boundsAnmi.duration = self.reLoading ? 0.0001f : 2.f;
- boundsAnmi.toValue = [NSValue valueWithCGRect:CGRectMake(5, 0, 2*lastPoint.x, self.chartScrollView.bounds.size.height)];
- boundsAnmi.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- boundsAnmi.fillMode = kCAFillModeForwards;
- boundsAnmi.delegate = self;
- boundsAnmi.autoreverses = NO;
- boundsAnmi.removedOnCompletion = NO;
- [gradientLayer addAnimation:boundsAnmi forKey:@"bounds"];
-
- }
- #pragma mark ----------获取所有坐标点-------------
- -(NSMutableArray *)addDataPointWith:(UIView *)view andArr:(NSArray *)DataArr andInterval:(CGFloat)interval{
- CGFloat height = self.chartScrollView.bounds.size.height - 13 - KCircleRadius1 / 2 - 4;
- /// 初始点
- NSMutableArray *arr = [NSMutableArray arrayWithArray:DataArr];
- NSMutableArray * marr = [NSMutableArray array];
- CGFloat xMargin = CGRectGetWidth(self.chartScrollView.frame) / (_xRow - 1);
- for (int i = 0; i<arr.count; i++) {
- float tempHeight = [arr[i] floatValue] / (interval * (_row - 1)) ;
- NSValue *point = [NSValue valueWithCGPoint:CGPointMake(xMargin * i + xMargin, (height *(1 - tempHeight) + 13))];
- if (i == 0) {
- NSValue *point1 = [NSValue valueWithCGPoint:CGPointMake(0 , (height *(1 - tempHeight) + 13))];
- [marr addObject:point1];
- }
- [marr addObject:point];
- }
- return marr;
- }
- #pragma mark ---------添加左侧Y轴标注--------------
- -(void)addLeftViews{
- CGFloat textWidth = 0;
- CGFloat labelWidth = 0;
- switch (_chartViewStyle) {
- case 0:
- textWidth = [NSString measureSinglelineStringWidth:[self unitValue:_jiange * (_row - 1)] andFont:_y_Font];
- break;
- case 1:
- textWidth = [NSString measureSinglelineStringWidth:[self unitValue:_jiange * (_row - 1)] andFont:_y_Font];
- break;
- case 2:
- textWidth = [NSString measureSinglelineStringWidth:[self unitValue:_leftJiange * (_row - 1)] andFont:_y_Font];
- break;
- default:
- break;
- }
- if (_unitStyle == LHYUnitMoneyPercentage) {
- textWidth = [NSString measureSinglelineStringWidth:@"100%" andFont:_y_Font];
- }
- if (_isShowYtext) labelWidth = textWidth + 2 > labelWidth ? textWidth + 2 : labelWidth;
- titleWOfY = labelWidth;
- [self.markArray removeAllObjects];
- CGFloat topHeight = 13;
- CGFloat chartScrollViewwidth = _chartViewStyle == LHYChartViewLeftRightLine ? self.bounds.size.width-labelWidth * 2 :self.bounds.size.width-labelWidth;
- for (NSInteger i = 0; i< _row ;i++ ) {
- UILabel * leftLabel = [self viewWithTag:1000 + i + 1];
- CGFloat height = CGRectGetHeight(self.chartScrollView.frame) - topHeight;
- if (!leftLabel) {
- leftLabel = [[UILabel alloc]init];
- leftLabel.tag = 1000 + i + 1;
- }
- CGRect frame = CGRectMake(0, topHeight + height- height / (_row - 1) * i - [_y_Font pointSize] / 2, labelWidth*2.0, [_y_Font pointSize]);
- [leftLabel setFrame:frame];
- leftLabel.font = _y_Font;
- leftLabel.textColor = _y_Color;
- if (_unitStyle == LHYUnitMoneyPercentage) {
- leftLabel.text = [NSString stringWithFormat:@"%.0f%%", 100.0 / (_row - 1) * i];
- } else{
- switch (_chartViewStyle) {
- case 0:
- leftLabel.text = [self unitValue:_jiange * i];
- break;
- case 1:
- leftLabel.text = [self unitValue:_jiange * i];
- break;
- case 2:
- leftLabel.text = [self unitValue:_leftJiange * i];
- break;
- default:
- break;
- }
- }
- if (i > 0 && self.isShowYtext) [self addSubview:leftLabel];
- if (self.isGrid){
- CGFloat xMargin = chartScrollViewwidth / (_xRow - 1);
- CGFloat width = 0;
- if (xMargin*(self.dataArrOfX.count + 1) < chartScrollViewwidth){
- width = chartScrollViewwidth;
- }else{
- width = xMargin*(self.dataArrOfX.count + 1);
- }
- UIView * crossView = [[UIView alloc] initWithFrame:CGRectMake(0,topHeight + height - height / (_row - 1) * i + 0.5,width, 0.5)];
- crossView.backgroundColor = _middleLineColor;
- [self.chartScrollView addSubview:crossView];
- if (i != 0) {
- UIView * markCrossView = [[UIView alloc] initWithFrame:CGRectMake(0,topHeight + height - height / (_row - 1) * i + 0.5,8, 0.5)];
- markCrossView.backgroundColor = _borderLineColor;
- [self.chartScrollView addSubview:markCrossView];
- [self.markArray addObject:markCrossView];
- }
- }
- }
- if (self.isGrid) {
- if (!self.dataArrOfX || self.dataArrOfX.count == 0 || (self.dataArrOfX.count > 0 && chartScrollViewwidth / (self.xRow - 3)*(self.dataArrOfX.count - 1) <= chartScrollViewwidth + 1)) {
- CGFloat verticalX = 0;
- while (verticalX <= chartScrollViewwidth + 0.5) {
- UIView * vertical = [[UIView alloc] initWithFrame:CGRectMake(verticalX - 0.5,topHeight,0.5, CGRectGetHeight(self.chartScrollView.frame) - topHeight)];
- vertical.backgroundColor = _middleLineColor;
- [self.chartScrollView addSubview:vertical];
- verticalX += chartScrollViewwidth / (self.xRow - 1);
- }
- }else{
- for (int i = 0; i < self.dataArrOfX.count + 2; i++) {
- UIView * vertical = [[UIView alloc] initWithFrame:CGRectMake(chartScrollViewwidth / (self.xRow - 1) * i - 0.5,topHeight,0.5, CGRectGetHeight(self.chartScrollView.frame)- topHeight)];
- vertical.backgroundColor = _middleLineColor;
- [self.chartScrollView addSubview:vertical];
- }
- }
- }
- }
- -(void)calculateLeftLabelMaxWidth{
- for (NSInteger i = 0;i< _row ;i++ ) {
- NSString * str = @"";
- switch (_chartViewStyle) {
- case 0:
- str = [self unitValue:_jiange * i];
- break;
- case 1:
- str = [self unitValue:_jiange * i];
- break;
- case 2:
- str = [self unitValue:_leftJiange * i];
- break;
- default:
- break;
- }
- }
- CGFloat width = _chartViewStyle == LHYChartViewLeftRightLine ? self.bounds.size.width-titleWOfY * 2 :self.bounds.size.width-titleWOfY;
- [_chartScrollView setFrame:CGRectMake(titleWOfY, 0, width, chartViewHeight)];
- [_xAxiCollectionView setFrame:CGRectMake(CGRectGetMinX(_chartScrollView.frame), CGRectGetMaxY(_chartScrollView.frame) + 10, CGRectGetWidth(self.frame) - CGRectGetMinX(_chartScrollView.frame), 20)];
- }
- -(NSString *)unitValue:(CGFloat)value{
- CGFloat returnValue = 0;
- if (_unitStyle == LHYUnitGe || _unitStyle == LHYUnitDefault) {
- returnValue = value;
- }else if (_unitStyle == LHYUnitMoneyPercentageDefault){
- return [NSString stringWithFormat:@"%.0f%%",value];
- }else if (_unitStyle == 1) {
- returnValue = value / 1000;
- }else if (_unitStyle == 2){
- returnValue = value / 10000;
- }else if (_unitStyle == 3){
- returnValue = value / 100000000;
- }else if (_unitStyle == 4){
- if (value >= 100000000) {
- return [NSString stringWithFormat:@"%.1f亿%@",value / 100000000,self.unitName];
- }else{
- return [NSString stringWithFormat:@"%.1f万%@",value / 10000,self.unitName];
- }
- }
- if (returnValue >= 1000) {
- return [NSString stringWithFormat:@"%.1fk%@",returnValue / 1000,self.unitName];
- }
- return [NSString stringWithFormat:@"%.0f%@",returnValue,self.unitName];
- }
- #pragma mark ---------添加右侧Y轴标注--------------
- -(void)addRightViews{
- for (NSInteger i = 0;i< _row ;i++ ) {
- CGFloat height = CGRectGetHeight(_chartScrollView.frame);
- CGFloat jiange = (height - _row * 15) / (_row - 1);
- CGRect frame = CGRectMake(CGRectGetWidth(self.frame) - titleWOfY + 5, height - 15*(i + 1) - jiange * i , titleWOfY - 5, 15);
- UILabel *leftLabel = [[UILabel alloc]initWithFrame:frame];
- leftLabel.font = [UIFont systemFontOfSize:10.0f];
- leftLabel.textColor = [UIColor colorWithHexString:@"0x999999"];
- leftLabel.textAlignment = NSTextAlignmentLeft;
- leftLabel.text = [NSString stringWithFormat:@"%.0f",_rightJiange * i];
- [self addSubview:leftLabel];
- }
- }
- -(void)addBottomViewsWith:(UIView *)View{
- NSArray *bottomArr;
- if (View == self.chartScrollView) {
- bottomArr = _dataArrOfX;
-
- }else{
- }
- }
- -(void)TopBtnAction:(UIButton *)sender{
- for (UIButton*btn in _leftBtnArr) {
- if (sender.tag == btn.tag) {
- btn.selected = YES;
- }else{
- btn.selected = NO;
- }
- }
- [self showDetailLabel:sender];
- }
- -(void)showDetailLabel:(UIButton *)sender{
- for (UILabel * label in _detailLabelArr) {
- if (sender.tag+200 == label.tag) {
- label.hidden = NO;
- }else{
- label.hidden = YES;
- }
- }
- }
- #define mark - 点击屏幕事件
- -(void)viewClick:(id)sender{
- if (!_isSelect) return;
- [self colorConversion];
- CGPoint point;
- if ([sender isKindOfClass:[UITapGestureRecognizer class]]) {
- point = [sender locationInView:self];
- self.lastValue = [NSValue valueWithCGPoint:point];
- point.x -= titleWOfY;
- } else {
- NSValue * value = sender;
- point = value.CGPointValue;
- self.lastValue = [NSValue valueWithCGPoint:point];
- point.x -= titleWOfY;
- }
- CGFloat xMargin = CGRectGetWidth(self.chartScrollView.frame) / (_xRow - 1);
- if (point.x > xMargin / 2 && point.x < CGRectGetMaxX(self.chartScrollView.frame) && point.y > CGRectGetMinY(self.chartScrollView.frame) && point.y < CGRectGetMaxY(self.chartScrollView.frame)) {
- point.x = point.x + self.chartScrollView.contentOffset.x;
- point.y = point.y + self.chartScrollView.contentOffset.y;
- NSInteger index = point.x / xMargin - 1;
- float indexF = point.x - xMargin * (index + 1);
- if (indexF>xMargin / 2) {
- index = index + 1;
- }
- self.selectTag = index;
- [self drawOtherLin:index AndPoint:point];
- return;
- }
- if (point.x > xMargin / 2 && point.x < CGRectGetMaxX(self.xAxiCollectionView.frame) && point.y > CGRectGetMinY(self.xAxiCollectionView.frame) && point.y < CGRectGetMaxY(self.xAxiCollectionView.frame)) {
- point.x = point.x + self.xAxiCollectionView.contentOffset.x;
- point.y = point.y + self.xAxiCollectionView.contentOffset.y;
- NSInteger index = point.x / xMargin - 1;
- float indexF = point.x - xMargin * (index + 1);
- if (indexF>xMargin / 2) {
- index = index + 1;
- }
- [self drawOtherLin:index AndPoint:point];
- return;
- }
- }
- /// 点击之后画出重点线以及数值
- -(void)drawOtherLin:(NSInteger)index AndPoint:(CGPoint)touchpoint{
- if(index >= self.dataArrOfX.count || index<0 || self.dataArrOfX.count == 0){
- return ;
- }
- [self.paopaoView removeFromSuperview];
- self.paopaoView = nil;
- if (self.showSelect && self.selectIndex== index) {
- self.lastIndex = 0;
- self.selectTag = -1;
- self.selectView.hidden = YES;
- self.paopaoView.hidden = YES;
- self.indexPathIndex = -1;
- for (UIView *view in self.charCircleViewArr) {
- [view removeFromSuperview];
- }
- self.showSelect = NO;
- [_xAxiCollectionView reloadData];
- return;
- }
- self.lastIndex = index + 1;
- self.showSelect = YES;
- self.selectIndex = index;
- self.indexPathIndex = index;
- [_xAxiCollectionView reloadData];
- [self setPaopaoUI:index];
- }
- -(void)setPaopaoUI:(NSInteger)index{
- CGFloat xMargin = CGRectGetWidth(self.chartScrollView.frame) / (_xRow - 1);
- [self.chartScrollView bringSubviewToFront:self.selectView];
- self.selectView.hidden = NO;
- self.selectView.backgroundColor = _markColor;
- if (xMargin*index + self.selectView.frame.size.width > self.chartScrollView.contentSize.width) {
- self.selectView.frame = CGRectMake(xMargin*index+xMargin, 0, self.selectView.frame.size.width, self.selectView.frame.size.height);
- } else {
- self.selectView.frame = CGRectMake(xMargin*index+xMargin, 0, self.selectView.frame.size.width, self.selectView.frame.size.height);
- }
- [self.chartScrollView bringSubviewToFront:self.paopaoView];
- self.paopaoView.hidden = NO;
- [self.chartScrollView bringSubviewToFront:self.selectView];
- NSMutableArray *dataArr = [NSMutableArray new];
- if (_chartViewStyle == LHYChartViewLeftRightLine) {
- if (self.paopaoDataArray.count > 0){
- [self.paopaoDataArray enumerateObjectsUsingBlock:^(NSArray<NSArray *> * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- if (index < obj.count) {
- [dataArr addObject:obj[index]];
- }
- }];
- } else {
- [self.leftDataArr enumerateObjectsUsingBlock:^(NSArray<NSArray *> * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- if (index < obj.count) {
- [dataArr addObject:obj[index]];
- }
- }];
- [self.rightDataArr enumerateObjectsUsingBlock:^(NSArray<NSArray *> * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- if (index < obj.count) {
- [dataArr addObject:obj[index]];
- }
- }];
- }
- } else {
- if (self.paopaoDataArray.count > 0){
- [self.paopaoDataArray enumerateObjectsUsingBlock:^(NSArray<NSArray *> * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- if (index < obj.count) {
- [dataArr addObject:obj[index]];
- }
- }];
- } else {
- [self.leftDataArr enumerateObjectsUsingBlock:^(NSArray<NSArray *> * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- if (index < obj.count) {
- [dataArr addObject:obj[index]];
- }
- }];
- }
- }
- NSMutableArray *colorArr = [NSMutableArray array];
- for (int i = 0; i < self.leftColorStrArr.count; i++) {
- [colorArr addObject:self.leftColorStrArr[i]];
- }
- for (int i = 0; i < self.rightColorStrArr.count ; i++) {
- [colorArr addObject:self.rightColorStrArr[i]];
- }
- CGSize size = [LHYLinesPaoPaoView getWidthAndHeight:dataArr];
- float paopao_x = index * xMargin + xMargin - size.width * 0.5;
- if (self.paopaoTitleArray.count == 0) {
- size.height -= 17;
- }
- size.height += 13;
- if (paopao_x < 0) {
- paopao_x = 0;
- } else if (paopao_x > self.chartScrollView.contentSize.width - size.width && paopao_x > self.chartScrollView.frame.size.width - size.width) {
- paopao_x = self.chartScrollView.contentSize.width - size.width;
- }
- self.paopaoView.frame = CGRectMake(paopao_x, self.paopaoView.frame.origin.y, size.width-20.0, size.height + 5);
- self.paopaoView.margin = xMargin;
- if (paopao_x == 0 && size.width > xMargin) {
- self.paopaoView.beyondLeft = YES;
- }else if ((index + 1) * xMargin + size.width * 0.5 > self.chartScrollView.contentSize.width && size.width > _Xmargin && (index + 1) * xMargin + size.width * 0.5 > self.chartScrollView.frame.size.width){
- self.paopaoView.beyondRight = YES;
- }
- NSArray * colorArray = _paopaoDataColors ? _paopaoDataColors : colorArr;
- UIColor* titleColor = _paopaoTitleColor ? _paopaoTitleColor : [UIColor colorWithHexString:@"0x999999"];
- self.paopaoView.chartContentWidth = self.chartScrollView.contentSize.width;
- NSMutableArray * colorMarray = [NSMutableArray array];
- if (colorArray.count != dataArr.count) {
- for (int s = 0; s < self.leftDataArr.count; s++) {
- NSArray * arr = self.leftDataArr[s];
- if (arr.count > index) {
- [colorMarray addObject:colorArr[s]];
- }
- }
- }else{
- [colorMarray addObjectsFromArray:colorArray];
- }
- if (self.paopaoTitleArray.count > 0) {
- if (self.leftPointArr.copy != 0) {
- NSArray * pointArray = self.leftPointArr[0];
- for (NSArray * arr in self.leftPointArr) {
- if (index < arr.count) {
- pointArray = arr;
- break;
- }
- }
- CGPoint showPoint = [pointArray[index] CGPointValue];
- self.paopaoView.pointX = showPoint.x;
- [self.paopaoView show:dataArr and:self.paopaoTitleArray[index] andTitleColor:titleColor colorArr:colorMarray];
- }
- } else {
- if (self.leftPointArr.count != 0) {
- NSArray * pointArray = self.leftPointArr[0];
- for (NSArray * arr in self.leftPointArr) {
- if (index < arr.count) {
- pointArray = arr;
- break;
- }
- }
- CGPoint showPoint = [pointArray[index] CGPointValue];
- self.paopaoView.pointX = showPoint.x;
- [self.paopaoView show:dataArr and:@"" andTitleColor:titleColor colorArr:colorMarray];
- }
- }
- [self addCircle:index];
- }
- /// 圆圈
- - (void)addCircle:(NSInteger)index{
- for (UIView *circleView in self.charCircleViewArr) {
- [circleView removeFromSuperview];
- }
- NSMutableArray * leftColorArr = [NSMutableArray array];
- switch (_chartViewStyle) {
- case 0:
- for (int i = 0; i < _leftColorStrArr.count; i++) {
- [leftColorArr addObject:_leftColorStrArr[i]];
- }
- [self.charCircleViewArr removeAllObjects];
- [self drawCircle:index arr:self.leftPointArr color:leftColorArr];
- break;
- case 1:
- for (int i = 0; i < _leftColorStrArr.count; i++) {
- [leftColorArr addObject:_leftColorStrArr[i]];
- }
- [self.charCircleViewArr removeAllObjects];
- [self drawCircle:index arr:self.leftPointArr color:leftColorArr];
- break;
- case 2:
- for (int i = 0; i < _leftColorStrArr.count; i++) {
- [leftColorArr addObject:_leftColorStrArr[i]];
- }
- for (int i = 0; i < _rightColorStrArr.count; i++) {
- [leftColorArr addObject:_rightColorStrArr[i]];
- }
- [self.charCircleViewArr removeAllObjects];
- [self drawCircle:index arr:self.leftPointArr color:leftColorArr];
- break;
- default:
- break;
- }
- }
- - (void)drawCircle:(NSInteger)index arr:(NSArray *)pointArr color:(NSArray<UIColor *> *)colors{
- CGFloat pointY = 0;
- for (int i = 0; i<pointArr.count; i++) {
- NSArray *arr = pointArr[i];
- if (arr.count > index){
- CGPoint point = [arr[index] CGPointValue];
- UIView * circleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KCircleRadius1*2, KCircleRadius1*2)];
- circleView.center = point;
- if (i == 0) {
- pointY = point.y;
- }
- if (self.isFloating) {
- if (point.y < pointY) {
- pointY = point.y;
- }
- }else if(i == 0){
- [self.paopaoView drawBoxWithDirection:directionTop];
- }
- circleView.backgroundColor = self.isShowCircleOutWhite ? colors[i] : [UIColor whiteColor];
- circleView.layer.cornerRadius = KCircleRadius1;
- circleView.layer.borderColor = self.isShowCircleOutWhite ? [UIColor whiteColor].CGColor : colors[i].CGColor;
- circleView.layer.borderWidth = 2;
- circleView.layer.masksToBounds = YES;
- self.circleView = circleView;
- [self.chartScrollView addSubview:circleView];
- [self.charCircleViewArr addObject:circleView];
- }else{
- [self.paopaoView drawBoxWithDirection:directionTop];
- }
- }
- if (self.isFloating) {
- if (pointY - CGRectGetHeight(self.paopaoView.frame) - KCircleRadius >= 0) {
- CGRect frame = self.paopaoView.frame;
- frame.origin.y = pointY - CGRectGetHeight(self.paopaoView.frame) - KCircleRadius;
- [self.paopaoView setFrame:frame];
- [self.paopaoView drawBoxWithDirection:directionTop];
-
- }else{
- CGRect frame = self.paopaoView.frame;
- frame.origin.y = pointY + KCircleRadius;
- [self.paopaoView setFrame:frame];
- [self.paopaoView drawBoxWithDirection:directionBottom];
- }
- }
- [self.chartScrollView bringSubviewToFront:self.paopaoView];
- }
- - (void)drawCircleArr:(NSArray *)pointArr color:(NSArray *)colors{
- for (int i = 0; i<pointArr.count; i++) {
- NSArray *arr = pointArr[i];
- for (int o = 0; o <arr.count; o++) {
- CGPoint point = [arr[o] CGPointValue];
- UIView * circleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KCircleRadius*2, KCircleRadius*2)];
- circleView.center = point;
- UIColor * color = colors[i];
- circleView.backgroundColor = color;
- circleView.layer.cornerRadius = KCircleRadius;
- circleView.layer.borderColor = [UIColor whiteColor].CGColor;
- circleView.layer.borderWidth = 1;
- circleView.layer.masksToBounds = YES;
- [self.chartScrollView addSubview:circleView];
- }
- }
- [self.chartScrollView bringSubviewToFront:self.circleView];
- [self.chartScrollView bringSubviewToFront:self.paopaoView];
- }
- -(void)addLines1With:(UIView *)view{
- CGFloat magrginHeight = (view.bounds.size.height)/ _row;
- Ymargin = magrginHeight;
- CAShapeLayer * dashLayer = [CAShapeLayer layer];
- _dashLayer = dashLayer;
- dashLayer.strokeColor = self.borderLineColor.CGColor;
- dashLayer.fillColor = self.borderTriangleColor.CGColor;
- // 默认设置路径宽度为0,使其在起始状态下不显示
- dashLayer.lineWidth = 0.5;
- UIBezierPath * path = [[UIBezierPath alloc]init];
- path.lineWidth = 1.0;
- [path moveToPoint:CGPointMake(titleWOfY, CGRectGetHeight(_chartScrollView.frame))];
- [path addLineToPoint:CGPointMake(titleWOfY,3)];
- [path addLineToPoint:CGPointMake(titleWOfY + 3,3)];
- [path addLineToPoint:CGPointMake(titleWOfY,0)];
- [path addLineToPoint:CGPointMake(titleWOfY - 3,3)];
- [path addLineToPoint:CGPointMake(titleWOfY,3)];
- [path moveToPoint:CGPointMake(titleWOfY, CGRectGetHeight(_chartScrollView.frame))];
- [path addLineToPoint:CGPointMake(CGRectGetMaxX(_chartScrollView.frame),CGRectGetHeight(_chartScrollView.frame))];
- if (_chartViewStyle == LHYChartViewLeftRightLine) {
- [path moveToPoint:CGPointMake(CGRectGetMaxX(_chartScrollView.frame) + 1, CGRectGetHeight(_chartScrollView.frame))];
- [path addLineToPoint:CGPointMake(CGRectGetMaxX(_chartScrollView.frame) + 1,0)];
- }else{
- [path addLineToPoint:CGPointMake(CGRectGetMaxX(_chartScrollView.frame),CGRectGetHeight(_chartScrollView.frame) - 3)];
- [path addLineToPoint:CGPointMake(CGRectGetMaxX(_chartScrollView.frame) + 3,CGRectGetHeight(_chartScrollView.frame))];
- [path addLineToPoint:CGPointMake(CGRectGetMaxX(_chartScrollView.frame),CGRectGetHeight(_chartScrollView.frame) + 3)];
- [path addLineToPoint:CGPointMake(CGRectGetMaxX(_chartScrollView.frame),CGRectGetHeight(_chartScrollView.frame))];
- }
- dashLayer.path = path.CGPath;
- [self.layer addSublayer:dashLayer];
- }
- @end
|