JavaScript Google Charts - Change the color of bars google charts change font,google charts change color,google charts

I want to change the color of each bar in my bar graph. Currently, I tried setting the colors option as specified in the documentation:

var options = {
        'title' : results.title,
        'width' : 400,
        'height' : 300,
        'is3D' : true,
        'colors' : ["#194D86","#699A36", "#000000"],
        'allowHtml' : true
    }

But it does not work. Basically, I would want each bar in the following graph to be the same color: http://jsfiddle.net/etiennenoel/ZThMp/12/

Is there a way to do that or do I have to change my code structure to do so ?

Answer:1

[Edit - there is a better method outlined in edit below]

The Visualization API colors data by series (or column in the DataTable, if you prefer). The solution is to split the data into multiple series using a DataView:

// get a list of all the labels in column 0
var group = google.visualization.data.group(data, [0], []);

// build the columns for the view
var columns = [0];
for (var i = 0; i < group.getNumberOfRows(); i++) {
    var label = group.getValue(i, 0);
    // set the columns to use in the chart's view
    // calculated columns put data belonging to each label in the proper column
    columns.push({
        type: 'number',
        label: label,
        calc: (function (name) {
            return function (dt, row) {
                return (dt.getValue(row, 0) == name) ? dt.getValue(row, 1) : null;
            }
        })(label)
    });
}
// create the DataView
var view = new google.visualization.DataView(data);
view.setColumns(columns);

Set the "isStacked" option in the chart to "true" to fix the column spacing issues that result, and draw the chart using the view instead of the DataTable:

var chart = new google.visualization.ColumnChart(document.querySelector('#chart_div'));
chart.draw(view, {
    // options
    isStacked: true
});

See an example here.

[Edit: new (improved) method available with update to the Visualization API]

You can now use the new "style" column role to specify styles for your columns. It works like this:

function drawChart() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Name');
    data.addColumn('number', 'Value');
    data.addColumn({type: 'string', role: 'style'});
    data.addRows([
        ['Foo', 5, 'color: #ac6598'],
        ['Bar', 7, 'color: #3fb0e9'],
        ['Baz', 3, 'color: #42c698']
    ]);

    var chart = new google.visualization.ColumnChart(document.querySelector('#chart_div'));
    chart.draw(data, {
        height: 400,
        width: 600,
        legend: {
            position: 'none'
        }
    });
}
google.load('visualization', '1', {packages:['corechart'], callback: drawChart});

see example here: http://jsfiddle.net/asgallant/gbzLB/

Answer:2

My first application is running on 8080, the second application is running on 8081, The code is as follows: server { listen 80; server_name greatwallprojects.com; location / { ...

My first application is running on 8080, the second application is running on 8081, The code is as follows: server { listen 80; server_name greatwallprojects.com; location / { ...

I run the below JS script but just find func2() output is foobar, not George, who can explain why func2 = func.bind(someuser) doesn't bind someuser to func? var someuser = { name: 'George', ...

I run the below JS script but just find func2() output is foobar, not George, who can explain why func2 = func.bind(someuser) doesn't bind someuser to func? var someuser = { name: 'George', ...

I'm pretty new at JavaScript. I have a variable and two buttons to add or minus 1 from that variable, which works; However I want to make is so that when the variable = 0 the minus button disables, so ...

I'm pretty new at JavaScript. I have a variable and two buttons to add or minus 1 from that variable, which works; However I want to make is so that when the variable = 0 the minus button disables, so ...

I have a progress bar, and I have my js getting the value of it with document.getElementsByTagName(progressbar)[num]. I have this in multiple functions, but when one of the progress bars are removed, ...

I have a progress bar, and I have my js getting the value of it with document.getElementsByTagName(progressbar)[num]. I have this in multiple functions, but when one of the progress bars are removed, ...