2015-07-28 78 views
0

我想在iOS中繪製折線圖。爲此我編碼如下。我正在使用PCLineChartViewiOS中的實時折線圖

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // Creating a LineChart View 

    PCLineChartView *lineChartView = [[PCLineChartView alloc]initWithFrame:CGRectMake(10, 10, [self.view bounds].size.width-20, [self.view bounds].size.height-20)]; 
    [lineChartView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; 
    lineChartView.minValue = 0; 
    lineChartView.maxValue = 100; 
    [self.view addSubview:lineChartView]; 

    //Associating Data with Line Chart 

    NSMutableArray *components = [NSMutableArray array]; 
    NSMutableDictionary *result = [self getGraphData]; 
    NSMutableArray *data = [result valueForKey:@"data"]; 

    for (NSDictionary *graphInfo in data) { 
     PCLineChartViewComponent *component = [[PCLineChartViewComponent alloc]init]; 
     [component setTitle:@"title"]; 
     [component setPoints:graphInfo [@"data"]]; 
     [component setShouldLabelValues:NO]; 
     [component setColour:PCColorRed]; 
     [components addObject:component]; 
    } 

    [lineChartView setComponents:components]; 
    [lineChartView setXLabels:[result valueForKey:@"xLabels"]]; 

    // Do any additional setup after loading the view, typically from a nib. 
} 

在上面的代碼中,我採用了已經生成的數據集。但問題是數據是一個實時流。我想在折線圖上顯示實時流數據。請告訴我如何繼續。我是一個新手。謝謝。

回答

0

您可以像創建一個NSTimer:

[NSTimer scheduledTimerWithTimeInterval:2.0 
    target:self 
    selector:@selector(targetMethod:) 
    userInfo:nil 
    repeats:YES]; 

然後根據您的區間的速率,計時器會觸發「targetMethod:」

然後,您可以在您更新線路圖「targetMethod:」

希望這會有所幫助!