xcode swift 3 中的折线图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42607702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:49:18  来源:igfitidea点击:

Line Chart in swift 3

xcodechartsswift3

提问by Denis

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

丹尼斯

采纳答案by Joyful Machines

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:

关于如何找出重载的提示,我一直在使用。在 xCode 的“Find Navigator”中,您可以搜索“class LineChartData”——然后您可以检查 pod 中定义了“LineChartData”类的代码,并查看其 init 方法中有哪些参数。无论如何,我相信基于此的答案是更改您说的行:

 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.

这匹配可用的重载。如果对你来说太晚了,上面可能会帮助别人。

回答by Sanjay Mali

Working code snippet: (Swift 3.0 and Chart 3.0)

工作代码片段:(Swift 3.0 和 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
}