如何使用Swift 4.0的“图表”向折线图中添加多条线?

时间:2018-11-06 09:32:45

标签: swift charts linechart

我想使用cocoapods“图表”,并希望在LinChart中有多行以便快速进行,但是我找不到swift 4.0的解决方案。

我尝试使用this网站进行展示,但不会显示任何内容

以下是我的代码:

let dollars1 = [10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0]
let dollars2 = [20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0]
let dollars3 = [30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0]
let months = ["Jan" , "Feb", "Mar", "Apr", "May", "June", "July"]

func setChartData(months : [String]) {

    var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
    for i in 0..<months.count{
        yVals1.append(ChartDataEntry(x: dollars1[i], y: Double(i)))
    }

    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))
    set1.setCircleColor(UIColor.red)
    set1.lineWidth = 2.0
    set1.circleRadius = 6.0
    set1.fillAlpha = 65 / 255.0
    set1.fillColor = UIColor.red
    set1.highlightColor = UIColor.white
    set1.drawCircleHoleEnabled = true

    var yVals2 : [ChartDataEntry] = [ChartDataEntry]()
    for i in 0..<months.count{
        yVals2.append(ChartDataEntry(x: dollars2[i], y: Double(i)))
    }

    let set2: LineChartDataSet = LineChartDataSet(values: yVals2, label: "Second Set")
    set2.axisDependency = .left // Line will correlate with left axis values
    set2.setColor(UIColor.green.withAlphaComponent(0.5))
    set2.setCircleColor(UIColor.green)
    set2.lineWidth = 2.0
    set2.circleRadius = 6.0
    set2.fillAlpha = 65 / 255.0
    set2.fillColor = UIColor.green
    set2.highlightColor = UIColor.white
    set2.drawCircleHoleEnabled = true

    var yVals3 : [ChartDataEntry] = [ChartDataEntry]()
    for i in 0..<months.count{
        yVals3.append(ChartDataEntry(x: dollars3[i], y: Double(i)))
    }

    let set3: LineChartDataSet = LineChartDataSet(values: yVals3, label: "Second Set")
    set3.axisDependency = .left // Line will correlate with left axis values
    set3.setColor(UIColor.blue.withAlphaComponent(0.5))
    set3.setCircleColor(UIColor.blue)
    set3.lineWidth = 2.0
    set3.circleRadius = 6.0
    set3.fillAlpha = 65 / 255.0
    set3.fillColor = UIColor.blue
    set3.highlightColor = UIColor.white
    set3.drawCircleHoleEnabled = true

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

    //4 - pass our months in for our x-axis label value along with our dataSets
    let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "Units Sold")
    let lineChartData = LineChartData(dataSet: lineChartDataSet)

    //5 - finally set our data
    self.mapLineChartView.data = lineChartData
}

和xCode返回消息

* C是项目名称

2018-11-06 17:20:12.254966+0800 C[42515:2004044] [Unknown process name] CGAffineTransformInvert: singular matrix.
2018-11-06 17:20:12.255111+0800 C[42515:2004044] [Unknown process name] CGAffineTransformInvert: singular matrix.
2018-11-06 17:20:12.256968+0800 C[42515:2004044] [Unknown process name] CGAffineTransformInvert: singular matrix.
2018-11-06 17:20:12.257078+0800 C[42515:2004044] [Unknown process name] CGAffineTransformInvert: singular matrix.

如何在Swift 4.0的折线图中添加多条线?

图表环境:

  

Xcode版本:10.0

     

快速版本:4.0

2 个答案:

答案 0 :(得分:4)

     constructor(private doctypesService: DoctypesService, private categoriasService: CategoriesService, 
          private metadataService: MetadataService, private breadcrumbService: BreadcrumbService, public translate: TranslateService) {
this.breadcrumbService.setItems([
    { label: this.translate.instant('Administración') },
    { label: this.translate.instant('Categorías'), routerLink: ['/categorias'] }
]);

我正在使用上面的代码,它正在工作x1,x2和x3是折线图的数据。你可以试一下吗?

答案 1 :(得分:-1)

对于第3步和第4步,只需使用此:

let lineChartData = LineChartData(dataSets: [set1, set2, set3])
self.mapLineChartView.data = lineChartData