Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

enter image description here

I also tried this in my options:

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

How do I get each of the series to have a different color?

like image 879
Kenny Wyland Avatar asked Apr 29 '15 05:04

Kenny Wyland


People also ask

How do you change the color of a chart on Google?

Changing the Color You can change the color of the lines that connect data points in Google Charts in two subtly different ways: with the colors option to change the chart palette, or with the series option to specify the color for particular series. In the following chart, we set the color of each series explicitly.

How do I change the color of my bar chart based on value?

Select the bar chart or column chart, then click Kutools > Charts > Color Chart by Value. Then in the popped-out dialog, set the value range and the relative color as you need. Click to free download now!


1 Answers

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. See the colors variable in the example below.

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: Bar Chart

Fiddle with the code:
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

like image 156
Dem Pilafian Avatar answered Oct 03 '22 04:10

Dem Pilafian