Reputation: 633
CorePlot: how to i know that the Graphic finish draw? anything like ViewDidLoad method?
Because i have a loading view at the front when it is loading content.
I am using AAPLOT from CorePlot Example code.
I thought the following function is tell me that it finish the graphic, but it is not.
-(void)dataPullerDidFinishFetch:(APYahooDataPuller *)dp;
is there any functuon like ViewDidLoad that can let me know when it has successful finish the drawing??
thank you
Upvotes: 4
Views: 777
Reputation: 13147
To build this functionality, I added some code to my implementation of numberForPlot:field:recordIndex:
to check to see whether the record index was equal to "the number of records in my source data minus one" (because the plot data source uses zero-based numbering).
Because CorePlot renders the bars on my bar chart sequentially, so I can be confident that when it gets to the bar number equal to the number of records in my data source minus one, it's on the last record (this may be a more heavily nuanced situation if you are drawing more than one plot in your graph).
At this point, I simply create an iOS notification named finishedDrawingGraph
in the numberForPlot:field:recordIndex:
method:
// If this is the last bar in the chart for this layer, send notification to
// say that the graph has finished drawing
if (index == [self.dataController.builds count]-1
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"finishedDrawingGraph" object:nil];
}
I have a listener in my view controller to listen for finishedDrawingGraph
events. As the notification system is relatively slow (compared with calling a method directly), by the time this notification is received, the graph has almost certainly finished drawing. To add more certainty, I could wait for a period of time after the notification is received.
- (void)eventHandler: (NSNotification *) notification
{
if ([notification.name isEqualToString:@"finishedDrawingGraph"])
{
NSLog2 (@"Graph drawn.");
// Do something with your graph.
// For example, create an image of the graph
UIImage *graphImage = [graph imageOfLayer];
}
}
This isn't a particularly elegant solution, and it may break under some circumstances (system under very heavy load, update to CorePlot), but it is more than adequate for my current needs.
Upvotes: 2
Reputation: 27381
Core Animation is responsible for all drawing in Core Plot. A complete Core Plot graph has many layers; each one is drawn independently. There is no defined drawing order, nor does Core Animation provide any call backs when drawing is complete.
You can put up the loading view until your datasource has been queried for all of the data. If loading the data takes a long time, you shouldn't block Core Plot waiting for it to come in. Cache the data internally and call -reloadData
on the graph (or a single plot) when it's ready. Just show your loading view while waiting for the data to come in. Once you tell Core Plot to load it, remove the loading view.
Upvotes: 3