Charts not plotting in tableViewCell

流过昼夜 提交于 2019-12-12 14:21:15

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!