从右向左移动核心绘图

时间:2014-07-24 11:14:56

标签: ios core-plot

我想动态地在x轴上从右到左移动核心图图。

例如,在核心PLot Gallery示例中,图形从左向右移动。我正在使用代码:

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

在上面的代码中,粗体代码用于从右向左移动我正在尝试的内容。

但它不会从右向左移动。你能帮我解决一下这个问题。

由于

1 个答案:

答案 0 :(得分:1)

看起来您复制了Core Plot附带的"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 - 动画将处理此问题。