开发者

2 core-plot graphs on 2 seperate UIviews with dynamic data update

HI, I am currently using core-plot to draw a dynamic graph and is working fine,now i am thinking to add 2 graph on 2 seperate views

i have started with the "Utility Application" with Mainview drawing Graph-1 and Flipview drawing Graph-2

The main view graph works fine and graph updates and reloads every 2 seconds, but when i touch "flipviewWindow" the graph2 always starts with new values and with a new graph2data array (probably because of the graph2timer being called again???) but graph1data maintains with just one NSTimer and works fine.

i dont want to release the values because i want to see the graph with the previous values like in Graph1.

Can some one advise on how to implement 2 core-plot graphs on 2 seperate UIviews with dynamic data update? my graph logic as below

In MainViewController.m
    -(void)viewDidLoad{
    [super viewDidLoad];
    [self ConstructGraph1]
}

-(void)ConstructGraph1 {
        graph1 = [[CPXYGraph alloc] initWithFrame:CGRectZero];
    CPTheme *theme = [CPTheme themeNamed:kCPStocksTheme];
    [graph1 applyTheme:theme];
    mainViewWindow.hostedGraph=graph1;
    .
    .
    .
    .
    .
       myTimer1=[NSTimer scheduledTimerWithTimeInterval:2 target:self sel开发者_StackOverflowector:@selector(graph1Timer:) userInfo:nil repeats:YES];
}

-(void) graph1Timer: (NSTimer *) Timer{
  NSTimeInterval aTimeInterval1 = [[NSDate date]
                                    timeIntervalSinceReferenceDate];
    Get JSON request and add object at every 2 sec, after succesfully fetching
    [graph1data addObject:...]
    [graph1 reloadData];
}

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)showFlipWindow:(id)sender
{    
    FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
    controller.delegate = self;
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];
    [controller release];
}

In FlipViewController.m
-(void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor]; 
    [self ConstructGraph2]

}
//ConstructGraph2 uses the same logic to draw the graph and to fetch the results

-(void) ConstructGraph2 { 
        graph2 = [[CPXYGraph alloc] initWithFrame:CGRectZero];
    CPTheme *theme = [CPTheme themeNamed:kCPStocksTheme];
    [graph1 applyTheme:theme];
    mainViewWindow.hostedGraph=graph1;
    .
    .
    .
    .
    .
       myTimer2=[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(graph2Timer:) userInfo:nil repeats:YES];
}

-(void) graph2Timer: (NSTimer *) Timer{
  NSTimeInterval aTimeInterval2 = [[NSDate date]
                                    timeIntervalSinceReferenceDate];
    Get JSON request and add object at every 2 sec, after succesfully fetching

    [graph2data addObject...];

    [graph2 reloadData];
}

- (IBAction)showMainWindow:(id)sender
{
    [self.delegate flipsideViewControllerDidFinish:self];
}


I have optimised my example by separating the data fetching with the main core plot and added NSNotification for updated in the data, i am having some issues with the memory leak (http://stackoverflow.com/questions/5927476/yajl-memory-leak-problem).

Also, i have the below changes to the controller so that it doesnt reload the whole graph everytime "Show Grpah2" is pressed.

if(controller==nil){
    controller=[[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil];
    controller.delegate = self;
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];
}
else{
    controller.delegate = self;
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜