【问题标题】:Live Line chart in iOSiOS 中的实时折线图
【发布时间】:2015-07-28 12:23:08
【问题描述】:

我想在 iOS 中绘制折线图。我为此编写了以下代码。我正在使用PCLineChartView

- (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.
}

在上面的代码中,我已经使用了已经生成的数据集。但问题是数据是实时流。我想在折线图上显示实时流数据。请建议我如何进行。我是新手。谢谢。

【问题讨论】:

    标签: ios iphone ios7 ios5


    【解决方案1】:

    你可以像这样创建一个 NSTimer:

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

    然后根据你的间隔率,定时器会触发“targetMethod:”

    然后您可以在“targetMethod:”中更新您的折线图

    希望这会有所帮助!

    【讨论】: