我正在使用图表框架(由丹尼尔gindi)。它工作得很好,但如何在piechart上显示%(符号)?
我也尝试使用NSNumberFormatter()方法来隐藏数据值,但没有得到%符号。
我在这里上传代码
@IBOutlet var pieChart: PieChartView!
override func viewDidLoad() {
super.viewDidLoad()
var data = [25.0,37.5,12.5,12.5,12.5] // pie chart data
var status = ["A","B","C","D","E"] // status
setChart(status, values: data)
self.pieChart.drawSliceTextEnabled = false
}
func setChart(dataPoints: [String], values: [Double]) {
pieChart.noDataText = "you need to provide data for chart"
pieChart.noDataTextDescription = ""
self.pieChart.descriptionText = ""
var dataEntries: [ChartDataEntry] = []
//pieChart.centerText = " "
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let pieChartDataSet = PieChartDataSet(yVals: dataEntries, label: "")
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
}
let pieChartData = PieChartData(xVals: dataPoints, dataSet: pieChartDataSet)
pieChart.data = pieChartData
pieChart.animate(yAxisDuration: 2.0, easingOption: .EaseInOutBack)
}
5条答案
按热度按时间yfjy0ee71#
在
Swift3
和最新版本的charts上,您可以在饼图上执行%
化,如下所示:aiazj4mn2#
在pieChart上显示%符号。
Download Example Project
使用下面的代码。
在切片上显示值后,将显示%符号。
来源https://github.com/danielgindi/Charts
3zwtqj6y3#
只需添加这些行即可在PieChart中格式化百分比。
jyztefdp4#
wbgh16ku5#
我用的是Charts 4.1.0
一旦将数据分配给图表视图,设置值格式化程序。