Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to adding multiple lines to Line Chart by using "Charts" for swift 4.0?

I would like to use cocoapods "Charts", and want to have multiple lines in LinChart for swift, But I can not find out solution for swift 4.0.

I try to use this website to show, but it wont't show anything

following is my code:

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
}

and xCode return message

*C is the project name

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.

How can I adding multiple lines to Line Chart for swift 4.0?

Charts Environment:

Xcode version:10.0

Swift version:4.0

like image 739
Peggy Avatar asked Dec 02 '22 10:12

Peggy


1 Answers

let data = LineChartData()
var lineChartEntry1 = [ChartDataEntry]()

for i in 0..<x1.count {
    lineChartEntry1.append(ChartDataEntry(x: Double(i), y: Double(x1[i]) ?? 0.0))
}
let line1 = LineChartDataSet(values: lineChartEntry1, label: "First Dataset")
data.addDataSet(line1)
if (x2.count > 0) {
    var lineChartEntry2 = [ChartDataEntry]()
    for i in 0..<x2.count {
        lineChartEntry2.append(ChartDataEntry(x: Double(i), y: Double(x2[i]) ?? 0.0))
    }
let line2 = LineChartDataSet(values: lineChartEntry2, label: "Second Dataset")
data.addDataSet(line2)
}
if (x3.count > 0) {
    var lineChartEntry3 = [ChartDataEntry]()
    for i in 0..<x3.count {
        lineChartEntry3.append(ChartDataEntry(x: Double(i), y: Double(x3[i]) ?? 0.0))
    }
    let line3 = LineChartDataSet(values: lineChartEntry3, label: "Third Dataset")
    data.addDataSet(line3)
}
self.myChartView.data = data

I am using above code and it is working x1,x2 and x3 are data of line chart's. Could you please try it?

like image 60
atalayasa Avatar answered Apr 30 '23 16:04

atalayasa