我正在尝试从ios-charts库实现饼图一切都很好,但是我从图中遗漏了传说 -
这是我的代码 -
// 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
}
我在here中看到了类似的问题并且构建和运行,问题中使用的饼图也没有在图表下方显示任何图例。任何帮助或指针将非常感激。
由于
答案 0 :(得分:5)
最后这个问题解决了,这不是bug。请找到详细解决方案here。
答案 1 :(得分:2)
我刚刚尝试使用
加入ChatsDemo legend.position = ChartLegendPositionBelowChartLeft;
工作正常。所以无论是你的数据问题,还是你修改过的东西。您没有显示如何设置x值。提供更多详细信息或自行调试。应该很容易