2

How can we change default color to 'green' for 'Executed' column and 'red' for 'Not Executed' column below? I tried using series/colors but no luck.

I tried adding color parameter, series in both options and view below. Nothings working.

Code:

<html>
       <head>
          <title>Google Charts Tutorial</title>
          <script type = "text/javascript" src = "https://www.gstatic.com/charts/loader.js">
          </script>
          <script type = "text/javascript">
             google.charts.load('current', {packages: ['corechart']});     
          </script>
           <script language = "JavaScript">
             function drawChart(){
                // Define the chart to be drawn.
                var data = google.visualization.arrayToDataTable([
                   ['Status', 'Outcome', { role: 'style' }],
                   ['Executed', 70,'green'],
                   ['Not Executed', 5,'red']
                ]);

        var groupData = google.visualization.data.group(
        data,
        [{column: 0, modifier: function () {return 'total'}, type:'string'}],
        [{column: 1, aggregation: google.visualization.data.sum, type: 'number'}]
      );

      var formatPercent = new google.visualization.NumberFormat({
        pattern: '#,##0.0%'
      });

      var formatShort = new google.visualization.NumberFormat({
        pattern: 'short'
      });

      var view = new google.visualization.DataView(data);
      view.setColumns([0, 1, {
        calc: function (dt, row) {
          var amount =  formatShort.formatValue(dt.getValue(row, 1));
          var percent = formatPercent.formatValue(dt.getValue(row, 1) / groupData.getValue(0, 1));
          return amount + ' (' + percent + ')';
        },
        type: 'string',
        role: 'annotation'
      }]);

            var options = { title: 'Google charts', 'legend':'none'}; 

            var chart = new google.visualization.ColumnChart(document.getElementById('container1'));
            chart.draw(view, options);
         }
         google.charts.setOnLoadCallback(drawChart);
          </script>
       </head>

       <body>
     <table border='1px' cellpadding='5' cellspacing='0' style='border: solid 1px Silver; font-size: x-small;width: 100%'>
     <tr align='center' valign='top'>
    <td align='center' style='width:100px' valign='center' bgcolor='white'>
        <div id="container1" style="width:400; height:280"></div>
    </td>
     </tr>
    </table>
       </body>
    </html>

All I need to do is to change blue color of bars to green and red. Kindly check and help.

WhiteHat
  • 59,912
  • 7
  • 51
  • 133
DeSon
  • 147
  • 13

1 Answers1

1

in the data view, need to include the column index for the style role from the data table...

var view = new google.visualization.DataView(data);
view.setColumns([0, 1, 2, {  // <-- include column index 2 for style role
  calc: function (dt, row) {
    var amount =  formatShort.formatValue(dt.getValue(row, 1));
    var percent = formatPercent.formatValue(dt.getValue(row, 1) / groupData.getValue(0, 1));
    return amount + ' (' + percent + ')';
  },
  type: 'string',
  role: 'annotation'
}]);

see following working snippet...

google.charts.load('current', {packages: ['corechart']});
function drawChart(){
  // Define the chart to be drawn.
  var data = google.visualization.arrayToDataTable([
     ['Status', 'Outcome', { role: 'style' }],
     ['Executed', 70,'green'],
     ['Not Executed', 5,'red']
  ]);

  var groupData = google.visualization.data.group(
    data,
    [{column: 0, modifier: function () {return 'total'}, type:'string'}],
    [{column: 1, aggregation: google.visualization.data.sum, type: 'number'}]
  );

  var formatPercent = new google.visualization.NumberFormat({
    pattern: '#,##0.0%'
  });

  var formatShort = new google.visualization.NumberFormat({
    pattern: 'short'
  });

  var view = new google.visualization.DataView(data);
  view.setColumns([0, 1, 2, {
    calc: function (dt, row) {
      var amount =  formatShort.formatValue(dt.getValue(row, 1));
      var percent = formatPercent.formatValue(dt.getValue(row, 1) / groupData.getValue(0, 1));
      return amount + ' (' + percent + ')';
    },
    type: 'string',
    role: 'annotation'
  }]);

  var options = { title: 'Google charts', 'legend':'none'};

  var chart = new google.visualization.ColumnChart(document.getElementById('container1'));
  chart.draw(view, options);
}
google.charts.setOnLoadCallback(drawChart);
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="container1"></div>
WhiteHat
  • 59,912
  • 7
  • 51
  • 133