Ios Swift PieChart

时间:2017-11-03 12:46:19

标签: ios swift ios-charts

如何在ios饼图中显示图例值?我只能获得非零值。有人可以帮忙吗?

func updateChartData12()  {

    let chart = PieChartView(frame: self.view.frame)
    // 2. generate chart data entries
    let track = ["Income", "Expense", "Wallet", "Bank"]
    let money = [650, 456.13, 0, 0]

    var entries = [PieChartDataEntry]()
    for (index, value) in money.enumerated() {
        let entry = PieChartDataEntry()
        if value != 0 {
            entry.y = value
            entry.label = track[index]
            entries.append( entry)
        }


    }

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

    for _ in 0..<money.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)
    }
    set.colors = colors
    let data = PieChartData(dataSet: set)
    chart.data = data
    chart.noDataText = "No data available"
    // user interaction



    let d = Description()

    chart.chartDescription = d
    chart.centerText = "Pie Chart"
    chart.holeRadiusPercent = 0.2
    chart.transparentCircleColor = UIColor.clear
    self.view.addSubview(chart)

}

我正在使用此代码..但我需要显示总y值,即底部的图例

1 个答案:

答案 0 :(得分:0)

您的饼图大于100%。我所拥有的数据旨在显示超过100%的总数以及它不起作用的原因。

let track = ["Income", "Expense", "Wallet", "Bank"]
let money = [650, 456.13, 0, 0] 

因此,如果它的工作正常,请使用以下货币数组,然后您要检查是否存在方法以确保饼图中的百分比总和为100%

let money = [65, 35, 0, 0]