如何使用丹尼尔gindi/Chart库在swift中显示饼图中的百分比符号(IOS Chart)

r1wp621o  于 2023-04-13  发布在  iOS
关注(0)|答案(5)|浏览(158)

我正在使用图表框架(由丹尼尔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)
}

输出:=〉

yfjy0ee7

yfjy0ee71#

Swift3和最新版本的charts上,您可以在饼图上执行%化,如下所示:

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

aiazj4mn2#

在pieChart上显示%符号。
Download Example Project

使用下面的代码。

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

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

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

3zwtqj6y

3zwtqj6y3#

只需添加这些行即可在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())
jyztefdp

jyztefdp4#

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

wbgh16ku5#

我用的是Charts 4.1.0
一旦将数据分配给图表视图,设置值格式化程序。

chartView.data = pieChartData

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

相关问题