How to show percent sign in a pie chart using daniel gindi / chart library in swift (IOS chart)

I am using a chart diagram (by Daniel gindi). It works fine, but how to show% (sign) on piechart?
I am also trying to use the NSNumberFormatter () method for hidden data values ​​in percantage, but not get the% sign.
I am loading the code here

@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) } 

Output: =>

enter image description here

+2
source share
4 answers

To show the% sign on pieChart.

Download sample project

Just use this code below.

 let formatter = NSNumberFormatter() formatter.numberStyle = .PercentStyle formatter.maximumFractionDigits = 1 formatter.multiplier = 1.0 pieChartData.dataSet?.valueFormatter = formatter 

You will get the% sign after displaying the value on the fragment.

enter image description here

Source https://github.com/danielgindi/Charts

Have a happy coding.

+6
source

In Swift3 with the latest version of charts, you can do % formatting on a pie chart, as shown below:

  let formatter = NumberFormatter() formatter.numberStyle = .percent formatter.maximumFractionDigits = 1 formatter.multiplier = 1.0 pieChartData.setValueFormatter(DefaultValueFormatter(formatter:formatter)) 
+8
source

Just add this line to format the percentage in PieChart .

  let formatter = NSNumberFormatter() formatter.numberStyle = .PercentStyle formatter.maximumFractionDigits = 1 formatter.multiplier = 1.0 pieChartData.setValueFormatter(formatter) // text color of percentage label slcCell.pieChartView.data?.setValueTextColor(UIColor.clearColor()) 
0
source
  let formatter = NumberFormatter() formatter.numberStyle = .percent formatter.maximumFractionDigits = 1 formatter.multiplier = 1.0 chart.data?.setValueFormatter(formatter as? IValueFormatter) 
0
source

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


All Articles