我正在尝试使用Charts swift库来实现饼图。我在图表库附带的预编码文件中收到错误。由于我的应用程序崩溃。我尝试修改那里的代码,但在文件中显示相同的错误。我还在示例代码后添加了错误屏幕截图。
的UIViewController
import UIKit
import Charts
import Alamofire
import SwiftyJSON
class pieView: UIViewController {
@IBOutlet weak var pie1: PieChartView!
let allCertificate = "URL HERE"
let expiredCertificate = "URL HERE"
let upcomingCertificate = "URL HERE"
var tempAllArray = [[String : AnyObject]]()
var tempExpArray = [[String:AnyObject]]()
var tempUpcomArray = [[String:AnyObject]]()
var allCount = Double()
var expiredCount = Double()
var upcomingCount = Double()
override func viewDidLoad() {
super.viewDidLoad()
getAllCertificate()
getUpcomingCertificate()
getExpiredCertificate()
}
func callingFunction(){
print(allCount)
let expPercent = (expiredCount / allCount) * 100
let upcomPercent = (upcomingCount / allCount) * 100
let otherPercent = ((allCount - (expiredCount + upcomingCount)) / allCount) * 100
let months = ["Expired","Upcoming","Other"]
let unitsSold = [expPercent, upcomPercent, otherPercent]
setChart(dataPoints: months, values: unitsSold)
pie1.chartDescription?.text = ""
pie1.noDataText = "You need to provide data for the chart"
}
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)
}
let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "")
let formatter = NumberFormatter()
formatter.numberStyle = .percent
formatter.maximumFractionDigits = 1
formatter.multiplier = 1.0
let pieChartData = PieChartData(dataSet: pieChartDataSet)
pieChartData.setValueFormatter(DefaultValueFormatter(formatter:formatter))
pie1.data = pieChartData
pie1.centerText = "Certificate"
pie1.usePercentValuesEnabled = true
var colors: [UIColor] = [#colorLiteral(red: 0, green: 0.4784313725, blue: 1, alpha: 1),#colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1),#colorLiteral(red: 0.3647058904, green: 0.06666667014, blue: 0.9686274529, alpha: 1)]
pieChartDataSet.colors = colors
let legend = pie1.legend
legend.setCustom(colors: colors, labels: ["Expired","Upcoming","Other"])
legend.font = UIFont(name: "Arial", size: 14.0)!
}
func getAllCertificate() {
Alamofire.request(self.allCertificate).responseJSON { response in
if((response.result.value) != nil) {
let swiftyJsonVar = JSON(response.result.value!)
if let resData = swiftyJsonVar["data"].arrayObject {
// print(resData)
self.allCount = Double(resData.count)
// print("all \(self.allCount)")
}
}
else
{
self.allCount = 0
}
self.callingFunction()
}
}
func getExpiredCertificate() {
Alamofire.request(self.expiredCertificate).responseJSON { response in
if((response.result.value) != nil) {
let swiftyJsonVar = JSON(response.result.value!)
if let resData = swiftyJsonVar["data"].arrayObject {
// print(resData)
self.expiredCount = Double(resData.count)
// print("expired\(self.expiredCount)")
}
}
else{
self.expiredCount = 0
}
self.callingFunction()
}
}
func getUpcomingCertificate() {
Alamofire.request(self.upcomingCertificate).responseJSON { response in
if((response.result.value) != nil) {
let swiftyJsonVar = JSON(response.result.value!)
if let resData = swiftyJsonVar["data"].arrayObject {
// print(resData)
self.upcomingCount = Double(resData.count)
print("all \(self.upcomingCount)")
}
}
self.callingFunction()
}
}
}
错误屏幕截图