如何添加"%"到ios-chart中的数据

时间:2016-11-06 19:30:35

标签: swift ios-charts

图表,这是我的代码:

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

}

enter image description here

如何添加&#34;%&#34;在PieChart数据? NSNumberFormatter()不再可用

--------------- SOLUTION ---------------

我为遇到同样问题的每个人发布此解决方案。 我是从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)
}

2 个答案:

答案 0 :(得分:2)

enter image description here

请参阅以下代码,我已添加%。

import UIKit

class ViewController: UIViewController {


        @IBOutlet weak var pieChartView: PieChartView!
        override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
        let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]

        setChart(months, values: unitsSold)




    }


    func setChart(dataPoints: [String], values: [Double]) {

        var dataEntries: [ChartDataEntry] = []

        for i in 0..<dataPoints.count {
            let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
            dataEntries.append(dataEntry)
        }

        let pieChartDataSet = PieChartDataSet(yVals: dataEntries, label: "Units Sold")

        let formatter = NSNumberFormatter()
        formatter.numberStyle = .PercentStyle
        formatter.maximumFractionDigits = 1
        formatter.multiplier = 1.0


        let pieChartData = PieChartData(xVals: dataPoints, dataSet: pieChartDataSet)

        pieChartData.dataSet?.valueFormatter = formatter


        pieChartView.data = pieChartData

        pieChartView.holeColor = UIColor.clearColor()

      //  pieChartView.holeRadiusPercent = 0.95

        pieChartView.centerText =  "Hello\nThis is Pie chart"

 pieChartView.usePercentValuesEnabled = true


        var colors: [UIColor] = []

        for i in 0..<dataPoints.count {
            let red = Double(arc4random_uniform(255))
            let green = Double(arc4random_uniform(255))
            let blue = Double(arc4random_uniform(255))

            let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
            colors.append(color)
        }

        pieChartDataSet.colors = colors




    }



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

如果我能帮助你,请告诉我。

有一个快乐的编码。

答案 1 :(得分:2)

如果您想在图表中添加%in以及隐藏/删除图表中的0.0值:#Swift 3 在下面的代码行中使用: -

   func updateChartData()  {

        let chart = PieChartView(frame: mViewOutlet.frame)
//         let chart = PieChartView(frame: CGRect(x: 122, y: 235 , width: self.mViewOutlet.frame.size.width, height: self.mViewOutlet.frame.size.height))


        // 2. generate chart data entries
        let track = ["Present","Leave", "EG/LC", "Halfday", "Absent", "Weeklyoff", "Holidays"]
        //        let money = [65, 13, 10, 2]
        let money = mDaysArray

        var entries = [PieChartDataEntry]()
        for (index, value) in money.enumerated() {
            print("index: \(index) \n value: \(value)")
            let entry = PieChartDataEntry()

            if value != 0 {
                 entry.y = Double(value)
            }else{

            }
            entries.append(entry)
//            entry.label = track[index]  // if we want to remove name label
        }

        // 3. chart setup
        let set = PieChartDataSet( values: entries, label: "Pie Chart")
        // this is custom extension method. Download the code for more details.

        //4. set chart color
        let presentColor = UIColor(red: 80.0/255.0, green: 180.0/255.0, blue: 50.0/255.0, alpha: 1.0)
        //        let lateColor = UIColor(red: 241.0/255.0, green: 194.0/255.0, blue: 114.0/255.0, alpha: 1.0)
        let leaveColor = UIColor(red: 203.0/255.0, green: 68.0/255.0, blue: 242.0/255.0, alpha: 1.0)
        let egColor = UIColor(red: 95.0/255.0, green: 180.0/255.0, blue: 239.0/255.0, alpha: 1.0)
        let halfdayColor = UIColor(red: 82.0/255.0, green: 64.0/255.0, blue: 152.0/255.0, alpha: 1.0)
        let absentColor = UIColor(red: 242.0/255.0, green: 58.0/255.0, blue: 02.0/255.0, alpha: 1.0)
        let weekOffColor = UIColor(red: 186.0/255.0, green: 221.0/255.0, blue: 79.0/255.0, alpha: 1.0)
        let holidayColor = UIColor(red: 35.0/255.0, green: 215.0/255.0, blue: 179.0/255.0, alpha: 1.0)

        let colors: [UIColor] = [presentColor,leaveColor,egColor,halfdayColor,absentColor,weekOffColor,holidayColor]

        set.colors = colors
        let data = PieChartData(dataSet: set)

        let formatter = NumberFormatter()
        formatter.numberStyle = .percent
        formatter.maximumFractionDigits = 2
        formatter.multiplier = 1.0
        formatter.percentSymbol = "%"
        formatter.zeroSymbol = ""
        data.setValueFormatter(DefaultValueFormatter(formatter: formatter))

        chart.data = data
        chart.noDataText = "No data available"
         chart.usePercentValuesEnabled = true
        // user interaction
        chart.isUserInteractionEnabled = false

        let d = Description()
//        d.text = "iOSCharts.io"
        chart.chartDescription = d
//        chart.tintColor = UIColor.black
//        chart.centerText = "Pie Chart"

        chart.holeRadiusPercent = 0.2
        chart.chartDescription?.enabled = false
        chart.legend.enabled = false

        chart.data?.notifyDataChanged()
        chart.notifyDataSetChanged()
        chart.setNeedsDisplay()
        chart.animate(xAxisDuration: 1.3, yAxisDuration: 1.3)

        chart.transparentCircleColor = UIColor.clear
//        self.view.addSubview(chart)
        self.mPieChartMainView.addSubview(chart)
    }