1

Im using Charts Version 3.0.1. Im currently teaching myself Swift by converting my Rails project. I have no knowledge of Objective C so please bear with me; I've done ok so far.

My code below is/may not be formatted correctly as Im missing the days array in the PieChart:

enter image description here

Numbers seem not to be correct. The code:

@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
}

Have I missed out something? Your second eye would be highly appreciated.

0

1 Answer 1

4

You have changed x & y values and hence index is coming as your pie chart's value. Also, please use PieChartDataEntry. (working code on my box)

  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
  }

Try this and let me know.

Sign up to request clarification or add additional context in comments.

1 Comment

My issue is here: let pieChartDataSet = PieChartDataSet(yVals: dataEntries, label: "Gross Income"). I get Incorrect argument label in cell ( have 'yVals:label:', expected 'values:label:'). Have something changed in Charts 3?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.