Search code examples
iosswift3ios-charts

How to draw LineChartData in swift 3?


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
}

Solution

  • 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
    }