如何使用danift gindi /图表库在swift中显示百分比符号饼图(IOS图表)

时间:2016-06-22 10:14:19

标签: ios swift pie-chart

我使用的是Charts框架(Daniel gindi)。它工作得很好但是 如何在饼图上显示%(符号)?
我也尝试使用NSNumberFormatter()方法来获取percantage中的隐蔽数据值,但不能获得%符号。
我在这里上传代码

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

输出:=&GT;

enter image description here

4 个答案:

答案 0 :(得分:8)

在使用最新版本chartsSwift3上,您可以在饼图上进行%格式设置,如下所示:

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

答案 1 :(得分:6)

在pieChart上显示%符号。

<强> Download Example Project

请使用以下代码。

StockTable table = ...;
List<StockRow> rows = ofy().load()
                           .type(StockRow.class) //target type
                           .ancestor(table) //parent entity or just key
                           .list(); //all of them

在切片上显示值后,您将获得%符号。

enter image description here

来源 https://github.com/danielgindi/Charts

编写一个快乐的代码。

答案 2 :(得分:0)

只需添加这些行即可格式化 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())

答案 3 :(得分:0)

    let formatter = NumberFormatter()
    formatter.numberStyle = .percent
    formatter.maximumFractionDigits = 1
    formatter.multiplier = 1.0
    chart.data?.setValueFormatter(formatter as? IValueFormatter)