Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
115
How do I update graph data in realtime?
posted

Background:

The graphs that I've added to my app plot data in realtime. Data is sent to the app over a BLE connection and I plot the data on the graph as it comes in. The graphs start off with no data in them. All of the graphs plot similar sets of data, just for different components, so if I can get one working they should all work.

A graph has two line series that plot readings from two different sensors. The are two y-axes on the graph, each plotting the reading from one of the sensors. The x-axis is a time series, it's values should correspond with when the readings come in over BLE.

Currently, I can't seem to get the graph to plot new points when I add a new data. If I pre-populate my data array with datapoints before i create the datasource helper object, those points are plotted on the graph correctly. Here is the relevant code for one of the line series:

@objcMembers
class DataModel: NSObject {
    var date: Date!
    var value: NSNumber!
    
    init(date: Date, value: NSNumber) {
        self.date = date
        self.value = value
    }
}

When data comes in for one of the plots, it's stored as a DataModel object and added to an array like:

var oilTempReadings: [DataModel] = []

These are the class types I'm using for my axes and data series:

var xAxisTemp: IGCategoryDateTimeXAxis!
    var yAxisTemp: IGNumericYAxis!
    var temperatureLineSeries: IGLineSeries!
    var temperatureDataSource: IGCategoryDateSeriesDataSourceHelper!

And this is how I'm setting up my datasource helper:

temperatureDataSource = IGCategoryDateSeriesDataSourceHelper.init(data: bleManager.coolantTempReadings, valuePath: "value", andDatePath: "date")
        temperatureLineSeries.yAxis = yAxisTemp
        temperatureLineSeries.xAxis = xAxisTemp
        temperatureLineSeries.dataSource = temperatureDataSource
         graph.add(xAxisTemp)
        graph.add(yAxisTemp)
        graph.addSeries(temperatureLineSeries)
        

If I have test data in the coolantTempReadings array at this point, they are immediately populated in the graph when I add the graph to the subview. However, when new data comes in and I try to insert it into the graph, it doesn't show up. This is how I'm trying to insert the new data:

graph.insertItem(at: bleManager.coolantTempReadings.count - 1, with: temperatureDataSource)
        

Any ideas why the graphs aren't updating with the new data?

Parents
  • 115
    Offline posted

    When I was first playing around with the graphing package, before reading up on how to update data in realtime, I was doing this each time a new data point came in (I was only plotting data values in a number array at that point, not DataModel objects):

            temperatureDataSource.values = bleManager.coolantTempReadings
            temperatureLineSeries.dataSource = temperatureDataSource
            doorDataSource.values = bleManager.coolantDoorReadings
            doorLineSeries.dataSource = doorDataSource
    graph.refresh()

    This worked well until the app had been running a while. New data points come in about every second, so this would slowly bog down the CPU and then eventually crash the app.

Reply Children