2014-07-24 34 views
0

我想要動態地在x軸上從右向左移動coreplot圖形。將核心繪圖從右向左移動

例如,在覈心PLot圖庫示例中,圖形從左向右移動。對於我使用的代碼:

NSTimer *dataTimer = [NSTimer timerWithTimeInterval:1.0 
              target:self 
              selector:@selector(newData:) 
              userInfo:nil 
              repeats:YES]; 
[[NSRunLoop mainRunLoop] addTimer:dataTimer forMode:NSDefaultRunLoopMode]; 

-(void)newData:(NSTimer *)theTimer 
{ 
CPTPlot *thePlot = [[self getCorePLotGraph] plotWithIdentifier:@"Device1"]; 

CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)[self getCorePLotGraph].defaultPlotSpace; 
NSUInteger location  = (currentIndex >= kMaxDataPoints ? currentIndex - kMaxDataPoints + 2 : 0); 

CPTPlotRange *newRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(location) 
                 length:CPTDecimalFromUnsignedInteger(kMaxDataPoints-2)]; 

**CPTMutablePlotRange *xRange = [[self getCoreplotSpace].xRange mutableCopy]; 
[xRange expandRangeByFactor:CPTDecimalFromCGFloat(-2.0)]; 
[self getCoreplotSpace].xRange = xRange;** 



[CPTAnimation animate:plotSpace 
      property:@"xRange" 
     fromPlotRange:plotSpace.xRange 
      toPlotRange:newRange 
      duration:CPTFloat(1.0/kFrameRate)]; 

currentIndex++; 


[temperatureValuesArray addObject:[NSNumber numberWithDouble:(1.0 - kAlpha) * [[temperatureValuesArray lastObject] doubleValue] + kAlpha * rand()/(double)RAND_MAX]]; 
[thePlot insertDataAtIndex:temperatureValuesArray.count - 1 numberOfRecords:1]; 
} 

在上面的代碼中,大膽的代碼是從從右向左移動了什麼我現在嘗試。

但它不是從右向左移動。你能幫我怎麼做。

感謝

+0

'plotSpace.xRange = newRange;'有什麼問題? –

+0

我想從右向左移動X軸,這就是爲什麼我使用了這樣的代碼。我是這個核心情節的新手。我以這種方式嘗試過。請糾正我 – Dhanunjaya

回答

1

它看起來像你從包含在覈心地塊(以下複製)的"Real Time Plot" demo複製的代碼。這工作你描述:

-(void)newData:(NSTimer *)theTimer 
{ 
    CPTGraph *theGraph = (self.graphs)[0]; 
    CPTPlot *thePlot = [theGraph plotWithIdentifier:kPlotIdentifier]; 

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

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

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

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

     currentIndex++; 
     [plotData addObject:@((1.0 - kAlpha) * [[plotData lastObject] doubleValue] + kAlpha * rand()/(double)RAND_MAX)]; 
     [thePlot insertDataAtIndex:plotData.count - 1 numberOfRecords:1]; 
    } 
} 

你並不需要更新此方法xRange在所有的動畫將採取照顧。