我的代码是这样的:
import UIKitimport Chartsclass 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 }}
但是当我运行它时,请分解这一行:
pIEChartVIEw.data = pIEChartData
我没有在任何图表库之前使用,但是我必须使用一个项目.这个图书馆或其他图书馆对我来说无关紧要.如果您知道回答我的问题,请帮助我解决.或者您可以告诉我另一个与switf 3兼容的库.谢谢.
解决方法 我以编程方式初始化了pIEChartVIEw并将其添加到子视图中.代码运行正常,不会崩溃.我做了必要的更改并在下面添加了它:import UIKitimport Chartsclass ChartVIEwController: UIVIEwController { var pIEChartVIEw: PIEChartVIEw! overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() // Do any additional setup after loading the vIEw. let months = ["Jan","Jun"] let unitsSold = [20.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),Alpha: 1) colors.append(color) } pIEChartDataSet.colors = colors }}总结
以上是内存溢出为你收集整理的饼图使用图表库与iOS swift 3全部内容,希望文章能够帮你解决饼图使用图表库与iOS swift 3所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)