Search code examples
objective-ccore-plotlinegraph

Core Plot - dynamic line graph - line drawing always starts from (0,0)


I'm using the following: Core Plot 2.2, Xcode 7.3.1, iOS 8.3, and Objective-C.

I'm currently able to take incoming voltage data from my Cypress BLE module, and store points of data in an NSMutableArray *voltageData. I use those points as values on my y-axis in the delegate method numberForPlot:field:recordIndex:. I have an NSTimer object that triggers a call to a method that captures the voltage data every 0.1 ms. Here is that method:

// kMaxDataPoints = 50;
// '_real' is the voltage data of type Float32

- (void)incomingData:(nonnull NSTimer *)theTimer {

    if (self.plotData.count >= kMaxDataPoints) {
        [self.plotData removeAllObjects];
        [self.plot deleteDataInIndexRange:NSMakeRange(0, kMaxDataPoints)];
        [self.voltageData removeAllObjects];
        self.currentIndex = -1;
    }

    self.currentIndex++;
    if (self.voltageData == nil) {
        self.voltageData = [[NSMutableArray alloc] init];
    }

    [self.voltageData addObject:[NSNumber numberWithFloat:_real]];
    [self.plotData addObject:@(_real)];
    [self.plot insertDataAtIndex:(self.plotData.count - 1) numberOfRecords:1];
}

My mutable arrays voltageData and plotData always have the correct values. Every time the graph reaches the end of the data points, it "restarts" at the correct voltage value but the data line is "attached" to another straight line coming from the origin (0,0). Imagine this straight line drawing out the data line...that's what it looks like.

And ideas as to what is happening? Just as a side note, I am not replacing any point with another point. I'm remove all points from the arrays, and clearing out the whole plot, before restarting again.


Solution

  • Call -reloadData on the plot instead of -deleteDataInIndexRange:. This makes sure that all of the old plot data is removed.