Chart Services‎ > ‎

Bar Chart

Here is an example of Bar Chart which takes data from spreadsheet to build the chart.

References:
Bar Chart

Spreadsheet Preview screenshot:

Here is the is the spreadsheet preview which serves as the data source for the chart.

Data Source

 
 

Example Code:
function doGet(){
  //Get the data from spreadsheet
  ssID ='SpreadsheetID';//Change it to yours
  var SS = SpreadsheetApp.openById('ssID');
  var sheet = SS.getSheets()[0];
  var data = sheet.getRange('A1:G7').getValues();
  
  //Build data table
  var dataTable = Charts.newDataTable();
  
  //Add Column types
  dataTable.addColumn(Charts.ColumnType.STRING, data[0][0]);
  for(var i=1; i<data[0].length-1; i++){
    dataTable.addColumn(Charts.ColumnType.NUMBER, data[0][i]);
  }
  
  //Add rows
  for(var j=1; j<data.length; j++){
    dataTable.addRow(data[j]);
    Logger.log(data[j])
  }
  
  //Create and build chart
  var chart = Charts.newBarChart()
      .setDataTable(dataTable)
      .setTitle("Sales by store")
      .build();
  
  var app = UiApp.createApplication().setTitle("AST Chart");
  app.add(chart)
  return app;
}


Comments