Reputation: 39
So I'm creating a fitness app to display a changing variable calories burnt today
stored as a Double in User defaults. I want to make it such that every hour, the view controller displaying the line graph would check the User default calories burnt today
and add a line chart entry containing the value stored in calories burnt today
and display it on the line graph so that the user would be able to monitor his calories burnt throughout the day.
But the problem is that the first time I segue into the viewController, there is a single point present in the line graph showing the number of calories burnt, but when I next segue into the ViewController, the line graph view is blank!
code is below:
import UIKit
import Charts
class DaysViewController: UIViewController, ChartViewDelegate {
@IBOutlet weak var graphsview: UIView!
var linechart = LineChartView()
override func viewDidLoad() {
super.viewDidLoad()
linechart.delegate = self
// Do any additional setup after loading the view.
}
override func viewDidLayoutSubviews() {
linechart.frame = CGRect(x: 0, y: 0, width: self.graphsview.frame.size.width, height: self.graphsview.frame.size.height)
linechart.center = graphsview.center
view.addSubview(linechart)
var entries = [ChartDataEntry]()
let caloriesburnt = UserDefaults.standard.double(forKey: "calories burnt today")
var hours3 = UserDefaults.standard.integer(forKey: "hoursofexerciserecorded")
UserDefaults.standard
let date = Date()
let calender = Calendar.current
let currenthour = calender.component(.minute, from: date)
let previoushour = UserDefaults.standard.integer(forKey: "hour")
let hour2 = previoushour + 1
if hour2 <= currenthour {
hours3 += 1
entries.append(ChartDataEntry(x: Double(hours3), y: caloriesburnt))
UserDefaults.standard.set(hours3, forKey: "daysofexerciserecorded")
UserDefaults.standard.set(currenthour, forKey: "hour")
}
let set = LineChartDataSet(entries: entries)
set.colors = ChartColorTemplates.colorful()
let data = LineChartData(dataSet: set)
linechart.data = data
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
Upvotes: 1
Views: 258
Reputation: 1374
I'm noticing a lot of errors inside this code.
1st
Why put this line view.addSubview(linechart)
inside viewDidLayoutSubviews()
?
viewDidLayoutSubviews
gets called n
times so you are adding n linechart
to your view.
2nd
Why do you get dimensions from graphsview
and then you add your linechart
to your view?
3rd
Remove that UserDefaults.standard
not used in your code
Edit (full code)
override func viewDidLoad() {
super.viewDidLoad()
linechart.frame = CGRect(x: 0, y: 0, width: yourWidht, height: yourHeight)
linechart.center = graphsview.center
graphsview.addSubview(linechart)
setChartData()
}
private func setChartData() {
var entries = [ChartDataEntry]()
let caloriesburnt = UserDefaults.standard.double(forKey: "calories burnt today")
var hours3 = UserDefaults.standard.integer(forKey: "hoursofexerciserecorded")
let date = Date()
let calender = Calendar.current
let currenthour = calender.component(.minute, from: date)
let previoushour = UserDefaults.standard.integer(forKey: "hour")
let hour2 = previoushour + 1
if hour2 <= currenthour {
hours3 += 1
entries.append(ChartDataEntry(x: Double(hours3), y: caloriesburnt))
UserDefaults.standard.set(hours3, forKey: "daysofexerciserecorded")
UserDefaults.standard.set(currenthour, forKey: "hour")
}
let set = LineChartDataSet(entries: entries)
set.colors = ChartColorTemplates.colorful()
let data = LineChartData(dataSet: set)
linechart.data = data
}
Upvotes: 1