Created
August 30, 2011 02:52
-
-
Save projectxcappe/1180057 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GOCustomView.m | |
// CustomViewExample | |
// | |
// Created by Cassidy Pangell on 8/26/11. | |
// Copyright (c) 2011 __MyCompanyName__. All rights reserved. | |
// | |
#import "CPCustomView.h" | |
#include "math.h" | |
#define LENGTH 250 | |
#define HEIGHT 250 | |
@implementation CPCustomView | |
@synthesize bgColor = _bgColor; | |
@synthesize MVO_U_Points = _MVO_U_Points; | |
@synthesize MVO_D_Points = _MVO_D_Points; | |
@synthesize TCP_U_Points = _TCP_U_Points; | |
@synthesize TCP_D_Points = _TCP_D_Points; | |
- (void)setBgColor:(UIColor *)bgColor{ | |
[_bgColor release]; | |
_bgColor = [bgColor retain]; | |
[self setNeedsDisplay]; | |
} | |
- (void)awakeFromNib{ | |
//self.bgColor = [UIColor orangeColor]; | |
} | |
- (void)drawRect:(CGRect)rect | |
{ | |
[self.bgColor setFill]; | |
[self graphPoints:_MVO_D_Points]; | |
// [self graphPoints:_MVO_U_Points]; | |
// [self graphPoints:_TCP_D_Points]; | |
// [self graphPoints:_TCP_U_Points]; | |
} | |
-(void) graphPoints:(NSArray *)nsArray{ | |
// NSLog(@"GraphPoints%@ ", nsArray); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
NSUInteger count = [nsArray count]; | |
CGFloat prevValX; | |
CGFloat prevValY; | |
CGFloat deltaX = LENGTH / (count - 1); | |
CGPoint zero; | |
zero.x = 0; | |
zero.y = 0; | |
//allocate data points for graph | |
for(int i=1; i<count; i++){ | |
NSValue *currentPointValue = [nsArray objectAtIndex:i]; | |
CGPoint currentPoint = [currentPointValue CGPointValue]; | |
CGPoint previousPoint = [[nsArray objectAtIndex:i-1] CGPointValue]; | |
NSLog(@"Point %@", previousPoint); | |
prevValX = previousPoint.x; | |
prevValY = previousPoint.y; | |
NSLog(@"prev X %@ Y %@", prevValX, prevValY); | |
//Scale the graph | |
CGFloat xVal = count + (deltaX * i); | |
CGFloat yVal = (currentPoint.y); | |
NSLog(@"xVal %f yVal %f", xVal, yVal); | |
//Draw Points | |
if (nsArray == _MVO_D_Points) { | |
// NSLog(@"MVO D"); | |
CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor); | |
CGContextFillEllipseInRect(context, CGRectMake(xVal, yVal, 3, 3)); | |
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor); | |
}else if (nsArray == _MVO_U_Points){ | |
//NSLog(@"MVO U"); | |
CGContextSetFillColorWithColor(context, [UIColor orangeColor].CGColor); | |
CGContextFillEllipseInRect(context, CGRectMake(xVal, yVal, 3, 3)); | |
CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor); | |
}else if (nsArray == _TCP_D_Points){ | |
//NSLog(@"TCP D"); | |
CGContextSetFillColorWithColor(context, [UIColor purpleColor].CGColor); | |
CGContextFillEllipseInRect(context, CGRectMake(xVal, yVal, 3, 3)); | |
CGContextSetStrokeColorWithColor(context, [UIColor purpleColor].CGColor); | |
}else{ //_TCP_U_Point | |
//NSLog(@"TCP U"); | |
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); | |
CGContextFillEllipseInRect(context, CGRectMake(xVal, yVal, 3, 3)); | |
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); | |
} | |
CGContextBeginPath(context); | |
CGContextMoveToPoint(context, prevValX, prevValY); | |
CGContextAddLineToPoint(context, xVal, yVal); | |
CGContextStrokePath(context); | |
} | |
//----- AXIS | |
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); | |
CGContextBeginPath(context); | |
//Vert Line | |
CGContextMoveToPoint(context, 25, 25); | |
CGContextAddLineToPoint(context, 25, 275); | |
//Horiz Line | |
CGContextMoveToPoint(context, 25, 275); | |
CGContextAddLineToPoint(context, 275, 275); | |
CGContextStrokePath(context); | |
//----- END AXIS | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment