iOS画折线图

代码例子效果:  下载地址:http://download.csdn.net/detail/qqmcy/6983187


 

 

LineChartViewDemo.h 

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface LineChartViewDemo : UIView  
  4.   
  5. //横竖轴距离间隔  
  6. @property (assign) NSInteger hInterval;  
  7. @property (assign) NSInteger vInterval;  
  8.   
  9. //横竖轴显示标签  
  10. @property (nonatomicstrongNSArray *hDesc;  
  11. @property (nonatomicstrongNSArray *vDesc;  
  12.   
  13. //点信息  
  14. @property (nonatomicstrongNSArray *array;  
  15.   
  16. @property (nonatomicstrong) NSArray* array1;  
  17.   
  18.   
  19. @end  


LineChartViewDemo.m 

  1. #import "LineChartViewDemo.h"  
  2. #import "EColumnChartLabel.h"  
  3. #define KlineHeight 30  
  4.   
  5. @interface LineChartViewDemo()  
  6. {  
  7.     CALayer *linesLayer;  
  8.       
  9.       
  10.     UIView *popView;  
  11.     UILabel *disLabel;  
  12.       
  13.     int x;  
  14.     int y;  
  15.       
  16. }  
  17.   
  18. @end  
  19.   
  20. @implementation LineChartViewDemo  
  21.   
  22. - (id)initWithFrame:(CGRect)frame  
  23. {  
  24.     self = [super initWithFrame:frame];  
  25.     if (self) {  
  26.         // Initialization code  
  27.         self.backgroundColor = [UIColor clearColor];  
  28.         x = frame.size.width;  
  29.         y = frame.size.height;  
  30.           
  31.           
  32.         _hInterval = 10;  
  33.         _vInterval = 50;  
  34.           
  35.         linesLayer = [[CALayer alloc] init];  
  36.         linesLayer.masksToBounds = YES;  
  37.         linesLayer.contentsGravity = kCAGravityLeft;  
  38.         linesLayer.backgroundColor = [[UIColor redColor] CGColor];  
  39.           
  40.         //[self.layer addSublayer:linesLayer];  
  41.           
  42.           
  43.         //PopView  
  44.         popView = [[UIView alloc]initWithFrame:CGRectMake(006030)];  
  45.         [popView setBackgroundColor:[UIColor whiteColor]];  
  46.         [popView setAlpha:0.0f];  
  47.           
  48.         disLabel = [[UILabel alloc]initWithFrame:popView.frame];  
  49.         [disLabel setTextAlignment:NSTextAlignmentCenter];  
  50.           
  51.         [popView addSubview:disLabel];  
  52.         [self addSubview:popView];  
  53.   
  54.     }  
  55.     return self;  
  56. }  
  57.   
  58.   
  59. // Only override drawRect: if you perform custom drawing.  
  60. // An empty implementation adversely affects performance during animation.  
  61. - (void)drawRect:(CGRect)rect  
  62. {  
  63.   
  64.     [self setClearsContextBeforeDrawing: YES];  
  65.       
  66.     CGContextRef context = UIGraphicsGetCurrentContext();  
  67.       
  68.     //画背景线条------------------  
  69.     CGColorRef backColorRef = [UIColor redColor].CGColor;  
  70.     CGFloat backLineWidth = 0.5f;  
  71.     CGFloat backMiterLimit = 0.f;  
  72.       
  73.     CGContextSetLineWidth(context, backLineWidth);//主线宽度  
  74.    // CGContextSetMiterLimit(context, backMiterLimit);//投影角度  
  75.       
  76.     //CGContextSetShadowWithColor(context, CGSizeMake(3, 5), 8, backColorRef);//设置双条线  
  77.       
  78.     CGContextSetLineJoin(context, kCGLineJoinRound);  
  79.       
  80.     CGContextSetLineCap(context, kCGLineCapRound );  
  81.       
  82.     CGContextSetBlendMode(context, kCGBlendModeNormal);  
  83.       
  84.     CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);  
  85.       
  86.    // CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:225.0f / 255.0f green:225.0f / 255.0f blue:225.0f / 255.0f alpha:1.0].CGColor);  
  87.       
  88. //    int x = 400 ;  
  89. //    //Y轴横线  
  90. //    int y = 300 ;  
  91.     int tempY = y;  
  92.       
  93.       
  94.     //添加纵轴标签和线  
  95.     for (int i=0; i<_vDesc.count; i++) {  
  96.           
  97.         CGPoint bPoint = CGPointMake(30, tempY);  
  98.         CGPoint ePoint = CGPointMake(x, tempY);  
  99.           
  100.         EColumnChartLabel *label = [[EColumnChartLabel alloc]initWithFrame:CGRectMake(003030)];  
  101.         [label setCenter:CGPointMake(bPoint.x-15, bPoint.y-30)];  
  102.         [label setTextAlignment:NSTextAlignmentCenter];  
  103.         [label setBackgroundColor:[UIColor clearColor]];  
  104.         [label setTextColor:[UIColor blackColor]];  
  105.         [label setText:[_vDesc objectAtIndex:i]];  
  106.         [self addSubview:label];  
  107.           
  108.         CGContextMoveToPoint(context, bPoint.x, bPoint.y-30);  
  109.         CGContextAddLineToPoint(context, ePoint.x, ePoint.y-30);  
  110.           
  111.         tempY -= y * 0.1;  
  112.           
  113.     }  
  114.       
  115.     for (int i=0; i<_array.count-1; i++) {  
  116.           
  117.         EColumnChartLabel *label = [[EColumnChartLabel alloc]initWithFrame:CGRectMake(i*KlineHeight+30, y - 304030)];  
  118.         [label setTextAlignment:NSTextAlignmentCenter];  
  119.         [label setBackgroundColor:[UIColor clearColor]];  
  120.         [label setTextColor:[UIColor redColor]];  
  121.         label.numberOfLines = 1;  
  122.         label.adjustsFontSizeToFitWidth = YES;  
  123.         label . minimumFontSize = 1.0f;  
  124.         [label setText:[_hDesc objectAtIndex:i]];  
  125.           
  126.         [self addSubview:label];  
  127.     }  
  128.     CGContextStrokePath(context);  
  129.   
  130.       
  131.       
  132.     //    //画点线条------------------  
  133.     CGColorRef pointColorRef = [UIColor colorWithRed:24.0f/255.0f green:116.0f/255.0f blue:205.0f/255.0f alpha:1.0].CGColor;  
  134.     CGFloat pointLineWidth = 1.5f;  
  135.     CGFloat pointMiterLimit = 5.0f;  
  136.       
  137.     CGContextSetLineWidth(context, pointLineWidth);//主线宽度  
  138.     CGContextSetMiterLimit(context, pointMiterLimit);//投影角度  
  139.       
  140.       
  141.     //CGContextSetShadowWithColor(context, CGSizeMake(3, 5), 8, pointColorRef);//设置双条线  
  142.       
  143.     CGContextSetLineJoin(context, kCGLineJoinRound);  
  144.       
  145.     CGContextSetLineCap(context, kCGLineCapRound );  
  146.       
  147.     CGContextSetBlendMode(context, kCGBlendModeNormal);  
  148.       
  149.     //CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);  
  150.     UIColor* color1 = c_BeforeLastYear;  
  151.     [color1 set];  
  152.   
  153.     //绘图  
  154.     CGPoint p1 = [[_array objectAtIndex:0] CGPointValue];  
  155.     int i = 0;  
  156.       
  157.     //获取视图的高  
  158.     int tempY1 = y;  
  159.     int tempWidth = y * 0.1f;  
  160.     float tempHeight = y * (270.0 / 320.0);  
  161.     float tempHeight1 = y * (20.0f / 320.0f);  
  162.  //   NSLog(@"%f");  
  163.     CGContextMoveToPoint(context, p1.x + 20, tempHeight-p1.y*tempWidth/tempHeight1);  
  164.     for (; i<[_array count]; i++)  
  165.     {  
  166.         p1 = [[_array objectAtIndex:i] CGPointValue];  
  167.         CGPoint goPoint = CGPointMake(p1.x + 20, tempHeight-p1.y*tempWidth/tempHeight1);  
  168.         CGContextAddLineToPoint(context, goPoint.x, goPoint.y);;  
  169.           
  170.         //添加触摸点  
  171.         UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];  
  172.           
  173.         [bt setBackgroundColor:[UIColor redColor]];  
  174.           
  175.         [bt setFrame:CGRectMake(001010)];  
  176.           
  177.         [bt setCenter:goPoint];  
  178.           
  179.         [bt addTarget:self  
  180.                action:@selector(btAction:)  
  181.      forControlEvents:UIControlEventTouchUpInside];  
  182.           
  183.         [self addSubview:bt];  
  184.     }  
  185.     CGContextStrokePath(context);  
  186.   
  187.       
  188.    // CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);  
  189.     UIColor* color = c_LastYear;  
  190.     [color set];  
  191.     //绘图  
  192.      p1 = [[_array1 objectAtIndex:0] CGPointValue];  
  193.      i = 0;  
  194.     CGContextMoveToPoint(context, p1.x + 20, tempHeight-p1.y*tempWidth/tempHeight1);  
  195.     for (; i<[_array1 count]; i++)  
  196.     {  
  197.         p1 = [[_array1 objectAtIndex:i] CGPointValue];  
  198.         CGPoint goPoint = CGPointMake(p1.x + 20, tempHeight-p1.y*tempWidth/tempHeight1);  
  199.         CGContextAddLineToPoint(context, goPoint.x, goPoint.y);;  
  200.           
  201.         //添加触摸点  
  202.         UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];  
  203.           
  204.         [bt setBackgroundColor:[UIColor redColor]];  
  205.           
  206.         [bt setFrame:CGRectMake(001010)];  
  207.           
  208.         [bt setCenter:goPoint];  
  209.           
  210.         [bt addTarget:self  
  211.                action:@selector(btAction:)  
  212.      forControlEvents:UIControlEventTouchUpInside];  
  213.           
  214.         [self addSubview:bt];  
  215.     }  
  216.   
  217.     CGContextStrokePath(context);  
  218.       
  219.       
  220.       
  221.       
  222.       
  223.       
  224.       
  225.   
  226. }  
  227. - (void)btAction:(id)sender{  
  228.     [disLabel setText:@"400"];  
  229.       
  230.     UIButton *bt = (UIButton*)sender;  
  231.     popView.center = CGPointMake(bt.center.x, bt.center.y - popView.frame.size.height/2);  
  232.     [popView setAlpha:1.0f];  
  233. }  


ViewController.m 

    1. #import "LineChartViewDemo.h"  
    2. #define CC_DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) * 0.01745329252f)   
    3. #define KlineHeight 20  
    4. #define KlineWidth 30  
    5.   
    6. @interface ViewController ()  
    7.   
    8. @end  
    9.   
    10. @implementation ViewController  
    11. - (void)viewDidLoad  
    12. {  
    13.       
    14.     [super viewDidLoad];  
    15.     // Do any additional setup after loading the view, typically from a nib.  
    16.     LineChartViewDemo* line = [[LineChartViewDemo alloc] initWithFrame:CGRectMake(00548250)];  
    17.    // line.layer.transform =  CATransform3DMakeRotation(CC_DEGREES_TO_RADIANS(90), 0, 0, 1);  
    18.     NSMutableArray *pointArr = [[NSMutableArray alloc]init];  
    19.       
    20.     //生成随机点   1  
    21.     //[pointArr addObject:[NSValue valueWithCGPoint:CGPointMake(KlineWidth*0, 0)]];  
    22.     for (int i = 0; i < 12; i++) {  
    23.         [pointArr addObject:[NSValue valueWithCGPoint:CGPointMake(KlineWidth* (i+1), 55 * i)]];  
    24.   
    25.     }  
    26.       
    27.         NSMutableArray* pointArr2 = [NSMutableArray array];  
    28.     //生成随机点   2  
    29.   
    30.     for (int i = 0; i < 12; i++) {  
    31.         [pointArr2 addObject:[NSValue valueWithCGPoint:CGPointMake(KlineWidth* (i + 1), 110 * i)]];  
    32.     }  
    33.       
    34.       
    35.       
    36.       
    37.     //竖轴  
    38.     NSMutableArray *vArr = [[NSMutableArray alloc]initWithCapacity:pointArr.count-1];  
    39.     for (int i=0; i<9; i++) {  
    40.         [vArr addObject:[NSString stringWithFormat:@"%d",i*20]];  
    41.     }  
    42.       
    43.       
    44.       
    45.       
    46.       
    47.       
    48.     //横轴  
    49.     NSMutableArray *hArr = [[NSMutableArray alloc]initWithCapacity:pointArr.count-1];  
    50.       
    51.     for (int i = 1; i <= 12; i++) {  
    52.         [hArr addObject:[NSString stringWithFormat:@"%d",i]];  
    53.     }  
    54.       
    55.      
    56.     [line setHDesc:hArr];  
    57.     [line setVDesc:vArr];  
    58.     [line setArray:pointArr];  
    59.     [line setArray1:pointArr2];  
    60.       
    61.     [self.view addSubview:line];  
    62.       
    63.   
    64.       
    65.       
    66.       
    67.       
    68.       
    69. }  
posted @ 2014-03-03 09:53  苹果吧  阅读(10389)  评论(0编辑  收藏  举报