Hello everyone, I am a little stuck on programming a NSView thats draws a graph live. My graph should draw itself for every point passed in the "liveGraph:forTime:" function. If the graph is reaching the top or bottom of the view, the curve (not the view) is resized (by half) so it will fit. I am trying to find a smooth way of doing so. So far, the resizing works, but it gives me small kinks every time it resizes. I am testing it with y=x+10.
Here is what I have so far:
Here is what I have so far:
Code:
- (void) awakeFromNib {
path = [[NSBezierPath alloc] init];
[path moveToPoint:NSMakePoint(0.0, 0.0)];
[path setLineWidth:3.0];
maxY = [self bounds].size.height;
maxX = [self bounds].size.width;
}
- (void) drawRect:(NSRect)rect {
[super drawRect:rect];
if (path) {
[[NSColor redColor] set];
[path stroke];
}
}
- (void) liveGraph:(float)y forTime:(float)x {
if (y > maxY) {
maxY = y*2;
NSAffineTransform* transform = [NSAffineTransform transform];
[transform scaleXBy:1 yBy:0.5];
[path transformUsingAffineTransform:transform];
}
if (x > maxX) {
maxX = x*2;
NSAffineTransform* transform = [NSAffineTransform transform];
[transform scaleXBy:0.5 yBy:1];
[path transformUsingAffineTransform:transform];
}
NSPoint point = NSMakePoint((x/maxX)*[self bounds].size.width, (y/maxY)*[self bounds].size.height);
[path lineToPoint:point];
[self setNeedsDisplay:YES];
}