折线图 iOS 不显示图表条目
Line chart iOS doesn't display chart entries
所以我正在创建一个健身应用程序来显示一个变化的变量 calories burnt today
在用户默认值中存储为 Double。我想让它每小时显示折线图的视图控制器都会检查用户默认值 calories burnt today
并添加一个包含存储在 calories burnt today
中的值的折线图条目并将其显示在折线图上这样用户就可以监控他全天消耗的卡路里。
但问题是,当我第一次进入 viewController 时,折线图中有一个点显示燃烧的卡路里数,但当我下次进入 [=22] =],折线图视图是空白的!
代码如下:
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.
}
*/
}
我注意到这段代码中有很多错误。
第一
为什么把view.addSubview(linechart)
这一行放在viewDidLayoutSubviews()
里面?
viewDidLayoutSubviews
被调用 n
次,因此您将 n linechart
添加到视图中。
第二
为什么要从 graphsview
获取维度,然后将 linechart
添加到视图中?
第三
删除您的代码中未使用的UserDefaults.standard
编辑(完整代码)
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
}
所以我正在创建一个健身应用程序来显示一个变化的变量 calories burnt today
在用户默认值中存储为 Double。我想让它每小时显示折线图的视图控制器都会检查用户默认值 calories burnt today
并添加一个包含存储在 calories burnt today
中的值的折线图条目并将其显示在折线图上这样用户就可以监控他全天消耗的卡路里。
但问题是,当我第一次进入 viewController 时,折线图中有一个点显示燃烧的卡路里数,但当我下次进入 [=22] =],折线图视图是空白的!
代码如下:
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.
}
*/
}
我注意到这段代码中有很多错误。
第一
为什么把view.addSubview(linechart)
这一行放在viewDidLayoutSubviews()
里面?
viewDidLayoutSubviews
被调用 n
次,因此您将 n linechart
添加到视图中。
第二
为什么要从 graphsview
获取维度,然后将 linechart
添加到视图中?
第三
删除您的代码中未使用的UserDefaults.standard
编辑(完整代码)
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
}