0

我有一个选项卡式应用程序。其中一个选项卡包含散点图。我想在每次viewDidAppear通话时动画绘制情节。为此,我借用了从 Core Plot RealTime 示例中添加新数据点的想法。

基本上我做了同样的事情,我的代码如下。正如您从日志中看到的那样,在每次计时器调用时都会添加点,并且新点的值由数据源委托获取。关键是剧情一点都不清爽。它保持空白并且没有绘制点(轴可以)。

我做了一些研究,有些人在这里报告了同样的问题http://code.google.com/p/core-plot/issues/detail?id=445,这里https://groups.google.com/forum/# !topic/coreplot-discuss/3uVUQI8v-5E 和此处Core Plot - RealTime plot 不会自动刷新

按照最后一个链接中的建议添加[graph reloadData]afterinsertDataAtIndex除了为新的值获取值,而且为所有点获取值之外,什么也没做。我不知道出了什么问题。Core Plot Real Time 示例不会[graph reloadData]在任何地方调用。

我的代码:

图形控制器.h

#import <UIKit/UIKit.h>
#import <sqlite3.h>
#import "ScatterPlot.h"

@interface GraphViewController : GAITrackedViewController{
    IBOutlet CPTGraphHostingView *_graphHostingView;
}

@property (nonatomic, retain) ScatterPlot *scatterPlot;
@end

图形控制器.m

NSMutableArray *data;

@interface GraphViewController ()

@end

@implementation GraphViewController

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    self.scatterPlot = [[ScatterPlot alloc] initWithHostingView:_graphHostingView    andData:data];
    self.scatterPlot.firstDate = firstDate;
    self.scatterPlot.lastDate = lastDate;
    [self.scatterPlot initialisePlot];
}

散点图.h

#import <Foundation/Foundation.h>
#import "CorePlot-CocoaTouch.h"

@interface ScatterPlot : NSObject <CPTPlotDataSource>

@property (nonatomic, retain) CPTGraphHostingView *hostingView;
@property (nonatomic, retain) NSMutableArray *graphData;
@property (nonatomic, readwrite) NSInteger currentPointsCount;
@property (nonatomic, readwrite) NSDate* firstDate;
@property (nonatomic, readwrite) NSDate* lastDate;

-(id)initWithHostingView:(CPTGraphHostingView *)hostingView andData:(NSMutableArray *)data;

-(void)initialisePlot;

-(void)newData:(NSTimer *)theTimer;

@end

散点图.m

#import "ScatterPlot.h"

NSInteger yCoordinateCrossValue = 0;
double kFrameRate = 60.0;  // frames per second
NSTimer *dataTimer;

@implementation ScatterPlot

-(id)initWithHostingView:(CPTGraphHostingView *)hostingView andData:(NSMutableArray *)data
{
    self = [super init];

    if ( self != nil ) {
        self.hostingView = hostingView;
        self.graphData = [[NSMutableArray alloc] initWithArray:data];
    }

    return self;
}

-(void)initialisePlot
{
    if ( (self.hostingView == nil) || (self.graphData == nil) ) {
        NSLog(@"TUTSimpleScatterPlot: Cannot initialise plot without hosting view or data.");
        return;
    }

    [self configureHost];
    [self configureGraph];
    [self configurePlots];
    [self configureAxes];

    dataTimer = nil;

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

//timer method
-(void)newData:(NSTimer *)theTimer{

    if (self.currentPointsCount < self.graphData.count) {
        CPTPlot *plot = [self.hostingView.hostedGraph plotWithIdentifier:@"weightPlot"];
        [plot insertDataAtIndex:self.currentPointsCount++ numberOfRecords:1];
        NSLog(@"New point added at index: %i", self.currentPointsCount);
    }else{
        [theTimer invalidate];
    }
}

//data source methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
    if ( [plot.identifier isEqual:@"weightPlot"] )
    {
        NSLog(@"Checking number of points in plot: %i", self.currentPointsCount);
        return self.currentPointsCount; //[self.graphData count];
    }

    return 0;
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
    if ( [plot.identifier isEqual:@"weightPlot"] )
    {
        NSValue *value = [self.graphData objectAtIndex:index];
        CGPoint point = [value CGPointValue];

        if ( fieldEnum == CPTScatterPlotFieldX )
        {
            NSLog(@"Get new point.X at index: %i", index);
            return [NSNumber numberWithFloat:point.x];
        }
        else    // Y-Axis
        {
            NSLog(@"Get new point.Y at index: %i", index);
            return [NSNumber numberWithFloat:point.y];
        }
    }

    return [NSNumber numberWithFloat:0];
}

日志:

2013-10-17 14:24:01.141 app[9741:c07] Checking number of points in plot: 0
2013-10-17 14:24:01.141 app[9741:c07] Checking number of points in plot: 0
2013-10-17 14:24:01.242 app[9741:c07] Checking number of points in plot: 1
2013-10-17 14:24:01.242 app[9741:c07] Checking number of points in plot: 1
2013-10-17 14:24:01.243 app[9741:c07] Get new point.X at index: 0
2013-10-17 14:24:01.244 app[9741:c07] Checking number of points in plot: 1
2013-10-17 14:24:01.245 app[9741:c07] Get new point.Y at index: 0
2013-10-17 14:24:01.246 app[9741:c07] Checking number of points in plot: 1
2013-10-17 14:24:01.246 app[9741:c07] New point added at index: 1
2013-10-17 14:24:01.267 app[9741:c07] Checking number of points in plot: 2
2013-10-17 14:24:01.268 app[9741:c07] Checking number of points in plot: 2
2013-10-17 14:24:01.269 app[9741:c07] Get new point.X at index: 1
2013-10-17 14:24:01.270 app[9741:c07] Checking number of points in plot: 2
2013-10-17 14:24:01.270 app[9741:c07] Get new point.Y at index: 1
2013-10-17 14:24:01.271 app[9741:c07] Checking number of points in plot: 2
2013-10-17 14:24:01.272 app[9741:c07] New point added at index: 2
2013-10-17 14:24:01.281 app[9741:c07] Checking number of points in plot: 3
2013-10-17 14:24:01.282 app[9741:c07] Checking number of points in plot: 3
2013-10-17 14:24:01.284 app[9741:c07] Get new point.X at index: 2
2013-10-17 14:24:01.285 app[9741:c07] Checking number of points in plot: 3
2013-10-17 14:24:01.286 app[9741:c07] Get new point.Y at index: 2
2013-10-17 14:24:01.287 app[9741:c07] Checking number of points in plot: 3
2013-10-17 14:24:01.288 app[9741:c07] New point added at index: 3
2013-10-17 14:24:01.297 app[9741:c07] Checking number of points in plot: 4
2013-10-17 14:24:01.298 app[9741:c07] Checking number of points in plot: 4
2013-10-17 14:24:01.299 app[9741:c07] Get new point.X at index: 3
2013-10-17 14:24:01.299 app[9741:c07] Checking number of points in plot: 4
2013-10-17 14:24:01.300 app[9741:c07] Get new point.Y at index: 3
2013-10-17 14:24:01.301 app[9741:c07] Checking number of points in plot: 4
2013-10-17 14:24:01.302 app[9741:c07] New point added at index: 4
2013-10-17 14:24:01.313 app[9741:c07] Checking number of points in plot: 5
2013-10-17 14:24:01.314 app[9741:c07] Checking number of points in plot: 5
2013-10-17 14:24:01.315 app[9741:c07] Get new point.X at index: 4
2013-10-17 14:24:01.316 app[9741:c07] Checking number of points in plot: 5
2013-10-17 14:24:01.317 app[9741:c07] Get new point.Y at index: 4
2013-10-17 14:24:01.317 app[9741:c07] Checking number of points in plot: 5
2013-10-17 14:24:01.318 app[9741:c07] New point added at index: 5
2013-10-17 14:24:01.330 app[9741:c07] Checking number of points in plot: 6
2013-10-17 14:24:01.331 app[9741:c07] Checking number of points in plot: 6
2013-10-17 14:24:01.333 app[9741:c07] Get new point.X at index: 5
2013-10-17 14:24:01.333 app[9741:c07] Checking number of points in plot: 6
2013-10-17 14:24:01.334 app[9741:c07] Get new point.Y at index: 5
2013-10-17 14:24:01.335 app[9741:c07] Checking number of points in plot: 6
2013-10-17 14:24:01.336 app[9741:c07] New point added at index: 6
2013-10-17 14:24:01.347 app[9741:c07] Checking number of points in plot: 7
2013-10-17 14:24:01.347 app[9741:c07] Checking number of points in plot: 7
2013-10-17 14:24:01.348 app[9741:c07] Get new point.X at index: 6
2013-10-17 14:24:01.349 app[9741:c07] Checking number of points in plot: 7
2013-10-17 14:24:01.350 app[9741:c07] Get new point.Y at index: 6
2013-10-17 14:24:01.351 app[9741:c07] Checking number of points in plot: 7
2013-10-17 14:24:01.351 app[9741:c07] New point added at index: 7
2013-10-17 14:24:01.364 app[9741:c07] Checking number of points in plot: 8
2013-10-17 14:24:01.365 app[9741:c07] Checking number of points in plot: 8
2013-10-17 14:24:01.365 app[9741:c07] Get new point.X at index: 7
2013-10-17 14:24:01.366 app[9741:c07] Checking number of points in plot: 8
2013-10-17 14:24:01.367 app[9741:c07] Get new point.Y at index: 7
2013-10-17 14:24:01.368 app[9741:c07] Checking number of points in plot: 8
2013-10-17 14:24:01.368 app[9741:c07] New point added at index: 8
2013-10-17 14:24:01.380 app[9741:c07] Checking number of points in plot: 9
2013-10-17 14:24:01.381 app[9741:c07] Checking number of points in plot: 9
2013-10-17 14:24:01.382 app[9741:c07] Get new point.X at index: 8
2013-10-17 14:24:01.382 app[9741:c07] Checking number of points in plot: 9
2013-10-17 14:24:01.383 app[9741:c07] Get new point.Y at index: 8
2013-10-17 14:24:01.383 app[9741:c07] Checking number of points in plot: 9
2013-10-17 14:24:01.384 app[9741:c07] New point added at index: 9
2013-10-17 14:24:01.397 app[9741:c07] Checking number of points in plot: 10
2013-10-17 14:24:01.398 app[9741:c07] Checking number of points in plot: 10
2013-10-17 14:24:01.399 app[9741:c07] Get new point.X at index: 9
2013-10-17 14:24:01.400 app[9741:c07] Checking number of points in plot: 10
2013-10-17 14:24:01.400 app[9741:c07] Get new point.Y at index: 9
2013-10-17 14:24:01.401 app[9741:c07] Checking number of points in plot: 10
2013-10-17 14:24:01.402 app[9741:c07] New point added at index: 10
2013-10-17 14:24:01.414 app[9741:c07] Checking number of points in plot: 11
2013-10-17 14:24:01.415 app[9741:c07] Checking number of points in plot: 11
2013-10-17 14:24:01.415 app[9741:c07] Get new point.X at index: 10
2013-10-17 14:24:01.416 app[9741:c07] Checking number of points in plot: 11
2013-10-17 14:24:01.417 app[9741:c07] Get new point.Y at index: 10
2013-10-17 14:24:01.418 app[9741:c07] Checking number of points in plot: 11
2013-10-17 14:24:01.419 app[9741:c07] New point added at index: 11
2013-10-17 14:24:01.430 app[9741:c07] Checking number of points in plot: 12
2013-10-17 14:24:01.431 app[9741:c07] Checking number of points in plot: 12
2013-10-17 14:24:01.432 app[9741:c07] Get new point.X at index: 11
2013-10-17 14:24:01.432 app[9741:c07] Checking number of points in plot: 12
2013-10-17 14:24:01.433 app[9741:c07] Get new point.Y at index: 11
2013-10-17 14:24:01.434 app[9741:c07] Checking number of points in plot: 12
2013-10-17 14:24:01.434 app[9741:c07] New point added at index: 12
2013-10-17 14:24:01.447 app[9741:c07] Checking number of points in plot: 13
2013-10-17 14:24:01.448 app[9741:c07] Checking number of points in plot: 13
2013-10-17 14:24:01.449 app[9741:c07] Get new point.X at index: 12
2013-10-17 14:24:01.449 app[9741:c07] Checking number of points in plot: 13
2013-10-17 14:24:01.450 app[9741:c07] Get new point.Y at index: 12
2013-10-17 14:24:01.450 app[9741:c07] Checking number of points in plot: 13
2013-10-17 14:24:01.451 app[9741:c07] New point added at index: 13
2013-10-17 14:24:01.463 app[9741:c07] Checking number of points in plot: 14
2013-10-17 14:24:01.464 app[9741:c07] Checking number of points in plot: 14
2013-10-17 14:24:01.465 app[9741:c07] Get new point.X at index: 13
2013-10-17 14:24:01.466 app[9741:c07] Checking number of points in plot: 14
2013-10-17 14:24:01.467 app[9741:c07] Get new point.Y at index: 13
2013-10-17 14:24:01.467 app[9741:c07] Checking number of points in plot: 14
2013-10-17 14:24:01.468 app[9741:c07] New point added at index: 14
2013-10-17 14:24:01.480 app[9741:c07] Checking number of points in plot: 15
2013-10-17 14:24:01.481 app[9741:c07] Checking number of points in plot: 15
2013-10-17 14:24:01.482 app[9741:c07] Get new point.X at index: 14
2013-10-17 14:24:01.483 app[9741:c07] Checking number of points in plot: 15
2013-10-17 14:24:01.484 app[9741:c07] Get new point.Y at index: 14
2013-10-17 14:24:01.484 app[9741:c07] Checking number of points in plot: 15
2013-10-17 14:24:01.485 app[9741:c07] New point added at index: 15
2013-10-17 14:24:01.497 app[9741:c07] Checking number of points in plot: 16
2013-10-17 14:24:01.498 app[9741:c07] Checking number of points in plot: 16
2013-10-17 14:24:01.498 app[9741:c07] Get new point.X at index: 15
2013-10-17 14:24:01.499 app[9741:c07] Checking number of points in plot: 16
2013-10-17 14:24:01.500 app[9741:c07] Get new point.Y at index: 15
2013-10-17 14:24:01.500 app[9741:c07] Checking number of points in plot: 16
2013-10-17 14:24:01.501 app[9741:c07] New point added at index: 16
2013-10-17 14:24:01.514 app[9741:c07] Checking number of points in plot: 17
2013-10-17 14:24:01.515 app[9741:c07] Checking number of points in plot: 17
2013-10-17 14:24:01.516 app[9741:c07] Get new point.X at index: 16
2013-10-17 14:24:01.516 app[9741:c07] Checking number of points in plot: 17
2013-10-17 14:24:01.517 app[9741:c07] Get new point.Y at index: 16
2013-10-17 14:24:01.518 app[9741:c07] Checking number of points in plot: 17
2013-10-17 14:24:01.518 app[9741:c07] New point added at index: 17
2013-10-17 14:24:01.530 app[9741:c07] Checking number of points in plot: 18
2013-10-17 14:24:01.531 app[9741:c07] Checking number of points in plot: 18
2013-10-17 14:24:01.532 app[9741:c07] Get new point.X at index: 17
2013-10-17 14:24:01.533 app[9741:c07] Checking number of points in plot: 18
2013-10-17 14:24:01.534 app[9741:c07] Get new point.Y at index: 17
2013-10-17 14:24:01.534 app[9741:c07] Checking number of points in plot: 18
2013-10-17 14:24:01.535 app[9741:c07] New point added at index: 18
2013-10-17 14:24:01.547 app[9741:c07] Checking number of points in plot: 19
2013-10-17 14:24:01.548 app[9741:c07] Checking number of points in plot: 19
2013-10-17 14:24:01.549 app[9741:c07] Get new point.X at index: 18
2013-10-17 14:24:01.549 app[9741:c07] Checking number of points in plot: 19
2013-10-17 14:24:01.550 app[9741:c07] Get new point.Y at index: 18
2013-10-17 14:24:01.550 app[9741:c07] Checking number of points in plot: 19
2013-10-17 14:24:01.550 app[9741:c07] New point added at index: 19
2013-10-17 14:24:01.564 app[9741:c07] Checking number of points in plot: 20
2013-10-17 14:24:01.564 app[9741:c07] Checking number of points in plot: 20
2013-10-17 14:24:01.565 app[9741:c07] Get new point.X at index: 19
2013-10-17 14:24:01.565 app[9741:c07] Checking number of points in plot: 20
2013-10-17 14:24:01.566 app[9741:c07] Get new point.Y at index: 19
2013-10-17 14:24:01.566 app[9741:c07] Checking number of points in plot: 20
2013-10-17 14:24:01.567 app[9741:c07] New point added at index: 20
2013-10-17 14:24:01.580 app[9741:c07] Checking number of points in plot: 21
2013-10-17 14:24:01.581 app[9741:c07] Checking number of points in plot: 21
2013-10-17 14:24:01.581 app[9741:c07] Get new point.X at index: 20
2013-10-17 14:24:01.582 app[9741:c07] Checking number of points in plot: 21
2013-10-17 14:24:01.582 app[9741:c07] Get new point.Y at index: 20
2013-10-17 14:24:01.583 app[9741:c07] Checking number of points in plot: 21
2013-10-17 14:24:01.583 app[9741:c07] New point added at index: 21
2013-10-17 14:24:01.597 app[9741:c07] Checking number of points in plot: 22
2013-10-17 14:24:01.598 app[9741:c07] Checking number of points in plot: 22
2013-10-17 14:24:01.599 app[9741:c07] Get new point.X at index: 21
2013-10-17 14:24:01.600 app[9741:c07] Checking number of points in plot: 22
2013-10-17 14:24:01.600 app[9741:c07] Get new point.Y at index: 21
2013-10-17 14:24:01.600 app[9741:c07] Checking number of points in plot: 22
2013-10-17 14:24:01.601 app[9741:c07] New point added at index: 22
2013-10-17 14:24:01.614 app[9741:c07] Checking number of points in plot: 23
2013-10-17 14:24:01.615 app[9741:c07] Checking number of points in plot: 23
2013-10-17 14:24:01.615 app[9741:c07] Get new point.X at index: 22
2013-10-17 14:24:01.616 app[9741:c07] Checking number of points in plot: 23
2013-10-17 14:24:01.616 app[9741:c07] Get new point.Y at index: 22
2013-10-17 14:24:01.617 app[9741:c07] Checking number of points in plot: 23
2013-10-17 14:24:01.617 app[9741:c07] New point added at index: 23
2013-10-17 14:24:01.630 app[9741:c07] Checking number of points in plot: 24
2013-10-17 14:24:01.631 app[9741:c07] Checking number of points in plot: 24
2013-10-17 14:24:01.631 app[9741:c07] Get new point.X at index: 23
2013-10-17 14:24:01.632 app[9741:c07] Checking number of points in plot: 24
2013-10-17 14:24:01.632 app[9741:c07] Get new point.Y at index: 23
2013-10-17 14:24:01.633 app[9741:c07] Checking number of points in plot: 24
2013-10-17 14:24:01.633 app[9741:c07] New point added at index: 24
2013-10-17 14:24:01.647 app[9741:c07] Checking number of points in plot: 25
2013-10-17 14:24:01.648 app[9741:c07] Checking number of points in plot: 25
2013-10-17 14:24:01.648 app[9741:c07] Get new point.X at index: 24
2013-10-17 14:24:01.649 app[9741:c07] Checking number of points in plot: 25
2013-10-17 14:24:01.649 app[9741:c07] Get new point.Y at index: 24
2013-10-17 14:24:01.650 app[9741:c07] Checking number of points in plot: 25
2013-10-17 14:24:01.650 app[9741:c07] New point added at index: 25
2013-10-17 14:24:01.664 app[9741:c07] Checking number of points in plot: 26
2013-10-17 14:24:01.665 app[9741:c07] Checking number of points in plot: 26
2013-10-17 14:24:01.665 app[9741:c07] Get new point.X at index: 25
2013-10-17 14:24:01.666 app[9741:c07] Checking number of points in plot: 26
2013-10-17 14:24:01.666 app[9741:c07] Get new point.Y at index: 25
2013-10-17 14:24:01.667 app[9741:c07] Checking number of points in plot: 26
2013-10-17 14:24:01.667 app[9741:c07] New point added at index: 26
2013-10-17 14:24:01.680 app[9741:c07] Checking number of points in plot: 27
2013-10-17 14:24:01.768 app[9741:c07] Checking number of points in plot: 27
2013-10-17 14:24:01.769 app[9741:c07] Get new point.X at index: 26
2013-10-17 14:24:01.770 app[9741:c07] Checking number of points in plot: 27
2013-10-17 14:24:01.770 app[9741:c07] Get new point.Y at index: 26
2013-10-17 14:24:01.771 app[9741:c07] Checking number of points in plot: 27
2013-10-17 14:24:01.772 app[9741:c07] New point added at index: 27
4

1 回答 1

0

可能您需要在计时器中更新绘图空间 x-range/y-range

CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
[plotSpace scaleToFitPlots:[self.graph allPlots]];

CPTPlot *thePlot   = [self.graph plotWithIdentifier:kIdentifier];
[thePlot reloadData];
于 2014-12-08T16:34:05.197 回答