使用带有 swift 的图表库的饼图

Pie chart using Charts library with swift

我正在使用 Charts 库将饼图集成到我的应用程序中,我的代码是

的图表数据出现问题
import UIKit
import Charts

class ViewController: UIViewController {

  @IBOutlet weak var pieChartView: PieChartView!

  override func viewDidLoad() {
    super.viewDidLoad()
    let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
    let unitsSold = [10.0, 4.0, 6.0, 3.0, 12.0, 16.0]
    setChart(dataPoints: months, values: unitsSold)
  }

  func setChart(dataPoints: [String], values: [Double]) {
    var dataEntries: [ChartDataEntry] = []
    for i in 0..<dataPoints.count {
      let dataEntry1 = ChartDataEntry(x: Double(i), y: values[i], data: dataPoints[i] as AnyObject)
      dataEntries.append(dataEntry1)
    }
    print(dataEntries[0].data)
    let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "Units Sold")
    let pieChartData = PieChartData(dataSet: pieChartDataSet)
    pieChartView.data = pieChartData
    
    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 dataEntry1 = ChartDataEntry(x: Double(i), y: values[i], data: dataPoints[i] as AnyObject)

试试这个

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

PieChartDataEntry 专门用于饼图,因此您应该会在图表中看到月份。

希望这能让你走上正轨

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)

}