Search code examples
jsonios8xcode6.3ios-charts

How to plot points in iOS-chart by taking values from a different method


I want to make a graph by taking values from json api. I have defined it in requestFinished method but I want to call the values in setDataCount method.

I have defined the model in requestFinished method as:

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

Now I have to plot the points with values 'val' from json. I have tried the following code where I am randomly generating numbers and plotting it. Instead of that how can I get val?

Code is as follows:

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

In place of yVals I want to do something like this:

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

But I can't get values from requestFinished method in setDataCount method.

I also want to display graph for one iid in one view. How can I do it?

I am using iOS- charts library. Please help.


Solution

  • I changed the for loop for getting yVals as

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

    Now it works perfectly.