我收到json响应,并且我想通过siaplay thatresponse作为图表来使用它的useg 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数组中时崩溃,我不明白为什么崩溃