Description: properties and methods for getting data for map from file.
#import <MapCharting/MapChartDataSource.h> @interface MapDataSource : NSObject<MapChartDataSource> { int m_index; NSMutableDictionary *m_fictionalData; // Data dictionary NSMutableArray *m_timelineTitles; // Array of timeline labels NSMutableArray *m_timeLineDate; // Array of dates } // Loads data from file with specified name - (id) initWithFileName:(NSString *) name; // Determine minimum and maximum data values - (void) prepare; @property (readonly,assign) double minValue; // Minimum data value @property (readonly,assign) double maxValue; // Maximum data value @end
#import "MapDataSource.h" @implementation MapDataSource @synthesize minValue = m_minValue; @synthesize maxValue = m_maxValue; // Loads data from file with specified name - (id)initWithFileName:(NSString *)name { self = [super init]; if (self) { // Get path to file NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil]; // Read file contents NSString *dataString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; m_timeLineDate = [NSMutableArray new]; // Split string with file contents into substrings split with a line break character NSArray *lines = [dataString componentsSeparatedByString:@"\n"]; NSMutableArray *data = [NSMutableArray new]; for (NSString *line in lines) { // Split each string into substrings split with the character ";" [data addObject:[line componentsSeparatedByString:@";"]]; } m_timelineTitles = [[data objectAtIndex:0] mutableCopy]; [m_timelineTitles removeObjectAtIndex:0]; m_fictionalData = [NSMutableDictionary new]; // Insert data on each country to dictionary for (int i = 1; i < [data count]; i++) { NSArray *line = [data objectAtIndex:i]; NSString *key = (NSString*)[line objectAtIndex:0]; NSMutableArray *values = [NSMutableArray new]; for (int j = 1; j < [line count]; j++) { [values addObject:[NSNumber numberWithFloat:[[line objectAtIndex:j] floatValue]] ]; } [m_fictionalData setValue:values forKey:key]; } } return self; } // Determines minimum and maximum data values - (void) prepare { BOOL startPrepare = YES; for (NSArray *series in [m_fictionalData allValues]) { for (int i = 0; i < [series count]; i++) { if(startPrepare) { m_minValue = [[series objectAtIndex:i] doubleValue]; m_maxValue = [[series objectAtIndex:i] doubleValue]; startPrepare = NO; continue; } double val = [[series objectAtIndex:i] doubleValue]; if(m_minValue > val) m_minValue = val; if(m_maxValue < val) m_maxValue = val; } } } // Returns array of dates - (NSArray *) timestampArray { return m_timeLineDate; } // Returns array of timeline labels - (NSArray *) timestampTitles { return m_timelineTitles; } // Sets index specifying value number in country data array - (void) setIndex:(int)index { m_index = index; } // Returns data on the specified country for the specified period of their dimension - (NSNumber *)valueWithId:(NSObject *)objectId forIndex:(int)index { NSArray *values = [m_fictionalData objectForKey:objectId]; if (values != nil) { return [values objectAtIndex:index]; } else { return [NSNumber numberWithInt:0]; } } // Returns data on the specified country for the current year of their dimension - (NSNumber *)valueWithId:(NSNumber *)objectId { return [self valueWithId:objectId forIndex:m_index]; } // Returns data on the country for all dates of their dimension - (NSArray *)seriesWithId:(NSNumber *)objectId { NSMutableArray *series = [NSMutableArray new]; series = [m_fictionalData valueForKey:[NSString stringWithFormat:@"%@", objectId]]; return series; } @end
See also: