Swift IOS图表-堆积条形图

时间:2019-06-03 08:19:56

标签: swift charts

我正在尝试使用iOS Chart设置一个简单的堆积条形图。

所有示例都显示了数据循环-我只想直接在代码中写入一个条目。

https://github.com/danielgindi/Charts/blob/master/ChartsDemo-iOS/Swift/Demos/StackedBarChartViewController.swift

我在let set = BarChartDataSet.

坠毁

错误是:

  

无法将“ BarChartDataEntry”类型的值转换为预期参数   键入“ [ChartDataEntry]?”

    func _costChart( comm: Double, margin: Double, expense: Double) {

        let yVals = BarChartDataEntry(x: 0.0, yValues: [comm, margin, expense ], icon: #imageLiteral(resourceName: "icon"))

        let set = BarChartDataSet(entries: yVals, label: "")

        let data = BarChartData(dataSets: set)

        costChart.data = data
}

我该如何写以确认为正确的格式?

1 个答案:

答案 0 :(得分:0)

BarChartDataSet期望为ChartDataEntry可为空的数组。 因此,如果要设置数据,则需要传递nil或数组。

func _costChart( comm: Double, margin: Double, expense: Double) {

   let yVals = BarChartDataEntry(x: 0.0, yValues: [comm, margin, expense ], icon: #imageLiteral(resourceName: "icon"))

   let set = BarChartDataSet(entries: [yVals], label: "") // Passing array to entries instead of just the BarChartDataEntry (just a single item)

   let data = BarChartData(dataSets: set)

   costChart.data = data 

}