How do i change React Google Charts background color?
Asked Answered
C

2

6

I am trying to change the background color of the charts to match my body background color.

I have tried to do the two following backgroundColor styles below but it doesn't seem to work. Any ideas on how i can get around this?

<Chart
                height={'300px'}
                width={'370px'}
                chartType='Bar'
                loader={<div>Loading Chart..</div>}
                data={[
                  ['', 'UPLIFT'],
                  ...topSpend.map(d => [d.PRODUCT, d.UPLIFT]),
                ]}
                options={{
                  chart: {
                    title: 'Spend Uplift',
             -----> backgroundColor: 'red'
                  },
                  colors: ['#FB7A21']
                }}
              />

<Chart
                height={'300px'}
                width={'370px'}
                chartType='Bar'
                loader={<div>Loading Chart..</div>}
                data={[
                  ['', 'UPLIFT'],
                  ...topSpend.map(d => [d.PRODUCT, d.UPLIFT]),
                ]}
                options={{
                  chart: {
                    title: 'Spend Uplift',
                  },
                  colors: ['#FB7A21'],
           -----> backgroundColor: 'red'
                }}
              />
Charmainecharmane answered 19/8, 2019 at 16:21 Comment(1)
According to the official docs you should be able to use as is.Waterproof
P
3

You need to use chartType as BarChart. For some reason, Bar seems to cause issues. In case you want the chart bars to be vertical, change it to ColumnChart.

You can see the working fiddle here

Projector answered 19/8, 2019 at 16:42 Comment(1)
Thanks for the help, I've started using ColumnChart now.Charmainecharmane
D
1

'Bar' is considered a Material chart.

vs. 'BarChart', which is a Classic chart.

with Material charts, an options conversion method needs to be used...

google.charts.Bar.convertOptions

which means you would need to access the google.charts namespace.

options={google.charts.Bar.convertOptions({
  chart: {
    title: 'Spend Uplift',
  },
  colors: ['#FB7A21'],
  backgroundColor: 'red'
})}
Dug answered 19/8, 2019 at 16:47 Comment(1)
Thanks for the help. I couldn't access google.charts namespace so I instead used ColumnChart and everything is working.Charmainecharmane

© 2022 - 2024 — McMap. All rights reserved.