Search code examples
iosswifttimeios-charts

ios-chart value won't plot based on x and y axis Swift


I am implementing ios-chart in Swift 4. And this is my code.

for loopValue in details {
        if let datetime = loopValue["datetime"]?.rawString(), let tempValue: Double = loopValue["temperature"]?.double {
            if let haveDate = DateFormatter.defaultAPIDateFormatter().date(from: datetime) {
                print("(\(haveDate), \(Double(haveDate.timeIntervalSince1970))\nY: \(tempValue))")
                let timeInterVel = Double(haveDate.timeIntervalSince1970)
                xyValues.append(ChartDataEntry(x: timeInterVel, y: tempValue))
            }
        }
    }

Also tried with direct value that too not working

let xyValues: [ChartDataEntry] = [ChartDataEntry.init(x: 1511360172.0, y: 59.13), ChartDataEntry.init(x: 1511360112.0, y: 59.13), ChartDataEntry.init(x: 1511360051.0, y: 59.13), ChartDataEntry.init(x: 1511359991.0, y: 59.13), ChartDataEntry.init(x: 1511359931.0, y: 59.13), ChartDataEntry.init(x: 1511359871.0, y: 59.13), ChartDataEntry.init(x: 1511359810.0, y: 59.13), ChartDataEntry.init(x: 1511359750.0, y: 59.13), ChartDataEntry.init(x: 1511359690.0, y: 59.13)]

My x and y axis values sample log

(2017-11-22 14:16:12 +0000, 1511360172.0
Y: 59.13)
(2017-11-22 14:15:12 +0000, 1511360112.0
Y: 59.13)
(2017-11-22 14:14:11 +0000, 1511360051.0
Y: 59.13)
(2017-11-22 14:13:11 +0000, 1511359991.0
Y: 59.13)
(2017-11-22 14:12:11 +0000, 1511359931.0
Y: 59.13)
(2017-11-22 14:11:11 +0000, 1511359871.0
Y: 59.13)
(2017-11-22 14:10:10 +0000, 1511359810.0
Y: 59.13)
(2017-11-22 14:09:10 +0000, 1511359750.0
Y: 59.13)
(2017-11-22 14:08:10 +0000, 1511359690.0
Y: 59.13)
(2017-11-22 14:07:10 +0000, 1511359630.0
Y: 59.13)
(2017-11-22 14:06:09 +0000, 1511359569.0
Y: 59.13)
(2017-11-22 14:05:09 +0000, 1511359509.0
Y: 59.13)
(2017-11-22 14:04:09 +0000, 1511359449.0
Y: 59.13)

enter image description here

When I tried with (Same chart configuration) with example data which given in ios-chart that works!

let now = Date().timeIntervalSince1970
    let hourSeconds: TimeInterval = 60

    let from = now - (Double(100) / 2) * hourSeconds
    let to = now + (Double(100) / 2) * hourSeconds

    let values = stride(from: from, to: to, by: hourSeconds).map { (x) -> ChartDataEntry in
        let y = arc4random_uniform(range) + 50
        return ChartDataEntry(x: x, y: Double(y))
    }

If my chart configuration is worng means that (Example data) too won't work.

Don't know where is the problem.

enter image description here

My x axis value are right (I think), because I got x axis values.

UPDATE

Tapped randomly got a call out from chart enter image description here Same code with a different set of value works as expected.


Solution

  • After a day found an answer. It's because of date should be given in order/ascending(today to tomorrow) I am doing like tomorrow to today.