I have a tabbed application. One of the tabs contains a scattergraph. I want to animate drawing the plot on every viewDidAppear call. To do this I borrowed an idea of adding new data points from Core Plot RealTime example.
Basically I did the same, my code is below. As you can see from logs points are added at each timer call and value for the new point is fetched by the data source delegate. The thing is the plot is not refreshing at all. It stays blank and no points are drawn (axes are ok).
I did some research and some people reported the same issue here http://code.google.com/p/core-plot/issues/detail?id=445, here https://groups.google.com/forum/#!topic/coreplot-discuss/3uVUQI8v-5E and here Core Plot - RealTime plot does not automatically refresh.
Adding [graph reloadData] after insertDataAtIndex as suggested in last link did nothing besides fetching values not only for the new one, but for all points. I have no idea what is wrong. Core Plot Real Time example does not invoke [graph reloadData] anywhere.
My code:
GraphController.h
#import <UIKit/UIKit.h>
#import <sqlite3.h>
#import "ScatterPlot.h"
@interface GraphViewController : GAITrackedViewController{
IBOutlet CPTGraphHostingView *_graphHostingView;
}
@property (nonatomic, retain) ScatterPlot *scatterPlot;
@end
GraphController.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];
}
ScatterPlot.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
ScatterPlot.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];
}
Logs:
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
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plotI have commented//[self.graphData count];I can remove timer and after rerun plot immediatelly shows up. I have noticed that switching to another tab and returning to graph tab during first seconds works like refreshing. After switching several times plot is eventually displayed but...it's not what I was expecting. - eltherion