19

I am integrating pie chart in my app using Charts library and getting issue with chart data my code is

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

and when run app I getting something this
enter image description here

but I need pie chart in full data records like in below screen shot enter image description here

10
  • both are same ? and ya if you want to print month name with your valye in chart then just add it with your value means append your text value when its go to build chart Commented Dec 17, 2016 at 9:36
  • I have already append text value initialise time in this line: let dataEntry1 = ChartDataEntry(x: Double(i), y: values[i], data: dataPoints[i] as AnyObject) Commented Dec 17, 2016 at 9:45
  • Using code from appcoda: appcoda.com/ios-charts-api-tutorial Commented Dec 17, 2016 at 9:48
  • just print your append data dataEntry1 Commented Dec 17, 2016 at 9:49
  • @Himanshu, I have print data and getting corresponding values. Commented Dec 17, 2016 at 9:58

2 Answers 2

35

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)

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

2 Comments

Is there a way to show percentage sign with digits?
how can I assign JSON array to piechart?
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

Comments

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.