Description: creating a data source for line chart.
#import <Charting/ChartDataSource.h> @interface DataSource : NSObject<ChartDataSource> { NSArray *_timestampTitles; NSArray *_timestampArray; double _yMin; double _yMax; double _xMin; double _xMax; double _secondaryYMin; double _secondaryYMax; double _accumulatedMin; double _accumulatedMax; double _accumulatedSecondaryMin; double _accumulatedSecondaryMax; NSMutableArray *data; } - (id) init; - (void) prepare; @end
#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 availableSeriesKeys = _availableSerKey; @synthesize secondaryYMax = _secondaryYMax; @synthesize secondaryYMin = _secondaryYMin; @synthesize timestampArray = _timestampArray; @synthesize timestampTitles = _timestampTitles; - (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; // Determine data array data = [NSMutableArray new]; // Determine array with X axis labels NSMutableArray *tmp = [NSMutableArray new]; // Generate labels for(int i=0;i < 15;i++) { [tmp addObject:[NSString stringWithFormat:@"%d",i]]; } _timestampTitles = [[NSArray arrayWithArray:tmp] retain]; _timestampArray = [[NSArray arrayWithArray:tmp] retain]; // Generate values for data array int val; for (int i=0; i < 4; i++) { NSMutableArray *line = [[NSMutableArray new] autorelease]; for (int j=0;j < 15;j++) { val = sinf(M_PI * j * 45 / 180)*5*(i+1)+i; [line addObject:[NSNumber numberWithInt:val]]; } [data addObject:line]; } return self; } return nil; } // Determine series data - (NSArray *) seriesDataWithIndex:(int)seriesIndex { return [data objectAtIndex:seriesIndex]; } // Determine series name displayed in legend - (NSString *)seriesNameWithIndex:(int)seriesIndex { return [NSString stringWithFormat:@"a%d",seriesIndex]; } // Calculate maximum and minimum data values - (void)prepare { for (int i=0; i < data.count; i++) { for (int j = 0; j < [[data objectAtIndex:0] count]; j++) { double value = [[[data objectAtIndex:i] objectAtIndex:j] doubleValue]; if (_yMax < value) { _yMax = value; } if (_yMin > value) { _yMin = value; } } _xMin = 0; _xMax = [[data objectAtIndex:0] count]; } } // Determine series headers - (NSDictionary *) availableSeriesHeaders { return nil; } // Determine label for X axis - (NSString *)xAxisNameForMask:(NSString *)mask { return NSLocalizedString(@"X_AXIS_CAPTION", nil); } // Determine label for Y axis - (NSString *)yAxisNameForMask:(NSString *)mask { return NSLocalizedString(@"Y_AXIS_CAPTION", nil); } // Determine label for secondary axis - (NSString *)secondaryAxisNameForMask:(NSString *)mask { return @""; } // Determine minimum and maximum values for all axes - (double)xMax { return _xMax; } - (double) xMin { return _xMin; } - (double) yMax { return _yMax; } - (double) yMin { return _yMin; } - (double)secondaryYMin { return 0.0; } - (double)secondaryYMax { return 0.0; } - (double)accumulatedSecondaryMin { return 0.0; } - (double)accumulatedSecondaryMax { return 0.0; } - (int)minTimestamp { return 0; } - (int)maxTimestamp { return 0; } - (void)setMaxTimestamp:(int)mt{} - (void)setMinTimestamp:(int)mt{} - (void)setStartDataIndexOffset:(int)startOffset andEndDataIndexOffset:(int)endOffset{} -(Brush *)brushForSeries:(int)seriesIndex forTimestamp:(int)ts { return nil; } - (NSArray *) availableSeriesKeys { return data; } // Determine whether data series is empty - (BOOL)isEmptySerieWithIndex:(int)index { BOOL empty = YES; NSArray *seriesData = [self seriesDataWithIndex:index]; for(NSObject *value in seriesData) { if ([value isKindOfClass:[NSNumber class]]) { empty = NO; break; } } return empty; } // Determine whether data series is empty - (BOOL)isEmptySerieWithIndex:(int)index { BOOL empty = YES; NSArray *seriesData = [self seriesDataWithIndex:index]; for(NSObject *value in seriesData) { if ([value isKindOfClass:[NSNumber class]]) { empty = NO; break; } } return empty; } @end
See also: