Loading chart data from file


#1

I am looking at the sample Accelerometer app from sample code suplied by Shinobicontrols and rather than use data coming in live from the accelerometer I want to load the valuse from a file in the format x, y, x, time, with one row in the file per reading.

I am trying this:

-(void)loadData {
    
    NSString *filePath = filePath = [[NSBundle mainBundle] pathForResource:@"Demonstration Data" ofType:@"csv"];
    
    // load the selected csv file
    NSString *csvData = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    
    // extract each csv row into array
    NSArray *csvRawData = [csvData componentsSeparatedByString:@"\n"];
    
    for(int i = 0; i < 4; i++) {
   
        _data[i] = [NSMutableArray new];
    }
    
    for (int x = 0; x < csvRawData.count; x++) {
        
        // get the entire row as string
        NSString *nextRowString = [NSString stringWithFormat:@"%@", csvRawData[x]];
        
        // extract out into array seperated by comma
        NSArray *row = [nextRowString componentsSeparatedByString:@","];
        
        for (int y = 0; y < 4; y++) {
            
            SChartDataPoint *pt = [SChartDataPoint new];
            
            if (y == 0) {
                
                pt.yValue = @([[row objectAtIndex:0] floatValue]); // x
            }
            else if (y == 1) {
                
                pt.yValue = @([[row objectAtIndex:1] floatValue]); // y
            }
            else if (y == 2) {
            
                pt.yValue = @([[row objectAtIndex:2] floatValue]); // z
            }
            
            pt.xValue = @([[row objectAtIndex:3] floatValue]);
            
            if (y == 3) {
                
                [_data[y] addObject:@([[row objectAtIndex:3] floatValue])]; // t
            }
            else {
          
                [_data[y] addObject:pt]; // x, y or z
            }
            
            [_chart appendNumberOfDataPoints:1 toEndOfSeriesAtIndex:y];
        }
    }
}

But I get a crash on first parse when the inner for loop y is at zero.  pt has correct values but this line courses a crash…

[_chart appendNumberOfDataPoints:1 toEndOfSeriesAtIndex:y];

This is what the console says:

*** Terminating app due to uncaught exception ‘NSRangeException’, reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array’

*** First throw call stack:

(

0   CoreFoundation                      0x01f481e4 __exceptionPreprocess + 180

1   libobjc.A.dylib                     0x01cc78e5 objc_exception_throw + 44

2   CoreFoundation                      0x01ee93f6 -[__NSArrayM objectAtIndex:] + 246

3   ShinobiTestTwo                      0x000b7333 -[ShinobiChart appendNumberOfDataPoints:toEndOfSeriesAtIndex:] + 195

4   ShinobiTestTwo                      0x00002d18 -[PIHViewController loadData] + 1912

5   ShinobiTestTwo                      0x00002437 -[PIHViewController viewDidLoad] + 1079

6   UIKit                               0x00aa633d -[UIViewController loadViewIfRequired] + 696

7   UIKit                               0x00aa65d9 -[UIViewController view] + 35

8   UIKit                               0x009c6267 -[UIWindow addRootViewControllerViewIfPossible] + 66

9   UIKit                               0x009c65ef -[UIWindow _setHidden:forced:] + 312

10  UIKit                               0x009c686b -[UIWindow _orderFrontWithoutMakingKey] + 49

11  UIKit                               0x009d13c8 -[UIWindow makeKeyAndVisible] + 65

12  UIKit                               0x00981bc0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 2097

13  UIKit                               0x00986667 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824

14  UIKit                               0x0099af92 -[UIApplication handleEvent:withNewEvent:] + 3517

15  UIKit                               0x0099b555 -[UIApplication sendEvent:] + 85

16  UIKit                               0x00988250 _UIApplicationHandleEvent + 683

17  GraphicsServices                    0x023f9f02 _PurpleEventCallback + 776

18  GraphicsServices                    0x023f9a0d PurpleEventCallback + 46

19  CoreFoundation                      0x01ec3ca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53

20  CoreFoundation                      0x01ec39db __CFRunLoopDoSource1 + 523

21  CoreFoundation                      0x01eee68c __CFRunLoopRun + 2156

22  CoreFoundation                      0x01eed9d3 CFRunLoopRunSpecific + 467

23  CoreFoundation                      0x01eed7eb CFRunLoopRunInMode + 123

24  UIKit                               0x00985d9c -[UIApplication _run] + 840

25  UIKit                               0x00987f9b UIApplicationMain + 1225

26  ShinobiTestTwo                      0x0000353d main + 141

27  libdyld.dylib                       0x02b9e701 start + 1

)

libc++abi.dylib: terminating with uncaught exception of type NSException

I’m just starting to learn the use of charts as part of an evaluation to see if it fits my needs

Any advice / help muchly appreciated

Thanks


#2

sorted  :grin:

After ages going through this I found the SChartDatasource methods were not getting called and via trail and erro found that in fact I did not need the line below as of cource the data is loaded in one hit file the CSV file … cool 

[_chart appendNumberOfDataPoints:1 toEndOfSeriesAtIndex:y];