Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm having an issue in displaying a simple line chart using iOS Charts

I followed this tutorial and it works.

When I try to put my data on the graph it displays correctly the grid and values on-axis, but not the line.

Here my code:

func setChartData(xLabel: [Double], yLabel: [Double]){

    let values = (0..<15).map { (i) -> ChartDataEntry in
        return ChartDataEntry(x: xLabel[i], y: yLabel[i])
    }

    let set1 = LineChartDataSet(values: values, label: "DataSet")
    let data = LineChartData(dataSet: set1)

    self.lineChartView.data = data

}

Then, in the viewDidLoad() I call my function and pass it these two lists:

let x = [19.0, 18.0, 17.0, 16.0, 15.0, 14.0, 13.0, 12.0, 11.0, 10.0, 9.0, 8.0, 7.0, 6.0, 5.0]

let y = [1.5587, 1.5591999999999999, 1.5587, 1.5587, 1.5604, 1.5528, 1.5544, 1.5427, 1.5475000000000001, 1.5475000000000001, 1.5475000000000001, 1.5402, 1.5488, 1.5445, 1.5468]

The result that I obtain is like that: enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
168 views
Welcome To Ask or Share your Answers For Others

1 Answer

Just Replace your declaration of 'x' with this in ViewDidLoad with

let x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

I hope it will work for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...