Swift-在图表视图中显示数据

时间:2018-08-02 11:11:56

标签: ios swift graph swiftcharts

我在条形图中显示了我的响应数据,但是问题是,如果我传递了静态数据,则可以正确获取响应,并在图形中获得了图形,但是它可以工作,但是如果我传递了响应值,则它不会显示。 这是我的代码

代码::

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
                 }
              }

            }
        }

}

我先解开值,然后传递图表,但是当我不打印noLongerOptional时,我正在获取数据,但无法传递图形。

静态值正确显示在图形中。谁能帮我吗?

1 个答案:

答案 0 :(得分:0)

Alamofire在它创建的网络线程而不是主线程上返回网络响应。您需要将UIView操作分派到主线程上

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
                 }
              }

            }
          }
        }
}

这就是为什么必须在主线程上执行UIView操作的原因:Why must UIKit operations be performed on the main thread?