博客> ios折线图,曲线图。
ios折线图,曲线图。
2017-12-11 21:13 评论:0 阅读:457 673100280
折线图 ios折线图 曲线图

折线图相信大家不会陌生,下面上一段代码有注释的那种、、、 首先自定义一个YJHFoldLineView .h里面写一下代码:

@interface YJHFoldLineView : UIView
//x轴坐标
@property(nonatomic,strong)NSArray XArr;
//y轴坐标
@property(nonatomic,strong)NSArray
YArr;
//实际数据的x坐标
@property(nonatomic,strong)NSArray realXArr;
//实际数据的y坐标
@property(nonatomic,strong)NSArray
realYArr;
//坐标的提示框
@property(nonatomic,strong)NSArray tipLabelsArr;
//实际数据的点
@property(nonatomic,strong)NSArray
buttonsArr;

-(void)start;
/
XArr x轴坐标数组
YArr y轴坐标数组
realXArr 实际数据的x坐标数组
realYArr 实际数据的y坐标数组
/
-(void)prepareColsAndCom:(NSArray )XArr andYArr:(NSArray )YArr andRealXArr:(NSArray )realXArr andRealYArr:(NSArray )realYArr;
@end

==========.m里面=============

import "YJHFoldLineView.h"

define left 0

define up 0

define down 0

@interface YJHFoldLineView ()

//表格宽度
@property(nonatomic,assign)CGFloat width;
//表格高度
@property(nonatomic,assign)CGFloat height;
//行高
@property(nonatomic,assign)CGFloat hangHeight;
//列宽
@property(nonatomic,assign)CGFloat lieWidth;

@end

@implementation YJHFoldLineView

  • (instancetype)initWithFrame:(CGRect)frame
    {

    self = [super initWithFrame:frame];
    if (self) {

    }
    return self;
    }

-(void)prepareColsAndCom:(NSArray )XArr andYArr:(NSArray )YArr andRealXArr:(NSArray )realXArr andRealYArr:(NSArray )realYArr{

NSComparator finderSort = ^(id string1,id string2){                                                                                        

    if ([string1 integerValue] > [string2 integerValue]) {                                                                                        
        return (NSComparisonResult)NSOrderedAscending;                                                                                        
    }else if ([string1 integerValue] < [string2 integerValue]){                                                                                        
        return (NSComparisonResult)NSOrderedDescending;                                                                                        
    }                                                                                        
    else                                                                                        
        return (NSComparisonResult)NSOrderedSame;                                                                                        
};                                                                                        

//数组排序:大 > 小                                                                                        
NSArray *resultArray = [realYArr sortedArrayUsingComparator:finderSort];                                                                                        
NSLog(@"数组排序:大 > 小 %@",resultArray);                                                                                        
self.XArr = XArr;                                                                                        
self.YArr = resultArray;                                                                                        
self.realXArr = realXArr;                                                                                        
self.realYArr = realYArr;                                                                                        

self.width = self.frame.size.width;                                                                                        
self.height = self.frame.size.height;                                                                                        
self.hangHeight = (self.height)/5;                                                                                        
self.lieWidth = (self.width)/(self.XArr.count+0.3);                                                                                        

CGFloat width = self.width;                                                                                        

//竖线和title                                                                                        
for (int i = 0; i &lt; self.XArr.count; i++) {                                                                                        

    UIView *shu = [[UIView alloc]init];                                                                                        
    shu.backgroundColor = Color_FromRGB(0x56627c);                                                                                        
    //        shu.frame=CGRectMake(left+i*self.lieWidth, up, 1, self.height-up-down);                                                                                        
    //        shu.frame=CGRectMake(left+i*self.lieWidth, up, 1, 5);                                                                                        
    shu.frame = CGRectMake(self.lieWidth+i*self.lieWidth, self.height-20, KWidthValue(0.5), 5);                                                                                        
    [self addSubview:shu];                                                                                        

    UILabel *label = [[UILabel alloc]init];                                                                                        
    label.text = self.XArr[i];                                                                                        
    //        label.textAlignment=NSTextAlignmentCenter;                                                                                        
    label.font = [UIFont systemFontOfSize:KWidthValue(10)];                                                                                        
    label.adjustsFontSizeToFitWidth = YES;                                                                                        
    CGPoint center = shu.center;                                                                                        
    CGFloat y = CGRectGetMaxY(shu.frame);                                                                                        
    label.textColor = Color_FromRGB(0x56627c);                                                                                        
    label.frame = CGRectMake(center.x-20,y+5 , (width-left*2)/(self.XArr.count), 13);                                                                                        
    [self addSubview:label];                                                                                        
}                                                                                        

//横线和title                                                                                        
for (int i = 0; i &lt; 5; i++) {                                                                                        

    UIView *heng = [[UIView alloc]init];                                                                                        
    heng.backgroundColor = Color_FromRGB(0x56627c);                                                                                        
    //        shu.frame=CGRectMake(left, up+self.hangHeight*i, self.width-left*2, 1);                                                                                        
    heng.frame = CGRectMake(left, up+self.hangHeight*i, 5, KWidthValue(0.5));                                                                                        
    [self addSubview:heng];                                                                                        
    UILabel *label = [[UILabel alloc]init];                                                                                        
    label.text = [NSString stringWithFormat:@"%.0f",([[self.YArr firstObject] floatValue] - [[self.Y                                                                                        Arr lastObject] floatValue])/4 * (4-i) + [[self.YArr lastObject] floatValue]];
                                                                                            //        label.textAlignment=NSTextAlignmentRight;                                                                                        
    label.font = [UIFont systemFontOfSize:KWidthValue(10)];                                                                                                                                                                                
    label.adjustsFontSizeToFitWidth = YES;                                                                                        
    CGPoint center = heng.center;                                                                                        
    label.frame = CGRectMake(10,center.y-7, 50, 13);                                                                                        
    label.textColor = Color_FromRGB(0x56627c);                                                                                        
    [self addSubview:label];                                                                                        

    //下方横线
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, self.height-15, MAIN_SCREEN_WIDTH, KWidthValue(0.5))];                                                                                        
    line.backgroundColor = Color_FromRGB(0x56627c);                                                                                        
    [self addSubview:line];                                                                                        

}                                                                                        
[self start];                                                                                        

for (int i = 0; i &lt; self.realYArr.count; i++) {                                                                                        

    UIButton *button = self.buttonsArr[i];                                                                                        

// CGFloat xmargin = ([self.realXArr[i] floatValue]-[[self.XArr firstObject] floatValue])(width-left2)/([[self.XArr lastObject] floatValue]-[[self.XArr firstObject] floatValue])+left;
// CGFloat ymargin = ([[self.YArr firstObject]floatValue]-[self.realYArr[i] floatValue])*(self.height-up-down)/([[self.YArr firstObject] floatValue]-[[self.YArr lastObject]floatValue])+up;

    CGFloat xmargin = self.lieWidth*(i+1);                                                                                        
    CGFloat ymargin = self.height-self.hangHeight-5-((KWidthValue(188)-self.hangHeight)/([[self.YArr firstObject] floatValue] - [[self.YArr lastObject] floatValue]+1)) * ([self.realYArr[i] floatValue] - [[self.YArr lastObject] floatValue]);                                                                                        

    button.frame = CGRectMake(xmargin-KWidthValue(4.5),ymargin, KWidthValue(9), KWidthValue(9));                                                                                        
    [self addSubview:button];                                                                                        

    UILabel *label = self.tipLabelsArr[i];                                                                                        
    label.textAlignment = NSTextAlignmentRight;                                                                                        
    label.frame = CGRectMake(xmargin-78, ymargin-15, 80, KWidthValue(10));                                                                                        
    [self addSubview:label];                                                                                        

}                                                                                        

}
/
圆点
/
-(NSArray *)buttonsArr{

if (!_buttonsArr) {                                                                                        

    NSMutableArray *tmp = [NSMutableArray array];                                                                                        

    for (int i = 0; i &lt; self.realXArr.count; i++) {                                                                                        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];                                                                                        
        button.layer.cornerRadius = KWidthValue(4.5);                                                                                        
        button.layer.masksToBounds = YES;                                                                                        
        button.backgroundColor = [UIColor yellowColor];                                                                                        
        button.tag = 100+i;                                                                                        
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];                                                                                        
        [tmp addObject:button];                                                                                        

        UIView *view = [[UIView alloc]initWithFrame:CGRectMake((KWidthValue(9)-KWidthValue(6))/2, (KWidthValue(9)-KWidthValue(6))/2, KWidthValue(6),KWidthValue(6))];                                                                                        
        view.layer.cornerRadius = KWidthValue(3);                                                                                        
        view.layer.masksToBounds = YES;                                                                                        
        view.backgroundColor = CELLCOLOR;                                                                                        
        [button addSubview:view];                                                                                        

    }                                                                                        
    _buttonsArr=[NSArray arrayWithArray:tmp];                                                                                        
}                                                                                        
return _buttonsArr;                                                                                        

}
/
点位上显示的Y值lable
/
-(NSArray *)tipLabelsArr{

if (!_tipLabelsArr) {                                                                                        

    NSMutableArray *tmp = [NSMutableArray array];                                                                                        

    for (int i = 0; i&lt;self.realXArr.count; i++) {                                                                                        
        UILabel *label = [UILabel new];                                                                                        
        label.adjustsFontSizeToFitWidth=YES;                                                                                        
        //            label.text = [NSString stringWithFormat:@"(%.1f,%.1f)",[self.realXArr[i] floatValue],[self.realYArr[i] floatValue]];                                                                                        
        label.text = [NSString stringWithFormat:@"   %.0f",[self.realYArr[i] floatValue]];                                                                                        
        label.textColor = Color_FromRGB(0xffd900);                                                                                        
        label.font = [UIFont systemFontOfSize:KWidthValue(10)];                                                                                        
        //            label.hidden = YES;                                                                                        
        [tmp addObject:label];                                                                                        
    }                                                                                        
    _tipLabelsArr=[NSArray arrayWithArray:tmp];                                                                                        
}                                                                                        
return _tipLabelsArr;                                                                                        

}

-(void)buttonAction:(UIButton *)button{

for (UILabel *label in self.tipLabelsArr) {                                                                                        
    //        label.hidden = YES;                                                                                        
}

NSInteger index = button.tag-100;                                                                                        

UILabel *label = self.tipLabelsArr[index];                                                                                        
label.hidden = NO;                                                                                        

}

-(void)start{

[self draw];                                                                                        

}

-(void)draw{

CGFloat width = self.width;                                                                                        
UIBezierPath *path = [UIBezierPath bezierPath];                                                                                        
CAShapeLayer *layer = [CAShapeLayer layer];                                                                                        

CGPoint prePoint;                                                                                        
CGPoint nowPoint;                                                                                        

for (int i = 0; i &lt; self.realXArr.count; i++) {                                                                                        

// CGFloat xmargin = ([self.realXArr[i] floatValue]-[[self.XArr firstObject] floatValue])(width-left2)/([[self.XArr lastObject] floatValue]-[[self.XArr firstObject] floatValue])+left;

// CGFloat ymargin = ([[self.YArr firstObject]floatValue]-[self.realYArr[i] floatValue])*(self.height-up-down)/([[self.YArr firstObject] floatValue]-[[self.YArr lastObject]floatValue])+up;

    CGFloat xmargin = self.lieWidth*(i+1);                                                                                        
    CGFloat ymargin = self.height-self.hangHeight-((KWidthValue(188)-self.hangHeight)/([[self.YArr firstObject] floatValue] - [[self.YArr lastObject] floatValue]+1)) * ([self.realYArr[i] floatValue] - [[self.YArr lastObject] floatValue]);                                                                                        

    if (i == 0) {                                                                                        

        prePoint = CGPointMake(xmargin, ymargin);                                                                                        
        [path moveToPoint:prePoint];                                                                                        
    }else{                                                                                        

        nowPoint = CGPointMake(xmargin, ymargin);                                                                                        

        //根据点位画直线/曲线                                                                                        

        //            [path addCurveToPoint:nowPoint controlPoint1:CGPointMake((prePoint.x+nowPoint.x)/2.0, prePoint.y) controlPoint2:CGPointMake((prePoint.x+nowPoint.x)/2.0, nowPoint.y)];                                                                                        

        [path addLineToPoint:CGPointMake(nowPoint.x, nowPoint.y)];                                                                                        
        prePoint = nowPoint;                                                                                        
    }                                                                                        
}                                                                                        
layer.path = path.CGPath;                                                                                        
layer.strokeColor = Color_FromRGB(0xeaeaea).CGColor;                                                                                        
layer.fillColor = nil;                                                                                        
layer.lineWidth = KWidthValue(1.5);                                                                                        

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];                                                                                        
animation.fromValue = @(0);                                                                                        
animation.toValue = @(1);                                                                                        
animation.duration = 3;                                                                                        
[layer addAnimation:animation forKey:nil];                                                                                        
animation.fillMode = kCAFillModeForwards;                                                                                        
animation.removedOnCompletion = NO;                                                                                        

[self.layer addSublayer:layer];                                                                                        

} @end

点个赞呗。

收藏
1
sina weixin mail 回到顶部