swaroop komma
swaroop komma

Reputation: 45

Different color for each bar in Google Chart

I am using Google Column Chart for creating column chart for my application. I would like to be able to have different colors for each bar. I am inserting the colors attribute into the options variable but it's not working.

Can anyone help me on this?

My fiddle

var options = {
        title: 'Motivation Level Throughout the Day',
        hAxis: {
          title: 'Time of Day',
          format: 'h:mm a',
          viewWindow: {
            min: [7, 30, 0],
            max: [17, 30, 0]
          }
        },
        vAxis: {
          title: 'Rating (scale of 1-10)'
        },
        tooltip: {isHtml: true},
        colors: ['red','yellow', 'blue', 'red','yellow', 'blue', 'red','yellow', 'red','yellow']
      };

Upvotes: 1

Views: 2562

Answers (2)

WhiteHat
WhiteHat

Reputation: 61275

each color in the colors config option maps to each series in the data table

a series is represented by each column after the x-axis column in the data table

here, there is only one series / data table column after the x-axis

[{v: [8, 0, 0], f: '8 am'}, 46],  // <-- one series column
[{v: [9, 0, 0], f: '9 am'}, 46],

in order to use colors, it would need to look something like...

[{v: [8, 0, 0], f: '8 am'}, 46, null, null, null],
[{v: [9, 0, 0], f: '9 am'}, null, 46, null, null],
[{v: [10, 0, 0], f:'10 am'}, null, null, 34, null],
[{v: [11, 0, 0], f: '11 am'}, null, null, null, 4],

instead, we can use a 'style' column role,
this allows us to change the color for each row...

[{v: [8, 0, 0], f: '8 am'}, 46, 'red'],
[{v: [9, 0, 0], f: '9 am'}, 46, 'yellow'],

see following working snippet,
a 'style' column is added to the data view,
the colors are pulled from the existing colors option...

google.charts.load('current', {
  packages: ['corechart']
}).then(drawBasic);

function drawBasic() {
  var dataTable = new google.visualization.DataTable();
  dataTable.addColumn('timeofday', 'Time of Day');
  dataTable.addColumn('number', 'Motivation Level');

  dataTable.addRows([
    [{v: [8, 0, 0], f: '8 am'}, 46],
    [{v: [9, 0, 0], f: '9 am'}, 46],
    [{v: [10, 0, 0], f:'10 am'}, 34],
    [{v: [11, 0, 0], f: '11 am'}, 4],
    [{v: [12, 0, 0], f: '12 pm'}, 5],
    [{v: [13, 0, 0], f: '1 pm'}, 6],
    [{v: [14, 0, 0], f: '2 pm'}, 7],
    [{v: [15, 0, 0], f: '3 pm'}, 8],
    [{v: [16, 0, 0], f: '4 pm'}, 9],
    [{v: [17, 0, 0], f: '5 pm'}, 10],
  ]);

  var options = {
    title: 'Motivation Level Throughout the Day',
    hAxis: {
      title: 'Time of Day',
      format: 'h:mm a',
      viewWindow: {
        min: [7, 30, 0],
        max: [17, 30, 0]
      }
    },
    vAxis: {
      title: 'Rating (scale of 1-10)'
    },
    tooltip: {isHtml: true},
    colors: ['red', 'yellow', 'blue', 'red', 'yellow', 'blue', 'red', 'yellow', 'red', 'yellow'],
    legend: {
      position: 'none'
    }
  };

  // create view with tooltip columns for each series
  var viewColumns = [0];
  var dataView = new google.visualization.DataView(dataTable);
  $.each(new Array(dataTable.getNumberOfColumns()), function (index) {
    // ignore x-axis column
    if (index === 0) {
      return;
    }

    // add series column
    viewColumns.push(index);

    // add tooltip column - column role should directly follow the series it represents
    viewColumns.push({
      calc: function (dt, row) {
        // build tooltip
        var tooltip = '<div class="ggl-tooltip"><div><span>';
        tooltip += dt.getFormattedValue(row, 0) + '</span></div>';
        tooltip += '<div>' + dt.getColumnLabel(index) + ':&nbsp;';
        tooltip += '<span>' + dt.getFormattedValue(row, index) + '</span></div>';
        tooltip += '<div>Add whatever you want for column: ' + index + ', row: ' + row + '</div></div>';

        return tooltip;
      },
      p: {html: true},
      role: 'tooltip',
      type: 'string'
    });

    // add style column for color
    viewColumns.push({
      calc: function (dt, row) {
        // get color based on row index
        return options.colors[row];
      },
      role: 'style',
      type: 'string'
    });
  });
  dataView.setColumns(viewColumns);

  var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
  chart.draw(dataView.toDataTable(), options);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

note: using a 'style' column role invalidates the legend,
which will only show the first color (color for first series)

Upvotes: 1

Prabhat-VS
Prabhat-VS

Reputation: 1530

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

function drawBasic() {

      var dataTable = new google.visualization.arrayToDataTable([]);
      
      dataTable.addColumn('timeofday', 'Time of Day');
      dataTable.addColumn('number', 'Motivation Level');

      dataTable.addRows([
        [{v: [8, 0, 0], f: '8 am'}, 46],
        [{v: [9, 0, 0], f: '9 am'}, 46],
        [{v: [10, 0, 0], f:'10 am'}, 34],
        [{v: [11, 0, 0], f: '11 am'}, 4],
        [{v: [12, 0, 0], f: '12 pm'}, 5],
        [{v: [13, 0, 0], f: '1 pm'}, 6],
        [{v: [14, 0, 0], f: '2 pm'}, 7],
        [{v: [15, 0, 0], f: '3 pm'}, 8],
        [{v: [16, 0, 0], f: '4 pm'}, 9],
        [{v: [17, 0, 0], f: '5 pm'}, 10],
      ]);
      
      
      var options = {
        title: 'Motivation Level Throughout the Day',
        hAxis: {
          title: 'Time of Day',
          format: 'h:mm a',
          viewWindow: {
            min: [7, 30, 0],
            max: [17, 30, 0]
          }
        },
        vAxis: {
          title: 'Rating (scale of 1-10)'
        },
		tooltip: {isHtml: true},
         colors: ['#FFD700', '#C0C0C0', '#8C7853']
      };



 // create view with tooltip columns for each series
  var viewColumns = [0];
  var dataView = new google.visualization.DataView(dataTable);
  $.each(new Array(dataTable.getNumberOfColumns()), function (index) {
    // ignore x-axis column
    if (index === 0) {
      return;
    }

    // add series column
    viewColumns.push(index);

    // add tooltip column - column role should directly follow the series it represents
    viewColumns.push({
      calc: function (dt, row) {
        // build tooltip
        var tooltip = '<div class="ggl-tooltip"><div><span>';
        tooltip += dt.getFormattedValue(row, 0) + '</span></div>';
        tooltip += '<div>' + dt.getColumnLabel(index) + ':&nbsp;';
        tooltip += '<span>' + dt.getFormattedValue(row, index) + '</span></div>';
        tooltip += '<div>Add whatever you want for column: ' + index + ', row: ' + row + '</div></div>';

        return tooltip;
      },
      p: {html: true},
      role: 'tooltip',
      type: 'string'
    });
  });
  dataView.setColumns(viewColumns); 
	  
	    
      var chart = new google.visualization.ColumnChart(
        document.getElementById('chart_div'));

      chart.draw(dataView.toDataTable(), options);
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

Upvotes: 0

Related Questions