问题
I am trying to plot a random chart in a tableViewCell in swift 3 using Charts 3.0.2 (an older pod version because I am using Xcode 8.3)
setChart() function is working in a view controller class.
Here is my TableViewCell Code -
import UIKit
import Charts
class ChartTVC: UITableViewCell {
@IBOutlet weak var barChartView: BarChartView!
var months: [String]!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func setChart() {
var dataEntries: [BarChartDataEntry] = []
barChartView.backgroundColor = UIColor.white
for i in 100..<105 {
let dataEntry = BarChartDataEntry(x: Double(i), y: Double(45))
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Visitor count")
let chartData = BarChartData()
chartData.addDataSet(chartDataSet)
chartData.setDrawValues(true)
chartDataSet.colors = [Colors.amber]
let xaxis:XAxis = XAxis()
barChartView.xAxis.labelPosition = .bottom
barChartView.xAxis.drawGridLinesEnabled = true
barChartView.xAxis.valueFormatter = xaxis.valueFormatter
barChartView.chartDescription?.enabled = true
barChartView.legend.enabled = true
barChartView.rightAxis.enabled = true
barChartView.leftAxis.drawGridLinesEnabled = true
barChartView.leftAxis.drawLabelsEnabled = true
barChartView.data = chartData
}
}
And This is how I am calling it from a view controller class
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ChartTVC", for: indexPath) as! ChartTVC
cell.setChart()
return cell
}
I read many tutorial blogs and I couldn't find anything wrong with the code. I had set a height constraint for bar chart view. Here's the screenshot.
Chart text is a label.
回答1:
Try to call this after you set chart data:
barChartView.notifyDataSetChanged()
来源:https://stackoverflow.com/questions/49779874/charts-not-plotting-in-tableviewcell