Getting Chart Data

Description: getting data for building a chart from text file.

The DataSource.h File

#import <Foundation/Foundation.h>
#import "Charting/ChartDataSource.h"
@interface DataSource : NSObject <ChartDataSource> {
    NSArray *_timestampTitles;
    NSArray *_timestampArray;
    double _yMin;
    double _yMax;
    double _xMin;
    double _xMax;
    double _minValue;
    double _maxValue;
    double _secondaryYMin;
    double _secondaryYMax;
    double _accumulatedMin;
    double _accumulatedMax;
    double _accumulatedSecondaryMin;
    double _accumulatedSecondaryMax;
    int seriesCount;
    NSMutableArray *legendTitles;
    NSMutableArray *data;
    int _currTimestamp;
    BOOL m_isBubble;
}
- (id) init;
- (void ) loadData;
@property (assign) int countSeries;
- (void )obtainMin:(double *)min andMax:(double *)max fromArray:(NSArray *)arr
addingPositivesTo:(NSMutableArray *)positiveValues addingNegativesTo:(NSMutableArray *)negativeValues;
- (void )prepare;
@end

The DataSource.m File

#import "DataSource.h"
@implementation DataSource
@synthesize accumulatedMax = _accumulatedMax;
@synthesize accumulatedMin = _accumulatedMin;
@synthesize accumulatedSecondaryMax = _accumulatedSecondaryMax;
@synthesize accumulatedSecondaryMin = _accumulatedSecondaryMin;
@synthesize yMax = _yMax;
@synthesize xMax = _xMax;
@synthesize yMin = _yMin;
@synthesize xMin = _xMin;
@synthesize minValue = _minValue;
@synthesize maxValue = _maxValue;
@synthesize secondaryYMax = _secondaryYMax;
@synthesize secondaryYMin = _secondaryYMin;
@synthesize timestampArray = _timestampArray;
@synthesize timestampTitles = _timestampTitles;
@synthesize countSeries = seriesCount;
@synthesize currentTimestamp = _currentTimestamp;
- (id) init
{
    self = [super init];
    if(self)
    {
        _yMin = 0.0;
        _yMax = 0.0;
        _xMin = 0.0;
        _xMax = 0.0;
        _secondaryYMin = 0.0;
        _secondaryYMax = 0.0;
        _accumulatedMin = 0.0;
        _accumulatedMax = 0.0;
        _accumulatedSecondaryMin = 0.0;
        _accumulatedSecondaryMax = 0.0;
        legendTitles = [NSMutableArray new];
        m_isBubble = YES;
        // Determine data array
        data = [NSMutableArray new];
        [self loadData];
        seriesCount = [data count];
        return self;
    }
    return nil;
}
- (void )loadData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"data.txt" ofType:nil]; // Get path to data file
    NSString *dataString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; // Read contents
    NSArray *lines = [dataString componentsSeparatedByString:@"\n"]; // Split string with contents into substrings and insert into array
    NSMutableArray *table = [NSMutableArray new];
    for (NSString *line in lines) {
        [table addObject:[line componentsSeparatedByString:@";"]]; // Split each array element into substrings
    }
    NSMutableArray *timelineTitles = [[table objectAtIndex:0] mutableCopy];
    [timelineTitles removeObjectAtIndex:0];
    _timestampTitles = [[NSArray arrayWithArray:timelineTitles] retain];
    _timestampArray = [[NSArray arrayWithArray:timelineTitles] retain];
    [timelineTitles release];
    // Insert data by each country into array
    for (int i = 1; i < [table count]; i++) {
        NSArray *line = [table objectAtIndex:i];
        NSMutableArray *values = [[NSMutableArray new] autorelease];
        [legendTitles addObject:[line objectAtIndex:0]];
        for (int j = 1; j < [line count]; j++) {
            [values addObject:[NSNumber numberWithFloat:[[line objectAtIndex:j] floatValue]] ];
        }
        [data addObject:values];
    }
}

// Series data
- (NSArray *) seriesDataWithIndex:(int)seriesIndex
{
    NSString *num = [NSString stringWithFormat:@"%d",_currTimestamp];
    int pos = [_timestampArray indexOfObject:num];
    NSArray *series = [data objectAtIndex:seriesIndex];
    if(m_isBubble)
    return [NSArray arrayWithObjects:[series objectAtIndex:0],[series objectAtIndex:1],[series objectAtIndex:1],[series objectAtIndex:1], nil];
    else
    return series;
}

- (NSArray *)seriesDataWithIndex:(int)seriesIndex andTimestampValue:(int)timestamp
{
    NSLog(@"%d",timestamp);
    NSString *num = [NSString stringWithFormat:@"%d",_currTimestamp];
    int pos = [_timestampArray indexOfObject:num];
    NSArray *series = [data objectAtIndex:seriesIndex];
    int currTime = (timestamp-1) <0 ? 0: (timestamp - 1);
    if(m_isBubble)
    return [NSArray arrayWithObjects:[series objectAtIndex:currTime],[series objectAtIndex:currTime],[series objectAtIndex:currTime],[series objectAtIndex:currTime], nil];
    else
    return series;
}

-(NSArray *)seriesDataWithSizeWithIndex:(int)seriesIndex andTimestampValue:(int)timestamp
{
    NSString *num = [NSString stringWithFormat:@"%d",_currTimestamp];
    int pos = [_timestampArray indexOfObject:num];
    NSArray *series = [data objectAtIndex:seriesIndex];
    int currTime = (timestamp - 1) <0 ? 0: (timestamp - 1);
    if(m_isBubble)
    return [NSArray arrayWithObjects:[series objectAtIndex:currTime],[series objectAtIndex:currTime],[series objectAtIndex:currTime],[series objectAtIndex:currTime], nil];
    else
    return series;
}

// Determine series name displayed in legend
- (NSString *)seriesNameWithIndex:(int)seriesIndex
{
    return [legendTitles objectAtIndex:seriesIndex];
}
// Calculate value of maximum and minimum elements in data
- (void )obtainMin:(double *)min andMax:(double *)max fromArray:(NSArray *)arr addingPositivesTo:(NSMutableArray *)positiveValues addingNegativesTo:(NSMutableArray *)negativeValues
{
    if(([positiveValues count] == 0) || ([negativeValues count] == 0) )
    {
        [positiveValues removeAllObjects];
        [negativeValues removeAllObjects];
        for (int i=0; i < [arr count]; i++) {
            NSNumber *value = [arr objectAtIndex:i];
            if ([value floatValue] > 0) {
                [positiveValues addObject:[arr objectAtIndex:i]];
            }
            [positiveValues addObject:[NSNumber numberWithFloat:0.0f]];
        }
        for (int i=0; i < [arr count]; i++) {
            NSNumber *value = [arr objectAtIndex:i];
            if ([value floatValue] < 0) {
                [negativeValues addObject:[arr objectAtIndex:i]];
            }
            [negativeValues addObject:[NSNumber numberWithFloat:0.0f]];
        }
    }
    else
    {
        for (int i=0; i < [arr count]; i++)
        {
            NSNumber *value = [arr objectAtIndex:i];
            if ([value floatValue] > 0)
            {
                NSNumber *num = [positiveValues objectAtIndex:i];
                num = [NSNumber numberWithFloat:[num floatValue] + [value floatValue]];
                [positiveValues replaceObjectAtIndex:i withObject:num];
            }
            else
            {
                NSNumber *num = [negativeValues objectAtIndex:i];
                num = [NSNumber numberWithFloat:[num floatValue] + [value floatValue]];
                [negativeValues replaceObjectAtIndex:i withObject:num];
            }
        }
        float _min = *min;
        float _max = *max;
        for (int i=0; i < [arr count]; i++) {
            NSNumber *value = [arr objectAtIndex:i];
            if ([value floatValue] > _max)
            _max = [value floatValue];
            if ([value floatValue] < _min)
            _min = [value floatValue];
        }
        *max = _max;
        *min = _min;
    }
}
- (void )prepare {
    NSMutableArray *positive = [NSMutableArray new];
    NSMutableArray *negative = [NSMutableArray new];
    for(int i = 0; i < seriesCount ; i++)
    {
        [self obtainMin:&_yMin andMax:&_yMax fromArray:[self seriesDataWithIndex:i] addingPositivesTo:positive addingNegativesTo:negative];
    }
    for (int i=0; i < [positive count]; i++) {
        NSNumber *value = [positive objectAtIndex:i];
        if ([value floatValue] > _accumulatedMax ) {
            _accumulatedMax = [value floatValue];
            _accumulatedSecondaryMax = [value floatValue];
        }
    }
    for (int i=0; i < [negative count]; i++) {
        NSNumber *value = [negative objectAtIndex:i];
        if ([value floatValue] < _accumulatedMax ) {
            _accumulatedMin = [value floatValue];
            _accumulatedSecondaryMin = [value floatValue];
        }
    }
    _xMax = _yMax;
    _xMin = _yMin;
    _secondaryYMax = _yMax;
    _secondaryYMin = _yMin;
    _maxValue = _yMax;
    _minValue = _yMin;
    [positive release];
    [negative release];
}
- (NSDictionary *) availableSeriesHeaders
{
    return nil;
}
// Determine X axis label
- (NSString *)xAxisNameForMask:(NSString *)mask
{
    return @"";
}
// Determine Y axis label
- (NSString *)yAxisNameForMask:(NSString *)mask
{
    return @"";
}
// Determine secondary axis label
- (NSString *)secondaryAxisNameForMask:(NSString *)mask
{
    return @"Secondary axis";
}
- (double )xMax
{
    return _xMax;
}
- (double ) xMin
{
    return _xMin;
}
- (double ) yMax
{
    return _yMax;
}
- (double ) yMin
{
    return _yMin;
}
- (double )secondaryYMin {
    return _secondaryYMin;
}
- (double )secondaryYMax {
    return _secondaryYMax;
}
- (double )accumulatedSecondaryMin
{
    return _accumulatedSecondaryMin;
}
- (double )accumulatedSecondaryMax
{
    return _accumulatedSecondaryMax;
}
- (int)minTimestamp {
    return 0;
}
- (int)maxTimestamp {
    return 0;
}
- (void )setMaxTimestamp:(int)mt
{
}
- (void )setMinTimestamp:(int)mt
{
}
- (void)setStartDataIndexOffset:(int)startOffset andEndDataIndexOffset:(int)endOffset
{
}
@end

See also:

Creating a Bubble Chart