user882670
user882670

Reputation:

How to load an Area Chart when the x-axis is date?

I want to build an area chart in Google spreadsheets using the Charts visualization service.

This is how my data looks like:

enter image description here

And the script:

var data = google.visualization.arrayToDataTable(range);
  var options = {
    title: 'Dados',
    hAxis: {title: 'Data',  titleTextStyle: {color: '#333'}}
  };
var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
chart.draw(data, options);

Which doesn't return any chart!

This is the code to retrieve the range values:

var range=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('mySheet').getRange("A1:B10").getValues();`

If the 1st column data is replaced by strings, it works perfectly. So I guess the problem is related with the data type of the 1st column. I've checked the reference, and Date should be allowed: https://google-developers.appspot.com/chart/interactive/docs/gallery/areachart#Data_Format

Help anyone?

Upvotes: 2

Views: 563

Answers (1)

Kalyan Reddy
Kalyan Reddy

Reputation: 1142

Are you getting an error such as The script completed but the returned value is not a supported return type.? If so, this is because even though the Google Charts API supports Date values, Date values can't be passed from Apps Script server side functions to client side functions. I used a map function to create a new 2D array that converts the Dates to strings before sending them to the client side and I was able to successfully create a chart:

var range = ...
return range.map(function (row) { return [row[0].toString(), row[1]]; } );  

Upvotes: 0

Related Questions