iOS开发之横道图

来源:互联网 发布:惠州干部网络大学堂 编辑:程序博客网 时间:2024/05/22 14:55

.h文件

#import <UIKit/UIKit.h>

@class ProgressChartView;

@protocol ProgressChartViewDelegate<NSObject>

@required

- (NSString *)progressBeginTimeInChartView:(ProgressChartView *)progressChartView;

- (NSString *)progressEndTimeInChartView:(ProgressChartView *)progressChartView;

- (NSArray *)progressColorArrayInChartView:(ProgressChartView *)progressChartView;

- (NSArray *)progressValueArrayInChartView:(ProgressChartView *)progressChartView;

- (NSArray *)progressxAxisTitleArrayInChartView:(ProgressChartView *)progressChartView;

@end


@interface ProgressChartView :UIView

@property (nonatomic,assign)id<ProgressChartViewDelegate>delegate;


- (void)reloadProgressChartView;


@end


.m文件

#import "ProgressChartView.h"

#import "UIView+Extension.h"

#import "GlobalTool.h"

#define kProgressYAxisWidth (33*WIDTH_PERCENT)

#define kProgressXAxisHeight (40*HEIGHT_PERCENT)

@interface ProgressChartView()

{

   UIView *_yAxisView;

   UIView *_xAxisView;

   UIView *_chartBgView;

   UIScrollView *_bgScorllView;

    

   double _xAxisLabelWidth;

   NSArray *_colorsArray;

   NSArray *_xAxisTitleArray;

   NSArray *_xValuesArray;

    

    

   NSString *_endTime;

   NSString *_startTime;

    

   long long _endTimeNum;

   long long _startTimeNum;

    

    

   NSMutableArray *_xAxisLabels;

}

@end

@implementation ProgressChartView

- (instancetype)initWithFrame:(CGRect)frame

{

   self = [superinitWithFrame:frame];

   if (self) {

        _xAxisLabelWidth =SCREEN_WIDTH/5.0;

        [selfaddYAxisView];

        [selfaddBgScrollView];

        [selfaddXAxisView];

       _xAxisLabels = [[NSMutableArrayalloc]init];

    }

    return self;

}

#pragma mark - setUI--------------

- (void)addYAxisView

{

    _yAxisView = [[UIViewalloc]initWithFrame:CGRectMake(0,0, kProgressYAxisWidth,self.height-kProgressXAxisHeight)];

    [_yAxisViewsetBackgroundColor:[UIColorclearColor]];

   NSArray *yAxisTitle = @[@"计划\n日期",@"实际\n日期"];

   for (int i =0; i<yAxisTitle.count; i++) {

        UILabel *l = [[UILabelalloc]initWithFrame:CGRectZero];

        [l setBackgroundColor:[UIColorclearColor]];

        [l setTextAlignment:NSTextAlignmentCenter];

        [lsetTextColor:kPageColor];

        [lsetFont:kPageSubFont];

        [lsetText:yAxisTitle[i]];

        l.numberOfLines =0;

        CGRect rect = [l.textboundingRectWithSize:CGSizeMake(_yAxisView.width,_yAxisView.height/3.0)options:NSStringDrawingUsesLineFragmentOriginattributes:@{NSFontAttributeName:kPageFont}context:nil];

        [lsetBounds:CGRectMake(0,0, _yAxisView.width, rect.size.height)];

        [lsetCenter:CGPointMake(_yAxisView.width/2.0,_yAxisView.height/3.0*(i+1))];

        [_yAxisViewaddSubview:l];

    }

    [selfaddSubview:_yAxisView];

    

}

- (void)addXAxisView

{

    if (_xAxisView) {

        [_xAxisViewremoveFromSuperview];

    }

    _xAxisView = [[UIViewalloc]initWithFrame:CGRectMake(0,_bgScorllView.height-kProgressXAxisHeight,_bgScorllView.width,kProgressXAxisHeight)];

    [_xAxisViewsetBackgroundColor:[UIColorclearColor]];

    [_bgScorllViewaddSubview:_xAxisView];

}

- (void)addBgScrollView

{

    _bgScorllView = [[UIScrollViewalloc]initWithFrame:CGRectMake(_yAxisView.width,0, self.width-_yAxisView.width,self.height)];

    _bgScorllView.showsHorizontalScrollIndicator =NO;

    [_bgScorllViewsetBackgroundColor:[UIColorclearColor]];

    

    [selfaddSubview:_bgScorllView];

    

}


#pragma -mark reloadChartView-------------------------------

- (void)reloadProgressChartView

{

    [_bgScorllViewsetContentOffset:CGPointMake(0,0)];

    _xValuesArray = [self.delegateprogressValueArrayInChartView:self];

    _colorsArray = [self.delegateprogressColorArrayInChartView:self];

    _xAxisTitleArray = [self.delegateprogressxAxisTitleArrayInChartView:self];

    _startTime = [self.delegateprogressBeginTimeInChartView:self];

    _endTime = [self.delegateprogressEndTimeInChartView:self];

    

    

   float scrollContentWidth = (_xAxisTitleArray.count+1)*_xAxisLabelWidth;

    [_bgScorllViewsetContentSize:CGSizeMake(scrollContentWidth>_bgScorllView.width?scrollContentWidth:_bgScorllView.width,_bgScorllView.height)];

    [_chartBgView setFrame:CGRectMake(0,0, _bgScorllView.contentSize.width,_bgScorllView.height-kProgressXAxisHeight)];

    

    if (_chartBgView) {

        [_chartBgViewremoveFromSuperview];

    }

    _chartBgView = [[UIViewalloc]initWithFrame:CGRectMake(0,0, _bgScorllView.width,_bgScorllView.height-kProgressXAxisHeight)];

    [_chartBgViewsetBackgroundColor:[UIColorclearColor]];

    [_bgScorllViewaddSubview:_chartBgView];

    

    

    //划横线

   for (int i =0; i<2; i++) {

       CAShapeLayer *shapeLayer = [CAShapeLayerlayer];

        UIBezierPath *path = [UIBezierPathbezierPath];

        [pathmoveToPoint:CGPointMake(_xAxisLabelWidth/2.0,i*(_chartBgView.height-0.5)+0.5)];

        [pathaddLineToPoint:CGPointMake(_bgScorllView.contentSize.width,i*(_chartBgView.height-0.5)+0.5)];

        shapeLayer.path = path.CGPath;

        shapeLayer.strokeColor = [[UIColorFromRGB(0xc1cfd9)colorWithAlphaComponent:1]CGColor];

        shapeLayer.fillColor = [[UIColorwhiteColor] CGColor];

        shapeLayer.lineWidth =0.5;

        [_chartBgView.layeraddSublayer:shapeLayer];

    }

    

    //划竖线

   for (int i =0; i<_xAxisTitleArray.count+1; i++) {

       CAShapeLayer *shapeLayer = [CAShapeLayerlayer];

        UIBezierPath *path = [UIBezierPathbezierPath];

        [path moveToPoint:CGPointMake(_xAxisLabelWidth/2.0+i*_xAxisLabelWidth,0)];

        [path addLineToPoint:CGPointMake(_xAxisLabelWidth/2.0+i*_xAxisLabelWidth,(_chartBgView.height-0.5))];

        shapeLayer.path = path.CGPath;

        shapeLayer.strokeColor = [[UIColorFromRGB(0xc1cfd9)colorWithAlphaComponent:1]CGColor];

        shapeLayer.fillColor = [[UIColorwhiteColor] CGColor];

        shapeLayer.lineWidth =0.5;

        [_chartBgView.layeraddSublayer:shapeLayer];

    }

    //设置x

    [selfaddXAxisView];

   NSInteger xAxisLabelCount = [_xAxisLabelscount];

   NSInteger curXAxisValueCount = [_xAxisTitleArraycount];

   if (xAxisLabelCount>curXAxisValueCount) {

       for (NSInteger i = curXAxisValueCount; i<xAxisLabelCount; i++)

        {

            [_xAxisLabelsremoveObjectAtIndex:i];

        }

    }

    

   for (int i =0; i < curXAxisValueCount; i++) {

        CGRect rect =CGRectMake(_xAxisLabelWidth*(1/2.0+2.0/16)+i*_xAxisLabelWidth,10,_xAxisLabelWidth*12.0/16,kProgressXAxisHeight-20);

       NSString *text = [_xAxisTitleArrayobjectAtIndex:i];

       if (i<xAxisLabelCount) {

           CATextLayer *xAxisLayer = [_xAxisLabelsobjectAtIndex:i];

            [xAxisLayersetFrame:rect];

            [xAxisLayersetString:text];

        }else{

           CATextLayer *xAxisLayer = [CATextLayernew];

            [xAxisLayersetBackgroundColor:[UIColorclearColor].CGColor];

            [xAxisLayersetFontSize:kPageFontNum];

            [xAxisLayersetString:text];

            [xAxisLayersetFrame:rect];

            [xAxisLayersetForegroundColor:kPageColor.CGColor];

            [xAxisLayersetAlignmentMode:@"center"];

            xAxisLayer.contentsScale =2.0f;

            

            xAxisLayer.wrapped =YES;

            [_xAxisView.layeraddSublayer:xAxisLayer];

        }

    }

    _endTimeNum = [selfexchangeTimeWithTimeString:_endTime];

    _startTimeNum = [selfexchangeTimeWithTimeString:_startTime];

    float chartXWidth =_chartBgView.width-1.0/2*_xAxisLabelWidth - _xAxisLabelWidth*9/16.0;

    

    CAShapeLayer *dottedLine = [CAShapeLayerlayer];

    [dottedLinesetFillColor:[UIColorclearColor].CGColor];

    

    [dottedLine setStrokeColor:[UIColorlightGrayColor].CGColor

     ];

    [dottedLinesetLineWidth:0.5f];

    [dottedLinesetLineJoin:kCALineJoinRound];

    [dottedLine setLineDashPattern:[NSArrayarrayWithObjects:[NSNumbernumberWithInt:3],[NSNumbernumberWithInt:1],nil]];

    CGMutablePathRef path =CGPathCreateMutable();

    //划进度

   for (int i =0; i<_xValuesArray.count; i++) {

       NSArray *childArray = [_xValuesArrayobjectAtIndex:i];

       for (int j =0; j<childArray.count; j++) {

           NSString *nodeTime = [childArray objectAtIndex:j];

           if (nodeTime.length) {

               long long curPointX = [selfexchangeTimeWithTimeString:nodeTime];

               float grade = (float)(curPointX-_startTimeNum)/(_endTimeNum -_startTimeNum);

               CGPoint point = CGPointMake(grade*chartXWidth+_xAxisLabelWidth*8/16.0, (i+1)*_chartBgView.height*1/3.0 - 6 * WIDTH_PERCENT);

               if (j!=childArray.count-1) {

                   NSString *nodeTime1 = [childArray objectAtIndex:j+1];

                   if (nodeTime1.length) {

                       long long curPointX1 = [selfexchangeTimeWithTimeString:nodeTime1];

                       float grade1 = (float)(curPointX1-_startTimeNum)/(_endTimeNum-_startTimeNum);

                       CGPoint point1 = CGPointMake(grade1*chartXWidth+_xAxisLabelWidth*8/16.0, (i+1)*_chartBgView.height*1/3.0 - 6 * WIDTH_PERCENT);

                       if ((point1.x-6*WIDTH_PERCENT)-(point.x+8*WIDTH_PERCENT)>12*WIDTH_PERCENT/2.0) {

                           CGPathMoveToPoint(path, NULL,point.x+8*WIDTH_PERCENT,point.y+6*WIDTH_PERCENT);

                           CGPathAddLineToPoint(path, NULL, point1.x-6*WIDTH_PERCENT,point1.y+6*WIDTH_PERCENT);

                        }

                    }

                    

                }


                [selfaddPointNodeWithPoint:point timeString:nodeTime nodeColor:[_colorsArrayobjectAtIndex:j] index:j];

            }

        }

    }

    [dottedLinesetPath:path];

    CGPathRelease(path);

    [_chartBgView.layeraddSublayer:dottedLine];


}

- (void)addPointNodeWithPoint:(CGPoint)point timeString:(NSString *)nodeTime nodeColor:(UIColor *)nodeColor index:(NSInteger)index

{

   CALayer *layer = [CALayernew];

    [layer setFrame:CGRectMake(point.x-5*WIDTH_PERCENT, point.y,12*WIDTH_PERCENT,12*WIDTH_PERCENT)];

    [layersetBackgroundColor:nodeColor.CGColor];

    [layersetCornerRadius:12*WIDTH_PERCENT/2.0 ];

    [layersetBorderColor:nodeColor.CGColor];

    [_chartBgView.layeraddSublayer:layer];

    

   CATextLayer *textLayer = [CATextLayernew];

   if (index%2==0) {

        [textLayersetFrame:CGRectMake(point.x-_xAxisLabelWidth*8.0/16, point.y

                                       +20*WIDTH_PERCENT,_xAxisLabelWidth, 12*HEIGHT_PERCENT)];

    }else{

        [textLayersetFrame:CGRectMake(point.x-_xAxisLabelWidth*8.0/16, point.y

                                       -20*WIDTH_PERCENT,_xAxisLabelWidth, 12*HEIGHT_PERCENT)];

    }

    [textLayersetString:nodeTime];

    [textLayersetAlignmentMode:@"center"];

    textLayer.contentsScale =2.0f;

    textLayer.wrapped =YES;

    [textLayersetFontSize:SCREEN_WIDTH>375?14:12];

    [textLayersetForegroundColor:kPageColor.CGColor];

    [_chartBgView.layeraddSublayer:textLayer];

}

- (longlong)exchangeTimeWithTimeString:(NSString *)timeString

{

    timeString = [timeStringsubstringToIndex:10];

   NSDateFormatter *inputFormatter = [[NSDateFormatteralloc] init];

    [inputFormattersetDateFormat:@"yyyy-MM-dd"];

   NSDate* inputDate = [inputFormatter dateFromString:timeString];

   long long time = [inputDatetimeIntervalSince1970];

   return time;

}


效果图:


0 0
原创粉丝点击