Swift 3折线图 [英] Line Chart in swift 3

查看:106
本文介绍了Swift 3折线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此代码创建折线图.我花了数小时来尝试各种不同事情的大部分代码. 在课堂上:

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

插座:

@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 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


}

我收到此错误:

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

谢谢

丹尼斯

推荐答案

关于如何计算过载的提示,我一直在使用.在xCode的查找导航器"中,您可以搜索"LineChartData类"-然后可以检查定义了"LineChartData"类的Pod中的代码,并查看其init方法中包含哪些参数.无论如何,我相信基于此的答案将是更改您所说的行:

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)

到此

 let data: LineChartData = LineChartData(dataSets: dataSets)

这与可用的重载匹配.如果对您来说太迟了,上面的内容可能会对其他人有所帮助.

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

这篇关于Swift 3折线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆