user1175172
user1175172

Reputation: 31

NSBezierPath Graph

I cannot figure out how to optimize the drawing of an NSView that contains a NSBezierPath.

Let me try to explain what I mean. I have a line graph, made by about 40K points, that I want to draw. I have all the points and it's easy for me to draw once the full graph using the following code:

NSInteger npoints=[delegate returnNumOfPoints:self]; //get the total number of points
aRange=NSMakeRange(0, npoints); //set the range
absMin=[delegate getMinForGraph:self inRange:aRange]; //get the Minimum y value
absMax=[delegate getMaxForGraph:self inRange:aRange]; //get the Maximum y value
float delta=absMax-absMin;  //get the height of bound
float aspectRatio=self.frame.size.width/self.frame.size.heigh //compensate for the real frame
float xscale=aspectRatio*(absMax-absMin); // get the width of bound
float step=xscale/npoints; //get the unit size
[self setBounds:NSMakeRect(0.0, absMin, xscale, delta)]; //now I can set the bound
NSSize unitSize={1.0,1.0};
unitSize= [self convertSize:unitSize fromView:nil];
[NSBezierPath setDefaultLineWidth:MIN(unitSize.height,unitSize.width)];
fullGraph=[NSBezierPath bezierPath];
[fullGraph moveToPoint:NSMakePoint(0.0, [delegate getValueForGraph:self forPoint:aRange.location])];
//Create the path
for (long i=1; i<npoints; i++)
    {
        y=[delegate getValueForGraph:self forPoint:i];
        x=i*step;
        [fullGraph lineToPoint:NSMakePoint(x,y)];
}
[[NSColor redColor] set];
[fullGraph stroke];

So now I have the whole graph stored in a NSBezierPath form in real coordinate, that I can stroke. But let's suppose that now I want to display the graph adding one point at time as fast as possible.

I do not want to draw the whole set of points every time. I want to use, if possible the complete graph and visualize only a small part. Let's say that I want to render in the same frame only the first 1000 points. Is there any possibility (modifying bounds and eventually scaling the path in some way) to render only the first part of the graph in correct bounds?

I was not able to obtain the result, because if I modify the bounds then the scale changes and I'm not able to fix the problem with linewidth.

Upvotes: 3

Views: 712

Answers (1)

user1118321
user1118321

Reputation: 26325

You can create a new path with just the new data, stroke it, then append that to your existing graph:

NSBezierPath* newPath = [NSBezierPath bezierPath];
//... draw the new lines in newPath ...
[newPath stroke];
[fullGraph appendBezierPath:newPath];

Upvotes: 1

Related Questions