David Weisser
David Weisser

Reputation: 316

Variable Colors in Google Bar Charts

I have a survey web app that serves a number different kinds of questions to Google Chart. In fact there are 26 of them. I need to change the colors of the bars depending on the question case. I CAN accomplish this, by copying the var options inside of each if (questioncase = 'X') 26 times - but I'm hoping for a short cut. Any thoughts would, as always be, much appreciated.

var options = {
 //width:w,
 width:500,
 height:h,
 isStacked:true,
 chartArea:{height:chartHeight,left:l,width:cw},
 backgroundColor:'transparent',
 bar:{groupWidth:'80%'},
 tooltip: {isHtml:true},
 legend:{position:pos,maxLines:50},
 hAxis: {title: 'Percentage',minValue:0,maxValue:100},
 hAxis: { textPosition: 'none',ticks: [0]},
 colors: ['#eeeeee', '#eeeeee', '#e4d00a', '#b30000', '#990000']
 }

if (questioncase == 'A') {

    var options = {
        colors: ['#134e13', '#008900', '#e4d00a', '#b30000', '#990000']
    }
}

Upvotes: 1

Views: 89

Answers (1)

WhiteHat
WhiteHat

Reputation: 61212

You can reuse the same options object, just change the colors property.

if (questioncase == 'A') {
  options.colors = ['#134e13', '#008900', '#e4d00a', '#b30000', '#990000'];
}

Upvotes: 1

Related Questions