Pie chart using Charts library with swift

蓝咒 提交于 2019-12-02 20:47:11

The reason all the information isn't showing up is because you are using a parent initialiser when creating the entry point.

Instead of

let dataEntry1 = ChartDataEntry(x: Double(i), y: values[i], data: dataPoints[i] as AnyObject)

try this instead

let dataEntry1 = PieChartDataEntry(value: Double(i), label: dataPoints[i], data:  dataPoints[i] as AnyObject)

The PieChartDataEntry is specifically for Pie charts so you should see the month show up in the chart.

Hopefully this gets you on the right track

Update for this question.

  func updateChartData()  {

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

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

    // 3. chart setup
    let set = PieChartDataSet( values: entries, label: "Pie Chart")
    // 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
    chart.isUserInteractionEnabled = true

    let d = Description()
    d.text = "iOSCharts.io"
    chart.chartDescription = d
    chart.centerText = "Pie Chart"
    chart.holeRadiusPercent = 0.2
    chart.transparentCircleColor = UIColor.clear
    self.view.addSubview(chart)

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!