I want to use piechart danielgindi/Charts library on Github. My codes like this:
我想在Github上使用piechart danielgindi / Charts库。我的代码是这样的:
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.
我没有在任何图表库之前使用,但是我必须使用一个项目。这个图书馆或其他图书馆对我来说无关紧要。如果你知道回答我的问题请帮我解决。或者你可以告诉我另一个兼容switf 3的库。谢谢。
1 个解决方案
#1
6
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:
我以编程方式初始化了pieChartView并将其添加到子视图中。代码运行正常,不会崩溃。我进行了必要的更改并在下面添加:
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
}
}
#1
6
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:
我以编程方式初始化了pieChartView并将其添加到子视图中。代码运行正常,不会崩溃。我进行了必要的更改并在下面添加:
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
}
}