Data Source

Description: properties and methods for getting a chart cube data.

The DataSource.h File

#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

The DataSource.mm File

#import &quot;DataSource.h&quot;
#include &quot;PPLDimensionElements.h&quot;
#include &quot;PPLDimensionElement.h&quot;
@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&lt;15;i++) {
            [tmp addObject:[NSString stringWithFormat:@&quot;%d&quot;,i]];
        }
        _timestampTitles = [[NSMutableArray arrayWithArray:tmp] retain];
        _timestampArray = [[NSMutableArray arrayWithArray:tmp] retain];
        // Generate data array values
        int val;
        for (int i=0; i&lt;4; i++)
        {
            NSMutableArray *line = [[NSMutableArray new] autorelease];
            for (int j=0;j&lt;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-&gt;selectedRowIndices();
    SNUIntArray selectedColumnIndices = pivotTable-&gt;selectedColumnIndices();
    BOOL useSelection = !(selectedRowIndices-&gt;count() == 1 &amp;&amp; selectedColumnIndices-&gt;count() == 1);
    BOOL hasSelectedRows = selectedRowIndices-&gt;count() &gt; 0 &amp;&amp; useSelection;
    BOOL hasSelectedColumns = selectedColumnIndices-&gt;count() &gt; 0 &amp;&amp; useSelection;
    
    [_seriesKeys removeAllObjects];
    [_seriesTitles removeAllObjects];
    [_timestampArray removeAllObjects];
    [_timestampTitles removeAllObjects];
    
    BOOL hasData = pivotTable-&gt;rowCount() &gt; 0 &amp;&amp; pivotTable-&gt;columnCount();
    
    if (hasData) {
        SPPLPivotTableHeader leftTableHeader = pivotTable-&gt;leftHeader();
        SPPLPivotTableHeader topTableHeader = pivotTable-&gt;topHeader();
        SPPLDimension leftDimension = leftTableHeader-&gt;dimensions()-&gt;objectAtIndex&lt;PPLDimension&gt;(0);
        SPPLDimension topDimension = topTableHeader-&gt;dimensions()-&gt;objectAtIndex&lt;PPLDimension&gt;(0);
        
        BOOL timestampLoaded = NO;
        
        for (uint r = 0; r &lt; pivotTable-&gt;rowCount(); ++r) {
            if (hasSelectedRows &amp;&amp; !selectedRowIndices-&gt;containsObject(r)) {
                continue;
            }
            
            SPPLPivotTableHeaderElement leftHeaderElement = leftTableHeader-&gt;elements()-&gt;getElementByIndex(r);
            if (leftTableHeader-&gt;elements()-&gt;isElementInTotals(leftHeaderElement)) {
                continue;
            }
            
            if (leftTableHeader-&gt;elements()-&gt;isElementCalculated(leftHeaderElement)) {
                continue;
            }
            
            NSNumber *serieKey = nil;
            if (leftHeaderElement-&gt;correspondingDimensionElements()-&gt;count() == 1) {
                SPPLDimensionElement serieElement = leftHeaderElement-&gt;correspondingDimensionElements()-&gt;objectAtIndex&lt;PPLDimensionElement&gt;(0);
                serieKey = [NSNumber numberWithLongLong:serieElement-&gt;key()];
            }
            else {
                serieKey = [NSNumber  numberWithUnsignedInt:r];
            }
            
            NSString *serieTitle = nil;
            
            serieTitle = [self titleForHeaderElement:leftHeaderElement];
            
            [_seriesKeys addObject:serieKey];
            [_seriesTitles addObject:serieTitle];
            
            for (uint c = 0; c &lt; pivotTable-&gt;columnCount(); ++c) {
                if (hasSelectedColumns &amp;&amp; !selectedColumnIndices-&gt;containsObject(c)) {
                    continue;
                }
                
                SPPLPivotTableHeaderElement topHeaderElement = topTableHeader-&gt;elements()-&gt;getElementByIndex(c);
                if (topTableHeader-&gt;elements()-&gt;isElementInTotals(topHeaderElement)) {
                    continue;
                }
                if (topTableHeader-&gt;elements()-&gt;isElementCalculated(topHeaderElement)) {
                    continue;
                }
                
                if (!timestampLoaded) {
                    NSString *timestampKey = [NSString stringWithFormat:@&quot;%d&quot;, 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-&gt;getData(r, c);
                if (dataVal != NULL) {
                    [serieData addObject:(NSObject *)dataVal-&gt;nsObject()];
                }
                else {
                    [serieData addObject:[NSNull null]];
                }
            }
            
            timestampLoaded = YES;
        }
    }
}
- (NSString *)titleForHeaderElement:(PPLPivotTableHeaderElement *)element {
    SNArray dimensionElements = element-&gt;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:

Data Analysis