I am trying to draw line chart in my project using SwiftChart pod. I am getting response from API as ["activity_count":0 , "date": "2020-10-31"] but graph works on double values so I don't know How I draw the chart.
import UIKit
import SwiftChart
class AnalyticsViewController: UIViewController {
@IBOutlet weak var graphContainer: UIView!
var graphData = [(x:0, y: 0.0)]
var graphpResponseData = [[String: Any]]()
override func viewDidLoad() {
super.viewDidLoad()
graphpResponseData.append(["activity_count":0 , "date": "2020-10-31"])
graphpResponseData.append(["activity_count":2 , "date": "2020-10-30"])
graphpResponseData.append(["activity_count":1 , "date": "2020-10-29"])
graphpResponseData.append(["activity_count":29 , "date": "2020-10-28"])
graphpResponseData.append(["activity_count":1 , "date": "2020-10-27"])
graphpResponseData.append(["activity_count":0 , "date": "2020-10-26"])
graphpResponseData.append(["activity_count":0 , "date": "2020-10-25"])
graphpResponseData.append(["activity_count":0 , "date": "2020-10-24"])
graphpResponseData.append(["activity_count":0 , "date": "2020-10-23"])
graphpResponseData.append(["activity_count":0 , "date": "2020-10-22"])
???? unable to proceed
//let chart = Chart(frame: CGRect(x: 0, y: 0, width: 300, height: 100))
//let series = ChartSeries(data: graphData)
//chart.add(series)
//chart.xLabels = [1, 2, 3, 4]
//chart.xLabelsFormatter = { String(Int(round($1))) + "Oct" }
//graphContainer.addSubview(chart)
}
}

I just had to solve this issue. I was using the day of the month of each date in memory for the x-axis, but it turned out that this would cause the graph to act unexpectedly and show randomly plotted points when the month changed.
I also tried using timestamps for x (of type
Double, which would be accepted by the ChartSeries's data initializer, but this failed too.The solution I found was to use an increment of one for each point on the x-axis, and using an array of
[Date]whose indices corresponded with the order of the datapoints in your chart'sdataproperty (for the sake of example, call thiscorrespondingDates). Inside ofchart.xLabelsFormatter, I'd access this array usingcorrespondingDates[value]. From there, I used two dateFormatter extensions to access the values.Here's the code:
Extensions for DateFormatter: