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 labels for timeline
NSMutableArray *m_timeLineDate; // Array of dates
}
// Loads data dfrom file with specified name
- (id) initWithFileName:(NSString *) name;
// Determines minimum and maximum data values
- (void) prepare;
@property (readonly,assign) double minValue; // Minimum data value
@property (readonly,assign) double maxValue; // Maximum data value
@property (atomic,assign) NSObject* objectId; // Identifier of map layer current area
@end
#import "MapDataSource.h"
@implementation MapDataSource
@synthesize minValue = m_minValue;
@synthesize maxValue = m_maxValue;
@synthesize objectId = m_objectId;
// Loads data from file with specified name
- (id)initWithFileName:(NSString *)name
{
self = [super init];
if (self) {
// Get file path
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 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;
}
// Returns name of used data cube
-(NSString *) cubeName {
return @"undefined";
}
// Sets index specifying value number in country data array
- (void) setIndex:(int)index
{
m_index = index;
}
// Returns maximum allowed index value
-(int)maxIndex {
NSArray *values = [[m_fictionalData allValues] objectAtIndex:0];
if (values != nil) {
return [values count];
} else {
return 0;
}
}
// Returns value for current map area layer
-(NSNumber *) value {
NSArray *values = [m_fictionalData objectForKey:[self objectId]];
if (values != nil && m_index <= [self maxIndex]) {
return [values objectAtIndex:m_index];
} else {
return [NSNumber numberWithInt:0];
}
}
// Returns data for specified country for specified year of their dimension
- (NSNumber *)valueWithId:(NSObject *)objectId forIndex:(int)index {
[self setObjectId:objectId];
[self setIndex:index];
return [self value];
}
// Returns data for specified map layer area for specified year of their dimension
- (NSNumber *)valueWithId:(NSNumber *)objectId {
return [self valueWithId:objectId forIndex:m_index];
}
// Returns data for map layer area 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: