influxdata / influxdb-client-swift

InfluxDB (v2+) Client Library for Swift
https://influxdata.github.io/influxdb-client-swift/
MIT License
26 stars 8 forks source link

Include example how to access time stamp and value #61

Open timbms opened 1 year ago

timbms commented 1 year ago

It would be great if you could add an example on how to access time stamp and values most effectively. I came up with the following setup

var dataPoints = [DataPoint]()
do {

    try records?.forEach {
        if let time = $0.values["_time"] as? Date, let values = $0.values["_value"] as? Double {
            dataPoints.append(DataPoint(date: time, value: values))
        }
    }
} catch {
    print (error)
}

to fill DataPoint to be used in SwiftUI Charts.

struct DataPoint: Identifiable {
    var date: Date
    var value: Double
    var id = UUID()
}

Maybe this could be done more elegantly. It would great to have your thoughts.

srebhan commented 3 weeks ago

@timbms a PR adding such an example would be welcome!