【问题标题】:NSBezierPath GraphNSBezierPath 图
【发布时间】:2012-01-28 23:35:32
【问题描述】:

我不知道如何优化包含 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 个点。是否有任何可能性(修改边界并最终以某种方式缩放路径)仅以正确的边界呈现图形的第一部分?

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

【问题讨论】:

    标签: objective-c nsview nsbezierpath


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的建议。我一定会尝试遵循这种方法。但可能我的描述有点误导,所以让我试着改写一下。第一次调用 drawRect 方法时,我能够创建包含整个图形的 fullGraph 贝塞尔路径(超过 30K 行,我知道这太多了),并且我修改了视图边界以匹配我的点单位。但现在我不想显示整个图表。我只想显示前两行。在以下调用中,我想增加 x 坐标以显示更大的部分等等。
    • 问题在于,对于每个 drawrect 调用,我尝试仅更改视图边界以匹配我想要显示的图形部分。但是在边界改变之后,线宽是巨大的。而且我无法理解如何缩小它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    相关资源
    最近更新 更多