Google Chart, different color for each bar
Asked Answered
E

6

17

I have this Google Bar Chart:

  function drawChart() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', '');
    data.addColumn('number', '');
    data.addRows(2);
    data.setValue(0, 0, 'Value 1');
    data.setValue(0, 1, 250);
    data.setValue(1, 0, 'Value 2');
    data.setValue(1, 1, 100);

    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    chart.draw(data, {
        width: 400, 
        height: 175, 
        title: 'Total',
        legend: 'none',
    });
  }

All runs OK, but the thing is, both bars have the same color, and I would like to be able to have different colors for each bar.

Can anyone help me on this?

Ejective answered 16/6, 2011 at 16:24 Comment(0)
A
13

One hacky way to do things is put them all in the same row, and API would assign distinct colors to this. Hence for your example

function drawChart(){

    var data = new google.visualization.DataTable();
    data.addColumn('number', 'Value 1');
    data.addColumn('number', 'Value 2');
    data.addRows(2);
    data.setValue(0, 0, 250);
    data.setValue(0, 1, 100);

    var chart = new google.visualization.ColumnChart(document.getElementById('visualization'));

    chart.draw(data, {
        width: 600, 
        height: 175, 
        title: 'Total',
        legend: 'none',
    });
} 

If you need your own colors add this in the chart.draw options like,

colors: ['red','yellow', 'blue'],

If there are too many bars though, this may be a bad option for that please look at

http://code.google.com/apis/chart/interactive/docs/reference.html#barformatter

Archenemy answered 16/6, 2011 at 17:36 Comment(1)
Nice! Thanks! Just what I needed.Ejective
D
23

Not sure why no-one mentioned style role columns - I assume they were added after the initial question, but for anyone looking for this now, a better way is:

function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', '');
        data.addColumn('number', '');
        data.addColumn({ type: 'string', role: 'style' });

        data.addRows(2);

        data.setValue(0, 0, 'Value 1');
        data.setValue(0, 1, 250);
        data.setValue(0, 2, 'rgb(200, 20, 60)');
        data.setValue(1, 0, 'Value 2');
        data.setValue(1, 1, 100);
        data.setValue(1, 2, 'rgb(20, 200, 60)');

        var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
        chart.draw(data, {
                width: 400, 
                height: 175, 
                title: 'Total',
                legend: 'none',
        });
    }

You can set many other CSS styles to make your charts REALLY ugly.

https://developers.google.com/chart/interactive/docs/gallery/columnchart#Colors

NOTE that it does not seem to support rgba() specified colors - you have to set opacity on the style role.

Here's a fiddle:

http://jsfiddle.net/a1og7rq4/

SIDENOTE: If you have multiple series, then you need a style role column after each series data column.

Here is another fiddle showing that (with opacity as well): http://jsfiddle.net/v5hfdm6c/1

Here is the modified function (left the unmodified one above for clarity)

function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', '');
data.addColumn('number', '');
data.addColumn({ type: 'string', role: 'style' });
data.addColumn('number', '');
data.addColumn({ type: 'string', role: 'style' });

data.addRows(2);

var i = 0;
data.setValue(0, i++, 'Value 1');
data.setValue(0, i++, 200);
data.setValue(0, i++, 'color:rgb(200, 20, 60); opacity:0.5');
data.setValue(0, i++, 250);
data.setValue(0, i++, 'rgb(200, 20, 60)');

i = 0;
data.setValue(1, i++, 'Value 2');
data.setValue(1, i++, 120);
data.setValue(1, i++, 'color:rgb(20, 200, 60); opacity:0.5');
data.setValue(1, i++, 100);
data.setValue(1, i++, 'rgb(20, 200, 60)');

var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, {
    width: 400, 
    height: 175, 
    title: 'Total',
    legend: 'none',
});

}

Dolf answered 19/11, 2014 at 15:41 Comment(1)
I appreciate those who add fiddle example, it is a tool available and everyone should use it for examples. Thanks.Phaedrus
A
13

One hacky way to do things is put them all in the same row, and API would assign distinct colors to this. Hence for your example

function drawChart(){

    var data = new google.visualization.DataTable();
    data.addColumn('number', 'Value 1');
    data.addColumn('number', 'Value 2');
    data.addRows(2);
    data.setValue(0, 0, 250);
    data.setValue(0, 1, 100);

    var chart = new google.visualization.ColumnChart(document.getElementById('visualization'));

    chart.draw(data, {
        width: 600, 
        height: 175, 
        title: 'Total',
        legend: 'none',
    });
} 

If you need your own colors add this in the chart.draw options like,

colors: ['red','yellow', 'blue'],

If there are too many bars though, this may be a bad option for that please look at

http://code.google.com/apis/chart/interactive/docs/reference.html#barformatter

Archenemy answered 16/6, 2011 at 17:36 Comment(1)
Nice! Thanks! Just what I needed.Ejective
F
10

Exactly, you have to insert the colors attribute into the options variable.

...

var options = {
    width: 600, 
    height: 175, 
    title: 'Total',
    legend: 'none',
    colors: ['red', 'green']
};

chart.draw(data, options);
...
Floreneflorentia answered 10/1, 2012 at 10:45 Comment(0)
B
3

Please add this in options:

colors:['red','#009900']

Like:

var options = {
          title: 'Company Performance',
          vAxis: {title: 'Year',  titleTextStyle: {color: 'red'}},
          colors:['red','#009900']
};
Blastomere answered 9/1, 2013 at 7:16 Comment(0)
H
0

Here is my answer. It takes care of converting a array into dataTable

function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Season');
        data.addColumn('number', 'Deaths');
        data.addColumn({
            type: 'string',
            role: 'style'
        });

        var dataArray = new Array();
        dataArray[0] = ['Season 1', 1000, "#A0A0A0"]
        dataArray[1] = ['Season 2', 1170, "#FF3344"]
        dataArray[2] = ['Season 3', 660, "#BCBCBC"]
        dataArray[3] = ['Season 4', 1248, "#B3B3B8"]
        dataArray[4] = ['Season 5', 14353, "RED"];

        var charTitle = "Total deaths in Game of Thrones Series VS Screen Play Time";
        var noOfBars = dataArray.length;

        data.addRows(noOfBars);

        for (var row = 0; row < noOfBars; row++) {
            var label=dataArray[row][0];
            var value=dataArray[row][1];
            var color=dataArray[row][2];
                data.setValue(row, 0, label);
                data.setValue(row, 1, value);
                data.setValue(row, 2, color);
        }



    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    chart.draw(data, {
        width: 600,
        height: 300,
        title: charTitle,
    });
}

google.load("visualization", "1", {
    packages: ["corechart"],
    callback: function () {
        drawChart();
    }
});
Heiney answered 12/11, 2015 at 9:39 Comment(0)
M
0

For those who are using Pie Chart:

Add the slices

// Set chart options
    var options = {'title':'How Much Pizza I Ate Last Night',
                   'width':400,
                   'height':300,
                    slices: {
                        0: { color: 'yellow' },
                        1: { color: 'transparent' }
                      }
                  };
Mcmahon answered 10/1, 2017 at 16:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.