Search code examples
iosswiftuicolorios-charts

cannot set custom colors for pie charts


I am using iOS charts to make a pie chart, and I cannot figure out how to set the colors as custom colors with RGBA values. here is the code

        var  colors: [UIColor] = []
        colors.append(UIColor.init(red: 206, green: 173, blue: 128, alpha: 1))
        colors.append(UIColor.init(red: 136, green: 169, blue: 132, alpha: 1))
        colors.append(UIColor.init(red: 183, green: 73, blue: 32, alpha: 1))
        colors.append(UIColor.init(red: 106, green: 152, blue: 150, alpha: 1))
        colors.append(UIColor.init(red: 226, green: 120, blue: 123, alpha: 1))
        colors.append(UIColor.init(red: 47, green: 122, blue: 153, alpha: 1))

        chartDataSet.colors = colors

but it doesn't work and the chart looks like

this

edit: here is the rest of the function


    func updateChartData() {

        let chartDataSet = PieChartDataSet(entries: moods, label: nil)
        let chartData = PieChartData(dataSet: chartDataSet)
        chartDataSet.drawValuesEnabled = false
        pieChartVIew.drawEntryLabelsEnabled = false

        var  colors: [UIColor] = []
        colors.append(UIColor.init(red: 206, green: 173, blue: 128, alpha: 1))
        colors.append(UIColor.init(red: 136, green: 169, blue: 132, alpha: 1))
        colors.append(UIColor.init(red: 183, green: 73, blue: 32, alpha: 1))
        colors.append(UIColor.init(red: 106, green: 152, blue: 150, alpha: 1))
        colors.append(UIColor.init(red: 226, green: 120, blue: 123, alpha: 1))
        colors.append(UIColor.init(red: 47, green: 122, blue: 153, alpha: 1))

        chartDataSet.colors = colors


        pieChartVIew.holeColor = UIColor.clear
        pieChartVIew.data = chartData
}

and here is where I set the values

    var happyDataEntry = PieChartDataEntry(value: 0)
    var happyCount = 0
    var moods = [PieChartDataEntry]()

    override func viewDidLoad() {
        super.viewDidLoad()

        happyDataEntry.value = Double(happyCount)
        happyDataEntry.label = "happy!"
        moods = [happyDataEntry}
        updateChartData()

}

Solution

  • I have updated your code a bit: (Value changed to: var happyCount = 1)

    var happyDataEntry = PieChartDataEntry(value: 0)
    var happyCount = 1
    var moods = [PieChartDataEntry]()
    

    viewDidLoad Method:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        happyDataEntry.value = Double(happyCount)
        happyDataEntry.label = "happy!"
    //  moods.append(happyDataEntry)   //If you uncomment this line you will see pie chart with 2 colors. 
        moods.append(happyDataEntry)
        updateChartData()
    }
    

    Updated Chart method: (I have updated the color init, please take a look. )

    func updateChartData() {        
        let chartDataSet = PieChartDataSet(entries: moods, label: nil)
        let chartData = PieChartData(dataSet: chartDataSet)
        chartDataSet.drawValuesEnabled = false
        pieChartVIew.drawEntryLabelsEnabled = false
    
        var  colors: [UIColor] = []
        colors.append(UIColor.init(red: 206.0/255.0, green: 173.0/255.0, blue: 128.0/255.0, alpha: 1.0))
        colors.append(UIColor.init(red: 136.0/255.0, green: 169.0/255.0, blue: 132.0/255.0, alpha: 1.0))
        colors.append(UIColor.init(red: 183.0/255.0, green: 73.0/255.0, blue: 32.0/255.0, alpha: 1.0))
        colors.append(UIColor.init(red: 106.0/255.0, green: 152.0/255.0, blue: 150.0/255.0, alpha: 1.0))
        colors.append(UIColor.init(red: 226.0/255.0, green: 120.0/255.0, blue: 123.0/255.0, alpha: 1.0))
        colors.append(UIColor.init(red: 47.0/255.0, green: 122.0/255.0, blue: 153.0/255.0, alpha: 1.0))
    
        chartDataSet.colors = colors
    
    
        pieChartVIew.holeColor = UIColor.clear
        pieChartVIew.data = chartData
    }
    

    Please comment if you have any questions.

    Happy to help!