Skip to content

Instantly share code, notes, and snippets.

@projectxcappe
Created August 30, 2011 15:56
Show Gist options
  • Save projectxcappe/1181234 to your computer and use it in GitHub Desktop.
Save projectxcappe/1181234 to your computer and use it in GitHub Desktop.
Multiple dynamic graphing
//
// 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 248
#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{
CGContextRef context = UIGraphicsGetCurrentContext();
NSUInteger count = [nsArray count];
//NSLog(@"COUNT %i",count);
CGFloat prevValX;
CGFloat prevValY;
CGFloat currValX;
CGFloat currValY;
CGFloat deltaX = LENGTH / (count - 1);
//allocate data points for graph
for(int i=1; i<count; i++){
NSValue *currentPointValue = [nsArray objectAtIndex:i];
CGPoint currentPoint = [currentPointValue CGPointValue];
NSValue *previousPointValue = [nsArray objectAtIndex:i-1];
CGPoint previousPoint = [previousPointValue CGPointValue];
currValX = currentPoint.x + (deltaX * (i));
currValY = currentPoint.y;
prevValX = previousPoint.x + ((deltaX) * (i-1));
prevValY = previousPoint.y;
// NSLog(@"CurPoint %@", currentPointValue);
// NSLog(@"PrevPoint %@", previousPointValue);
// NSLog(@"curr (%f, %f) -- prev (%f, %f)", currValX, currValY, prevValX, prevValY);
//Draw Points (better way to do this. Suggestions?)
if (nsArray == _MVO_D_Points) {
NSLog(@"MVO D");
CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor);
CGContextFillEllipseInRect(context, CGRectMake(currValX-1.5, currValY-1.5, 3, 3));
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
}else if (nsArray == _MVO_U_Points){
NSLog(@"MVO U");
CGContextSetFillColorWithColor(context, [UIColor orangeColor].CGColor);
CGContextFillEllipseInRect(context, CGRectMake(currValX-1.5, currValY-1.5, 3, 3));
CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor);
}else if (nsArray == _TCP_D_Points){
NSLog(@"TCP D");
CGContextSetFillColorWithColor(context, [UIColor purpleColor].CGColor);
CGContextFillEllipseInRect(context, CGRectMake(currValX-1.5, currValY-1.5, 3, 3));
CGContextSetStrokeColorWithColor(context, [UIColor purpleColor].CGColor);
}else{ //_TCP_U_Point
NSLog(@"TCP U");
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillEllipseInRect(context, CGRectMake(currValX-1.5, currValY-1.5, 3, 3));
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
}
CGContextBeginPath(context);
CGContextMoveToPoint(context, prevValX, prevValY);
CGContextAddLineToPoint(context, currValX, currValY);
CGContextStrokePath(context);
}
//----- AXIS
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextBeginPath(context);
//Vert Line
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, 0, 300);
//Horiz Line
CGContextMoveToPoint(context, 0, 300);
CGContextAddLineToPoint(context, 300, 300);
CGContextStrokePath(context);
//----- END AXIS
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment