Ios-charts Missing PieChart label (legends)

I am trying to implement a pie chart from the ios-charts library, everything works fine, however I do not have enough legends from the chart -

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 saw a similar problem in here , and also built and run, the pie chart used in this problem also does not show any legends below the chart. Any help or pointer would be really appreciated.

thanks

+4
source share
2 answers

Finally, this problem is resolved, but it is not a mistake. Please find a detailed solution here .

+5
source

I just tried ChatsDemo with

legend.position = ChartLegendPositionBelowChartLeft;

. , - . , x. .

+2

Source: https://habr.com/ru/post/1612074/


All Articles