chart, here is my code:
private func setChart(pieChartView: PieChartView, values: [Double]) {
var dataEntries: [PieChartDataEntry] = []
let dataPoints = ["Mar","Apr","May"]
for i in 0..<dataPoints.count {
let dataEntry = PieChartDataEntry(value: values[i], label: dataPoints[i])
dataEntries.append(dataEntry)
}
let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "")
pieChartDataSet.colors = [UIColor(red: 47/255, green: 164/255, blue: 59/255, alpha: 1.0),UIColor(red: 17/255, green: 128/255, blue: 127/255, alpha: 1.0),UIColor.orange]
let pieChartData = PieChartData(dataSet: pieChartDataSet)
pieChartView.data = pieChartData
pieChartView.centerText = "Amount Spent"
pieChartView.chartDescription?.text = ""
pieChartView.usePercentValuesEnabled = true
pieChartView.legend.horizontalAlignment = .center
}
How to add "%" in PieChart data ? NSNumberFormatter() is no longer available
---------------SOLUTION---------------
I post this solution for everyone who have same problem. I got it from chart-ios githup :
private func setChart(pieChartView: PieChartView, values: [Double]) {
var dataEntries = [ChartDataEntry]()
let dataPoints = ["Mar","Apr","May"]
for i in 0..<dataPoints.count {
let dataEntry = PieChartDataEntry(value: values[i], label: dataPoints[i])
dataEntries.append(dataEntry)
}
let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "")
let green = UIColor(red: 47/255, green: 164/255, blue: 59/255, alpha: 1.0)
let blue = UIColor(red: 17/255, green: 128/255, blue: 127/255, alpha: 1.0)
pieChartDataSet.colors = [green, blue, .orange]
let pieChartData = PieChartData(dataSet: pieChartDataSet)
let formatter = NumberFormatter()
formatter.numberStyle = .percent
formatter.maximumFractionDigits = 2
formatter.multiplier = 1.0
formatter.percentSymbol = "%"
pieChartData.setValueFormatter(DefaultValueFormatter(formatter: formatter))
pieChartView.data = pieChartData
pieChartView.centerText = "Amount Spent"
pieChartView.chartDescription?.text = ""
pieChartView.usePercentValuesEnabled = true
pieChartView.legend.horizontalAlignment = .center
pieChartView.drawEntryLabelsEnabled = false
pieChartView.holeRadiusPercent = 0.55
pieChartView.highlightPerTapEnabled = false
pieChartView.animate(yAxisDuration: 2.0, easingOption: .easeInBack)
}
Please refer below code, i have added %.
Let me know if i could help you.
Have a happy coding.
if you want to add % in in your graph as well as hide/remove 0.0 values from graph : # Swift 3 used below lines of code:-