I am using the Charts library in Swift to draw bar charts.
Here is my code:
class barChartVC: UIViewController {
let months = ["January" , "February" , "March" , "April" , "May" , "June" , "July" , "August"]
let unitsSold = [10.0,20.0,30.0,20.0,20.0,10.0,15.0,20.0]
let unitsSold1 = [20.0,30.0,50.0,40.0,30.0,20.0,45.0,50.0]
var dataEntries: [BarChartDataEntry] = []
@IBOutlet var barChartView: BarChartView!
override func viewDidLoad() {
super.viewDidLoad()
setChartBarGroupDataSet(dataPoints: months, values: unitsSold, values2: unitsSold1, sortIndex: 0)
}
func setChartBarGroupDataSet(dataPoints: [String], values: [Double], values2: [Double],sortIndex:Int) {
for i in 0..<dataPoints.count
{
let dataEntry = BarChartDataEntry(x: values[i], y: Double(i))
let dataEntry1 = BarChartDataEntry(x: values2[i], y: Double(i))
dataEntries.append(dataEntry)
dataEntries.append(dataEntry1)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: " ")
var COLOR_SET = [UIColor]()
COLOR_SET = [UIColor.red,UIColor.green]
chartDataSet.setColors(COLOR_SET, alpha: 1.0)
let dataSets: [BarChartDataSet] = [chartDataSet]
let data = BarChartData(dataSets: dataSets)
barChartView.data = data // Fatal error is here
barChartView.descriptionText = "Bar Chart"
barChartView.rightAxis.drawGridLinesEnabled = false
barChartView.rightAxis.drawAxisLineEnabled = false
barChartView.rightAxis.drawLabelsEnabled = false
barChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInBounce)
}
}
I am getting a fatal error on the line that I marked in the code.
fatal error: unexpectedly found nil while unwrapping an Optional value
Thank you in advance to people who help me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.