How to change the bar colors in the columnchart..

1,812 views
Skip to first unread message

Surya

unread,
Nov 21, 2011, 8:31:48 AM11/21/11
to Google Visualization API
Heps,

I am really stuck up with this for a while and need the help asap.

I want to have different colors for different bars in the columnchart.
There is a provision to change the color of all the bars using colors:
option but i want different colors for different bars..

for instance in the example referred in the url...
http://code.google.com/apis/chart/interactive/docs/gallery/columnchart.html

for 2004 two bards are shown in blue and red and the same for other
years.. but my requirement is to have blue color (both the bars ) in
2004. red for both bars for 2005 and green for 2006 and red for 2007.

How can i achieve this??
Pl help me ..

Even if there is any alternative also pl suggest..

~Surya~

asgallant

unread,
Nov 21, 2011, 1:49:20 PM11/21/11
to google-visua...@googlegroups.com
There is a way to hack around the problem which has been discussed extensively on this forum (search for 'bar colors'), ex: https://groups.google.com/d/msg/google-visualization-api/BPNbQ3GKCdg/R-7P0RwBOq8J

Roni Biran

unread,
Nov 21, 2011, 1:59:39 PM11/21/11
to google-visua...@googlegroups.com
i wrote you a nice hack that will do the trick.

I think that it's self explanatory, but if you'll have any questions feel free to ask:

here is the JavaScript for it:

function drawVisualization({
  var data new google.visualization.DataTable();
  data.addColumn('string''Year');
  data.addColumn('number''Sales');
  data.addColumn('number''Expenses');
  data.addRows(4);
  data.setValue(00'2004');
  data.setValue(011000);
  data.setValue(02400);
  data.setValue(10'2005');
  data.setValue(111170);
  data.setValue(12460);
  data.setValue(20'2006');
  data.setValue(21660);
  data.setValue(221120);
  data.setValue(30'2007');
  data.setValue(311030);
  data.setValue(32540);
  
  var chart new google.visualization.ColumnChart(document.getElementById('visualization'));
  chart.draw(datawidth640height480title'Company Performance',
                     vAxistitle'Year'titleTextStylecolor'red'},
                     legend'none'colors['#cc00cc''#ccffcc']
                    });
  
  changeColors();
  
}

function changeColors({
  var chartArea document.getElementsByTagName('iframe')[0].contentDocument.getElementById('chartArea');
  var nodes chartArea.getElementsByTagName('rect');
  
  // finding all <rect> elements with #cc00cc fill color and replacing them with 'blue','red','green','blue'
  for (var 0nodes.lengthi++{
    var node nodes[i];
    if (node.getAttribute('fill'&& node.getAttribute('fill'== '#cc00cc'{
      switch (4{
        case 0:
          node.setAttribute('fill''blue');
          break;
        case 1:
          node.setAttribute('fill''red');
          break;
        case 2:
          node.setAttribute('fill''green');
          break;
        case 3:
          node.setAttribute('fill''red');
          break;
      }
    }
  }
  
  // finding all <rect> elements with #ccffcc fill color and replacing them with 'blue','red','green','blue'
  for (var 0nodes.lengthi++{
    var node nodes[i];
    if (node.getAttribute('fill'&& node.getAttribute('fill'== '#ccffcc'{
      switch (4{
        case 0:
          node.setAttribute('fill''blue');
          break;
        case 1:
          node.setAttribute('fill''red');
          break;
        case 2:
          node.setAttribute('fill''green');
          break;
        case 3:
          node.setAttribute('fill''red');
          break;
      }
    }
  }
}

good luck :-)





On Mon, Nov 21, 2011 at 8:49 PM, asgallant <drew_g...@abtassoc.com> wrote:
There is a way to hack around the problem which has been discussed extensively on this forum (search for 'bar colors'), ex: https://groups.google.com/d/msg/google-visualization-api/BPNbQ3GKCdg/R-7P0RwBOq8J

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-visualization-api/-/AikF9KPEcS4J.

To post to this group, send email to google-visua...@googlegroups.com.
To unsubscribe from this group, send email to google-visualizati...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-visualization-api?hl=en.

asgallant

unread,
Nov 21, 2011, 2:11:19 PM11/21/11
to google-visua...@googlegroups.com
I like that approach - it solves some messy DataTable problems and allows for integration with some of the other API tools.

Surya

unread,
Nov 21, 2011, 2:21:02 PM11/21/11
to Google Visualization API
Excellent Roni!!

Thank you very much..
Now atleast i got some clue/hint on how to handle this..
I got the logic of changing the colors depending on the color you are
assigning to the main chart,..

Actually what i want is to change the color depending on the values
which are set for the bars..
Any hint Pl?

> On Mon, Nov 21, 2011 at 8:49 PM, asgallant <drew_gall...@abtassoc.com>wrote:
>
>
>
>
>
>
>
> > There is a way to hack around the problem which has been discussed
> > extensively on this forum (search for 'bar colors'), ex:

> >https://groups.google.com/d/msg/google-visualization-api/BPNbQ3GKCdg/...

Surya

unread,
Nov 21, 2011, 2:21:52 PM11/21/11
to Google Visualization API
Thank you for the response. This is not what i am looking for..

BR,
Surya

On Nov 21, 11:49 pm, asgallant <drew_gall...@abtassoc.com> wrote:
> There is a way to hack around the problem which has been discussed

> extensively on this forum (search for 'bar colors'), ex:https://groups.google.com/d/msg/google-visualization-api/BPNbQ3GKCdg/...

asgallant

unread,
Nov 21, 2011, 3:10:48 PM11/21/11
to google-visua...@googlegroups.com
You can do this by mixing views into the methods I've described on the forums.  Something like this: http://jsfiddle.net/6NkWx/1/

Surya

unread,
Nov 21, 2011, 3:20:54 PM11/21/11
to Google Visualization API
Thank you for the response.
I have checked that and looks good.. but for my query i think Roni
suggestion is better but i have a challenge in that..
The moment i do a mouseover on the bar.. the original color defined is
coming back

I guess this is because of the tooltip shown on top of the columnchart
is perhaps redrawing the bar..
even i put some dummy callback mouseover and mouselick events but
still the color gets changed..

my intention is color the bars depending on the values assigned to
them...

BR,
Surya

asgallant

unread,
Nov 21, 2011, 3:41:36 PM11/21/11
to google-visua...@googlegroups.com
Try calling changecolors() from the 'onmouseover' and 'onmouseout' events:


google.visualization.events.addListener(chart'onmouseover'function ({
    changeColors();
});
google.visualization.events.addListener(chart'onmouseout'function ({
    changeColors();
});

Surya

unread,
Nov 21, 2011, 11:06:19 PM11/21/11
to Google Visualization API
I tried already :)

Its not working .!!!

Roni Biran

unread,
Nov 22, 2011, 2:41:56 AM11/22/11
to google-visua...@googlegroups.com
Hi and sorry for the delay (it was night where i live....)

What asgallant suggested can work with some modifications.

pass the event to the coloring function like this:

google.visualization.events.addListener(chart, 'onmouseover', function (evt) {
changeColors(evt);
});
google.visualization.events.addListener(chart, 'onmouseout', function (evt) {
changeColors(evt);
});

also I changed the function to use this to replace the color to the correct one.
the event passing in the listener holds both datatable column and row of the hovered column, thus allows us to decide on the color we want to show.

so now, it will look like this:

function changeColors(evt) {
  var chartArea = document.getElementsByTagName('iframe')[0].contentDocument.getElementById('chartArea');
  var nodes = chartArea.getElementsByTagName('rect');
  
  if (evt) {
    // replacing the over/out element
    for (var i = 0; i < nodes.length; i++) {
      var node = nodes[i];
      if (node.getAttribute('fill') && (node.getAttribute('fill') == '#ccffcc' || node.getAttribute('fill') == '#cc00cc')) {
        switch (evt.row % 4) {
          case 0:
            node.setAttribute('fill', 'blue');
            break;
          case 1:
            node.setAttribute('fill', 'red');
            break;
          case 2:
            node.setAttribute('fill', 'green');
            break;
          case 3:
            node.setAttribute('fill', 'red');
            break;
        }
 }
}
  } else {

please bear in mind that there are mouse movement limitations. if you move your mouse too fast, the script have some glitches. sorry about that ;-)

let me know how it works. (or not work)



--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
Reply all
Reply to author
Forward
0 new messages