iOS图表库 - 无法将数据绑定到图表视图

时间:2017-07-06 11:38:54

标签: ios ios-charts

我正在使用charts library并希望显示条形图。我面临将数据绑定到图表的问题。这是我的代码。

class FirstViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

@IBOutlet weak var barChartView: BarChartView!
@IBOutlet weak var yearDropDown: UIPickerView!

@IBOutlet weak var rmDropDown: UIPickerView!

@IBOutlet weak var txtYear: UITextField!
@IBOutlet weak var txtRm: UITextField!

var months = [String]()


override func viewDidLoad() {
super.viewDidLoad()

        months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
        let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]


        getYear()
        setChart(dataPoints: months, values: unitsSold)

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func getYear()
{
    url = RestMonitorServer.baseUrl + "/foo/bar"
    Alamofire.request(url, method: .get, parameters: ["foo": bar,"foo":bar,"foo":bar])
        .responseJSON(){
    (response) in
            print("get year list response ",response)

            if ((response.result.value) != nil) {
                let jsonData = JSON(response.result.value!)

                if jsonData["duration"].arrayObject != nil {
                    self.durationArray.append("duration")
                    print("duration array ", self.durationArray)
                }

            } else if (((response.error)) != nil) {
                let jsonError = response.error
                print("Error in getting year list ", jsonError as Any)
            }

    }

}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    return durationArray.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {


    if pickerView == yearDropDown {

        let titleRow = durationArray[row]

        return titleRow
    }
    return ""
}

func setChart(dataPoints: [String], values: [Double]) {

    var dataEntries: [BarChartDataEntry] = []

    for i in 0..<dataPoints.count {
        let dataEntry = BarChartDataEntry(x:values[i],y:Double(i))
        dataEntries.append(dataEntry)
    }
    print("data entries ",dataEntries)
    let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
    print("chart data set ", chartDataSet)
    let chartData = BarChartData(dataSets: [chartDataSet])
    print("chart data ",chartData)
    barChartView.data = chartData //error at this line as fatal error: unexpectedly found nil while unwrapping an Optional value
    print("bar chart data" , barChartView.data!)   
}

}

这是我的完整代码。我收到barChartView的错误,我已经初步化了,但它仍然给我错误。

由于

1 个答案:

答案 0 :(得分:0)

根据我的exp,您需要将功能移至BarChartView扩展名,如下所示:

extension BarChartView {

    func setBarChartData(xValues: [String], yValues: [Double], label: String) {

        var dataEntries: [BarChartDataEntry] = []

        for i in 0..<yValues.count {
            let dataEntry = BarChartDataEntry(x: Double(i), y: yValues[i])
            dataEntries.append(dataEntry)
        }

        let chartDataSet = BarChartDataSet(values: dataEntries, label: label)
        let chartData = BarChartData(dataSets: [chartDataSet])

        self.data = chartData

    }
}

并按以下方式调用:

override func viewDidLoad() {
        super.viewDidLoad()
        barChartView.setBarChartData(xValues: months, yValues: unitsSold, label: "Monthly Sales")
}

试试这个,让我知道它是否有效。

还有一件事就是设置几个月的静态数据。值我的问题是你在哪里填写服务器响应数据?