简体   繁体   English

Swift IOS图表 - 堆积条形图

[英]Swift IOS Chart - Stacked Bar Chart

I am trying to setup a simple stacked bar chart using iOS Chart . 我正在尝试使用iOS Chart设置一个简单的堆积条形图。

the examples all show looping though data - I just want to write one entry straight into the code. 示例都显示循环数据 - 我只是想直接在代码中写入一个条目。

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

I am crashing at the: let set = BarChartDataSet. 我崩溃了: let set = BarChartDataSet.

Error is: 错误是:

Cannot convert value of type 'BarChartDataEntry' to expected argument type '[ChartDataEntry]?' 无法将'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
}

How can I write this to confirm to the proper format? 我怎么写这个以确认正确的格式?

BarChartDataSet expects a nullable array of ChartDataEntry . BarChartDataSet需要一个可以为null的ChartDataEntry数组。 So if you want to set the data, you need to pass nil or a array. 因此,如果要设置数据,则需要传递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 

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM