简体   繁体   English

将核心图从右移到左

[英]Move the core plot graph from right to left

I want to move the coreplot graph from right to left on x-axis dynamically. 我想在x轴上从右向左动态移动coreplot图。

For example, In core PLot Gallery example, graph is moving from left to right. 例如,在核心PLot Gallery示例中,图形从左向右移动。 FOr that I am using the code: 我正在使用代码:

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

In the above code, the bold code is for moving from right to left what I am trying now. 在上面的代码中,粗体代码用于从右向左移动我现在正在尝试的内容。

But it is not moving from right to left. 但是它并没有从右向左移动。 Could you please help me how to do this. 您能帮我怎么做吗。

Thanks 谢谢

It looks like you copied the code from the "Real Time Plot" demo included with Core Plot (copied below). 看起来您是从Core Plot随附的“ Real Time Plot”演示中复制了代码(复制如下)。 This works as you describe: 正如您所描述的那样:

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

You don't need to update the xRange in this method at all—the animation will take care of that. 您根本不需要在此方法中更新xRange动画将解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM