问题
The following is based on the LineChartView
example (from youtube) which worked fine in a standard View (single view app) & the BarChartView
worked fine in a TableViewCell
using the identical idiom to that shown below. I have a time expiring every two seconds to update the cells however not hooked-up yet.
The x,y Axis are generated correctly (final graphic showing just 2 of the 9 cells), it's just that the points are not plotted. However, failure only occurs in the LineChartView
. I've viewed other references to this issue but not found my specific fix yet. Any ideas?
The TableViewController
class GraphTableTableViewController: UITableViewController {
let model = [1,2,3,4,5,6,7,8,9]
var timer: Timer? = nil
var noSamples = 0
override func viewDidLoad() {
super.viewDidLoad()
timer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: true)
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return model.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "lineChart_cell", for: indexPath)
let graphCell = cell as? LineGraphCell
graphCell!.setChartValues()
print("\(model[indexPath.row])")
return graphCell!
}
@objc func fireTimer() {
print("Timer Fired")
noSamples += 1
(view as? UITableView)?.reloadData()
}
}
The TableViewCell implementation is shown below:
class LineGraphCell: UITableViewCell {
@IBOutlet weak var lineChart: LineChartView!
let x = [0,1,2,3,4,5,6,7,8,9]
let y = [0,1,2,3,4,5,6,7,8,9]
func setChartValues() {
var values = [ChartDataEntry]()
for index in x {
let value = ChartDataEntry(x: Double(index), y: Double(y[index]))
values.append(value)
}
let set1 = ChartDataSet(entries: values, label: "Set1")
let data = ChartData(dataSet: set1)
// Some debug output
if data.entryCount > 0 {
let yValue = "\(values[data.entryCount-1].y)"
print("Y : \(yValue)")
for value in values {
print("\(value.y) ", terminator: "")
}
}
self.lineChart.data = data
}
}
The storyboard is set up as follows:
回答1:
try using following ...
let x = [0,1,2,3,4,5,6,7,8,9]
let y = [0,1,2,3,4,5,6,7,8,9]
func setChartValues() {
var values = [ChartDataEntry]()
for index in x {
let value = ChartDataEntry(x: Double(index), y: Double(y[index]))
values.append(value)
}
let set1 = LineChartDataSet(entries: values, label: "Set1")
set1.color = UIColor.blue // red // green whatever you want.
let data = LineChartData()
data.addDataSet(data:set1)
// Some debug output
if data.entryCount > 0 {
let yValue = "\(values[data.entryCount-1].y)"
print("Y : \(yValue)")
for value in values {
print("\(value.y) ", terminator: "")
}
}
self.lineChart.data = data
}
回答2:
You need to replace your ChartDataSet
with LineChartDataSet
like below
Old Code:
let set1 = ChartDataSet(entries: values, label: "Set1")
let data = ChartData(dataSet: set1)
Replace With:
let set1 = LineChartDataSet(entries: values, label: "Set1")
let data = LineChartData(dataSet: set1)
This will solve your problem.
来源:https://stackoverflow.com/questions/57711264/ioschart-not-displaying-linechartview-points-in-uitableviewcell