6

I want to use piechart danielgindi/Charts library on Github. My codes like this:

import UIKit
import Charts

class ChartViewController: UIViewController {

    @IBOutlet weak var pieChartView: PieChartView!

    override func viewDidLoad() {
        super.viewDidLoad()

    // Do any additional setup after loading the view.

        let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
        let unitsSold = [20.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 = PieChartDataEntry(value: Double(i), label: dataPoints[i], data:  dataPoints[i] as AnyObject)
            dataEntries.append(dataEntry1)
        }

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

But when I run it, break down this line:

pieChartView.data = pieChartData

I dont use before any chart libraries, however I have to use one project. This library or another library doesn't matter for me. If you know answer my problem please help to me for solve. Or you can advise to me another library compatible with switf 3. Thank you.

2
  • Did you connect you pieChartView outlet? What error is the compiler showing you? Commented Feb 18, 2017 at 15:28
  • Yes I connected pieChartView @TomKnapen. Just break out when pieChartView. in console (lldb) I guess nil object error. Commented Feb 18, 2017 at 15:52

1 Answer 1

8

I initialized pieChartView programatically and added it to subview. The code runs fine and doesn't crash. I made the necessary changes and added it below:

import UIKit
import Charts

class ChartViewController: UIViewController {

   var pieChartView: PieChartView!

   override func viewDidLoad() {
       super.viewDidLoad()

       // Do any additional setup after loading the view.

       let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
       let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]

       pieChartView = PieChartView(frame: self.view.bounds)
       self.view.addSubview(pieChartView!)
       setChart(dataPoints: months, values: unitsSold)
   }

   func setChart(dataPoints: [String], values: [Double]) {

       var dataEntries: [ChartDataEntry] = []

       for i in 0..<dataPoints.count {
           let dataEntry1 = PieChartDataEntry(value: values[i], label: dataPoints[i])
           dataEntries.append(dataEntry1)
       }

       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
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

it worked for me thank you. But why doesn't use unitsSold values?
@DeliCevat, That's because you are not setting the data correctly in your original code. Let me update my answer to include that.
thanks, I wonder that why just at below view just there is 'Jan' ? Could be every months?

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.