如何设置一个特定的颜色如果条件是真的在swift

kjthegm6  于 2022-12-17  发布在  Swift
关注(0)|答案(1)|浏览(146)
var chartdataentries = [ChartDataEntry]()
        for i in 0..<days.count{
            chartdataentries.append(ChartDataEntry(x: Double(data[i]), y: 1.0, data: data[i]))
        }
        let set = PieChartDataSet(entries: chartdataentries)
        set.sliceSpace = 1
        for i in 0..<data.count{
        if (chartdataentries[i].x >= 30){
            set.colors = setcolor()
            }
        }
        let data = PieChartData(dataSet: set)
        data.setDrawValues(false)
        pieChart.data = data
    }

如果条件为真,我尝试为特定切片设置颜色,但它设置了整个图表的颜色
如果条件为真,则设置特定颜色.......

tp5buhyn

tp5buhyn1#

为颜色创建一个数组,如

var colors : [UIColor] = [UIColor.red,UIColor.blue,...]

而且状况良好

if (chartdataentries[i].x >= 30){
        colors[i] = UIColor.purple // your desired color
        }
 }

然后设置图表的颜色

set.colors = colors

确保colors计数必须等于数据计数。

相关问题