0

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 selector:@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];
}
Linus
  • 825
  • 4
  • 20
  • 33
  • What's the scope of myTimer1 and myTimer2? – Paul Ardeleanu May 03 '11 at 11:56
  • @Paul Ardeleanu myTimer1 and myTimer2 fetch the JSON values add to the respective array (for plotting), recalculate the core-plot Xrange and reloads the graph. (so the graphs is moving along as the data points come in).in fact i only need to run call this timer once and leave them runnning.but when i do the flip, only timer2 is being called again (randomly), graph1 and timer1 are just fine and there is only one instance of them running... so not sure how to go abt this.. – Linus May 03 '11 at 13:49
  • Linus, I think you should separate the retrieval process from the display. Moreover, you should run the data fetching on a separate thread (asynchronously). – Paul Ardeleanu May 04 '11 at 23:32
  • Also, you can use NSNotifications to let the main thread know that new data is in and the graph needs to be redraw. Don't use a timer to refresh the view - this should be refresh only in response to "refresh" requests. – Paul Ardeleanu May 04 '11 at 23:34

1 Answers1

0

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];
}
Linus
  • 825
  • 4
  • 20
  • 33
  • This looks like an update to your question, not an answer to it. This should have been added as an edit to your original question. – mttrb May 21 '12 at 05:31