not able to show response data on charts

92 Views Asked by At

i am getting json response and i want to siaplay thatresponse as chart an useg SwiftCharts library for that but i am not able to show on chart let me show my response

Response

{
    "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
        }
    ]
}

and now i show u my code which i done for show on chart

Code

  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)

    }

please check this updated code i am getting crash while i am adding my response data in my another searchList array a i not understand why i am getting crash

0

There are 0 best solutions below