简体   繁体   中英

Swift IOS Chart - Stacked Bar Chart

I am trying to setup a simple stacked bar chart using 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

I am crashing at the: let set = BarChartDataSet.

Error is:

Cannot convert value of type 'BarChartDataEntry' to expected argument type '[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 . So if you want to set the data, you need to pass nil or a array.

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 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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