2014-04-16 19 views
0

我在CorePlot中按照示例向散點圖中添加新點,以獲取它們。我使用CoreData附帶的RealTimePlot示例模擬了我的圖形。我注意到,當我運行我的圖表時,一旦屏幕開始滾動以呈現新數據,CPU將掛鉤到100%。我嘗試了將數據添加到我的數據源以及更新調用的不同方式,但我無法獲得任何響應和可接受的CPU負載。CorePlot CPU問題。實時添加數據到圖形

經過多次試驗和錯誤之後,我啓動了RealTimePlot示例以查看CPU負載是多少,並且它的掛鉤率也是100%。這是在iPhone 5s上,所以我很害怕看到4將如何行事。我的問題;有沒有其他方法來更新圖表?

無論如何,這裏是我的更新循環。我正在加速度計數據並繪製它。

-(void)motionController:(VWWMotionController*)sender didUpdateAcceleremeters:(CMAccelerometerData*)accelerometers{ 
    static NSInteger counter = 0; 

    CPTGraph *theGraph = self.graph; 
    CPTPlot *thePlot = [theGraph plotWithIdentifier:@"Blue Plot"]; 

    if (thePlot) { 

     if (self.dataForPlot.count >= NUM_POINTS) { 
      [self.dataForPlot removeObjectAtIndex:0]; 
      [thePlot deleteDataInIndexRange:NSMakeRange(0, 1)]; 
     } 

     CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)theGraph.defaultPlotSpace; 
     NSUInteger location  = (counter >= NUM_POINTS ? counter - NUM_POINTS + 2 : 0); 

     CPTPlotRange *oldRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger((location > 0) ? (location - 1) : 0) 
                   length:CPTDecimalFromUnsignedInteger(NUM_POINTS - 2)]; 
     CPTPlotRange *newRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(location) 
                   length:CPTDecimalFromUnsignedInteger(NUM_POINTS - 2)]; 

     [CPTAnimation animate:plotSpace 
        property:@"xRange" 
       fromPlotRange:oldRange 
        toPlotRange:newRange 
        duration:CPTFloat(0)]; 


     NSNumber *x = @(counter); 
     NSNumber *y = @(accelerometers.acceleration.x); 
     [self.dataForPlot addObject:@{ @"x": x, 
             @"y": y }]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      [thePlot insertDataAtIndex:self.dataForPlot.count - 1 numberOfRecords:1]; 
     }); 


     counter++; 
    } 
} 

僅供參考。我最好喜歡有30 FPS。我已經調整了加速度計回調從1/5到1/30與一些CPU的改進,但後來我失去了幀速率。

如果我註釋掉insertDataAtIndex行,CPU會掛起1%,但當然圖形不會跟隨新數據。 我沒有看到插入方法的任何衍生物。

我也讀過其他線程重新計算軸標籤是資源敏感。我已經嘗試刪除它們,但幾乎沒有改進。

我正在考慮去這個工作的OpenGL或CoreGraphics。

你有什麼建議?

+0

爲什麼動畫的持續時間爲零(0)?只需直接(在主線程上)設置'xRange'並完成它。如果您想要從一個點到下一個點平滑過渡,請使用1 /(幀速率)的持續時間。 –

回答

0

注意:我沒有在iPhone 5s上測試過(我做過iPhone 4s,5,iPad 3,iPad 4,iPad Mini 1),我們的應用似乎有不同的要求,所以我不積極這將幫助你。

無論如何,在我的測試中,我發現每次獲取新數據點時都會通過重繪所有點來獲得最佳,最具響應性的結果。這對我來說是最好的,因爲我不得不每秒更新圖表120次。我沒有iPhone來測試我們的應用程序的CPU,但在iPad Mini上,CPU是< 50%,圖形運行2個圖(每個約125分,儘管這相當敏感,每個約250分)。該圖只在y軸上有標籤,並且不會更改。 x軸是時間,標籤對我們來說是不必要的。

我們的要求可能太不同了,這個實施可以幫助你,但它可能是值得一試的。如果你願意,它可以這樣:

-(void)UpdateGraph 
{ 
    //foreach scatterplot 
    { 
     //Remove point from array 
     //[linePlot deleteDataInIndexRange:NSMakeRange(0, [array count])] 
    } 
    //Insert new data point into array(s) 

    //foreach scatterplot 
    { 
     //[linePlot insertDataAtIndex:0 numberOfRecords:[array count]] 
    } 
    //Schedule next update if necessary 
    //[self performSelector:@selector(UpdateGraph) withObject:nil afterDelay:(1/(CGFloat)Your_FPS_Here)]; 
} 

希望能幫助你,祝你好運!

0

關鍵是使圖形簡單快速的繪製。設置標籤政策,以便您沒有太多標籤。不要使用小勾號或網格線(將線條樣式設置爲nil)。如果你不需要它們,也關閉主要的網格線。不要在地塊上使用區域填充。