IOS line chart

Source: Internet
Author: User

 

 

 

LineChartViewDemo. h

 

# Import
 
  
@ Interface LineChartViewDemo: UIView // distance between the horizontal and vertical axes @ property (assign) NSInteger hInterval; @ property (assign) NSInteger vInterval; // display the label @ property (nonatomic, strong) NSArray * hDesc; @ property (nonatomic, strong) NSArray * vDesc; // Point Information @ property (nonatomic, strong) NSArray * array; @ property (nonatomic, strong) NSArray * array1; @ end
 

LineChartViewDemo. m

 

 

# Import LineChartViewDemo. h # import EColumnChartLabel. h # define KlineHeight 30 @ interface LineChartViewDemo () {CALayer * linesLayer; UIView * popView; UILabel * disLabel; int x; int y ;}@ end @ implementation LineChartViewDemo-(id) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {// Initialization code self. backgroundColor = [UIColor clearColor]; x = frame. size. width; y = frame. size. height; _ hInterval = 10; _ vInterval = 50; linesLayer = [[CALayer alloc] init]; linesLayer. masksToBounds = YES; linesLayer. contentsGravity = kCAGravityLeft; linesLayer. backgroundColor = [[UIColor redColor] CGColor]; // [self. layer addSublayer: linesLayer]; // PopView popView = [[UIView alloc] initWithFrame: CGRectMake (0, 0, 60, 30)]; [popView setBackgroundColor: [UIColor whiteColor]; [popView setAlpha: 0.0f]; disLabel = [[UILabel alloc] initWithFrame: popView. frame]; [disLabel setTextAlignment: NSTextAlignmentCenter]; [popView addSubview: disLabel]; [self addSubview: popView];} return self ;}// Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. -(void) drawRect :( CGRect) rect {[self failed: YES]; CGContextRef context = UIGraphicsGetCurrentContext (); // draw the background line ---------------- CGColorRef backColorRef = [UIColor redColor]. CGColor; CGFloat backLineWidth = 0.5f; CGFloat backMiterLimit = 0.f; CGContextSetLineWidth (context, backLineWidth); // main line width // CGContextSetMiterLimit (context, backMiterLimit ); // projection angle // CGContextSetShadowWithColor (context, CGSizeMake (3, 5), 8, backColorRef); // you can specify two-line CGContextSetLineJoin (context, kCGLineJoinRound). CGContextSetLineCap (context, kCGLineCapRound); CGContextSetBlendMode (context, kCGBlendModeNormal); CGContextSetStrokeColorWithColor (context, [UIColor grayColor]. CGColor); // CGContextSetStrokeColorWithColor (context, [UIColor colorWithRed: 22520.f/255.0f green: 22520.f/255.0f blue: 22520.f/255.0f alpha: 1.0]. CGColor); // int x = 400; // y axis x = 300; // int y =; int tempY = y; // Add the vertical axis label and line for (int I = 0; I <_ vDesc. count; I ++) {CGPoint bPoint = CGPointMake (30, tempY); CGPoint ePoint = CGPointMake (x, tempY); EColumnChartLabel * label = [[EColumnChartLabel alloc] initWithFrame: CGRectMake (0, 0, 30, 30)]; [label setCenter: CGPointMake (bPoint. x-15, bPoint. y-30)]; [label setTextAlignment: NSTextAlignmentCenter]; [label setBackgroundColor: [UIColor clearColor]; [label setTextColor: [UIColor blackColor]; [label setText: [_ vDesc objectAtIndex: i]; [self addSubview: label]; CGContextMoveToPoint (context, bPoint. x, bPoint. y-30); CGContextAddLineToPoint (context, ePoint. x, ePoint. y-30); tempY-= y * 0.1;} for (int I = 0; I <_ array. count-1; I ++) {EColumnChartLabel * label = [[EColumnChartLabel alloc] initWithFrame: CGRectMake (I * KlineHeight + 30, y-30, 40, 30)]; [label setTextAlignment: NSTextAlignmentCenter]; [label setBackgroundColor: [UIColor clearColor]; [label setTextColor: [UIColor redColor]; label. numberOfLines = 1; label. adjustsFontSizeToFitWidth = YES; label. minimumFontSize = 1.0f; [label setText: [_ hDesc objectAtIndex: I]; [self addSubview: label];} CGContextStrokePath (context ); /// draw a line ---------------- CGColorRef pointColorRef = [UIColor colorWithRed: 24366f/255.0f green: 116.0f/255.0f blue: 205.0f/255.0f alpha: 1.0]. CGColor; CGFloat pointLineWidth = 1.5f; CGFloat pointMiterLimit = 5.0f; CGContextSetLineWidth (context, pointLineWidth); // The width of the Main Line (context, pointMiterLimit); // The projection angle // context (context, limit, CGSizeMake (3, 5), 8, pointColorRef); // you can specify CGContextSetLineJoin (context, kCGLineJoinRound), CGContextSetLineCap (context, callback), and CGContextSetBlendMode (context, callback ); // CGContextSetStrokeColorWithColor (context, [UIColor blueColor]. CGColor); UIColor * color1 = c_BeforeLastYear; [color1 set]; // plot CGPoint p1 = [[_ array objectAtIndex: 0] CGPointValue]; int I = 0; // obtain the high int tempY1 = y; int tempWidth = y * 0.1f; float tempHeight = y * (270.0/320.0); float tempHeight1 = y * (20366f/3201_f ); // NSLog (@ % f); CGContextMoveToPoint (context, p1.x + 20, tempHeight-p1.y * tempWidth/tempHeight1); for (; I <[_ array count]; I ++) {p1 = [[_ array objectAtIndex: I] CGPointValue]; CGPoint goPoint = CGPointMake (p1.x + 20, tempHeight-p1.y * tempWidth/tempHeight1); CGContextAddLineToPoint (context, goPoint. x, goPoint. y); // Add the touch point UIButton * bt = [UIButton buttonWithType: UIButtonTypeCustom]; [bt setBackgroundColor: [UIColor redColor]; [bt setFrame: CGRectMake (0, 0, 10, 10)]; [bt setCenter: goPoint]; [bt addTarget: self action: @ selector (btAction :) forControlEvents: UIControlEventTouchUpInside]; [self addSubview: bt];} CGContextStrokePath (context); // CGContextSetStrokeColorWithColor (context, [UIColor greenColor]. CGColor); UIColor * color = c_LastYear; [color set]; // plot p1 = [[_ array1 objectAtIndex: 0] CGPointValue]; I = 0; CGContextMoveToPoint (context, p1.x + 20, tempHeight-p1.y * tempWidth/tempHeight1); for (; I <[_ array1 count]; I ++) {p1 = [[_ array1 objectAtIndex: I] CGPointValue]; CGPoint goPoint = CGPointMake (p1.x + 20, tempHeight-p1.y * tempWidth/tempHeight1); CGContextAddLineToPoint (context, goPoint. x, goPoint. y); // Add the touch point UIButton * bt = [UIButton buttonWithType: UIButtonTypeCustom]; [bt setBackgroundColor: [UIColor redColor]; [bt setFrame: CGRectMake (0, 0, 10, 10)]; [bt setCenter: goPoint]; [bt addTarget: self action: @ selector (btAction :) forControlEvents: UIControlEventTouchUpInside]; [self addSubview: bt];} CGContextStrokePath (context);}-(void) btAction :( id) sender {[disLabel setText: @ 400]; UIButton * bt = (UIButton *) sender; popView. center = CGPointMake (bt. center. x, bt. center. y-popView. frame. size. height/2); [popView setAlpha: 1.0f];}

ViewController. m

 

 

# Import LineChartViewDemo. h # define CC_DEGREES_TO_RADIANS (_ ANGLE _) * 0.01745329252f) # define KlineHeight 20 # define KlineWidth 30 @ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. lineChartViewDemo * line = [[LineChartViewDemo alloc] initWithFrame: CGRectMake (0, 0,548,250)]; // line. layer. transform = CATransform3DMakeRotation (cost (90), 0, 0, 1); NSMutableArray * pointArr = [[NSMutableArray alloc] init]; // generate random vertex 1 // [pointArr addObject: [NSValue valueWithCGPoint: CGPointMake (KlineWidth * 0, 0)]; for (int I = 0; I <12; I ++) {[pointArr addObject: [NSValue valueWithCGPoint: CGPointMake (KlineWidth * (I + 1), 5 * I)];} NSMutableArray * pointArr2 = [NSMutableArray array]; // generate random vertex 2 for (int I = 0; I <12; I ++) {[pointArr2 addObject: [NSValue valueWithCGPoint: CGPointMake (KlineWidth * (I + 1), 10 * I)];} // vertical axis NSMutableArray * vArr = [[NSMutableArray alloc] initWithCapacity: pointArr. count-1]; for (int I = 0; I <9; I ++) {[vArr addObject: [NSString stringWithFormat: @ % d, I * 20];} // horizontal axis NSMutableArray * hArr = [[NSMutableArray alloc] initWithCapacity: pointArr. count-1]; for (int I = 1; I <= 12; I ++) {[hArr addObject: [NSString stringWithFormat: @ % d, I];} [line setHDesc: hArr]; [line setVDesc: vArr]; [line setArray: pointArr]; [line setArray1: pointArr2]; [self. view addSubview: line];}


 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.