3

我无法弄清楚如何优化包含 NSBezierPath 的 NSView 的绘图。

让我试着解释一下我的意思。我有一个要绘制的由大约 40K 点组成的折线图。我掌握了所有要点,使用以下代码可以轻松绘制完整的图表:

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];

所以现在我将整个图形存储在实际坐标中的 NSBezierPath 形式中,我可以描边。但是让我们假设现在我想尽可能快地显示添加一个时间点的图表。

我不想每次都画出整套点。如果可能的话,我想使用完整的图表并只可视化一小部分。假设我只想在同一帧中渲染前 1000 个点。是否有任何可能性(修改边界并最终以某种方式缩放路径)仅以正确的边界呈现图形的第一部分?

我无法获得结果,因为如果我修改边界,那么比例会改变,我无法解决线宽问题。

4

1 回答 1

1

您可以仅使用新数据创建新路径,对其进行描边,然后将其附加到现有图表中:

NSBezierPath* newPath = [NSBezierPath bezierPath];
//... draw the new lines in newPath ...
[newPath stroke];
[fullGraph appendBezierPath:newPath];
于 2012-01-28T15:35:32.117 回答