简体   繁体   English

更改Google堆积条形图颜色(材料条形图)

[英]Changing Google Stacked Bar Chart colors (Material Bar Chart)

var data = google.visualization.arrayToDataTable(stats_data);

var options = {
    width: 1400,
    height: 400,
    legend: { position: 'top', maxLines: 3 },
    bar: { groupWidth: '75%' },
    isStacked: true,
    bars: 'vertical',
    colors:['#999','#346ac9', '#279423', '#fc9826'],
};

var chart = new google.charts.Bar(document.getElementById('chart-recent'));
chart.draw(data, google.charts.Bar.convertOptions(options));

I've got a stacked bar chart and I want each of the colors to be different (grey, blue, green, orange). 我有一个堆积条形图,我希望每种颜色都不同(灰色,蓝色,绿色,橙色)。 However, the colors of the sections just take the first color (grey) in multiple brightnesses. 但是,这些部分的颜色只是采用多种亮度的第一种颜色(灰色)。

在此输入图像描述

I also tried this in my options : 我也在我的options试过这个:

series: [
    {color: '#999'},
    {color: '#346ac9'},
    {color: '#279423'},
    {color: '#fc9826'}
]

How do I get each of the series to have a different color? 如何让每个系列都有不同的颜色?

At the time of the original question, the Material Bar Chart probably did not yet support custom colors for stacked bar charts. 在原始问题时, 材料条形图可能还不支持堆积条形图的自定义颜色。

Today, the series configuration option as an array of color objects seems to work fine. 今天,作为颜色对象数组series配置选项似乎工作正常。 See the colors variable in the example below. 请参阅下面示例中的colors变量。

Example code: 示例代码:

const data = [
   ['Sector',        'High', 'Medium', 'Low' ],
   ['Agriculture',   18,  9, 29],
   ['Education',      2, 14, 10],
   ['Healthcare',     4,  6, 41],
   ['Manufacturing', 36, 10,  3]
   ];
const colors = [
   { color: 'indianred' },  //high
   { color: 'khaki' },      //medium
   { color: 'seagreen' },   //low
   ];

const drawChart = () => {
   const options = {
      chart:   { title: 'Risk by sector' },
      legend:  { position: 'top' },  //not yet supported
      bars:    'horizontal',
      stacked: true,
      series:  colors
      };
   const chartData = google.visualization.arrayToDataTable(data);
   const elem = $('figure#health-chart')[0];
   const chart = new google.charts.Bar(elem);
   chart.draw(chartData, options);
   };

google.charts.load('current', { packages: ['bar'] });
google.charts.setOnLoadCallback(drawChart);

Resulting chart: 结果图表: 条形图

Fiddle with the code: 摆弄代码:
https://jsfiddle.net/p8bnfe2h https://jsfiddle.net/p8bnfe2h

Configuration options 配置选项

series
An array of objects, each describing the format of the corresponding series in the chart. 一组对象,每个对象描述图表中相应系列的格式。 To use default values for a series, specify an empty object {}. 要使用系列的默认值,请指定一个空对象{}。 If a series or a value is not specified, the global value will be used. 如果未指定系列或值,则将使用全局值。

Documentation: 文档:
https://developers.google.com/chart/interactive/docs/gallery/barchart#configuration-options https://developers.google.com/chart/interactive/docs/gallery/barchart#configuration-options

I was able to get the colors working here 我能够在这里使用颜色

When generating the graph I had to use: new google.visualization.ColumnChart(...) instead of new google.charts.Bar(...) otherwise it would not stack properly. 在生成图形时,我必须使用: new google.visualization.ColumnChart(...)而不是new google.charts.Bar(...)否则它将无法正确堆叠。


You may also want to make sure you are using the latest version of Google Charts. 您可能还想确保使用最新版本的Google Charts。 In the fiddle above, I used samples from the developer docs which were using: https://www.google.com/jsapi to autoload all the things. 在上面的小提琴中,我使用了以下开发人员文档中的示例: https://www.google.com/jsapihttps://www.google.com/jsapi来自动加载所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM