Home > Enterprise >  how to set a specific color if the condition is true in swift
how to set a specific color if the condition is true in swift

Time:12-14

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
    }

I tried to set color to a specific slice if the condition is true but it set whole chart color

set a specific color if the condition is true .......

CodePudding user response:

Make a array for colors like

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

And in condition

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

Then set the colors for chart

 set.colors = colors

Make sure the colors count must be equal to your data count.

  • Related