ioschart 不显示折线图

ioschart not showing the linechart

我在使用 iOS Charts

显示简单折线图时遇到问题

我按照 this 教程进行操作并且有效。

当我尝试将我的数据放在图表上时,它正确显示了轴上的网格和值,但不是直线。

这是我的代码:

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

}

然后,在 viewDidLoad() 中调用我的函数并将这两个列表传递给它:

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]

我得到的结果是这样的:

试试这个:

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

}

只需在 ViewDidLoad 中将 'x' 的声明替换为

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

希望对你有用。

为了查看结果,x 列表应升序

在我的例子中,它将是:

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