Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios-charts PieChart's label (legends) missing

I am trying to implement pie chart from ios-charts library everything is working great, however I am missing legends from the graph -

enter image description here

Here is my code -

// This is the delegate method for creating data for chart
        func offDaysDidLoaded(controller: DataModel,chartArray:[PFObject]) {


                let formatter = NSDateFormatter()
                formatter.dateFormat = "MMM"
                dataDict = [:]
                for od in chartArray {
                    let date = od["Date"] as! NSDate
                    let month = formatter.stringFromDate(date)
                    if self.dateDict.indexForKey(month) != nil {
                        self.dateDict[month]! += 1.0
                    }else{
                        self.dateDict.updateValue(1.0, forKey: month)
                    }
                }
                let dataPointArray = Array(dateDict.keys)
                let valuesArray = Array(dateDict.values)
                pieChartView.data = nil
                pieChartView.backgroundColor = UIColor.grayColor()
                setChart(dataPointArray, values: valuesArray)
            }


func setChart(dataPoints: [String], values: [Double]) {
    var dataEntries: [ChartDataEntry] = []
    for i in 0..<dataPoints.count {

        let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
        dataEntries.append(dataEntry)
    }

    let pieChartDataSet = PieChartDataSet(yVals: dataEntries, label: "Months")

    let pieChartData = PieChartData(xVals: dataPoints, dataSet: pieChartDataSet)
    pieChartData.setDrawValues(false)
    pieChartView.data = pieChartData
    pieChartView.animate(xAxisDuration: NSTimeInterval(5))

    var colors: [UIColor] = []

    for _ in 0..<dataPoints.count {
        let red = Double(arc4random_uniform(256))
        let green = Double(arc4random_uniform(256))
        let blue = Double(arc4random_uniform(256))

        let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
        colors.append(color)
    }

    pieChartDataSet.colors = colors

}

I have seen similar issue in here and build and run, pie chart used in the issue also doesn't show any legends below the chart. Any help or pointer would be really appreciated.

Thanks

like image 209
AppsWise Avatar asked Mar 15 '23 10:03

AppsWise


2 Answers

Finally this issue is resolved it wasn't the bug. Please find the detailed solution here.

like image 53
AppsWise Avatar answered Mar 25 '23 19:03

AppsWise


I just did try ChatsDemo with

legend.position = ChartLegendPositionBelowChartLeft;

works fine. So either your data problem, or you modified something. You don't show how you setup x values. Give more details or debug on your own. Should be easy

like image 31
Wingzero Avatar answered Mar 25 '23 18:03

Wingzero