ioschart not showing the linechart

2019-09-21 17:53发布

问题:

I'm having a issue in displaying a simple linechart using ioschart library.

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:

回答1:

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.



回答2:

Try this:

func draw() {

    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]

    var lineChartEntry = [ChartDataEntry]()

             for i in 0..<y.count {
               let value = ChartDataEntry(x: x[i], y: y[i])
               lineChartEntry.append(value)

        }

        let line1 = LineChartDataSet(values: lineChartEntry, label: "Data")
        line1.axisDependency = .left

        let data = LineChartData()
        data.addDataSet(line1)
        self.lineChartView.data = data

}