Description: properties and methods for getting a chart cube data.
#import <Foundation/Foundation.h>
#import "ChartDataSource.h"
#include "PPLPivotTable.h"
#include "PPLDimension.h"
#include "NFoundation.h"
@interface DataSource : NSObject<ChartDataSource>
{
NSMutableArray *_timestampTitles;
NSMutableArray *_timestampArray;
NSMutableArray *_seriesKeys;
NSMutableArray *_seriesTitles;
double _yMin;
double _yMax;
double _xMin;
double _xMax;
double _secondaryYMin;
double _secondaryYMax;
double _accumulatedMin;
double _accumulatedMax;
double _accumulatedSecondaryMin;
double _accumulatedSecondaryMax;
NSMutableArray *data;
NSMutableDictionary *seriesByKeys;
SPPLPivotTable pivotTable;
}
- (id) init;
- (id) initWithPivotTable:(PPLPivotTable *) olap;
- (void) prepare;
@end
#import "DataSource.h"
#include "PPLDimensionElements.h"
#include "PPLDimensionElement.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 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 = [[NSMutableArray arrayWithArray:tmp] retain];
_timestampArray = [[NSMutableArray arrayWithArray:tmp] retain];
// Generate data array values
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;
}
-(id)initWithPivotTable:(PPLPivotTable *)olap {
if (self=[super init]) {
pivotTable = olap;
_seriesKeys = [NSMutableArray new];
_seriesTitles = [NSMutableArray new];
_timestampArray = [NSMutableArray new];
_timestampTitles = [NSMutableArray new];
seriesByKeys = [NSMutableDictionary new];
_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;
[self showData];
}
return self;
}
- (void)showData{
SNUIntArray selectedRowIndices = pivotTable->selectedRowIndices();
SNUIntArray selectedColumnIndices = pivotTable->selectedColumnIndices();
BOOL useSelection = !(selectedRowIndices->count() == 1 && selectedColumnIndices->count() == 1);
BOOL hasSelectedRows = selectedRowIndices->count() > 0 && useSelection;
BOOL hasSelectedColumns = selectedColumnIndices->count() > 0 && useSelection;
[_seriesKeys removeAllObjects];
[_seriesTitles removeAllObjects];
[_timestampArray removeAllObjects];
[_timestampTitles removeAllObjects];
BOOL hasData = pivotTable->rowCount() > 0 && pivotTable->columnCount();
if (hasData) {
SPPLPivotTableHeader leftTableHeader = pivotTable->leftHeader();
SPPLPivotTableHeader topTableHeader = pivotTable->topHeader();
SPPLDimension leftDimension = leftTableHeader->dimensions()->objectAtIndex<PPLDimension>(0);
SPPLDimension topDimension = topTableHeader->dimensions()->objectAtIndex<PPLDimension>(0);
BOOL timestampLoaded = NO;
for (uint r = 0; r < pivotTable->rowCount(); ++r) {
if (hasSelectedRows && !selectedRowIndices->containsObject(r)) {
continue;
}
SPPLPivotTableHeaderElement leftHeaderElement = leftTableHeader->elements()->getElementByIndex(r);
if (leftTableHeader->elements()->isElementInTotals(leftHeaderElement)) {
continue;
}
if (leftTableHeader->elements()->isElementCalculated(leftHeaderElement)) {
continue;
}
NSNumber *serieKey = nil;
if (leftHeaderElement->correspondingDimensionElements()->count() == 1) {
SPPLDimensionElement serieElement = leftHeaderElement->correspondingDimensionElements()->objectAtIndex<PPLDimensionElement>(0);
serieKey = [NSNumber numberWithLongLong:serieElement->key()];
}
else {
serieKey = [NSNumber numberWithUnsignedInt:r];
}
NSString *serieTitle = nil;
serieTitle = [self titleForHeaderElement:leftHeaderElement];
[_seriesKeys addObject:serieKey];
[_seriesTitles addObject:serieTitle];
for (uint c = 0; c < pivotTable->columnCount(); ++c) {
if (hasSelectedColumns && !selectedColumnIndices->containsObject(c)) {
continue;
}
SPPLPivotTableHeaderElement topHeaderElement = topTableHeader->elements()->getElementByIndex(c);
if (topTableHeader->elements()->isElementInTotals(topHeaderElement)) {
continue;
}
if (topTableHeader->elements()->isElementCalculated(topHeaderElement)) {
continue;
}
if (!timestampLoaded) {
NSString *timestampKey = [NSString stringWithFormat:@"%d", c];
NSString *timestampTitle = nil;
timestampTitle = [self titleForHeaderElement:topHeaderElement];
[_timestampArray addObject:timestampKey];
[_timestampTitles addObject:timestampTitle];
}
NSMutableArray *serieData = [seriesByKeys objectForKey:serieKey];
if (serieData == NULL) {
serieData = [NSMutableArray array];
[seriesByKeys setObject:serieData forKey:serieKey];
}
SNID dataVal = pivotTable->getData(r, c);
if (dataVal != NULL) {
[serieData addObject:(NSObject *)dataVal->nsObject()];
}
else {
[serieData addObject:[NSNull null]];
}
}
timestampLoaded = YES;
}
}
}
- (NSString *)titleForHeaderElement:(PPLPivotTableHeaderElement *)element {
SNArray dimensionElements = element->correspondingDimensionElements();
NSMutableString *title = [NSMutableString string];
BOOL isFirstTime = YES;
N_FOREACH(SPPLDimensionElement, dimensionElement, dimensionElements) {
[title appendFormat:(isFirstTime ? @"%@" : @"\n%@"), dimensionElement->name()->nsString()];
isFirstTime = NO;
}
return title;
}
// Determine series data
- (NSArray *) seriesDataWithIndex:(int)seriesIndex {
NSNumber *serieKey = [_seriesKeys objectAtIndex:seriesIndex];
return [seriesByKeys objectForKey:serieKey];
}
// Determine series name entered in legend
- (NSString *)seriesNameWithIndex:(int)seriesIndex {
return [_seriesTitles objectAtIndex:seriesIndex];
}
// Calculate maximum and minimum data values
- (void)prepare {
NSArray *keys = [seriesByKeys allKeys];
for(NSNumber *key in keys)
{
NSArray *serieData = [seriesByKeys objectForKey:key];
for (int j = 0; j<[serieData count]; j++)
{
NSNumber *num = [serieData objectAtIndex:j];
double value = ([num isKindOfClass:[NSNull class]]) ? 0 : [num doubleValue];
if (_yMax < value)
{
_yMax = value;
}
if (_yMin > value)
{
_yMin = value;
}
}
_xMax = serieData.count;
_xMin = 0;
}
}
// Determine series title
- (NSArray*) availableSeriesKeys {
return _timestampArray;
}
// Determine X axis label
- (NSString *)xAxisNameForMask:(NSString *)mask {
return NSLocalizedString(@"X_AXIS_CAPTION", nil);
}
// Determine Y axis label
- (NSString *)yAxisNameForMask:(NSString *)mask {
return NSLocalizedString(@"Y_AXIS_CAPTION", nil);
}
// Determine secondary axis label
- (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;
}
@end
See also: