简体   繁体   中英

Swift - Show data in chartview

I am showing my response data in bar chart but the issue is that I am getting response properly and in graph if I pass static data then it works but if I pass my response value then it is not showing. Here is my code

Code::

let usersubcategory = ["user_id": 77 ,"access_token": "f7abee7bffa89898755174dbc6548bd2","account_id": "Mike50430315","year": 2018] as [String : Any]

print(usersubcategory)
Alamofire.request(CallAPI, method: .post, parameters: usersubcategory).responseJSON
    {
        response in

        print(response)
        let result = response.result

        if let dict = result.value as? Dictionary<String,AnyObject>{
            if let categorylist = dict["data"]{

                self.searchlist = categorylist as! [AnyObject]


                 for item in self.searchlist{
                 let value = item["month"]
                 guard let value_chart = value else {
                 continue
                 }
                 let optionalvalue = value_chart
                 if let noLongerOptional = optionalvalue {
                 print("\(noLongerOptional)")
                    let chartConfig = BarsChartConfig(valsAxisConfig: ChartAxisConfig(from: 0, to: 800, by: 100))
                    let frame = CGRect(x: 0, y: 270, width: self.view.frame.width, height: 450)
                    let chart = BarsChart(frame: frame,
                    chartConfig: chartConfig,
                    xTitle: "Months",
                    yTitle: "Count",
                    bars: [
                    ("Jan", noLongerOptional as! Double),
                    ("Feb", noLongerOptional as! Double),
                    ("Mar", noLongerOptional as! Double),
                    ("Apr", noLongerOptional as! Double),
                    ("May", noLongerOptional as! Double),
                    ("Jun", noLongerOptional as! Double),
                    ("July",noLongerOptional as! Double),
                    ("Aug", noLongerOptional as! Double),
                    ("Sep", noLongerOptional as! Double),
                    ("Oct", noLongerOptional as! Double),
                    ("Nov", noLongerOptional as! Double),
                    ("Dec", noLongerOptional as! Double)
                        ],
                                          color: UIColor.darkGray,
                                          barWidth: 15
                    )
                    self.view.addSubview(chart.view)
                    self.chartView = chart
                 }
              }

            }
        }

}

I am unwrapping the value and then passing in charts but when I print no noLongerOptional then I am getting data but not able to pass in graph.

Static value is showing correctly in graph. Can any one please help me?

Alamofire returns the network response on the networking thread it creates rather than the main thread. You need to dispatch your UIView operations onto the main thread

print(usersubcategory)
Alamofire.request(CallAPI, method: .post, parameters: usersubcategory).responseJSON
    {
        response in

        print(response)
        let result = response.result

        DispatchQueue.main.async {
          if let dict = result.value as? Dictionary<String,AnyObject>{
            if let categorylist = dict["data"]{

                self.searchlist = categorylist as! [AnyObject]


                 for item in self.searchlist{
                 let value = item["month"]
                 guard let value_chart = value else {
                 continue
                 }
                 let optionalvalue = value_chart
                 if let noLongerOptional = optionalvalue {
                 print("\(noLongerOptional)")
                    let chartConfig = BarsChartConfig(valsAxisConfig: ChartAxisConfig(from: 0, to: 800, by: 100))
                    let frame = CGRect(x: 0, y: 270, width: self.view.frame.width, height: 450)
                    let chart = BarsChart(frame: frame,
                    chartConfig: chartConfig,
                    xTitle: "Months",
                    yTitle: "Count",
                    bars: [
                    ("Jan", noLongerOptional as! Double),
                    ("Feb", noLongerOptional as! Double),
                    ("Mar", noLongerOptional as! Double),
                    ("Apr", noLongerOptional as! Double),
                    ("May", noLongerOptional as! Double),
                    ("Jun", noLongerOptional as! Double),
                    ("July",noLongerOptional as! Double),
                    ("Aug", noLongerOptional as! Double),
                    ("Sep", noLongerOptional as! Double),
                    ("Oct", noLongerOptional as! Double),
                    ("Nov", noLongerOptional as! Double),
                    ("Dec", noLongerOptional as! Double)
                        ],
                                          color: UIColor.darkGray,
                                          barWidth: 15
                    )
                    self.view.addSubview(chart.view)
                    self.chartView = chart
                 }
              }

            }
          }
        }
}

This is why UIView operations have to be done on the main thread: Why must UIKit operations be performed on the main thread?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM