Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pie chart using Charts library with iOS swift 3

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.

like image 344
Mayday Avatar asked Feb 18 '17 14:02

Mayday


People also ask

How do you create a chart in Swift?

To create a chart with Swift Charts, define your data and initialize a Chart view with marks and data properties. Then use modifiers to customize different components of the chart, like the legend, axes, and scale.

Can pie charts be 3D?

A 3D pie chart is like regular pie chart. They're identical in configuration, except that the former uses PieChart3D class to instantiate the chart, and PieSeries3D for its series. It also introduces additional settings depth and angle to configure depth (height) and angle at which we are viewing the chart.

How do you insert a 3D pie chart?

Click Insert > Chart > Pie, and then pick the pie chart you want to add to your slide. In the spreadsheet that appears, replace the placeholder data with your own information. For more information about how to arrange pie chart data, see Data for pie charts. When you've finished, close the spreadsheet.


1 Answers

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
   }
}
like image 84
KrishnaCA Avatar answered Sep 28 '22 01:09

KrishnaCA