正确的数据在IOS图表没有显示(Correct data not showing in ios Ch

2019-09-29 07:49发布

IM使用图表 3.0.1版。 即时通讯目前自学通过转换我的Rails项目斯威夫特。 我没有知识目标C的所以请多多包涵; 我迄今所做OK。

下面我的代码是/可能无法正确格式化为林缺少days在阵列PieChart

数字似乎并不正确。 编码:

@IBOutlet weak var chartView: PieChartView!

override func viewDidLoad() {
    super.viewDidLoad()

    let days = ["Mo", "Tu", "Wed", "Thur", "Fri", "Sat", "Sun"]
    let gross = [11.00, 90.95, 250.00, 40.90, 60.88, 99.99, 25.00]

    setChart(dataPoints: days, values: gross)
}

func setChart(dataPoints: [String], values: [Double]) {
    var dataEntries: [ChartDataEntry] = []

    for i in 0..<dataPoints.count {

        let dataEntry = ChartDataEntry(x: values[i], y: Double(i))
        dataEntries.append(dataEntry)

    }
    let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "Gross Income")
    pieChartDataSet.sliceSpace = 2.0
    let pieChartData = PieChartData(dataSets: [pieChartDataSet]) // somewhere here confuses me.

    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

    chartView.data = pieChartData
}

难道我错过了什么? 你的第二眼将高度赞赏。

Answer 1:

你已经改变了X&Y值,因此指数即将为您的饼图的价值。 另外,请使用PieChartDataEntry。 (在我的箱子工作代码)

  override func viewDidLoad() {
    super.viewDidLoad()
    let days = ["Mo", "Tu", "Wed", "Thur", "Fri", "Sat", "Sun"]
    let gross = [11.00, 90.95, 250.00, 40.90, 60.88, 99.99, 25.00]

    setChart(days: days, gross: gross)
  }

  func setChart(days: [String], gross: [Double]) {
    var dataEntries: [ChartDataEntry] = []

    for i in 0..<days.count {
      let dataEntry = PieChartDataEntry(value : gross[i], label : days[i])
      dataEntries.append(dataEntry)
    }

    let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "Gross Income")
    pieChartDataSet.sliceSpace = 2.0
    let pieChartData = PieChartData(dataSets: [pieChartDataSet])

    var colors: [UIColor] = []

    for _ in 0..<days.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
    pieChartView.data = pieChartData
  }

试试这个,让我知道。



文章来源: Correct data not showing in ios Charts