4

我想通过从 json api 获取值来制作图表。我已经在 requestFinished 方法中定义了它,但我想在 setDataCount 方法中调用这些值。

我在 requestFinished 方法中将模型定义为:

    for (NSMutableArray *dictionary in jsonDictionary)
    {
        Model *model = [[Model alloc]init];

        model.cid = [[dictionary valueForKey:@"cid"]intValue];
        model.iid = [[dictionary valueForKey:@"iid"]intValue];
        model.yr = [[dictionary valueForKey:@"yr"]intValue];
        model.val = [dictionary valueForKey:@"val"];

        [head addObject:[NSString stringWithFormat:@"%ld", model.yr]];
        [left addObject:[NSString stringWithFormat:@"%ld", model.iid]];
        [mainTableData addObject:model];

    }

    NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:head];
    headData = [[orderedSet array] mutableCopy];

    [headData sortUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2) {
        return [str1 compare:str2 options:(NSNumericSearch)];
    }];

    NSOrderedSet *orderedSet1 = [NSOrderedSet orderedSetWithArray:left];
    arrLeft = [[orderedSet1 array] mutableCopy];

    [leftTableData addObject:arrLeft];

现在我必须用 json 中的值 'val' 绘制点。我已经尝试了以下代码,我在其中随机生成数字并绘制它。取而代之的是,我怎样才能获得 val?

代码如下:

- (void)setDataCount:(int)count range:(float)range
{
    NSMutableArray *xVals = [[NSMutableArray alloc] init];

    for (int i = 0; i < headData.count; i++)
    {
        [xVals addObject:[NSString stringWithFormat:@"%@", headData[i]]];
    }

    NSMutableArray *yVals = [[NSMutableArray alloc] init];

    for (int i = 0; i < headData.count; i++)
    {
        float mult = (range + 1);
        float val = (float) (arc4random_uniform(mult)) + 3;
        [yVals addObject:[[ChartDataEntry alloc] initWithValue:val xIndex:i]];
}

代替 yVals 我想做这样的事情:

for (int i = 0; i < arrLeft.count; i++)
{
    array = [[NSMutableArray alloc] init];
    for (int j = 0; j < headData.count; j++)
    {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.iid == %ld AND SELF.yr == %ld", [[arrLeft objectAtIndex:i] intValue], [[headData objectAtIndex:j] intValue]];
        NSArray *filteredArray = [mainTableData filteredArrayUsingPredicate:predicate];

        if([filteredArray count]>0)
        {
            Model *model = [filteredArray objectAtIndex:0];

            [array addObject:model.val];
        }
        else
            [array addObject:@"-"];
    }

    [yVals addObject:array];
}

[rightTableData addObject:yVals];

但是我无法从 setDataCount 方法中的 requestFinished 方法中获取值。

我还想在一个视图中显示一个 iid 的图表。我该怎么做?

我正在使用 iOS 图表库。请帮忙。

4

1 回答 1

3

我更改了 for 循环以获取 yVals 作为

for (int i = 0; i < headData.count; i++)
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.iid == %ld AND SELF.yr == %ld", /*10653*/[[arrLeft objectAtIndex:0]intValue], [[headData objectAtIndex:i] intValue]];
    NSArray *filteredArray = [mainTableData filteredArrayUsingPredicate:predicate];

    if([filteredArray count] > 0)
    {
        Model *model = [filteredArray objectAtIndex:0];
        [array addObject:model.val];
        float val = [model.val floatValue];
        [yVals addObject:[[ChartDataEntry alloc]initWithValue:val xIndex:i]];
    }
}

现在它完美地工作了。

于 2015-04-27T04:49:01.837 回答