javascript Google Charts - 更改条形的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17684837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:11:58  来源:igfitidea点击:

Google Charts - Change the color of bars

javascripthtmlgoogle-visualization

提问by Etienne No?l

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/

但它不起作用。基本上,我希望下图中的每个条形都具有相同的颜色: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 ?

有没有办法做到这一点,或者我是否必须更改我的代码结构才能这样做?

回答by asgallant

[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:

Visualization API 按系列(或 DataTable 中的列,如果您愿意)为数据着色。解决方案是使用 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
});
// 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);

将图表中的“isStacked”选项设置为“true”以修复导致的列间距问题,并使用视图而不是数据表绘制图表:

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]

[编辑:新的(改进的)方法可用于可视化 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/

请参阅此处的示例:http: //jsfiddle.net/asgallant/gbzLB/

回答by Abinaya Selvaraju

There is a solution for your problem.You need to add seriesin your options. I have already answered for the similar type of question. Refer my answer here.I hope this will help you.

您的问题有一个解决方案。您需要添加series您的选项。类似的问题我已经回答过了。在这里参考我的答案我希望这能帮到您。