How to draw LineChartData in swift 3?

半腔热情 提交于 2019-12-08 06:58:49

问题


I switched to swift 3 and I am now struggling to get my chart data shown again. When I use it like that, I just see an empty chart. I think some initializers changed?

I am using the Swift-3.0 branch.

@IBOutlet weak var lineChartView: LineChartView!

override func viewDidLoad() {

  for x in data {
    ...
    let dataSet = LineChartDataSet(yVals: dataEntries, label: player.getName())
    lineChartData.addDataSet(dataSet)
  }

  lineChartView.data = lineChartData
}

回答1:


They changed the way, how is data constructed. Enclosed please find following sample:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    let dollars1 = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
    let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    // 1 - creating an array of data entries
    var yValues : [ChartDataEntry] = [ChartDataEntry]()

    for i in 0 ..< months.count {
        yValues.append(ChartDataEntry(x: Double(i + 1), y: dollars1[i]))
    }

    let data = LineChartData()
    let ds = LineChartDataSet(values: yValues, label: "Months")

    data.addDataSet(ds)
    self.lineChartView.data = data
}


来源:https://stackoverflow.com/questions/39531900/how-to-draw-linechartdata-in-swift-3

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