4

Hi would like to change the background color of my iOS-chart.

I tried:

barChartView.backgroundColor = UIColor.redcolor()

But it changed the color around my bargraph, not the color inside, ie. behind the vertical bars.

Any Idea?

Larme
  • 24,190
  • 6
  • 51
  • 81
Eric
  • 592
  • 10
  • 26

3 Answers3

10

barChartView.backgroundColor references the UIView of which your BarChartView is a subview. You need to reference the base class of the BarChart itself to change the properties of the bars or the grid behind the bars. To change the color of the space behind the bars try:

barChartView.gridBackgroundColor = UIColor.redcolor()
Japes
  • 393
  • 5
  • 11
  • 1
    it would me great if u can explain the answer also for a easy understanding for another person having similar problem – Kumar Saurabh Sep 30 '15 at 15:13
  • 2
    You also need to write the following line of code: barChartView.drawGridBackgroundEnabled = true – levan Apr 24 '19 at 12:10
8

If Japes answer still does not work, make sure it's enabled.

barChartView.drawGridBackgroundEnabled = true
Eric Hodgins
  • 546
  • 1
  • 9
  • 14
  • 2
    I don't know why this is down voted. By default drawGridBackgroundEnabled is false and setting its color does nothing. – Wilson May 02 '16 at 02:58
2
// Set grid background color
barChartView.gridBackgroundColor = UIColor.orange

// It is required to enable 'drawGridBackgroundColor' to see effect of gridBackground color 
barChartView.drawGridBackgroundEnabled = true
Krunal
  • 77,632
  • 48
  • 245
  • 261