I am a newcomer to the ios app, writing a simple app that shows the api json data. The problem I am currently having is that I want to display the json data in the view, using the line chart to render
First of all, this is my json data.
{
TH_5min: [
{
Data: "2019-02-23T00:00:00",
Time: "11:00:00",
XTP_A: 10.5, //temperature 1
XHP_A: 11.5, //humidity 1
XTP_B: 33.5,
XHP_B: 44.6,
XTP_C: 88.9,
XHP_C: 66.6,
XTP_D: 77.9,
XHP_D: 99.6,
XTP_E: 87.87,
XHP_E: 66.66
},
{
Data: "2019-02-23T00:00:00",
Time: "11:05:00",
XTP_A: 55.2, //temperature 1
XHP_A: 44.3, //humidity 1
XTP_B: 66.6,
XHP_B: 77.87,
XTP_C: 87.77,
XHP_C: 87.87,
XTP_D: 8.87,
XHP_D: 78.78,
XTP_E: 87.78,
XHP_E: 87.87
}
]
}
This is the format I read json data and stored, using swift code
private var th_5mins = [Th_5min]()
@objc func getlatestTh_5min(){
guard let th_5minUrl = URL(string: kivaLoanURl) else{
return
}
let request = URLRequest(url: th_5minUrl)
let task = URLSession.shared.dataTask(with: request, completionHandler: {(data,response,error) -> Void in
if let error = error {
print(error)
return
}
if let data = data {
self.th_5mins = self.pardrJsonData(data: data)
}
})
task.resume()
}
func pardrJsonData(data: Data) -> [Th_5min]{
var th_5mins = [Th_5min]()
do {
let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
let jsonTh_5mins = jsonResult?["TH_5min"] as! [AnyObject]
for jsonTh_5min in jsonTh_5mins{
var th_5min = Th_5min()
th_5min.Data = jsonTh_5min["Data"] as! String
th_5min.Time = jsonTh_5min["Time"] as! String
th_5min.XTP_A = jsonTh_5min["XTP_A"] as! Double
th_5min.XHP_A = jsonTh_5min["XHP_A"] as! Double
th_5mins.append(th_5min)
} }catch{
print(error)
}
return th_5mins
}
}
Here is my current implementation of the line chart method, using swift code
override func viewDidLoad() {
super.viewDidLoad()
getlatestTh_5min()
//
chartView = LineChartView()
chartView.frame = CGRect(x: 20, y: 80, width: self.view.bounds.width - 40,height: 300)
self.view.addSubview(chartView)
//Generate data for the first line chart
var dataEntries1 = [ChartDataEntry]()
for i in 0...5 {
let y = Th_5min().XTP_A
let entry = ChartDataEntry.init(x: Double(i), y: Double(y))
dataEntries1.append(entry)
}
let chartDataSet1 = LineChartDataSet(entries: dataEntries1, label: "溫度")
//Generate data for the second line chart
var dataEntries2 = [ChartDataEntry]()
for i in 0..<8 {
let y = arc4random()%100
let entry = ChartDataEntry.init(x: Double(i), y: Double(y))
dataEntries2.append(entry)
}
let chartDataSet2 = LineChartDataSet(entries: dataEntries2, label: "濕度")
//
let chartData = LineChartData(dataSets: [chartDataSet1, chartDataSet2])
//show line chart data
chartView.data = chartData
}
The current result is only the second line successfully displayed, but it is not the data I want. I only have a random value to display the data. The first one, no matter how you try, the json data is never lost in the chart. I want to implement it. All the data captured by the api (temperature 1, humidity 1) are thrown into the linechart, one shows the temperature and one shows the humidity, and the json's time is displayed in the upper X-axis part, but how to throw the json to the chart at present I don't know what to do, please help me!