为每个堆栈条形图上色

时间:2020-09-21 05:29:36

标签: ios swift

我正在使用 danielgindi

的库 iOS图表

我想以此处理每个条形图的颜色:

enter image description here

对此:

enter image description here

let xVals: [DashboardChartItem] =  [DashboardChartItem(name: "taskA", color: [.red], value: 1.0,yVals: [6.0]),                                        DashboardChartItem(name: "taskB", color: [.orange], value: 2.0, yVals: [1.0, 2.0]),DashboardChartItem(name: "taskC",color: [.yellow], value: 3.0,yVals: [1.0, 1.0, 2.0])]


override func viewDidLoad() {
    super.viewDidLoad()
    setDataCount(items: xVals)
}

func setDataCount(items: [DashboardChartItem]) {

    let yVals = items.map { (i) -> BarChartDataEntry in
        var yStackVal: [Double] = []
        yStackVal.append(contentsOf: i.yVals)
        return BarChartDataEntry(x: i.value, yValues: yStackVal)
    }
    
    let set = BarChartDataSet(entries: yVals, label: "")
    set.drawIconsEnabled = false
    set.barBorderColor = .white
    set.barBorderWidth = 3
    set.colors = [.red,.orange,.yellow]
    
    
    let data = BarChartData(dataSet: set)
    data.setValueTextColor(.clear)
    
    
    barChartView.fitBars = true
    barChartView.data = data

请提出建议

0 个答案:

没有答案
相关问题