4

Im trying to create a line chart with this code. I've laid out most of the code I've been messing around with it for hours trying different things. In class:

PendingViewController: UIViewController, ChartViewDelegate

Outlet:

@IBOutlet weak var lineChartView: LineChartView!

ViewDidLoad:
    let months = ["Jan" , "Feb", "Mar", "Apr", "May", "June", "July", "August", "Sept", "Oct", "Nov", "Dec"]
    let dollars1 = [1453.0,2352,5431,1442,5451,6486,1173,5678,9234,1345,9411,2212]
    self.lineChartView.delegate = self
    // 2
    self.lineChartView.descriptionText = "Tap node for details"
    // 3
    self.lineChartView.chartDescription?.textColor = UIColor.white
    self.lineChartView.gridBackgroundColor = UIColor.darkGray
    // 4
    self.lineChartView.noDataText = "No data provided"
    // 5
    setChartData(months: months)

Func:

func setChartData(months : [String]) {

    // 1 - creating an array of data entries
    var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
    for i in 0 ..< months.count {
        yVals1.append(ChartDataEntry(x: dollars1[i], y: Double(i)))
    }

    // 2 - create a data set with our array
    let set1: LineChartDataSet = LineChartDataSet(values: yVals1, label: "First Set")
    set1.axisDependency = .left // Line will correlate with left axis values
    set1.setColor(UIColor.red.withAlphaComponent(0.5)) // our line's opacity is 50%
    set1.setCircleColor(UIColor.red) // our circle will be dark red
    set1.lineWidth = 2.0
    set1.circleRadius = 6.0 // the radius of the node circle
    set1.fillAlpha = 65 / 255.0
    set1.fillColor = UIColor.red
    set1.highlightColor = UIColor.white
    set1.drawCircleHoleEnabled = true

    //3 - create an array to store our LineChartDataSets
    var dataSets : [LineChartDataSet] = [LineChartDataSet]()
    dataSets.append(set1)

    //4 - pass our months in for our x-axis label value along with our dataSets
    let data: LineChartData = LineChartData(xVals: months, dataSets: dataSets)
    data.setValueTextColor(UIColor.white)

    //5 - finally set our data
    self.lineChartView.data = data


}

Im receiving this error:

Cannot invoke initializer for typel 'LineChartData' with an argument list of type '(xVals: [String], dataSets: [LineChartDataSet])'

Thank You,

Denis

11
  • Can someone help me out?
    – Denis
    Mar 6, 2017 at 2:46
  • Can you add the link of library.
    – Nirav D
    Mar 9, 2017 at 9:03
  • pod 'Charts'? Is that what you mean?
    – Denis
    Mar 9, 2017 at 9:14
  • Is using pods a good practice in your opinion?
    – Denis
    Mar 9, 2017 at 9:15
  • There is no issue with pod, I was just asking about the chart library I got it you are using Library with name Charts. Let me check that one will respond you.
    – Nirav D
    Mar 9, 2017 at 9:24

2 Answers 2

3

Working code snippet: (Swift 3.0 and Chart 3.0)

fileprivate func setChart(_ lineChartView: LineChartView, dataPoints: [Double], values: [Double]) {    
  var dataEntries: [ChartDataEntry] = []

  for i in 0..<dataPoints.count {
    let dataEntry = ChartDataEntry(x: Double(i), y: values[i])
    dataEntries.append(dataEntry)
  }

  let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "Altitude")
  lineChartDataSet.setColor(UIColor.blue)
  // lineChartDataSet.drawCubicEnabled = true
  lineChartDataSet.mode = .cubicBezier
  lineChartDataSet.drawCirclesEnabled = false
  lineChartDataSet.lineWidth = 1.0
  lineChartDataSet.circleRadius = 5.0
  lineChartDataSet.highlightColor = UIColor.red
  lineChartDataSet.drawHorizontalHighlightIndicatorEnabled = true

  var dataSets = [IChartDataSet]()
  dataSets.append(lineChartDataSet)

  let lineChartData = LineChartData(dataSets: dataSets)

  lineChartView.data = lineChartData
}
0
0

A hint for how you can figure out the overloads and I use all the time. In the "Find Navigator" in xCode you can search for "class LineChartData" -- then you can go examine the code in your pod where the "LineChartData" class is defined and see what arguments are in its init methods. Anyway, the answer I believe, base on this would be to change the line where you say:

 let data: LineChartData = LineChartData(xVals: months, dataSets: dataSets)

to THIS

 let data: LineChartData = LineChartData(dataSets: dataSets)

This matches the available overloads. If too late for you, above may help somebody else.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.