Different colors for bars at BarChart depend on value.

How can I change the color of a single line depending on its value on the histogram? For example: I have five different values ​​(= five different bars) on my histogram. All bars that have a value of less than 30 should be red, all bars from 30 to 70 are orange, and all bars above 70 should be green.

+4
source share
2 answers

In ios diagrams, the colors of the bars are set in an array. For example, if your dataset is called barChartDataset, you should set the colors as follows

barChartDataset.colors = [UIColor.red,UIColor.orange,UIColor.green,UIColor.black,UIColor.blue]

Bars will have these colors in this order and will be repeated. So if you have 10 bars, you will have 2 red bars, etc.

. .

func setColor(value: Double) -> UIColor{

    if(value < 30){
        return UIColor.red
    }
    else if(value <= 70 && value >= 30){
        return UIColor.orange
    }
    else if(value > 70){
        return UIColor.green
    }

    else { //In case anything goes wrong
    return UIColor.black
    }
}

, ,

 barChartDataset.colors = [setColor(barOneValue),setColor(barTwoValue),setColor(barThreeValue),setColor(barFourValue),setColor(barFiveValue)]

, !

+5

API , . :

  • ChartColorTemplates.liberty()
  • ChartColorTemplates.joyful()
  • ChartColorTemplates.pastel()
  • ChartColorTemplates.colorful()
  • ChartColorTemplates.vordiplom()

:

chartDataSet.colors = ChartColorTemplates.colorful()

: https://www.appcoda.com/ios-charts-api-tutorial/

-1

Source: https://habr.com/ru/post/1659913/


All Articles