0

我收到了 json 响应,我想将响应作为图表使用 SwiftCharts 库,但我无法在图表上显示让我显示我的响应

回复

{
    "success": "1",
    "data": [
        {
            "month": 0
        },
        {
            "month": 0
        },
        {
            "month": 0
        },
        {
            "month": 0
        },
        {
            "month": 0
        },
        {
            "month": 0
        },
        {
            "month": 0
        },
        {
            "month": 3
        },
        {
            "month": 0
        },
        {
            "month": 0
        },
        {
            "month": 0
        },
        {
            "month": 0
        }
    ],
    "like_data": [
        {
            "like": 0
        },
        {
            "like": 0
        },
        {
            "like": 0
        },
        {
            "like": 0
        },
        {
            "like": 0
        },
        {
            "like": 0
        },
        {
            "like": 0
        },
        {
            "like": 6
        },
        {
            "like": 0
        },
        {
            "like": 0
        },
        {
            "like": 0
        },
        {
            "like": 0
        }
    ],
    "cmnt_data": [
        {
            "cmnt": 0
        },
        {
            "cmnt": 0
        },
        {
            "cmnt": 0
        },
        {
            "cmnt": 0
        },
        {
            "cmnt": 0
        },
        {
            "cmnt": 0
        },
        {
            "cmnt": 0
        },
        {
            "cmnt": 1
        },
        {
            "cmnt": 0
        },
        {
            "cmnt": 0
        },
        {
            "cmnt": 0
        },
        {
            "cmnt": 0
        }
    ]
}

现在我向您展示我为在图表上显示而完成的代码

代码

  let data = response.result
                if let dict = data.value as? Dictionary<String,AnyObject>{
                    if let categorylist = dict["data"]{
                        self.searchlist = categorylist as! [AnyObject]
                        let task = [1.0,4.0,3.0,5.0]
                        self.setChart(dataPoints: self.searchlist as! [String], values: task)
                    }
                }

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

        chartView.noDataText = "Nothining to display"
        var dataEntries : [BarChartDataEntry] = []
        var counter = 0.0

        for i in 0..<dataPoints.count {
            counter += 1
            let dataEntery = BarChartDataEntry(x: counter, y:values[i], data: days as AnyObject)
            dataEntries.append(dataEntery)
        }

        let ChartDataSet = BarChartDataSet(values: dataEntries, label: "Time")
        let chartData = BarChartData()
        chartData.addDataSet(ChartDataSet)
        chartView.data = chartData
        ChartDataSet.colors = ChartColorTemplates.joyful()
        chartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)

    }

请检查这个更新的代码,当我在我的另一个 searchList 数组中添加我的响应数据时,我遇到了崩溃我不明白为什么我会崩溃

4

0 回答 0