【问题标题】:2 core-plot graphs on 2 seperate UIviews with dynamic data update2 个独立视图上的 2 个核心图,具有动态数据更新
【发布时间】:2011-05-03 10:23:48
【问题描述】:

嗨,我目前正在使用 core-plot 绘制动态图并且工作正常,现在我正在考虑在 2 个单独的视图上添加 2 个图

我已经开始使用 Mainview 绘图 Graph-1 和 Flipview 绘图 Graph-2 的“实用程序”

主视图图形工作正常,图形每 2 秒更新和重新加载一次,但是当我触摸“flipviewWindow”时,graph2 总是以新值和新的 graph2data 数组开始(可能是因为 graph2timer 再次被调用??? ) 但 graph1data 仅使用一个 NSTimer 维护并且工作正常。

我不想发布这些值,因为我想查看具有先前值的图形,例如 Graph1。

有人可以建议如何通过动态数据更新在 2 个单独的 UIview 上实现 2 个核心图吗? 我的图表逻辑如下

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];
}

【问题讨论】:

  • myTimer1和myTimer2的作用域是什么?
  • @Paul Ardeleanu myTimer1 和 myTimer2 获取添加到各自数组(用于绘图)的 JSON 值,重新计算 core-plot Xrange 并重新加载图表。 (所以图表随着数据点的进入而移动)。事实上我只需要运行一次调用这个计时器并让它们运行。但是当我做翻转时,只有 timer2 被再次(随机)调用,graph1 和timer1 很好,它们只有一个实例在运行……所以不知道该怎么做……
  • Linus,我认为您应该将检索过程与显示分开。此外,您应该在单独的线程(异步)上运行数据获取。
  • 另外,您可以使用 NSNotifications 让主线程知道有新数据进入,并且需要重新绘制图形。不要使用计时器刷新视图 - 这应该仅在响应“刷新”请求时刷新。

标签: nstimer core-plot flip nstimeinterval


【解决方案1】:

我已经优化了我的示例,通过将数据获取与主核心图分开并添加 NSNotification 以更新数据,我遇到了一些内存泄漏问题(http://stackoverflow.com/questions/5927476/yajl -内存泄漏问题)。

另外,我对控制器进行了以下更改,以便每次按下“显示 Grpah2”时它都不会重新加载整个图形。

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];
}

【讨论】:

  • 这看起来像是对您的问题的更新,而不是对它的回答。这应该作为对您原始问题的修改添加。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多