I am trying to implement pie chart from ios-charts library everything is working great, however I am missing legends from the graph -
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
Finally this issue is resolved it wasn't the bug. Please find the detailed solution here.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With